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