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