1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* CDK - 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
 * modify it under the terms of the GNU Lesser General Public
 * 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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Modified by the CTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the CTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * CTK+ at ftp://ftp.ctk.org/pub/ctk/. 
 */

#include "config.h"

#include "cdkdeviceprivate.h"
#include "cdkinternals.h"
#include "cdkintl.h"
#include "cdkasync.h"
#include "cdkdisplay-x11.h"
#include "cdkprivate-x11.h"

#include <glib/gprintf.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>

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

#ifdef HAVE_XKB
#include <X11/XKBlib.h>
#endif

/**
 * SECTION:x_interaction
 * @Short_description: X backend-specific functions
 * @Title: X Window System Interaction
 *
 * The functions in this section are specific to the CDK X11 backend.
 * To use them, you need to include the `<cdk/cdkx.h>` header and use
 * the X11-specific pkg-config files to build your application (either
 * `cdk-x11-3.0` or `ctk+-x11-3.0`).
 *
 * To make your code compile with other CDK backends, guard backend-specific
 * calls by an ifdef as follows. Since CDK may be built with multiple
 * backends, you should also check for the backend that is in use (e.g. by
 * using the CDK_IS_X11_DISPLAY() macro).
 * |[
 * #ifdef CDK_WINDOWING_X11
 *   if (CDK_IS_X11_DISPLAY (display))
 *     {
 *       // make X11-specific calls here
 *     }
 *   else
 * #endif
 * #ifdef CDK_WINDOWING_QUARTZ
 *   if (CDK_IS_QUARTZ_DISPLAY (display))
 *     {
 *       // make Quartz-specific calls here
 *     }
 *   else
 * #endif
 *   g_error ("Unsupported CDK backend");
 * ]|
 */

typedef struct _CdkPredicate CdkPredicate;

struct _CdkPredicate
{
  CdkEventFunc func;
  gpointer data;
};

/* non-CDK previous error handler */
typedef int (*CdkXErrorHandler) (Display *, XErrorEvent *);
static CdkXErrorHandler _cdk_old_error_handler;
/* number of times we've pushed the CDK error handler */
static int _cdk_error_handler_push_count = 0;

/*
 * Private function declarations
 */

static int	    cdk_x_error			 (Display     *display, 
						  XErrorEvent *error);
static int	    cdk_x_io_error		 (Display     *display);

void
_cdk_x11_windowing_init (void)
{
  XSetErrorHandler (cdk_x_error);
  XSetIOErrorHandler (cdk_x_io_error);

  cdk_window_add_filter (NULL,
                         _cdk_wm_protocols_filter,
                         NULL);
  cdk_window_add_filter (NULL,
                         _cdk_x11_dnd_filter,
                         NULL);
}

CdkGrabStatus
_cdk_x11_convert_grab_status (gint status)
{
  switch (status)
    {
    case GrabSuccess:
      return CDK_GRAB_SUCCESS;
    case AlreadyGrabbed:
      return CDK_GRAB_ALREADY_GRABBED;
    case GrabInvalidTime:
      return CDK_GRAB_INVALID_TIME;
    case GrabNotViewable:
      return CDK_GRAB_NOT_VIEWABLE;
    case GrabFrozen:
      return CDK_GRAB_FROZEN;
    }

  g_assert_not_reached();

  return 0;
}

/*
 * _cdk_x11_window_grab_check_unmap:
 * @window: a #CdkWindow
 * @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
_cdk_x11_window_grab_check_unmap (CdkWindow *window,
                                  gulong     serial)
{
  CdkDisplay *display = cdk_window_get_display (window);
  CdkDeviceManager *device_manager;
  GList *devices, *d;

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
  device_manager = cdk_display_get_device_manager (display);

  /* Get all devices */
  devices = cdk_device_manager_list_devices (device_manager, CDK_DEVICE_TYPE_MASTER);
  devices = g_list_concat (devices, cdk_device_manager_list_devices (device_manager, CDK_DEVICE_TYPE_SLAVE));
  devices = g_list_concat (devices, cdk_device_manager_list_devices (device_manager, CDK_DEVICE_TYPE_FLOATING));
  G_GNUC_END_IGNORE_DEPRECATIONS;

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

  g_list_free (devices);
}

