summaryrefslogtreecommitdiff
path: root/dsimple.c
blob: d1accf78ce7b481c7caff8de53fa297a60cc592c (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
/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 *
 * 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 (including the next
 * paragraph) 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.
 */
/*

Copyright 1993, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

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 OPEN GROUP 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 name of The Open Group 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 Open Group.

*/

#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <xcb/xcb_icccm.h>
#include <X11/cursorfont.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "clientwin.h"
#include "dsimple.h"

/*
 * Just_display: A group of routines designed to make the writing of simple
 *               X11 applications which open a display but do not open
 *               any windows much faster and easier.  Unless a routine says
 *               otherwise, it may be assumed to require program_name
 *               to be already defined on entry.
 *
 * Written by Mark Lillibridge.   Last updated 7/1/87
 */


/* This stuff is defined in the calling program by dsimple.h */
char    *program_name = "unknown_program";

/*
 * Get_Display_Name (argc, argv) - return string representing display name
 * that would be used given the specified argument (i.e. if it's NULL, check
 * getenv("DISPLAY") - always returns a non-NULL pointer, though it may be
 * an unwritable constant, so is safe to printf() on platforms that crash
 * on NULL printf arguments.
 */
const char *Get_Display_Name (const char *display_name)
{
    const char *name = display_name;

    if (!name) {
	name = getenv ("DISPLAY");
	if (!name)
	    name = "";
    }
    return (name);
}


/*
 * Setup_Display_And_Screen: This routine opens up the correct display (i.e.,
 *                           it calls Get_Display_Name) and then stores a
 *                           pointer to it in dpy.  The default screen
 *                           for this display is then stored in screen.
 */
void Setup_Display_And_Screen (
    const char *display_name,
    xcb_connection_t **dpy,	/* MODIFIED */
    xcb_screen_t **screen)	/* MODIFIED */
{
    int screen_number, i;

    /* Open Display */
    *dpy = xcb_connect (display_name, &screen_number);
    if (xcb_connection_has_error (*dpy)) {
	Fatal_Error ("unable to open display \"%s\"",
		     Get_Display_Name(display_name) );
    }

    if (screen) {
	/* find our screen */
	const xcb_setup_t *setup = xcb_get_setup(*dpy);
	xcb_screen_iterator_t screen_iter = xcb_setup_roots_iterator(setup);

	for (i = 0; i < screen_number; i++)
	    xcb_screen_next(&screen_iter);
	*screen = screen_iter.data;
    }
}

/*
 * xcb equivalent of XCreateFontCursor
 */
static xcb_cursor_t
Create_Font_Cursor (xcb_connection_t *dpy, uint16_t glyph)
{
    static xcb_font_t cursor_font;
    xcb_cursor_t cursor;

    if (!cursor_font) {
	cursor_font = xcb_generate_id (dpy);
	xcb_open_font (dpy, cursor_font, strlen ("cursor"), "cursor");
    }

    cursor = xcb_generate_id (dpy);
    xcb_create_glyph_cursor (dpy, cursor, cursor_font, cursor_font,
			     glyph, glyph + 1,
                             0, 0, 0, 0xffff, 0xffff, 0xffff);  /* rgb, rgb */

    return cursor;
}

/*
 * Routine to let user select a window using the mouse
 */

xcb_window_t Select_Window(xcb_connection_t *dpy,
			   const xcb_screen_t *screen,
			   int descend)
{
    xcb_cursor_t cursor;
    xcb_generic_event_t *event;
    xcb_window_t target_win = XCB_WINDOW_NONE;
    xcb_window_t root = screen->root;
    int buttons = 0;
    xcb_generic_error_t *err;
    xcb_grab_pointer_cookie_t grab_cookie;
    xcb_grab_pointer_reply_t *grab_reply;

    /* Make the target cursor */
    cursor = Create_Font_Cursor (dpy, XC_crosshair);

    /* Grab the pointer using target cursor, letting it room all over */
    grab_cookie = xcb_grab_pointer
	(dpy, False, root,
	 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE,
	 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
	 root, cursor, XCB_TIME_CURRENT_TIME);
    grab_reply = xcb_grab_pointer_reply (dpy, grab_cookie, &err);
    if (grab_reply->status != XCB_GRAB_STATUS_SUCCESS)
	Fatal_Error ("Can't grab the mouse.");

    /* Let the user select a window... */
    while ((target_win == XCB_WINDOW_NONE) || (buttons != 0)) {
	/* allow one more event */
	xcb_allow_events (dpy, XCB_ALLOW_SYNC_POINTER, XCB_TIME_CURRENT_TIME);
	xcb_flush (dpy);
	event = xcb_wait_for_event (dpy);
	switch (event->response_type & 0x7f) {
	case XCB_BUTTON_PRESS:
	{
	    xcb_button_press_event_t *bp = (xcb_button_press_event_t *)event;

	    if (target_win == XCB_WINDOW_NONE) {
		target_win = bp->child; /* window selected */
		if (target_win == XCB_WINDOW_NONE)
		    target_win = root;
	    }
	    buttons++;
	    break;
	}
	case XCB_BUTTON_RELEASE:
	    if (buttons > 0) /* there may have been some down before we started */
		buttons--;
	    break;
	default:
	    /* just discard all other events */
	    break;
	}
	free (event);
    }

    xcb_ungrab_pointer (dpy, XCB_TIME_CURRENT_TIME); /* Done with pointer */

    if (!descend || (target_win == root))
	return (target_win);

    target_win = Find_Client (dpy, root, target_win);

    return (target_win);
}


/*
 * Window_With_Name: routine to locate a window with a given name on a display.
 *                   If no window with the given name is found, 0 is returned.
 *                   If more than one window has the given name, the first
 *                   one found will be returned.  Only top and its subwindows
 *                   are looked at.  Normally, top should be the RootWindow.
 */

struct wininfo_cookies {
    xcb_get_property_cookie_t get_wm_name;
    xcb_query_tree_cookie_t query_tree;
};

static xcb_window_t
recursive_Window_With_Name  (
    xcb_connection_t *dpy,
    xcb_window_t window,
    struct wininfo_cookies *cookies,
    const char *name)
{
    xcb_window_t *children;
    unsigned int nchildren;
    int i;
    xcb_window_t w = 0;
    xcb_generic_error_t *err;
    xcb_get_text_property_reply_t prop;
    xcb_query_tree_reply_t *tree;
    struct wininfo_cookies *child_cookies;

    if (xcb_get_wm_name_reply (dpy, cookies->get_wm_name, &prop, &err)) {
	/* can't use strcmp, since prop.name is not null terminated */
	if (strncmp (prop.name, name, prop.name_len) == 0) {
	    w = window;
	}

	xcb_get_text_property_reply_wipe (&prop);

	if (w)
	{
	    xcb_discard_reply (dpy, cookies->query_tree.sequence);
	    return w;
	}
    } else if (err) {
	if (err->response_type == 0)
	    Print_X_Error (dpy, err);
	return 0;
    }

    tree = xcb_query_tree_reply (dpy, cookies->query_tree, &err);
    if (!tree) {
	if (err->response_type == 0)
	    Print_X_Error (dpy, err);
	return 0;
    }

    nchildren = xcb_query_tree_children_length (tree);
    children = xcb_query_tree_children (tree);
    child_cookies = calloc(nchildren, sizeof(struct wininfo_cookies));

    if (child_cookies == NULL)
	Fatal_Error("Failed to allocate memory in recursive_Window_With_Name");

    for (i = 0; i < nchildren; i++) {
	child_cookies[i].get_wm_name = xcb_get_wm_name (dpy, children[i]);
	child_cookies[i].query_tree = xcb_query_tree (dpy, children[i]);
    }
    xcb_flush (dpy);

    for (i = 0; i < nchildren; i++) {
	w = recursive_Window_With_Name (dpy, children[i],
					&child_cookies[i], name);
	if (w)
	    break;
    }

    if (w)
    {
	/* clean up remaining replies */
	for (/* keep previous i */; i < nchildren; i++) {
	    xcb_discard_reply (dpy, child_cookies[i].get_wm_name.sequence);
	    xcb_discard_reply (dpy, child_cookies[i].query_tree.sequence);
	}
    }

    free (child_cookies);
    free (tree); /* includes storage for children[] */
    return (w);
}

xcb_window_t
Window_With_Name (
    xcb_connection_t *dpy,
    xcb_window_t top,
    const char *name)
{
    struct wininfo_cookies cookies;

    cookies.get_wm_name = xcb_get_wm_name (dpy, top);
    cookies.query_tree = xcb_query_tree (dpy, top);
    return recursive_Window_With_Name(dpy, top, &cookies, name);
}


/*
 * Standard fatal error routine - call like printf
 */
void Fatal_Error (char *msg, ...)
{
    va_list args;
    fflush (stdout);
    fflush (stderr);
    fprintf (stderr, "%s: error: ", program_name);
    va_start (args, msg);
    vfprintf (stderr, msg, args);
    va_end (args);
    fprintf (stderr, "\n");
    exit (EXIT_FAILURE);
}

/*
 * Print X error information like the default Xlib error handler
 */
void
Print_X_Error (
    xcb_connection_t *dpy,
    xcb_generic_error_t *err
    )
{
    char buffer[256] = "";

    if ((err == NULL) || (err->response_type != 0)) /* not an error */
	return;

    /* Todo: find a more user friendly way to show request/extension info */
    if (err->error_code >= 128)
    {
	fprintf (stderr, "X Extension Error:  Error code %d\n",
		 err->error_code);
    }
    else
    {
	switch (err->error_code)
	{
	    case XCB_REQUEST:
		snprintf (buffer, sizeof(buffer), ": Bad Request");
		break;

	    case XCB_VALUE:
		snprintf (buffer, sizeof(buffer),
			  ": Bad Value: 0x%x", err->resource_id);
		break;

	    case XCB_WINDOW:
		snprintf (buffer, sizeof(buffer),
			  ": Bad Window: 0x%x", err->resource_id);
		break;

	    case XCB_PIXMAP:
		snprintf (buffer, sizeof(buffer),
			  ": Bad Pixmap: 0x%x", err->resource_id);
		break;

	    case XCB_ATOM:
		snprintf (buffer, sizeof(buffer),
			  ": Bad Atom: 0x%x", err->resource_id);
		break;

	    case XCB_CURSOR:
		snprintf (buffer, sizeof(buffer),
			  ": Bad Cursor: 0x%x", err->resource_id);
		break;

	    case XCB_FONT:
		snprintf (buffer, sizeof(buffer),
			  ": Bad Font: 0x%x", err->resource_id);
		break;

	    case XCB_MATCH:
		snprintf (buffer, sizeof(buffer), ": Bad Match");
		break;

	    case XCB_DRAWABLE:
		snprintf (buffer, sizeof(buffer),
			  ": Bad Drawable: 0x%x", err->resource_id);
		break;

	    case XCB_ACCESS:
		snprintf (buffer, sizeof(buffer), ": Access Denied");
		break;

	    case XCB_ALLOC:
		snprintf (buffer, sizeof(buffer),
			  ": Server Memory Allocation Failure");
		break;

	    case XCB_COLORMAP:
		snprintf (buffer, sizeof(buffer),
			  ": Bad Color: 0x%x", err->resource_id);
		break;

	    case XCB_G_CONTEXT:
		snprintf (buffer, sizeof(buffer),
			  ": Bad GC: 0x%x", err->resource_id);
		break;

	    case XCB_ID_CHOICE:
		snprintf (buffer, sizeof(buffer),
			  ": Bad XID: 0x%x", err->resource_id);
		break;

	    case XCB_NAME:
		snprintf (buffer, sizeof(buffer),
			  ": Bad Name");
		break;

	    case XCB_LENGTH:
		snprintf (buffer, sizeof(buffer),
			  ": Bad Request Length");
		break;

	    case XCB_IMPLEMENTATION:
		snprintf (buffer, sizeof(buffer),
			  ": Server Implementation Failure");
		break;

	    default:
		snprintf (buffer, sizeof(buffer), ": Unknown error");
		break;
	}
	fprintf (stderr, "X Error: %d%s\n", err->error_code, buffer);
    }

    fprintf (stderr, "  Request Major code: %d\n", err->major_code);
    if (err->major_code >= 128)
    {
	fprintf (stderr, "  Request Minor code: %d\n", err->minor_code);
    }

    fprintf (stderr, "  Request serial number: %d\n", err->full_sequence);
}