summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/xcb/xcb-util/xcb_aux.c
blob: c81039885fde98a6f218972d1b1041e494db5a96 (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
/*
 * Copyright © 2008 Bart Massey <bart@cs.pdx.edu>
 * Copyright © 2008 Ian Osgood <iano@quirkster.com>
 * Copyright © 2008 Jamey Sharp <jamey@minilop.net>
 * Copyright © 2008 Josh Triplett <josh@freedesktop.org>
 *
 * 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 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.
 *
 * Except as contained in this notice, the names of the authors or
 * their institutions shall not be used in advertising or otherwise to
 * promote the sale, use or other dealings in this Software without
 * prior written authorization from the authors.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

/* Connection related functions */

uint8_t
xcb_aux_get_depth (xcb_connection_t *c,
                   xcb_screen_t     *screen)
{
  xcb_drawable_t            drawable;
  xcb_get_geometry_reply_t *geom;
  int                       depth = 0;

  drawable = screen->root;
  geom = xcb_get_geometry_reply (c, xcb_get_geometry(c, drawable), 0);

  if (geom) {
    depth = geom->depth;
    free (geom);
  }

  return depth;
}

uint8_t
xcb_aux_get_depth_of_visual (xcb_screen_t *screen,
			     xcb_visualid_t id)
{
    xcb_depth_iterator_t i;
    xcb_visualtype_iterator_t j;
    for (i = xcb_screen_allowed_depths_iterator(screen);
	 i.rem; xcb_depth_next(&i))
	for (j = xcb_depth_visuals_iterator(i.data);
	     j.rem; xcb_visualtype_next(&j))
	    if (j.data->visual_id == id)
		return i.data->depth;
    return 0;
}

xcb_screen_t *
xcb_aux_get_screen (xcb_connection_t *c,
                    int               screen)
{
  xcb_screen_iterator_t i = xcb_setup_roots_iterator(xcb_get_setup(c));
  for (; i.rem; --screen, xcb_screen_next(&i))
    if (screen == 0)
      return i.data;
  return 0;
}

xcb_visualtype_t *
xcb_aux_get_visualtype (xcb_connection_t *c,
                        int               scr,
                        xcb_visualid_t    vid)
{
  xcb_screen_t        *screen;
  xcb_depth_t         *depth;
  xcb_visualtype_iterator_t iter;
  int               cur;

  screen = xcb_aux_get_screen (c, scr);
  if (!screen) return NULL;

  depth = xcb_screen_allowed_depths_iterator(screen).data;
  if (!depth) return NULL;
   
   iter = xcb_depth_visuals_iterator(depth);
   for (cur = 0 ; cur < iter.rem ; xcb_visualtype_next(&iter), ++cur)
      if (vid == iter.data->visual_id)
	 return iter.data;

   return NULL;
}

xcb_visualtype_t *
xcb_aux_find_visual_by_id (xcb_screen_t *screen,
			   xcb_visualid_t id)
{
    xcb_depth_iterator_t i;
    xcb_visualtype_iterator_t j;
    for (i = xcb_screen_allowed_depths_iterator(screen);
	 i.rem; xcb_depth_next(&i))
	for (j = xcb_depth_visuals_iterator(i.data);
	     j.rem; xcb_visualtype_next(&j))
	    if (j.data->visual_id == id)
		return j.data;
    return 0;
}

xcb_visualtype_t *
xcb_aux_find_visual_by_attrs (xcb_screen_t *screen,
			      int8_t class,
			      int8_t depth)
{
    xcb_depth_iterator_t i;
    xcb_visualtype_iterator_t j;
    for (i = xcb_screen_allowed_depths_iterator(screen);
	 i.rem; xcb_depth_next(&i)) {
	if (depth != -1 && i.data->depth != depth)
	    continue;
	for (j = xcb_depth_visuals_iterator(i.data);
	     j.rem; xcb_visualtype_next(&j))
	    if (class == -1 || j.data->_class == class)
		return j.data;
    }
    return 0;
}

void
xcb_aux_sync (xcb_connection_t *c)
{
    free(xcb_get_input_focus_reply(c, xcb_get_input_focus(c), NULL));
}

/* structs instead of value lists */
/* TODO: generate the struct types and functions from protocol masks and descriptions */

/* This generic implementation of pack_list depends on:
   a) structs packed to uint32_t size
   b) structs consist of just uint32_t/int32_t fields in the same order as bitmask
*/

static void
pack_list( uint32_t mask, const uint32_t *src, uint32_t *dest )
{
	for ( ; mask; mask >>= 1, src++)
		if (mask & 1)
			*dest++ = *src;
}

xcb_void_cookie_t
xcb_aux_create_window (xcb_connection_t       *c,
                       uint8_t                depth,
                       xcb_window_t           wid,
                       xcb_window_t           parent,
                       int16_t                x,
                       int16_t                y,
                       uint16_t               width,
                       uint16_t               height,
                       uint16_t               border_width,
                       uint16_t               _class,
                       xcb_visualid_t         visual,
                       uint32_t               mask,
                       const xcb_params_cw_t *params)
{
	uint32_t value_list[16];
	pack_list(mask, (const uint32_t *)params, value_list);
	return xcb_create_window(c, depth, wid, parent,
		x, y, width, height, border_width,
		_class, visual, mask, value_list);
}

xcb_void_cookie_t
xcb_aux_create_window_checked (xcb_connection_t       *c,
			       uint8_t                depth,
			       xcb_window_t           wid,
			       xcb_window_t           parent,
			       int16_t                x,
			       int16_t                y,
			       uint16_t               width,
			       uint16_t               height,
			       uint16_t               border_width,
			       uint16_t               _class,
			       xcb_visualid_t         visual,
			       uint32_t               mask,
			       const xcb_params_cw_t *params)
{
	uint32_t value_list[16];
	pack_list(mask, (const uint32_t *)params, value_list);
	return xcb_create_window_checked(c, depth, wid, parent,
					 x, y, width, height, border_width,
					 _class, visual, mask, value_list);
}

xcb_void_cookie_t
xcb_aux_change_window_attributes_checked (xcb_connection_t      *c,
                                          xcb_window_t           window,
                                          uint32_t               mask,
                                          const xcb_params_cw_t *params)
{
	uint32_t value_list[16];
	pack_list(mask, (const uint32_t *)params, value_list);
	return xcb_change_window_attributes_checked( c, window, mask, value_list );
}

xcb_void_cookie_t
xcb_aux_change_window_attributes (xcb_connection_t      *c,
                                  xcb_window_t           window,
                                  uint32_t               mask,
                                  const xcb_params_cw_t *params)
{
	uint32_t value_list[16];
	pack_list(mask, (const uint32_t *)params, value_list);
	return xcb_change_window_attributes( c, window, mask, value_list );
}

xcb_void_cookie_t
xcb_aux_configure_window (xcb_connection_t                    *c,
                          xcb_window_t                         window,
                          uint16_t                             mask,
                          const xcb_params_configure_window_t *params)
{
	uint32_t value_list[8];
	pack_list(mask, (const uint32_t *)params, value_list);
	return xcb_configure_window( c, window, mask, value_list );
}

xcb_void_cookie_t
xcb_aux_create_gc (xcb_connection_t      *c,
                   xcb_gcontext_t         gid,
                   xcb_drawable_t         drawable,
                   uint32_t               mask,
                   const xcb_params_gc_t *params)
{
	uint32_t value_list[32];
	pack_list(mask, (const uint32_t *)params, value_list);
	return xcb_create_gc( c, gid, drawable, mask, value_list );
}

xcb_void_cookie_t
xcb_aux_create_gc_checked (xcb_connection_t      *c,
			   xcb_gcontext_t         gid,
			   xcb_drawable_t         drawable,
			   uint32_t               mask,
			   const xcb_params_gc_t *params)
{
	uint32_t value_list[32];
	pack_list(mask, (const uint32_t *)params, value_list);
	return xcb_create_gc_checked( c, gid, drawable, mask, value_list);
}

xcb_void_cookie_t
xcb_aux_change_gc (xcb_connection_t     *c,
                   xcb_gcontext_t        gc,
                   uint32_t              mask,
                   const xcb_params_gc_t *params)
{
	uint32_t value_list[32];
	pack_list(mask, (const uint32_t *)params, value_list);
	return xcb_change_gc( c, gc, mask, value_list );
}

xcb_void_cookie_t
xcb_aux_change_gc_checked (xcb_connection_t     *c,
			   xcb_gcontext_t        gc,
			   uint32_t              mask,
			   const xcb_params_gc_t *params)
{
	uint32_t value_list[32];
	pack_list(mask, (const uint32_t *)params, value_list);
	return xcb_change_gc_checked( c, gc, mask, value_list );
}

xcb_void_cookie_t
xcb_aux_change_keyboard_control (xcb_connection_t            *c,
                                 uint32_t                     mask,
                                 const xcb_params_keyboard_t *params)
{
	uint32_t value_list[16];
	pack_list(mask, (const uint32_t *)params, value_list);
	return xcb_change_keyboard_control( c, mask, value_list );
}

/* Color related functions */

/* Return true if the given color name can be translated locally,
   in which case load the components.  Otherwise, a lookup_color request
   will be needed, so return false. */
int
xcb_aux_parse_color(char *color_name,
		    uint16_t *red,  uint16_t *green,  uint16_t *blue)
{
    int n, r, g, b, i;
    if (!color_name || *color_name != '#')
	return 0;
    /*
     * Excitingly weird RGB parsing code from Xlib.
     */
    n = strlen (color_name);
    color_name++;
    n--;
    if (n != 3 && n != 6 && n != 9 && n != 12)
	return 0;
    n /= 3;
    g = b = 0;
    do {
	r = g;
	g = b;
	b = 0;
	for (i = n; --i >= 0; ) {
	    char c = *color_name++;
	    b <<= 4;
	    if (c >= '0' && c <= '9')
		b |= c - '0';
	    else if (c >= 'A' && c <= 'F')
		b |= c - ('A' - 10);
	    else if (c >= 'a' && c <= 'f')
		b |= c - ('a' - 10);
	    else return 0;
	}
    } while (*color_name != '\0');
    n <<= 2;
    n = 16 - n;
    *red = r << n;
    *green = g << n;
    *blue = b << n;
    return 1;
}

/* Drawing related functions */

/* Adapted from Xlib */
xcb_void_cookie_t
xcb_aux_set_line_attributes_checked (xcb_connection_t *dpy,
				     xcb_gcontext_t gc,
				     uint16_t linewidth,
				     int32_t linestyle,
				     int32_t capstyle,
				     int32_t joinstyle)
{
    uint32_t mask = 0;
    xcb_params_gc_t gv;

    XCB_AUX_ADD_PARAM(&mask, &gv, line_width, linewidth);
    XCB_AUX_ADD_PARAM(&mask, &gv, line_style, linestyle);
    XCB_AUX_ADD_PARAM(&mask, &gv, cap_style, capstyle);
    XCB_AUX_ADD_PARAM(&mask, &gv, join_style, joinstyle);
    return xcb_aux_change_gc_checked(dpy, gc, mask, &gv);
}

/* Adapted from Xlib */
/* XXX It would be wiser for apps just to call
   clear_area() directly. */
xcb_void_cookie_t
xcb_aux_clear_window(xcb_connection_t *  dpy,
		     xcb_window_t        w)
{
    return xcb_clear_area(dpy, 0, w, 0, 0, 0, 0);
}