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