/*
 * _cdk_x11_window_grab_check_destroy:
 * @window: a #CdkWindow
 * 
 * Checks to see if window is the current grab window, and if
 * so, clear the current grab window.
 **/
void
_cdk_x11_window_grab_check_destroy (CdkWindow *window)
{
  CdkDisplay *display = cdk_window_get_display (window);
  CdkDeviceManager *device_manager;
  CdkDeviceGrabInfo *grab;
  GList *devices, *d;

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
  device_manager = cdk_display_get_device_manager (display);

  /* Get all devices */
  devices = cdk_device_manager_list_devices (device_manager, CDK_DEVICE_TYPE_MASTER);
  devices = g_list_concat (devices, cdk_device_manager_list_devices (device_manager, CDK_DEVICE_TYPE_SLAVE));
  devices = g_list_concat (devices, cdk_device_manager_list_devices (device_manager, CDK_DEVICE_TYPE_FLOATING));
  G_GNUC_END_IGNORE_DEPRECATIONS;

  for (d = devices; d; d = d->next)
    {
      /* Make sure there is no lasting grab in this native window */
      grab = _cdk_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;
        }
    }

  g_list_free (devices);
}

/*
 *--------------------------------------------------------------
 * cdk_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
cdk_x_io_error (Display *display)
{
  /* 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 g_debug() instead of g_warning(), because g_warning()
   * could possibly be redirected to the log
   */
  g_debug ("%s: Fatal IO error %d (%s) on X server %s.\n",
           g_get_prgname (),
           errno, g_strerror (errno),
           display ? DisplayString (display) : cdk_get_display_arg_name ());

  _exit (1);
}

/* X error handler. Keep the name the same because people are used to
 * breaking on it in the debugger.
 */
