summaryrefslogtreecommitdiff
path: root/libsn/sn-common.c
blob: 6da3eedc1b93fb67e2a5a1f3b1feb7344cfe31da (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
/* 
 * Copyright (C) 2002 Red Hat, Inc.
 * 
 * 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"

struct SnDisplay
{
  int refcount;
  Display *xdisplay;
  int n_screens;
  Screen **screens;
  SnDisplayErrorTrapPush push_trap_func;
  SnDisplayErrorTrapPop  pop_trap_func;
  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->xdisplay = xdisplay;
  display->n_screens = ScreenCount (xdisplay);
  display->screens = sn_new (Screen*, display->n_screens);
  display->refcount = 1;

  display->push_trap_func = push_trap_func;
  display->pop_trap_func = pop_trap_func;
  
  for (i = 0; i < display->n_screens; ++i)
    display->screens[i] = ScreenOfDisplay (display->xdisplay, 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);
      sn_free (display->screens);
      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)
{

  return display->xdisplay;
}

/**
 * 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 (number < 0 || number >= display->n_screens)
    return NULL;
  else
    return display->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)
        return None;
    return RootWindow (display->xdisplay, 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_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)
{
  if (display->push_trap_func)
    (* display->push_trap_func) (display, display->xdisplay);
}

/**
 * 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)
{
  if (display->pop_trap_func)
    (* display->pop_trap_func) (display, display->xdisplay);
}

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;
}