gdkmain-x11.c 15.8 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6
7
8
9
10
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
16
17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19
20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22
23
24
25
26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
28

29
30
31
32
33
34
35
36
#include "gdkx.h"
#include "gdkasync.h"
#include "gdkdisplay-x11.h"
#include "gdkinternals.h"
#include "gdkprivate-x11.h"
#include "gdkintl.h"
#include "gdkdeviceprivate.h"

37
#include <glib/gprintf.h>
Elliot Lee's avatar
Elliot Lee committed
38
39
#include <stdlib.h>
#include <string.h>
Owen Taylor's avatar
Owen Taylor committed
40
#include <unistd.h>
Elliot Lee's avatar
Elliot Lee committed
41
#include <limits.h>
42
#include <errno.h>
Elliot Lee's avatar
Elliot Lee committed
43
44
45
46
47

#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

Havoc Pennington's avatar
Havoc Pennington committed
48
49
50
51
#ifdef HAVE_XKB
#include <X11/XKBlib.h>
#endif

52
53
typedef struct _GdkPredicate        GdkPredicate;
typedef struct _GdkGlobalErrorTrap  GdkGlobalErrorTrap;
Elliot Lee's avatar
Elliot Lee committed
54
55
56
57
58
59
60

struct _GdkPredicate
{
  GdkEventFunc func;
  gpointer data;
};

61
/* non-GDK previous error handler */
62
63
typedef int (*GdkXErrorHandler) (Display *, XErrorEvent *);
static GdkXErrorHandler _gdk_old_error_handler;
64
65
66
67
/* number of times we've pushed the GDK error handler */
static int _gdk_error_handler_push_count = 0;

struct _GdkGlobalErrorTrap
68
{
69
  GSList *displays;
70
71
};

Elliot Lee's avatar
Elliot Lee committed
72
73
74
/* 
 * Private function declarations
 */
75

76
#ifndef HAVE_XCONVERTCASE
77
78
79
static void	 gdkx_XConvertCase	(KeySym	       symbol,
					 KeySym	      *lower,
					 KeySym	      *upper);
80
81
82
#define XConvertCase gdkx_XConvertCase
#endif

83
static int	    gdk_x_error			 (Display     *display, 
Elliot Lee's avatar
Elliot Lee committed
84
						  XErrorEvent *error);
85
static int	    gdk_x_io_error		 (Display     *display);
Elliot Lee's avatar
Elliot Lee committed
86
87
88

/* Private variable declarations
 */
89
static GQueue gdk_error_traps;
90

91
const GOptionEntry _gdk_windowing_args[] = {
92
93
  { "sync", 0, 0, G_OPTION_ARG_NONE, &_gdk_synchronize, 
    /* Description of --sync in --help output */ N_("Make X calls synchronous"), NULL },
94
95
  { NULL }
};
Elliot Lee's avatar
Elliot Lee committed
96

97
void
98
_gdk_x11_windowing_init (void)
Elliot Lee's avatar
Elliot Lee committed
99
{
100
  _gdk_x11_initialize_locale ();
101
102

  g_queue_init (&gdk_error_traps);
Elliot Lee's avatar
Elliot Lee committed
103
104
  XSetErrorHandler (gdk_x_error);
  XSetIOErrorHandler (gdk_x_io_error);
Havoc Pennington's avatar
Havoc Pennington committed
105

Matthias Clasen's avatar
Matthias Clasen committed
106
  _gdk_selection_property = gdk_atom_intern_static_string ("GDK_SELECTION");
107
108
}

109
110
GdkGrabStatus
_gdk_x11_convert_grab_status (gint status)
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{
  switch (status)
    {
    case GrabSuccess:
      return GDK_GRAB_SUCCESS;
    case AlreadyGrabbed:
      return GDK_GRAB_ALREADY_GRABBED;
    case GrabInvalidTime:
      return GDK_GRAB_INVALID_TIME;
    case GrabNotViewable:
      return GDK_GRAB_NOT_VIEWABLE;
    case GrabFrozen:
      return GDK_GRAB_FROZEN;
    }

  g_assert_not_reached();
127
128

  return 0;
129
130
}

