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