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