131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 * _gdk_xgrab_check_unmap:
 * @window: a #GdkWindow
 * @serial: serial from Unmap event (or from NextRequest(display)
 *   if the unmap is being done by this client.)
 * 
 * Checks to see if an unmap request or event causes the current
 * grab window to become not viewable, and if so, clear the
 * the pointer we keep to it.
 **/
void
_gdk_xgrab_check_unmap (GdkWindow *window,
			gulong     serial)
{
145
  GdkDisplay *display = gdk_window_get_display (window);
146
147
  GdkDeviceManager *device_manager;
  GList *devices, *d;
148

149
  device_manager = gdk_display_get_device_manager (display);
150

151
152
153
154
  /* Get all devices */
  devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
  devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_SLAVE));
  devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING));
155

156
157
158
  /* End all grabs on the newly hidden window */
  for (d = devices; d; d = d->next)
    _gdk_display_end_device_grab (display, d->data, serial, window, TRUE);
159

160
  g_list_free (devices);
161
162
163
164
165
166
167
168
169
170
171
172
}

/**
 * _gdk_xgrab_check_destroy:
 * @window: a #GdkWindow
 * 
 * Checks to see if window is the current grab window, and if
 * so, clear the current grab window.
 **/
void
_gdk_xgrab_check_destroy (GdkWindow *window)
{
173
  GdkDisplay *display = gdk_window_get_display (window);
174
175
176
  GdkDeviceManager *device_manager;
  GdkDeviceGrabInfo *grab;
  GList *devices, *d;
177

178
179
180
181
182
183
184
185
  device_manager = gdk_display_get_device_manager (display);

  /* Get all devices */
  devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
  devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_SLAVE));
  devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING));

  for (d = devices; d; d = d->next)
186
    {
187
188
189
190
191
192
193
194
195
196
197
198
199
      /* Make sure there is no lasting grab in this native window */
      grab = _gdk_display_get_last_device_grab (display, d->data);

      if (grab && grab->native_window == window)
        {
          /* We don't know the actual serial to end, but it
             doesn't really matter as this only happens
             after we get told of the destroy from the
             server so we know its ended in the server,
             just make sure its ended. */
          grab->serial_end = grab->serial_start;
          grab->implicit_ungrab = TRUE;
        }
200
    }
201
202

  g_list_free (devices);
203
}
204

Elliot Lee's avatar
Elliot Lee committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 *--------------------------------------------------------------
 * gdk_x_io_error
 *
 *   The X I/O error handling routine.
 *
 * Arguments:
 *   "display" is the X display the error orignated from.
 *
 * Results:
 *   An X I/O error basically means we lost our connection
 *   to the X server. There is not much we can do to
 *   continue, so simply print an error message and exit.
 *
 * Side effects:
 *
 *--------------------------------------------------------------
 */

static int
gdk_x_io_error (Display *display)
{
227
228
229
230
231
232
233
234
  /* This is basically modelled after the code in XLib. We need
   * an explicit error handler here, so we can disable our atexit()
   * which would otherwise cause a nice segfault.
   * We fprintf(stderr, instead of g_warning() because g_warning()
   * could possibly be redirected to a dialog
   */
  if (errno == EPIPE)
    {
235
236
237
238
239
      g_warning ("The application '%s' lost its connection to the display %s;\n"
                 "most likely the X server was shut down or you killed/destroyed\n"
                 "the application.\n",
                 g_get_prgname (),
                 display ? DisplayString (display) : gdk_get_display_arg_name ());
240
241
242
    }
  else
    {
243
244
245
246
      g_warning ("%s: Fatal IO error %d (%s) on X server %s.\n",
                 g_get_prgname (),
                 errno, g_strerror (errno),
                 display ? DisplayString (display) : gdk_get_display_arg_name ());
247
248
249
    }

  exit(1);
Elliot Lee's avatar
Elliot Lee committed
250
251
}

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* X error handler. Keep the name the same because people are used to
 * breaking on it in the debugger.
 */
