summaryrefslogtreecommitdiff
path: root/libsn/sn-common.c
blob: be4831703a0a71242f45bd0611b65eb5a8c08c7f (plain)
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
/* 
 * Copyright (C) 2002 Red Hat, Inc.
 * Copyright (C) 2009 Julien Danjou <julien@danjou.info>
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <config.h>
#include "sn-common.h"
#include "sn-internals.h"

#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>

struct SnDisplay
{
  int refcount;
  enum SnDisplayType type;
  union
  {
      struct
      {
          Display *xdisplay;
          Screen **screens;
          SnDisplayErrorTrapPush push_trap_func;
          SnDisplayErrorTrapPop  pop_trap_func;
      } xlib;
      struct
      {
          xcb_connection_t *xconnection;
          xcb_screen_t **screens;
          SnXcbDisplayErrorTrapPush push_trap_func;
          SnXcbDisplayErrorTrapPop  pop_trap_func;
      } xcb;
  } x;
  int n_screens;
  SnList *xmessage_funcs;
  SnList *pending_messages;
};

/**
 * sn_display_new:
 * @xdisplay: an X window system display
 * @push_trap_func: function to push an X error trap
 * @pop_trap_func: function to pop an X error trap
 * 
 * Creates a new #SnDisplay object, containing
 * data that libsn associates with an X display.
 *
 * @push_trap_func should be a function that causes X errors to be
 * ignored until @pop_trap_func is called as many times as
 * @push_trap_func has been called. (Nested push/pop pairs must be
 * supported.) The outermost @pop_trap_func in a set of nested pairs
 * must call XSync() to ensure that all errors that will occur have in
 * fact occurred. These functions are used to avoid X errors due to
 * BadWindow and such.
 * 
 * Return value: the new #SnDisplay
 **/
SnDisplay*
sn_display_new (Display                *xdisplay,
                SnDisplayErrorTrapPush  push_trap_func,
                SnDisplayErrorTrapPop   pop_trap_func)
{
  SnDisplay *display;
  int i;
  
  display = sn_new0 (SnDisplay, 1);

  display->type = SN_DISPLAY_TYPE_XLIB;
  display->x.xlib.xdisplay = xdisplay;
  display->n_screens = ScreenCount (xdisplay);
  display->x.xlib.screens = sn_new (Screen*, display->n_screens);
  display->refcount = 1;

  display->x.xlib.push_trap_func = push_trap_func;
  display->x.xlib.pop_trap_func = pop_trap_func;

  for (i = 0; i < display->n_screens; ++i)
    display->x.xlib.screens[i] = ScreenOfDisplay (display->x.xlib.xdisplay, i);

  return display;
}

/**
 * sn_xcb_display_new:
 * @xdisplay: an X window system connection
 * @push_trap_func: function to push an X error trap
 * @pop_trap_func: function to pop an X error trap
 *
 * Creates a new #SnDisplay object, containing
 * data that libsn associates with an X connection.
 *
 * @push_trap_func should be a function that causes X errors to be
 * ignored until @pop_trap_func is called as many times as
 * @push_trap_func has been called. (Nested push/pop pairs must be
 * supported.)
 * These functions are used to avoid X errors due to BadWindow and
 * such.
 *
 * Return value: the new #SnDisplay
 **/
SnDisplay*
sn_xcb_display_new (xcb_connection_t          *xconnection,
                    SnXcbDisplayErrorTrapPush  push_trap_func,
                    SnXcbDisplayErrorTrapPop   pop_trap_func)
{
  SnDisplay *display;
  int i;

  display = sn_new0 (SnDisplay, 1);

  display->type = SN_DISPLAY_TYPE_XCB;
  display->x.xcb.xconnection = xconnection;
  display->n_screens = xcb_setup_roots_length (xcb_get_setup (xconnection));
  display->x.xcb.screens = sn_new (xcb_screen_t*, display->n_screens);
  display->refcount = 1;

  display->x.xcb.push_trap_func = push_trap_func;
  display->x.xcb.pop_trap_func = pop_trap_func;
  
  for (i = 0; i < display->n_screens; ++i)
    display->x.xcb.screens[i] = xcb_aux_get_screen(xconnection, i);

  return display;
}

