]> Pileus Git - ~andy/gtk/blob - gdk/gdkdisplay.c
Do not interpret distant clicks as double-clicks (#116541, Bernhard
[~andy/gtk] / gdk / gdkdisplay.c
1 /* GDK - The GIMP Drawing Kit
2  * gdkdisplay.c
3  * 
4  * Copyright 2001 Sun Microsystems Inc. 
5  *
6  * Erwann Chenede <erwann.chenede@sun.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <glib.h>
25 #include "gdk.h"                /* gdk_event_send_client_message() */
26 #include "gdkdisplay.h"
27 #include "gdkinternals.h"
28 #include "gdkmarshalers.h"
29 #include "gdkscreen.h"
30
31 enum {
32   CLOSED,
33   LAST_SIGNAL
34 };
35
36 static void gdk_display_class_init (GdkDisplayClass *class);
37 static void gdk_display_init       (GdkDisplay      *display);
38 static void gdk_display_dispose    (GObject         *object);
39 static void gdk_display_finalize   (GObject         *object);
40
41
42 void       singlehead_get_pointer        (GdkDisplay       *display,
43                                           GdkScreen       **screen,
44                                           gint             *x,
45                                           gint             *y,
46                                           GdkModifierType  *mask);
47 GdkWindow* singlehead_window_get_pointer (GdkDisplay       *display,
48                                           GdkWindow        *window,
49                                           gint             *x,
50                                           gint             *y,
51                                           GdkModifierType  *mask);
52 GdkWindow* singlehead_window_at_pointer  (GdkDisplay       *display,
53                                           gint             *win_x,
54                                           gint             *win_y);
55
56 GdkWindow* singlehead_default_window_get_pointer (GdkWindow       *window,
57                                                   gint            *x,
58                                                   gint            *y,
59                                                   GdkModifierType *mask);
60 GdkWindow* singlehead_default_window_at_pointer  (GdkScreen       *screen,
61                                                   gint            *win_x,
62                                                   gint            *win_y);
63
64 static guint signals[LAST_SIGNAL] = { 0 };
65
66 static GObjectClass *parent_class;
67 static char *gdk_sm_client_id;
68
69 static const GdkDisplayPointerHooks default_pointer_hooks = {
70   _gdk_windowing_get_pointer,
71   _gdk_windowing_window_get_pointer,
72   _gdk_windowing_window_at_pointer
73 };
74
75 static const GdkDisplayPointerHooks singlehead_pointer_hooks = {
76   singlehead_get_pointer,
77   singlehead_window_get_pointer,
78   singlehead_window_at_pointer
79 };
80
81 static const GdkPointerHooks singlehead_default_pointer_hooks = {
82   singlehead_default_window_get_pointer,
83   singlehead_default_window_at_pointer
84 };
85
86 static const GdkPointerHooks *singlehead_current_pointer_hooks = &singlehead_default_pointer_hooks;
87
88 GType
89 gdk_display_get_type (void)
90 {
91
92   static GType object_type = 0;
93
94   if (!object_type)
95     {
96       static const GTypeInfo object_info = {
97         sizeof (GdkDisplayClass),
98         (GBaseInitFunc) NULL,
99         (GBaseFinalizeFunc) NULL,
100         (GClassInitFunc) gdk_display_class_init,
101         NULL,                   /* class_finalize */
102         NULL,                   /* class_data */
103         sizeof (GdkDisplay),
104         0,                      /* n_preallocs */
105         (GInstanceInitFunc) gdk_display_init
106       };
107       object_type = g_type_register_static (G_TYPE_OBJECT,
108                                             "GdkDisplay", &object_info, 0);
109     }
110
111   return object_type;
112 }
113
114 static void
115 gdk_display_class_init (GdkDisplayClass *class)
116 {
117   GObjectClass *object_class = G_OBJECT_CLASS (class);
118   
119   parent_class = g_type_class_peek_parent (class);
120
121   object_class->finalize = gdk_display_finalize;
122   object_class->dispose = gdk_display_dispose;
123
124   /**
125    * GdkDisplay::closed:
126    * @display: the object on which the signal is emitted
127    * @is_error: %TRUE if the display was closed due to an error
128    *
129    * The ::closed signal is emitted when the connection to the windowing
130    * system for @display is closed.
131    *
132    * Since: 2.2
133    */   
134   signals[CLOSED] =
135     g_signal_new ("closed",
136                   G_OBJECT_CLASS_TYPE (object_class),
137                   G_SIGNAL_RUN_LAST,
138                   G_STRUCT_OFFSET (GdkDisplayClass, closed),
139                   NULL, NULL,
140                   gdk_marshal_VOID__BOOLEAN,
141                   G_TYPE_NONE,
142                   1,
143                   G_TYPE_BOOLEAN);
144 }
145
146 static void
147 gdk_display_init (GdkDisplay *display)
148 {
149   _gdk_displays = g_slist_prepend (_gdk_displays, display);
150
151   display->button_click_time[0] = display->button_click_time[1] = 0;
152   display->button_window[0] = display->button_window[1] = NULL;
153   display->button_number[0] = display->button_number[1] = -1;
154   display->button_x[0] = display->button_x[1] = 0;
155   display->button_y[0] = display->button_y[1] = 0;
156
157   display->double_click_time = 250;
158   display->double_click_distance = 5;
159
160   display->pointer_hooks = &default_pointer_hooks;
161 }
162
163 static void
164 gdk_display_dispose (GObject *object)
165 {
166   GdkDisplay *display = GDK_DISPLAY_OBJECT (object);
167
168   g_list_foreach (display->queued_events, (GFunc)gdk_event_free, NULL);
169   g_list_free (display->queued_events);
170   display->queued_events = NULL;
171   display->queued_tail = NULL;
172
173   _gdk_displays = g_slist_remove (_gdk_displays, object);
174
175   if (gdk_display_get_default() == display)
176     gdk_display_manager_set_default_display (gdk_display_manager_get(), NULL);
177 }
178
179 static void
180 gdk_display_finalize (GObject *object)
181 {
182   parent_class->finalize (object);
183 }
184
185 /**
186  * gdk_display_close:
187  * @display: a #GdkDisplay
188  *
189  * Closes the connection to the windowing system for the given display,
190  * and cleans up associated resources.
191  *
192  * Since: 2.2
193  */
194 void
195 gdk_display_close (GdkDisplay *display)
196 {
197   g_return_if_fail (GDK_IS_DISPLAY (display));
198
199   if (!display->closed)
200     {
201       display->closed = TRUE;
202       
203       g_signal_emit (display, signals[CLOSED], 0, FALSE);
204       g_object_run_dispose (G_OBJECT (display));
205       
206       g_object_unref (display);
207     }
208 }
209
210 /**
211  * gdk_display_get_event:
212  * @display: a #GdkDisplay
213  * 
214  * Gets the next #GdkEvent to be processed for @display, fetching events from the
215  * windowing system if necessary.
216  * 
217  * Return value: the next #GdkEvent to be processed, or %NULL if no events
218  * are pending. The returned #GdkEvent should be freed with gdk_event_free().
219  *
220  * Since: 2.2
221  **/
222 GdkEvent*
223 gdk_display_get_event (GdkDisplay *display)
224 {
225   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
226   
227   _gdk_events_queue (display);
228   return _gdk_event_unqueue (display);
229 }
230
231 /**
232  * gdk_display_peek_event:
233  * @display: a #GdkDisplay 
234  * 
235  * Gets a copy of the first #GdkEvent in the @display's event queue, without
236  * removing the event from the queue.  (Note that this function will
237  * not get more events from the windowing system.  It only checks the events
238  * that have already been moved to the GDK event queue.)
239  * 
240  * Return value: a copy of the first #GdkEvent on the event queue, or %NULL 
241  * if no events are in the queue. The returned #GdkEvent should be freed with
242  * gdk_event_free().
243  *
244  * Since: 2.2
245  **/
246 GdkEvent*
247 gdk_display_peek_event (GdkDisplay *display)
248 {
249   GList *tmp_list;
250
251   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
252
253   tmp_list = _gdk_event_queue_find_first (display);
254   
255   if (tmp_list)
256     return gdk_event_copy (tmp_list->data);
257   else
258     return NULL;
259 }
260
261 /**
262  * gdk_display_put_event:
263  * @display: a #GdkDisplay
264  * @event: a #GdkEvent.
265  *
266  * Appends a copy of the given event onto the front of the event
267  * queue for @display.
268  *
269  * Since: 2.2
270  **/
271 void
272 gdk_display_put_event (GdkDisplay *display,
273                        GdkEvent   *event)
274 {
275   g_return_if_fail (GDK_IS_DISPLAY (display));
276   g_return_if_fail (event != NULL);
277
278   _gdk_event_queue_append (display, gdk_event_copy (event));
279 }
280
281 /**
282  * gdk_pointer_ungrab:
283  * @time_: a timestamp from a #GdkEvent, or %GDK_CURRENT_TIME if no 
284  *  timestamp is available.
285  *
286  * Ungrabs the pointer, if it is grabbed by this application.
287  **/
288 void
289 gdk_pointer_ungrab (guint32 time)
290 {
291   gdk_display_pointer_ungrab (gdk_display_get_default (), time);
292 }
293
294 /**
295  * gdk_pointer_is_grabbed:
296  * 
297  * Returns %TRUE if the pointer is currently grabbed by this application.
298  *
299  * Note that this does not take the inmplicit pointer grab on button
300  * presses into account.
301
302  * Return value: %TRUE if the pointer is currently grabbed by this application.* 
303  **/
304 gboolean
305 gdk_pointer_is_grabbed (void)
306 {
307   return gdk_display_pointer_is_grabbed (gdk_display_get_default ());
308 }
309
310 /**
311  * gdk_keyboard_ungrab:
312  * @time_: a timestamp from a #GdkEvent, or %GDK_CURRENT_TIME if no
313  *        timestamp is available.
314  * 
315  * Ungrabs the keyboard, if it is grabbed by this application.
316  **/
317 void
318 gdk_keyboard_ungrab (guint32 time)
319 {
320   gdk_display_keyboard_ungrab (gdk_display_get_default (), time);
321 }
322
323 /**
324  * gdk_beep:
325  * 
326  * Emits a short beep.
327  **/
328 void
329 gdk_beep (void)
330 {
331   gdk_display_beep (gdk_display_get_default ());
332 }
333
334 /**
335  * gdk_event_send_client_message:
336  * @event: the #GdkEvent to send, which should be a #GdkEventClient.
337  * @winid:  the window to send the X ClientMessage event to.
338  * 
339  * Sends an X ClientMessage event to a given window (which must be
340  * on the default #GdkDisplay.)
341  * This could be used for communicating between different applications,
342  * though the amount of data is limited to 20 bytes.
343  * 
344  * Return value: non-zero on success.
345  **/
346 gboolean
347 gdk_event_send_client_message (GdkEvent        *event,
348                                GdkNativeWindow  winid)
349 {
350   g_return_val_if_fail (event != NULL, FALSE);
351
352   return gdk_event_send_client_message_for_display (gdk_display_get_default (),
353                                                     event, winid);
354 }
355
356 /**
357  * gdk_event_send_clientmessage_toall:
358  * @event: the #GdkEvent to send, which should be a #GdkEventClient.
359  *
360  * Sends an X ClientMessage event to all toplevel windows on the default
361  * #GdkScreen.
362  *
363  * Toplevel windows are determined by checking for the WM_STATE property, as
364  * described in the Inter-Client Communication Conventions Manual (ICCCM).
365  * If no windows are found with the WM_STATE property set, the message is sent
366  * to all children of the root window.
367  **/
368 void
369 gdk_event_send_clientmessage_toall (GdkEvent *event)
370 {
371   g_return_if_fail (event != NULL);
372
373   gdk_screen_broadcast_client_message (gdk_screen_get_default (), event);
374 }
375
376 /**
377  * gdk_device_get_core_pointer:
378  * 
379  * Returns the core pointer device for the default display.
380  * 
381  * Return value: the core pointer device; this is owned by the
382  *   display and should not be freed.
383  **/
384 GdkDevice *
385 gdk_device_get_core_pointer (void)
386 {
387   return gdk_display_get_core_pointer (gdk_display_get_default ());
388 }
389
390 /**
391  * gdk_display_get_core_pointer:
392  * @display: a #GdkDisplay
393  * 
394  * Returns the core pointer device for the given display
395  * 
396  * Return value: the core pointer device; this is owned by the
397  *   display and should not be freed.
398  *
399  * Since: 2.2
400  **/
401 GdkDevice *
402 gdk_display_get_core_pointer (GdkDisplay *display)
403 {
404   return display->core_pointer;
405 }
406
407 /**
408  * gdk_set_sm_client_id:
409  * @sm_client_id: the client id assigned by the session manager when the
410  *    connection was opened, or %NULL to remove the property.
411  * 
412  * Sets the <literal>SM_CLIENT_ID</literal> property on the application's leader window so that
413  * the window manager can save the application's state using the X11R6 ICCCM
414  * session management protocol.
415  *
416  * See the X Session Management Library documentation for more information on
417  * session management and the Inter-Client Communication Conventions Manual
418  * (ICCCM) for information on the <literal>WM_CLIENT_LEADER</literal> property. 
419  * (Both documents are part of the X Window System distribution.)
420  **/
421 void
422 gdk_set_sm_client_id (const gchar* sm_client_id)
423 {
424   GSList *displays, *tmp_list;
425   
426   g_free (gdk_sm_client_id);
427   gdk_sm_client_id = g_strdup (sm_client_id);
428
429   displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
430   for (tmp_list = displays; tmp_list; tmp_list = tmp_list->next)
431     _gdk_windowing_display_set_sm_client_id (tmp_list->data, sm_client_id);
432
433   g_slist_free (displays);
434 }
435
436 /**
437  * _gdk_get_sm_client_id:
438  * 
439  * Gets the client ID set with gdk_set_sm_client_id(), if any.
440  * 
441  * Return value: Session ID, or %NULL if gdk_set_sm_client_id()
442  *               has never been called.
443  **/
444 const char *
445 _gdk_get_sm_client_id (void)
446 {
447   return gdk_sm_client_id;
448 }
449
450 /**
451  * gdk_display_get_pointer:
452  * @display: a #GdkDisplay
453  * @screen: location to store the screen that the
454  *          cursor is on, or %NULL.
455  * @x: location to store root window X coordinate of pointer, or %NULL.
456  * @y: location to store root window Y coordinate of pointer, or %NULL.
457  * @mask: location to store current modifier mask, or %NULL
458  * 
459  * Gets the current location of the pointer and the current modifier
460  * mask for a given display.
461  *
462  * Since: 2.2
463  **/
464 void
465 gdk_display_get_pointer (GdkDisplay      *display,
466                          GdkScreen      **screen,
467                          gint            *x,
468                          gint            *y,
469                          GdkModifierType *mask)
470 {
471   GdkScreen *tmp_screen;
472   gint tmp_x, tmp_y;
473   GdkModifierType tmp_mask;
474   
475   g_return_if_fail (GDK_IS_DISPLAY (display));
476
477   display->pointer_hooks->get_pointer (display, &tmp_screen, &tmp_x, &tmp_y, &tmp_mask);
478
479   if (screen)
480     *screen = tmp_screen;
481   if (x)
482     *x = tmp_x;
483   if (y)
484     *y = tmp_y;
485   if (mask)
486     *mask = tmp_mask;
487 }
488
489 /**
490  * gdk_display_get_window_at_pointer:
491  * @display: a #GdkDisplay
492  * @win_x: return location for origin of the window under the pointer
493  * @win_y: return location for origin of the window under the pointer
494  * 
495  * Obtains the window underneath the mouse pointer, returning the location
496  * of that window in @win_x, @win_y for @screen. Returns %NULL if the window 
497  * under the mouse pointer is not known to GDK (for example, belongs to
498  * another application).
499  * 
500  * Returns: the window under the mouse pointer, or %NULL
501  *
502  * Since: 2.2
503  **/
504 GdkWindow *
505 gdk_display_get_window_at_pointer (GdkDisplay *display,
506                                    gint       *win_x,
507                                    gint       *win_y)
508 {
509   gint tmp_x, tmp_y;
510   GdkWindow *window;
511
512   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
513
514   window = display->pointer_hooks->window_at_pointer (display, &tmp_x, &tmp_y);
515
516   if (win_x)
517     *win_x = tmp_x;
518   if (win_y)
519     *win_y = tmp_y;
520
521   return window;
522 }
523
524 /**
525  * gdk_display_set_pointer_hooks:
526  * @display: a #GdkDisplay
527  * @new_hooks: a table of pointers to functions for getting
528  *   quantities related to the current pointer position,
529  *   or %NULL to restore the default table.
530  * 
531  * This function allows for hooking into the operation
532  * of getting the current location of the pointer on a particular
533  * display. This is only useful for such low-level tools as an
534  * event recorder. Applications should never have any
535  * reason to use this facility.
536  *
537  * Return value: the previous pointer hook table
538  *
539  * Since: 2.2
540  **/
541 GdkDisplayPointerHooks *
542 gdk_display_set_pointer_hooks (GdkDisplay                   *display,
543                                const GdkDisplayPointerHooks *new_hooks)
544 {
545   const GdkDisplayPointerHooks *result;
546
547   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
548   result = display->pointer_hooks;
549
550   if (new_hooks)
551     display->pointer_hooks = new_hooks;
552   else
553     display->pointer_hooks = &default_pointer_hooks;
554
555   return (GdkDisplayPointerHooks *)result;
556 }
557
558 void
559 singlehead_get_pointer (GdkDisplay       *display,
560                         GdkScreen       **screen,
561                         gint             *x,
562                         gint             *y,
563                         GdkModifierType  *mask)
564 {
565   GdkScreen *default_screen = gdk_display_get_default_screen (display);
566   GdkWindow *root_window = gdk_screen_get_root_window (default_screen);
567
568   *screen = default_screen;
569
570   singlehead_current_pointer_hooks->get_pointer (root_window, x, y, mask);
571 }
572
573 GdkWindow*
574 singlehead_window_get_pointer (GdkDisplay       *display,
575                                GdkWindow        *window,
576                                gint             *x,
577                                gint             *y,
578                                GdkModifierType  *mask)
579 {
580   return singlehead_current_pointer_hooks->get_pointer (window, x, y, mask);
581 }
582
583 GdkWindow*
584 singlehead_window_at_pointer   (GdkDisplay *display,
585                                 gint       *win_x,
586                                 gint       *win_y)
587 {
588   GdkScreen *default_screen = gdk_display_get_default_screen (display);
589
590   return singlehead_current_pointer_hooks->window_at_pointer (default_screen,
591                                                               win_x, win_y);
592 }
593
594 GdkWindow*
595 singlehead_default_window_get_pointer (GdkWindow       *window,
596                                        gint            *x,
597                                        gint            *y,
598                                        GdkModifierType *mask)
599 {
600   return _gdk_windowing_window_get_pointer (gdk_drawable_get_display (window),
601                                             window, x, y, mask);
602 }
603
604 GdkWindow*
605 singlehead_default_window_at_pointer  (GdkScreen       *screen,
606                                        gint            *win_x,
607                                        gint            *win_y)
608 {
609   return _gdk_windowing_window_at_pointer (gdk_screen_get_display (screen),
610                                            win_x, win_y);
611 }
612
613 /**
614  * gdk_set_pointer_hooks:
615  * @new_hooks: a table of pointers to functions for getting
616  *   quantities related to the current pointer position,
617  *   or %NULL to restore the default table.
618  * 
619  * This function allows for hooking into the operation
620  * of getting the current location of the pointer. This
621  * is only useful for such low-level tools as an
622  * event recorder. Applications should never have any
623  * reason to use this facility.
624  *
625  * This function is not multihead safe. For multihead operation,
626  * see gdk_display_set_pointer_hooks().
627  * 
628  * Return value: the previous pointer hook table
629  **/
630 GdkPointerHooks *
631 gdk_set_pointer_hooks (const GdkPointerHooks *new_hooks)
632 {
633   const GdkPointerHooks *result = singlehead_current_pointer_hooks;
634
635   if (new_hooks)
636     singlehead_current_pointer_hooks = new_hooks;
637   else
638     singlehead_current_pointer_hooks = &singlehead_default_pointer_hooks;
639
640   gdk_display_set_pointer_hooks (gdk_display_get_default (),
641                                  &singlehead_pointer_hooks);
642   
643   return (GdkPointerHooks *)result;
644 }
645
646