static int
gdk_x_error (Display	 *xdisplay,
	     XErrorEvent *error)
{
  if (error->error_code)
    {
      GdkDisplay *error_display;
      GdkDisplayManager *manager;
      GSList *displays;

      /* Figure out which GdkDisplay if any got the error. */
      error_display = NULL;
      manager = gdk_display_manager_get ();
      displays = gdk_display_manager_list_displays (manager);
      while (displays != NULL)
        {
          GdkDisplayX11 *gdk_display = displays->data;

          if (xdisplay == gdk_display->xdisplay)
            {
              error_display = GDK_DISPLAY_OBJECT (gdk_display);
              g_slist_free (displays);
              displays = NULL;
            }
          else
            {
              displays = g_slist_delete_link (displays, displays);
            }
        }

      if (error_display == NULL)
        {
          /* Error on an X display not opened by GDK. Ignore. */

          return 0;
        }
      else
        {
          _gdk_x11_display_error_event (error_display, error);
        }
    }

  return 0;
}

300
void
301
_gdk_x11_error_handler_push (void)
302
{
303
304
305
  GdkXErrorHandler previous;

  previous = XSetErrorHandler (gdk_x_error);
306

307
308
  if (_gdk_error_handler_push_count > 0)
    {
309
      if (previous != gdk_x_error)
310
311
        g_warning ("XSetErrorHandler() called with a GDK error trap pushed. Don't do that.");
    }
312
313
314
315
  else
    {
      _gdk_old_error_handler = previous;
    }
316

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  _gdk_error_handler_push_count += 1;
}

void
_gdk_x11_error_handler_pop  (void)
{
  g_return_if_fail (_gdk_error_handler_push_count > 0);

  _gdk_error_handler_push_count -= 1;

  if (_gdk_error_handler_push_count == 0)
    {
      XSetErrorHandler (_gdk_old_error_handler);
      _gdk_old_error_handler = NULL;
    }
}

/**
 * gdk_error_trap_push:
 *
 * This function allows X errors to be trapped instead of the normal
 * behavior of exiting the application. It should only be used if it
 * is not possible to avoid the X error in any other way. Errors are
 * ignored on all #GdkDisplay currently known to the
 * #GdkDisplayManager. If you don't care which error happens and just
 * want to ignore everything, pop with gdk_error_trap_pop_ignored().
 * If you need the error code, use gdk_error_trap_pop() which may have
 * to block and wait for the error to arrive from the X server.
 *
 * This API exists on all platforms but only does anything on X.
 *
 * You can use gdk_x11_display_error_trap_push() to ignore errors
 * on only a single display.
 *
 * <example>
 * <title>Trapping an X error</title>
 * <programlisting>
 * gdk_error_trap_push (<!-- -->);
 *
 *  // ... Call the X function which may cause an error here ...
 *
 *
 * if (gdk_error_trap_pop (<!-- -->))
 *  {
 *    // ... Handle the error here ...
 *  }
 * </programlisting>
 * </example>
 *
 */
void
gdk_error_trap_push (void)
{
  GdkGlobalErrorTrap *trap;
  GdkDisplayManager *manager;
  GSList *tmp_list;

  trap = g_slice_new (GdkGlobalErrorTrap);
  manager = gdk_display_manager_get ();
  trap->displays = gdk_display_manager_list_displays (manager);

  g_slist_foreach (trap->displays, (GFunc) g_object_ref, NULL);
  for (tmp_list = trap->displays;
       tmp_list != NULL;
       tmp_list = tmp_list->next)
    {
      gdk_x11_display_error_trap_push (tmp_list->data);
    }
385

386
  g_queue_push_head (&gdk_error_traps, trap);
387
388
}