/**
 * sn_display_ref:
 * @display: an #SnDisplay
 * 
 * Increment the reference count for @display
 **/
void
sn_display_ref (SnDisplay *display)
{
  display->refcount += 1;
}

/**
 * sn_display_unref:
 * @display: an #SnDisplay
 * 
 * Decrement the reference count for @display, freeing
 * display if the reference count reaches zero.
 **/
void
sn_display_unref (SnDisplay *display)
{
  display->refcount -= 1;
  if (display->refcount == 0)
    {
      if (display->xmessage_funcs)
        sn_list_free (display->xmessage_funcs);
      if (display->pending_messages)
        sn_list_free (display->pending_messages);
      switch (display->type)
      {
       case SN_DISPLAY_TYPE_XCB:
        sn_free (display->x.xcb.screens);
        break;
       case SN_DISPLAY_TYPE_XLIB:
        sn_free (display->x.xlib.screens);
        break;
      }
      sn_free (display);
    }
}

/**
 * sn_display_get_x_display:
 * @display: an #SnDisplay
 * 
 * 
 * 
 * Return value: X display for this #SnDisplay
 **/
Display*
sn_display_get_x_display (SnDisplay *display)
{
  if(display->type == SN_DISPLAY_TYPE_XLIB)
      return display->x.xlib.xdisplay;
  return NULL;
}

/**
 * sn_display_get_x_connection:
 * @display: an #SnDisplay
 *
 *
 *
 * Return value: X connection for this #SnDisplay
 **/
xcb_connection_t*
sn_display_get_x_connection(SnDisplay *display)
{
  if(display->type == SN_DISPLAY_TYPE_XCB)
      return display->x.xcb.xconnection;
  return NULL;
}

/**
 * sn_internal_display_get_id:
 * @display: an #SnDisplay
 *
 *
 *
 * Return value: X display id.
 **/
void *
sn_internal_display_get_id (SnDisplay *display)
{
  switch (display->type)
  {
   case SN_DISPLAY_TYPE_XLIB:
    return display->x.xlib.xdisplay;
   case SN_DISPLAY_TYPE_XCB:
    return display->x.xcb.xconnection;
  }
  return NULL;
}

/**
 * sn_internal_display_get_x_screen:
 * @display: an #SnDisplay
 * @number: screen number to get
 *
 * Gets a screen by number; if the screen number
 * does not exist, returns %NULL.
 *
 * Return value: X screen or %NULL
 **/
Screen*
sn_internal_display_get_x_screen (SnDisplay *display,
                                  int        number)
{
  if (display->type != SN_DISPLAY_TYPE_XLIB || number < 0 || number >= display->n_screens)
    return NULL;
  else
    return display->x.xlib.screens[number];
}

/**
 * sn_internal_display_get_root_window:
 * @display: an #SnDisplay
 * @number: screen number to get root window from
 *
 * Gets a root window; if the screen number
 * does not exist, returns %NULL.
 *
 * Return value: X root window or %NULL
 **/
Window
sn_internal_display_get_root_window (SnDisplay *display,
                                     int       number)
{
    if (number >= 0 && number < display->n_screens)
      switch (display->type)
      {
       case SN_DISPLAY_TYPE_XLIB:
        return RootWindow (display->x.xlib.xdisplay, number);
       case SN_DISPLAY_TYPE_XCB:
        return display->x.xcb.screens[number]->root;
      }
    return None;
}

/**
 * sn_internal_display_get_type
 * @display: an #SnDisplay
 *
 *
 *
 * Return value: X connection type
 */
enum SnDisplayType
sn_internal_display_get_type (SnDisplay *display)
{
    return display->type;
}

/**
 * sn_internal_display_get_x_screen:
 * @display: an #SnDisplay
 * @number: screen number to get
 * 
 * Gets a screen by number; if the screen number
 * does not exist, returns %NULL.
 * 
 * Return value: X screen or %NULL
 **/