static int
cdk_x_error (Display	 *xdisplay,<--- Parameter 'xdisplay' can be declared as pointer to const
	     XErrorEvent *error)
{
  if (error->error_code)
    {
      CdkDisplay *error_display;
      CdkDisplayManager *manager;
      GSList *displays;

      /* Figure out which CdkDisplay if any got the error. */
      error_display = NULL;
      manager = cdk_display_manager_get ();
      displays = cdk_display_manager_list_displays (manager);
      while (displays != NULL)
        {
          CdkX11Display *cdk_display = displays->data;

          if (CDK_IS_X11_DISPLAY (cdk_display) &&
              xdisplay == cdk_display->xdisplay)
            {
              error_display = CDK_DISPLAY (cdk_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 CDK. Ignore. */

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

  return 0;
}

void
_cdk_x11_error_handler_push (void)
{
  CdkXErrorHandler previous;

  previous = XSetErrorHandler (cdk_x_error);

  if (_cdk_error_handler_push_count > 0)
    {
      if (previous != cdk_x_error)<--- You might need to cast the function pointer here
        g_warning ("XSetErrorHandler() called with a CDK error trap pushed. Don't do that.");
    }
  else
    {
      _cdk_old_error_handler = previous;
    }

  _cdk_error_handler_push_count += 1;
}

void
_cdk_x11_error_handler_pop  (void)
{
  g_return_if_fail (_cdk_error_handler_push_count > 0);

  _cdk_error_handler_push_count -= 1;

  if (_cdk_error_handler_push_count == 0)
    {
      XSetErrorHandler (_cdk_old_error_handler);
      _cdk_old_error_handler = NULL;
    }
}

gint
_cdk_x11_display_send_xevent (CdkDisplay *display,
                              Window      window,
                              gboolean    propagate,
                              glong       event_mask,
                              XEvent     *event_send)
{
  gboolean result;

  if (cdk_display_is_closed (display))
    return FALSE;

  cdk_x11_display_error_trap_push (display);
  result = XSendEvent (CDK_DISPLAY_XDISPLAY (display), window,
                       propagate, event_mask, event_send);
  XSync (CDK_DISPLAY_XDISPLAY (display), False);

  if (cdk_x11_display_error_trap_pop (display))
    return FALSE;

  return result;
}

void
_cdk_x11_region_get_xrectangles (const cairo_region_t *region,
                                 gint             x_offset,
                                 gint             y_offset,
                                 gint             scale,
                                 XRectangle     **rects,
                                 gint            *n_rects)
{
  XRectangle *rectangles;
  cairo_rectangle_int_t box;
  gint i, n;
  
  n = cairo_region_num_rectangles (region);
  rectangles = g_new (XRectangle, n);

  for (i = 0; i < n; i++)
    {
      cairo_region_get_rectangle (region, i, &box);
      rectangles[i].x = CLAMP ((box.x + x_offset) * scale, G_MINSHORT, G_MAXSHORT);
      rectangles[i].y = CLAMP ((box.y + y_offset) * scale, G_MINSHORT, G_MAXSHORT);
      rectangles[i].width = CLAMP (box.width * scale, G_MINSHORT, G_MAXSHORT);
      rectangles[i].height = CLAMP (box.height * scale, G_MINSHORT, G_MAXSHORT);
    }

  *n_rects = n;
  *rects = rectangles;
}

/**
 * cdk_x11_grab_server:
 * 
 * Call cdk_x11_display_grab() on the default display. 
 * To ungrab the server again, use cdk_x11_ungrab_server(). 
 *
 * cdk_x11_grab_server()/cdk_x11_ungrab_server() calls can be nested.
 **/ 
void
cdk_x11_grab_server (void)
{
  cdk_x11_display_grab (cdk_display_get_default ());
}

/**
 * cdk_x11_ungrab_server:
 *
 * Ungrab the default display after it has been grabbed with 
 * cdk_x11_grab_server(). 
 **/
void
cdk_x11_ungrab_server (void)
{
  cdk_x11_display_ungrab (cdk_display_get_default ());
}

/**
 * cdk_x11_get_default_screen:
 * 
 * Gets the default CTK+ screen number.
 * 
 * Returns: returns the screen number specified by
 *   the --display command line option or the DISPLAY environment
 *   variable when cdk_init() calls XOpenDisplay().
 **/
gint
cdk_x11_get_default_screen (void)
{
  return cdk_x11_screen_get_number (cdk_screen_get_default ());
}

/**
 * cdk_x11_get_default_root_xwindow:
 * 
 * Gets the root window of the default screen 
 * (see cdk_x11_get_default_screen()).  
 * 
 * Returns: an Xlib Window.
 **/
Window
cdk_x11_get_default_root_xwindow (void)
{
  return CDK_SCREEN_XROOTWIN (cdk_screen_get_default ());
}

/**
 * cdk_x11_get_default_xdisplay:
 * 
 * Gets the default CTK+ display.
 * 
 * Returns: (transfer none): the Xlib Display* for
 * the display specified in the `--display` command
 * line option or the `DISPLAY` environment variable.
 **/
Display *
cdk_x11_get_default_xdisplay (void)
{
  return CDK_DISPLAY_XDISPLAY (cdk_display_get_default ());
}

/**
 * cdk_x11_get_parent_relative_pattern:
 *
 * Used with cdk_window_set_background_pattern() to inherit background from
 * parent window. Useful for imitating transparency when compositing is not
 * available. Otherwise behaves like a transparent pattern.
 *
 * Since: 3.24.2
 *
 * Deprecated: 3.24: Don't use this function
 **/
cairo_pattern_t *
cdk_x11_get_parent_relative_pattern (void)
{
  static cairo_pattern_t *parent_relative_pattern = NULL;

  if (G_UNLIKELY (parent_relative_pattern == NULL))
    parent_relative_pattern = cairo_pattern_create_rgba (0.0, 0.0, 0.0, 0.0);

  return parent_relative_pattern;
}