389
390
static gint
gdk_error_trap_pop_internal (gboolean need_code)
391
{
392
  GdkGlobalErrorTrap *trap;
393
  gint result;
394
  GSList *tmp_list;
395

396
  trap = g_queue_pop_head (&gdk_error_traps);
397

398
399
400
401
402
403
404
405
406
407
408
409
410
  g_return_val_if_fail (trap != NULL, Success);

  result = Success;
  for (tmp_list = trap->displays;
       tmp_list != NULL;
       tmp_list = tmp_list->next)
    {
      gint code = Success;

      if (need_code)
        code = gdk_x11_display_error_trap_pop (tmp_list->data);
      else
        gdk_x11_display_error_trap_pop_ignored (tmp_list->data);
411

412
413
414
415
      /* we use the error on the last display listed, why not. */
      if (code != Success)
        result = code;
    }
416

417
418
  g_slist_foreach (trap->displays, (GFunc) g_object_unref, NULL);
  g_slist_free (trap->displays);
419

420
  g_slice_free (GdkGlobalErrorTrap, trap);
421

422
423
424
  return result;
}

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
 * gdk_error_trap_pop_ignored:
 *
 * Removes an error trap pushed with gdk_error_trap_push(), but
 * without bothering to wait and see whether an error occurred.  If an
 * error arrives later asynchronously that was triggered while the
 * trap was pushed, that error will be ignored.
 *
 * Since: 3.0
 */
void
gdk_error_trap_pop_ignored (void)
{
  gdk_error_trap_pop_internal (FALSE);
}

/**
 * gdk_error_trap_pop:
 *
 * Removes an error trap pushed with gdk_error_trap_push().
 * May block until an error has been definitively received
 * or not received from the X server. gdk_error_trap_pop_ignored()
 * is preferred if you don't need to know whether an error
 * occurred, because it never has to block. If you don't
 * need the return value of gdk_error_trap_pop(), use
 * gdk_error_trap_pop_ignored().
 *
 * Prior to GDK 3.0, this function would not automatically
 * sync for you, so you had to gdk_flush() if your last
 * call to Xlib was not a blocking round trip.
 *
 * Return value: X error code or 0 on success
 */
gint
gdk_error_trap_pop (void)
{
  return gdk_error_trap_pop_internal (TRUE);
}

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
 * _gdk_send_xevent:
 * @display: #GdkDisplay which @window is on
 * @window: window ID to which to send the event
 * @propagate: %TRUE if the event should be propagated if the target window
 *             doesn't handle it.
 * @event_mask: event mask to match against, or 0 to send it to @window
 *              without regard to event masks.
 * @event_send: #XEvent to send
 * 
 * Send an event, like XSendEvent(), but trap errors and check
 * the result.
 * 
 * Return value: %TRUE if sending the event succeeded.
 **/
479
gint 
480
481
482
483
484
_gdk_send_xevent (GdkDisplay *display,
		  Window      window, 
		  gboolean    propagate, 
		  glong       event_mask,
		  XEvent     *event_send)
485
{
486
  gboolean result;
487

488
  if (gdk_display_is_closed (display))
489
490
    return FALSE;

491
  gdk_x11_display_error_trap_push (display);
492
493
494
  result = XSendEvent (GDK_DISPLAY_XDISPLAY (display), window, 
		       propagate, event_mask, event_send);
  XSync (GDK_DISPLAY_XDISPLAY (display), False);
495
  
496
  if (gdk_x11_display_error_trap_pop (display))
497
498
499
    return FALSE;
 
  return result;
500
}
501
502

