summaryrefslogtreecommitdiff
path: root/Build/source/utils/xpdfopen/sendx.c
blob: f97cfb59d5a38d2ee365c197d6086284375d7909 (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
/*
 * Modified by Jim Diamond (jim.diamond@acadiau.ca) April 24, 2010
 * to reset error_status to 0 at the beginning of open_channel().
 *
 * Also replaced 0xfff with KeyPressMask and KeyReleaseMask (as
 * appropriate) in sendx_channel(); otherwise (at least in Slackware64 13.0)
 * the events also get sent to the terminal running pdfopen; this is a Bad
 * Thing.
 *
 * Also added set_focus() and reset_focus() functions.
 * NOTE: they do not work, at least under FVWM2 on Slackware64-13.0.
 * They return success, but the focus never changes (or is reverted to the
 * window containing the mouse before a call to sendx_token() can do its
 * work).
 */

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

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>

#include "sendx.h"

static char * display_name = NULL;
static Display * display;
static Window root;
static Window window = None;
static XKeyEvent event;

static char error_message[256];
static int error_status = 0;


static void
throw_exception3(const char * msg, const char * s, int i)
{
    sprintf(error_message, msg, s, i);
    error_status = 1;
}


static void
throw_exception3s(const char * msg1, const char * msg2, const char * msg3)
{
    sprintf(error_message, msg1, msg2, msg3);
    error_status = 1;
}


static void
throw_exception(const char * msg)
{
    strncpy(error_message, msg, 256);
    error_status = 1;
}



/*
 * Added for window managers like swm and tvtwm that follow solbourne's
 * virtual root window concept 
 */
static Window
GetRootWindow(Display * disp, int scrn)
{
    Atom __SWM_VROOT = None;
    Window root, rootReturn, parentReturn, * children;
    unsigned int numChildren;
    unsigned i;

    root = RootWindow(disp, scrn);

    /*
     * see if there is a virtual root 
     */
    __SWM_VROOT = XInternAtom(disp, "__SWM_VROOT", False);
    XQueryTree(disp, root, &rootReturn, &parentReturn, &children, &numChildren);

    for (i = 0; i < numChildren; i++)
    {
        Atom actual_type;
        int actual_format;
        unsigned long nitems, bytesafter;
        unsigned char * prop_return = NULL;

        if (XGetWindowProperty(disp, children[i], __SWM_VROOT, 0, 1,
                               False, XA_WINDOW, &actual_type, &actual_format,
                               &nitems, &bytesafter, &prop_return)
             == Success && prop_return)
        {
            root = *(Window *)prop_return;
            break;
        }
    }

    if (children)
        XFree((char *)children);

    return (root);
}



/*
 * [These functions are from the file "dsimple.c" used with xwininfo.]
 * 
 * Written by Mark Lillibridge.  Last updated 7/1/87
 *
 * 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 Root Window.  
 */

static Window
Window_With_Name(Display * dpy, Window top, const char * name)
{
    Window * children, dummy;
    unsigned int nchildren;
    unsigned i;
    Window w = 0;
    char * window_name;

    if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name))
        return (top);

    if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
        return (0);

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

    if (children)
        XFree((char *)children);

    return (w);
}



static int
open_channel(const char * wname)
{
    /*
     * display_name = ":0.0"; 
     */

    error_status = 0;

    if ((display = XOpenDisplay(display_name)) == NULL)
    {
        throw_exception("can't open display");
        return 1;
    }

    if ((root = GetRootWindow(display, DefaultScreen(display))) == 0)
    {
        throw_exception("Cannot get DefaultScreen");
        return 1;
    }

    if ((wname[0] == '\0') && (window != None))
    {
    }                                  /* take selected window */
    else if (wname[0] != '\0')
    {
        if ((window = Window_With_Name(display, root, wname)) == None)
        {
            throw_exception3s("Display %s: can't open window named \"%s\"",
                              XDisplayName(display_name), wname);
            return 1;
        }
    }
    else
    {
        throw_exception3("bad condition in %s at line %d", __FILE__, __LINE__);
        return 1;
    }

    event.type = KeyPress;
    event.serial = 0;
    event.send_event = False;
    event.display = display;
    event.x = event.y = event.x_root = event.y_root = 0;
    event.time = CurrentTime;
    event.same_screen = True;
    event.subwindow = None;
    event.window = window;
    event.root = root;

    return 0;
}