xcb_screen_t*
sn_internal_display_get_xcb_screen (SnDisplay *display,
                                    int        number)
{
  if (display->type != SN_DISPLAY_TYPE_XCB || number < 0 || number >= display->n_screens)
    return NULL;
  else
    return display->x.xcb.screens[number];
}

/**
 * sn_internal_display_get_screen_number:
 * @display an #SnDisplay
 *
 *
 *
 * Return value: The number of screen for this #SnDisplay
 **/
int
sn_internal_display_get_screen_number (SnDisplay *display)
{
    return display->n_screens;
}

/**
 * sn_display_process_event:
 * @display: a display
 * @xevent: X event
 * 
 * libsn should be given a chance to see all X events by passing them
 * to this function. If the event was a property notify or client
 * message related to the launch feedback protocol, the
 * sn_display_process_event() returns true. Calling
 * sn_display_process_event() is not currently required for launchees,
 * only launchers and launch feedback displayers. The function returns
 * false for mapping, unmapping, window destruction, and selection
 * events even if they were involved in launch feedback.
 * 
 * Return value: true if the event was a property notify or client message involved in launch feedback
 **/
sn_bool_t
sn_display_process_event (SnDisplay *display,
                          XEvent    *xevent)
{
  sn_bool_t retval;

  retval = FALSE;

  if (sn_internal_monitor_process_event (display))
    retval = TRUE;

  if (sn_internal_xmessage_process_event (display, xevent))
    retval = TRUE;
  
  return retval;
}

/**
 * sn_xcb_display_process_event:
 * @display: a display
 * @xevent: X event
 *
 * libsn should be given a chance to see all X events by passing them
 * to this function. If the event was a property notify or client
 * message related to the launch feedback protocol, the
 * sn_display_process_event() returns true. Calling
 * sn_display_process_event() is not currently required for launchees,
 * only launchers and launch feedback displayers. The function returns
 * false for mapping, unmapping, window destruction, and selection
 * events even if they were involved in launch feedback.
 *
 * Return value: true if the event was a property notify or client message involved in launch feedback
 **/
sn_bool_t
sn_xcb_display_process_event (SnDisplay           *display,
                              xcb_generic_event_t *xevent)
{
  sn_bool_t retval;

  retval = FALSE;

  if (sn_internal_monitor_process_event (display))
    retval = TRUE;

  if (sn_xcb_internal_xmessage_process_event (display, xevent))
    retval = TRUE;

  return retval;
}

/**
 * sn_display_error_trap_push:
 * @display: a display
 *
 *  Calls the push_trap_func from sn_display_new() if non-NULL.
 **/
void
sn_display_error_trap_push (SnDisplay *display)
{
  switch (display->type)
  {
   case SN_DISPLAY_TYPE_XCB:
    if (display->x.xcb.push_trap_func)
      (* display->x.xcb.push_trap_func) (display, display->x.xcb.xconnection);
    break;
   case SN_DISPLAY_TYPE_XLIB:
    if (display->x.xlib.push_trap_func)
      (* display->x.xlib.push_trap_func) (display, display->x.xlib.xdisplay);
    break;
  }
}

/**
 * sn_display_error_trap_pop:
 * @display: a display
 *
 *  Calls the pop_trap_func from sn_display_new() if non-NULL.
 **/
void
sn_display_error_trap_pop  (SnDisplay *display)
{
  switch (display->type)
  {
   case SN_DISPLAY_TYPE_XCB:
    if (display->x.xcb.pop_trap_func)
      (* display->x.xcb.pop_trap_func) (display, display->x.xcb.xconnection);
    break;
   case SN_DISPLAY_TYPE_XLIB:
    if (display->x.xlib.pop_trap_func)
      (* display->x.xlib.pop_trap_func) (display, display->x.xlib.xdisplay);
    break;
  }
}

void
sn_internal_display_get_xmessage_data (SnDisplay              *display,
                                       SnList                **funcs,
                                       SnList                **pending)
{
  if (display->xmessage_funcs == NULL)
    display->xmessage_funcs = sn_list_new ();

  if (display->pending_messages == NULL)
    display->pending_messages = sn_list_new ();
  
  if (funcs)
    *funcs = display->xmessage_funcs;
  if (pending)
    *pending = display->pending_messages;
}