void
503
_gdk_region_get_xrectangles (const cairo_region_t *region,
504
505
506
507
                             gint             x_offset,
                             gint             y_offset,
                             XRectangle     **rects,
                             gint            *n_rects)
508
{
509
510
511
  XRectangle *rectangles;
  cairo_rectangle_int_t box;
  gint i, n;
512
  
513
514
515
516
  n = cairo_region_num_rectangles (region);
  rectangles = g_new (XRectangle, n);

  for (i = 0; i < n; i++)
517
    {
518
519
520
521
522
      cairo_region_get_rectangle (region, i, &box);
      rectangles[i].x = CLAMP (box.x + x_offset, G_MINSHORT, G_MAXSHORT);
      rectangles[i].y = CLAMP (box.y + y_offset, G_MINSHORT, G_MAXSHORT);
      rectangles[i].width = CLAMP (box.width, G_MINSHORT, G_MAXSHORT);
      rectangles[i].height = CLAMP (box.height, G_MINSHORT, G_MAXSHORT);
523
524
    }

525
  *n_rects = n;
526
527
  *rects = rectangles;
}
528

Matthias Clasen's avatar
Matthias Clasen committed
529
530
531
532
533
534
535
536
/**
 * gdk_x11_grab_server:
 * 
 * Call gdk_x11_display_grab() on the default display. 
 * To ungrab the server again, use gdk_x11_ungrab_server(). 
 *
 * gdk_x11_grab_server()/gdk_x11_ungrab_server() calls can be nested.
 **/ 
537
void
Matthias Clasen's avatar
Matthias Clasen committed
538
gdk_x11_grab_server (void)
539
{
Owen Taylor's avatar
Owen Taylor committed
540
  gdk_x11_display_grab (gdk_display_get_default ());
541
542
}

Matthias Clasen's avatar
Matthias Clasen committed
543
544
545
546
547
548
/**
 * gdk_x11_ungrab_server:
 *
 * Ungrab the default display after it has been grabbed with 
 * gdk_x11_grab_server(). 
 **/
549
void
Matthias Clasen's avatar
Matthias Clasen committed
550
gdk_x11_ungrab_server (void)
551
{
Owen Taylor's avatar
Owen Taylor committed
552
  gdk_x11_display_ungrab (gdk_display_get_default ());
553
}
554
555
556
557
558
559
560

/**
 * gdk_x11_get_default_screen:
 * 
 * Gets the default GTK+ screen number.
 * 
 * Return value: returns the screen number specified by
561
562
 *   the --display command line option or the DISPLAY environment
 *   variable when gdk_init() calls XOpenDisplay().
563
564
565
566
 **/
gint
gdk_x11_get_default_screen (void)
{
Owen Taylor's avatar
Owen Taylor committed
567
  return gdk_screen_get_number (gdk_screen_get_default ());
568
569
}

Matthias Clasen's avatar
Matthias Clasen committed
570
571
572
573
574
575
576
577
/**
 * gdk_x11_get_default_root_xwindow:
 * 
 * Gets the root window of the default screen 
 * (see gdk_x11_get_default_screen()).  
 * 
 * Return value: an Xlib <type>Window</type>.
 **/
578
579
580
Window
gdk_x11_get_default_root_xwindow (void)
{
Owen Taylor's avatar
Owen Taylor committed
581
  return GDK_SCREEN_XROOTWIN (gdk_screen_get_default ());
582
583
}

Matthias Clasen's avatar
Matthias Clasen committed
584
585
586
587
588
589
590
591
592
/**
 * gdk_x11_get_default_xdisplay:
 * 
 * Gets the default GTK+ display.
 * 
 * Return value: the Xlib <type>Display*</type> for the display
 * specified in the <option>--display</option> command line option 
 * or the <envar>DISPLAY</envar> environment variable.
 **/
593
594
595
Display *
gdk_x11_get_default_xdisplay (void)
{
Owen Taylor's avatar
Owen Taylor committed
596
  return GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());
597
}