static void
close_channel(void)
{
    /*
     * XFlush(display); 
     */
    XCloseDisplay(display);
}



static void
sendx_channel(KeySym ks, int km)
{
    /*
     * km: 0=regular, 1=shift, 2=lock, 4=control 
     */
    if (ks < 256)
    {
        event.state = isupper((char)ks);
        switch (ks)
        {
          case 0x08:
            ks = XK_BackSpace;
            break;
          case 0x09:
            ks = XK_Tab;
            break;
          case 0x0A:
            ks = XK_Linefeed;
            break;
          case 0x0B:
            ks = XK_Clear;
            break;
          case 0x0D:
            ks = XK_Return;
            break;
          case 0x13:
            ks = XK_Pause;
            break;
          case 0x14:
            ks = XK_Scroll_Lock;
            break;
          case 0x1B:
            ks = XK_Escape;
            break;
        }
    }
    else
        event.state = 0;

    event.type = KeyPress;
    event.state = km;                  // Mod1Mask
    event.keycode = XKeysymToKeycode(display, ks);

    if (XSendEvent(display, window, True, KeyPressMask, (XEvent *)&event) == 0)
        throw_exception("Error in XSendEvent");

    event.type = KeyRelease;
    if (XSendEvent(display, window, True, KeyReleaseMask, (XEvent *)&event)
	== 0)
        throw_exception("Error in XSendEvent");

    return;
}



int
sendx_string(const char * string, const char * window)
{
    const char * p;

    if (open_channel(window))
        return error_status;

    p = string;
    while (*p)
        sendx_channel(*p++, 0);
    close_channel();

    return error_status;
}



int
sendx_token(const char * string, const char * window)
{
    if (open_channel(window))
        return error_status;

    sendx_channel(XStringToKeysym(string), 0);
    close_channel();

    return error_status;
}



int
sendx_alt_token(const char * string, const char * window)
{
    if (open_channel(window))
        return error_status;

    sendx_channel(XStringToKeysym(string), Mod1Mask);
    close_channel();

    return error_status;
}



int
sendx_controlalt_token(const char * string, const char * window)
{
    if (open_channel(window))
        return error_status;

    sendx_channel(XStringToKeysym(string), Mod1Mask | ControlMask);
    close_channel();

    return error_status;
}



int
sendx_control_token(const char * string, const char * window)
{
    if (open_channel(window))
        return error_status;

    sendx_channel(XStringToKeysym(string), ControlMask);
    close_channel();

    return error_status;
}




static Window previous_window;
static int previous_window_set = 0;
static int revert_to;

/*
 * Attempt to give focus to the given window.
 * Return 0 on success, non-0 on failure.
 * Record the previous window for reset_focus() on success.
 */ 

int
set_focus(const char * wname)
{
    if ((display = XOpenDisplay(display_name)) == NULL)
    {
        throw_exception("can't open display");
        return 1;
    }

    if ((root = GetRootWindow(display, DefaultScreen(display))) == 0)
    {
        throw_exception("Cannot get DefaultScreen");
        return 1;
    }

    (void)XGetInputFocus(display, &previous_window, &revert_to);
    if (XSetInputFocus(display, Window_With_Name(display, root, wname),
	previous_window, CurrentTime))
    {
	previous_window_set = 1;
	return 0;
    }

    throw_exception3s("Display %s: can't focus window named \"%s\"",
		      XDisplayName(display_name), wname);
    return 1;
}



int
reset_focus(void)
{
    if (previous_window_set)
    {
	XSetInputFocus(display, previous_window, revert_to, CurrentTime);
	previous_window_set = 0;
	return 0;
    }

    throw_exception("reset_focus() called when previous window not saved");
    return 1;
}



/*
 * void PrintKeySyms () { int i; for (i = 32; i < 127; i++) { printf ("%s[%c]
 * ", XKeysymToString (i), i); } for (i = 128 + 32; i < 128 + 127; i++) {
 * printf ("%s[%c] ", XKeysymToString (i), i); } printf ("\n"); } 
 */