Debian Codegraph @

xcb/3rdparty/dsimple.c

raw
1 /*
2
3 Copyright 1993, 1998 The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of The Open Group shall
23 not be used in advertising or otherwise to promote the sale, use or
24 other dealings in this Software without prior written authorization
25 from The Open Group.
26
27 */
28
29 //#include "config.h"
30
31 #include <xcb/xcb.h>
32 #include <xcb/xproto.h>
33 #ifdef USE_XCB_ICCCM
34 #include <xcb/xcb_icccm.h>
35 #endif
36 #include <X11/cursorfont.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include "clientwin.h"
42 #include "dsimple.h"
43
44 /*
45 * Just_display: A group of routines designed to make the writing of simple
46 * X11 applications which open a display but do not open
47 * any windows much faster and easier. Unless a routine says
48 * otherwise, it may be assumed to require program_name
49 * to be already defined on entry.
50 *
51 * Written by Mark Lillibridge. Last updated 7/1/87
52 */
53
54 /* This stuff is defined in the calling program by dsimple.h */
55 const char *program_name = "unknown_program";
56
57 /*
58 * Get_Display_Name (argc, argv) - return string representing display name
59 * that would be used given the specified argument (i.e. if it's NULL, check
60 * getenv("DISPLAY") - always returns a non-NULL pointer, though it may be
61 * an unwritable constant, so is safe to printf() on platforms that crash
62 * on NULL printf arguments.
63 */
64 const char *Get_Display_Name(const char *display_name)
65 {
66 const char *name = display_name;
67
68 if (!name) {
69 name = getenv("DISPLAY");
70 if (!name)
71 name = "";
72 }
73 return (name);
74 }
75
76 /*
77 * Setup_Display_And_Screen: This routine opens up the correct display (i.e.,
78 * it calls Get_Display_Name) and then stores a
79 * pointer to it in dpy. The default screen
80 * for this display is then stored in screen.
81 */
82 void Setup_Display_And_Screen(
83 const char *display_name,
84 xcb_connection_t **dpy, /* MODIFIED */
85 xcb_screen_t **screen) /* MODIFIED */
86 {
87 int screen_number, i, err;
88
89 /* Open Display */
90 *dpy = xcb_connect(display_name, &screen_number);
91 if ((err = xcb_connection_has_error(*dpy)) != 0) {
92 switch (err) {
93 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
94 Fatal_Error("Failed to allocate memory in xcb_connect");
95 case XCB_CONN_CLOSED_PARSE_ERR:
96 Fatal_Error("unable to parse display name \"%s\"",
97 Get_Display_Name(display_name));
98 #ifdef XCB_CONN_CLOSED_INVALID_SCREEN
99 case XCB_CONN_CLOSED_INVALID_SCREEN:
100 Fatal_Error("invalid screen %d in display \"%s\"",
101 screen_number, Get_Display_Name(display_name));
102 #endif
103 default:
104 Fatal_Error("unable to open display \"%s\"",
105 Get_Display_Name(display_name));
106 }
107 }
108
109 if (screen) {
110 /* find our screen */
111 const xcb_setup_t *setup = xcb_get_setup(*dpy);
112 xcb_screen_iterator_t screen_iter = xcb_setup_roots_iterator(setup);
113 int screen_count = xcb_setup_roots_length(setup);
114 if (screen_count <= screen_number) {
115 Fatal_Error("unable to access screen %d, max is %d",
116 screen_number, screen_count - 1);
117 }
118
119 for (i = 0; i < screen_number; i++)
120 xcb_screen_next(&screen_iter);
121 *screen = screen_iter.data;
122 }
123 }
124
125 /*
126 * xcb equivalent of XCreateFontCursor
127 */
128 static xcb_cursor_t
129 Create_Font_Cursor(xcb_connection_t *dpy, uint16_t glyph)
130 {
131 static xcb_font_t cursor_font;
132 xcb_cursor_t cursor;
133
134 if (!cursor_font) {
135 cursor_font = xcb_generate_id(dpy);
136 xcb_open_font(dpy, cursor_font, strlen("cursor"), "cursor");
137 }
138
139 cursor = xcb_generate_id(dpy);
140 xcb_create_glyph_cursor(dpy, cursor, cursor_font, cursor_font,
141 glyph, glyph + 1,
142 0, 0, 0, 0xffff, 0xffff, 0xffff); /* rgb, rgb */
143
144 return cursor;
145 }
146
147 /*
148 * Routine to let user select a window using the mouse
149 */
150
151 xcb_window_t Select_Window(xcb_connection_t *dpy,
152 const xcb_screen_t *screen,
153 int descend)
154 {
155 xcb_cursor_t cursor;
156 xcb_generic_event_t *event;
157 xcb_window_t target_win = XCB_WINDOW_NONE;
158 xcb_window_t root = screen->root;
159 int buttons = 0;
160 xcb_generic_error_t *err;
161 xcb_grab_pointer_cookie_t grab_cookie;
162 xcb_grab_pointer_reply_t *grab_reply;
163
164 /* Make the target cursor */
165 cursor = Create_Font_Cursor(dpy, XC_crosshair);
166
167 /* Grab the pointer using target cursor, letting it room all over */
168 grab_cookie = xcb_grab_pointer(dpy, False, root,
169 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE,
170 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
171 root, cursor, XCB_TIME_CURRENT_TIME);
172 grab_reply = xcb_grab_pointer_reply(dpy, grab_cookie, &err);
173 if (grab_reply->status != XCB_GRAB_STATUS_SUCCESS)
174 Fatal_Error("Can't grab the mouse.");
175
176 /* Let the user select a window... */
177 while ((target_win == XCB_WINDOW_NONE) || (buttons != 0)) {
178 /* allow one more event */
179 xcb_allow_events(dpy, XCB_ALLOW_SYNC_POINTER, XCB_TIME_CURRENT_TIME);
180 xcb_flush(dpy);
181 event = xcb_wait_for_event(dpy);
182 if (event == NULL)
183 Fatal_Error("Fatal IO error");
184 switch (event->response_type & 0x7f) {
185 case XCB_BUTTON_PRESS: {
186 xcb_button_press_event_t *bp = (xcb_button_press_event_t *)event;
187
188 if (target_win == XCB_WINDOW_NONE) {
189 target_win = bp->child; /* window selected */
190 if (target_win == XCB_WINDOW_NONE)
191 target_win = root;
192 }
193 buttons++;
194 break;
195 }
196 case XCB_BUTTON_RELEASE:
197 if (buttons > 0) /* there may have been some down before we started */
198 buttons--;
199 break;
200 default:
201 /* just discard all other events */
202 break;
203 }
204 free(event);
205 }
206
207 xcb_ungrab_pointer(dpy, XCB_TIME_CURRENT_TIME); /* Done with pointer */
208
209 if (!descend || (target_win == root))
210 return (target_win);
211
212 target_win = Find_Client(dpy, root, target_win);
213
214 return (target_win);
215 }
216
217 /*
218 * Window_With_Name: routine to locate a window with a given name on a display.
219 * If no window with the given name is found, 0 is returned.
220 * If more than one window has the given name, the first
221 * one found will be returned. Only top and its subwindows
222 * are looked at. Normally, top should be the RootWindow.
223 */
224
225 struct wininfo_cookies {
226 xcb_get_property_cookie_t get_net_wm_name;
227 xcb_get_property_cookie_t get_wm_name;
228 xcb_query_tree_cookie_t query_tree;
229 };
230
231 #ifndef USE_XCB_ICCCM
232 #define xcb_icccm_get_wm_name(Dpy, Win) \
233 xcb_get_property(Dpy, False, Win, XCB_ATOM_WM_NAME, \
234 XCB_GET_PROPERTY_TYPE_ANY, 0, BUFSIZ)
235 #endif
236
237 static xcb_atom_t atom_net_wm_name, atom_utf8_string;
238
239 #define xcb_get_net_wm_name(Dpy, Win) \
240 xcb_get_property(Dpy, False, Win, atom_net_wm_name, \
241 atom_utf8_string, 0, BUFSIZ)
242
243 static xcb_window_t
244 recursive_Window_With_Name(
245 xcb_connection_t *dpy,
246 xcb_window_t window,
247 struct wininfo_cookies *cookies,
248 const char *name,
249 size_t namelen)
250 {
251 xcb_window_t *children;
252 unsigned int nchildren;
253 unsigned int i;
254 xcb_window_t w = 0;
255 xcb_generic_error_t *err;
256 xcb_query_tree_reply_t *tree;
257 struct wininfo_cookies *child_cookies;
258 xcb_get_property_reply_t *prop;
259
260 if (cookies->get_net_wm_name.sequence) {
261 prop = xcb_get_property_reply(dpy, cookies->get_net_wm_name, &err);
262
263 if (prop) {
264 if (prop->type == atom_utf8_string) {
265 const char *prop_name = xcb_get_property_value(prop);
266 int prop_name_len = xcb_get_property_value_length(prop);
267
268 /* can't use strcmp, since prop.name is not null terminated */
269 if ((namelen == (size_t)prop_name_len) && memcmp(prop_name, name, namelen) == 0) {
270 w = window;
271 }
272 }
273 free(prop);
274 } else if (err) {
275 if (err->response_type == 0)
276 Print_X_Error(dpy, err);
277 return 0;
278 }
279 }
280
281 if (w) {
282 xcb_discard_reply(dpy, cookies->get_wm_name.sequence);
283 } else {
284 #ifdef USE_XCB_ICCCM
285 xcb_icccm_get_text_property_reply_t nameprop;
286
287 if (xcb_icccm_get_wm_name_reply(dpy, cookies->get_wm_name,
288 &nameprop, &err)) {
289 /* can't use strcmp, since nameprop.name is not null terminated */
290 if ((namelen == (size_t)nameprop.name_len) && memcmp(nameprop.name, name, namelen) == 0) {
291 w = window;
292 }
293
294 xcb_icccm_get_text_property_reply_wipe(&nameprop);
295 }
296 #else
297 prop = xcb_get_property_reply(dpy, cookies->get_wm_name, &err);
298
299 if (prop) {
300 if (prop->type == XCB_ATOM_STRING) {
301 const char *prop_name = xcb_get_property_value(prop);
302 int prop_name_len = xcb_get_property_value_length(prop);
303
304 /* can't use strcmp, since prop.name is not null terminated */
305 if ((namelen == (size_t)prop_name_len) && memcmp(prop_name, name, namelen) == 0) {
306 w = window;
307 }
308 }
309 free(prop);
310 }
311 #endif
312 else if (err) {
313 if (err->response_type == 0)
314 Print_X_Error(dpy, err);
315 return 0;
316 }
317 }
318
319 if (w) {
320 xcb_discard_reply(dpy, cookies->query_tree.sequence);
321 return w;
322 }
323
324 tree = xcb_query_tree_reply(dpy, cookies->query_tree, &err);
325 if (!tree) {
326 if (err->response_type == 0)
327 Print_X_Error(dpy, err);
328 return 0;
329 }
330
331 nchildren = (unsigned int)xcb_query_tree_children_length(tree);
332 children = xcb_query_tree_children(tree);
333 child_cookies = calloc(nchildren, sizeof(struct wininfo_cookies));
334
335 if (child_cookies == NULL)
336 Fatal_Error("Failed to allocate memory in recursive_Window_With_Name");
337
338 for (i = 0; i < nchildren; i++) {
339 if (atom_net_wm_name && atom_utf8_string)
340 child_cookies[i].get_net_wm_name =
341 xcb_get_net_wm_name(dpy, children[i]);
342 child_cookies[i].get_wm_name = xcb_icccm_get_wm_name(dpy, children[i]);
343 child_cookies[i].query_tree = xcb_query_tree(dpy, children[i]);
344 }
345 xcb_flush(dpy);
346
347 for (i = 0; i < nchildren; i++) {
348 w = recursive_Window_With_Name(dpy, children[i],
349 &child_cookies[i], name, namelen);
350 if (w)
351 break;
352 }
353
354 if (w) {
355 /* clean up remaining replies */
356 for (/* keep previous i */; i < nchildren; i++) {
357 if (child_cookies[i].get_net_wm_name.sequence)
358 xcb_discard_reply(dpy,
359 child_cookies[i].get_net_wm_name.sequence);
360 xcb_discard_reply(dpy, child_cookies[i].get_wm_name.sequence);
361 xcb_discard_reply(dpy, child_cookies[i].query_tree.sequence);
362 }
363 }
364
365 free(child_cookies);
366 free(tree); /* includes storage for children[] */
367 return (w);
368 }
369
370 xcb_window_t
371 Window_With_Name(
372 xcb_connection_t *dpy,
373 xcb_window_t top,
374 const char *name)
375 {
376 struct wininfo_cookies cookies;
377
378 atom_net_wm_name = Get_Atom(dpy, "_NET_WM_NAME");
379 atom_utf8_string = Get_Atom(dpy, "UTF8_STRING");
380
381 if (atom_net_wm_name && atom_utf8_string)
382 cookies.get_net_wm_name = xcb_get_net_wm_name(dpy, top);
383 cookies.get_wm_name = xcb_icccm_get_wm_name(dpy, top);
384 cookies.query_tree = xcb_query_tree(dpy, top);
385 xcb_flush(dpy);
386 return recursive_Window_With_Name(dpy, top, &cookies, name, strlen(name));
387 }
388
389 /*
390 * Standard fatal error routine - call like printf
391 */
392 void Fatal_Error(const char *msg, ...)
393 {
394 va_list args;
395 fflush(stdout);
396 fflush(stderr);
397 fprintf(stderr, "%s: error: ", program_name);
398 va_start(args, msg);
399 vfprintf(stderr, msg, args);
400 va_end(args);
401 fprintf(stderr, "\n");
402 exit(EXIT_FAILURE);
403 }
404
405 /*
406 * Print X error information like the default Xlib error handler
407 */
408 void Print_X_Error(
409 xcb_connection_t *dpy,
410 xcb_generic_error_t *err)
411 {
412 (void)dpy; // unused
413 char buffer[256] = "";
414
415 if ((err == NULL) || (err->response_type != 0)) /* not an error */
416 return;
417
418 /* Todo: find a more user friendly way to show request/extension info */
419 if (err->error_code >= 128) {
420 fprintf(stderr, "X Extension Error: Error code %d\n",
421 err->error_code);
422 } else {
423 switch (err->error_code) {
424 case XCB_REQUEST:
425 snprintf(buffer, sizeof(buffer), "Bad Request");
426 break;
427
428 case XCB_VALUE:
429 snprintf(buffer, sizeof(buffer),
430 "Bad Value: 0x%x", err->resource_id);
431 break;
432
433 case XCB_WINDOW:
434 snprintf(buffer, sizeof(buffer),
435 "Bad Window: 0x%x", err->resource_id);
436 break;
437
438 case XCB_PIXMAP:
439 snprintf(buffer, sizeof(buffer),
440 "Bad Pixmap: 0x%x", err->resource_id);
441 break;
442
443 case XCB_ATOM:
444 snprintf(buffer, sizeof(buffer),
445 "Bad Atom: 0x%x", err->resource_id);
446 break;
447
448 case XCB_CURSOR:
449 snprintf(buffer, sizeof(buffer),
450 "Bad Cursor: 0x%x", err->resource_id);
451 break;
452
453 case XCB_FONT:
454 snprintf(buffer, sizeof(buffer),
455 "Bad Font: 0x%x", err->resource_id);
456 break;
457
458 case XCB_MATCH:
459 snprintf(buffer, sizeof(buffer), "Bad Match");
460 break;
461
462 case XCB_DRAWABLE:
463 snprintf(buffer, sizeof(buffer),
464 "Bad Drawable: 0x%x", err->resource_id);
465 break;
466
467 case XCB_ACCESS:
468 snprintf(buffer, sizeof(buffer), "Access Denied");
469 break;
470
471 case XCB_ALLOC:
472 snprintf(buffer, sizeof(buffer),
473 "Server Memory Allocation Failure");
474 break;
475
476 case XCB_COLORMAP:
477 snprintf(buffer, sizeof(buffer),
478 "Bad Color: 0x%x", err->resource_id);
479 break;
480
481 case XCB_G_CONTEXT:
482 snprintf(buffer, sizeof(buffer),
483 "Bad GC: 0x%x", err->resource_id);
484 break;
485
486 case XCB_ID_CHOICE:
487 snprintf(buffer, sizeof(buffer),
488 "Bad XID: 0x%x", err->resource_id);
489 break;
490
491 case XCB_NAME:
492 snprintf(buffer, sizeof(buffer),
493 "Bad Name");
494 break;
495
496 case XCB_LENGTH:
497 snprintf(buffer, sizeof(buffer),
498 "Bad Request Length");
499 break;
500
501 case XCB_IMPLEMENTATION:
502 snprintf(buffer, sizeof(buffer),
503 "Server Implementation Failure");
504 break;
505
506 default:
507 snprintf(buffer, sizeof(buffer), "Unknown error");
508 break;
509 }
510 fprintf(stderr, "X Error: %d: %s\n", err->error_code, buffer);
511 }
512
513 fprintf(stderr, " Request Major code: %d\n", err->major_code);
514 if (err->major_code >= 128) {
515 fprintf(stderr, " Request Minor code: %d\n", err->minor_code);
516 }
517
518 fprintf(stderr, " Request serial number: %d\n", err->full_sequence);
519 }
520
521 /*
522 * Cache for atom lookups in either direction
523 */
524 struct atom_cache_entry {
525 xcb_atom_t atom;
526 const char *name;
527 xcb_intern_atom_cookie_t intern_atom;
528 struct atom_cache_entry *next;
529 };
530
531 static struct atom_cache_entry *atom_cache;
532
533 /*
534 * Send a request to the server for an atom by name
535 * Does not create the atom if it is not already present
536 */
537 struct atom_cache_entry *Intern_Atom(xcb_connection_t *dpy, const char *name)
538 {
539 struct atom_cache_entry *a;
540
541 for (a = atom_cache; a != NULL; a = a->next) {
542 if (strcmp(a->name, name) == 0)
543 return a; /* already requested or found */
544 }
545
546 a = calloc(1, sizeof(struct atom_cache_entry));
547 if (a != NULL) {
548 a->name = name;
549 a->intern_atom = xcb_intern_atom(dpy, False, (uint16_t)strlen(name), (name));
550 a->next = atom_cache;
551 atom_cache = a;
552 }
553 return a;
554 }
555
556 /* Get an atom by name when it is needed. */
557 xcb_atom_t Get_Atom(xcb_connection_t *dpy, const char *name)
558 {
559 struct atom_cache_entry *a = Intern_Atom(dpy, name);
560
561 if (a == NULL)
562 return XCB_ATOM_NONE;
563
564 if (a->atom == XCB_ATOM_NONE) {
565 xcb_intern_atom_reply_t *reply;
566
567 reply = xcb_intern_atom_reply(dpy, a->intern_atom, NULL);
568 if (reply) {
569 a->atom = reply->atom;
570 free(reply);
571 } else {
572 a->atom = (xcb_atom_t)-1;
573 }
574 }
575 if (a->atom == (xcb_atom_t)-1) /* internal error */
576 return XCB_ATOM_NONE;
577
578 return a->atom;
579 }
580
581 /* Get the name for an atom when it is needed. */
582 const char *Get_Atom_Name(xcb_connection_t *dpy, xcb_atom_t atom)
583 {
584 struct atom_cache_entry *a;
585
586 for (a = atom_cache; a != NULL; a = a->next) {
587 if (a->atom == atom)
588 return a->name; /* already requested or found */
589 }
590
591 a = calloc(1, sizeof(struct atom_cache_entry));
592 if (a != NULL) {
593 xcb_get_atom_name_cookie_t cookie = xcb_get_atom_name(dpy, atom);
594 xcb_get_atom_name_reply_t *reply = xcb_get_atom_name_reply(dpy, cookie, NULL);
595
596 a->atom = atom;
597 if (reply) {
598 int len = xcb_get_atom_name_name_length(reply);
599 char *name = malloc((size_t)len + 1);
600 if (name) {
601 memcpy(name, xcb_get_atom_name_name(reply), (size_t)len);
602 name[len] = '\0';
603 a->name = name;
604 }
605 free(reply);
606 }
607
608 a->next = atom_cache;
609 atom_cache = a;
610
611 return a->name;
612 }
613 return NULL;
614 }