annotate runtime/tools/xcmdsrv_client.c @ 489:b1e87b7f5db6 v7.0133

updated for version 7.0133
author vimboss
date Sun, 21 Aug 2005 22:20:28 +0000
parents 3fc0f57ecb91
children 1a5d34492798
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4 * X-Windows communication by Flemming Madsen
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6 * Do ":help uganda" in Vim to read copying and usage conditions.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 * Do ":help credits" in Vim to see a list of people who contributed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 * See README.txt for an overview of the Vim source code.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 * Client for sending commands to an '+xcmdsrv' enabled vim.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 * This is mostly a de-Vimified version of if_xcmdsrv.c in vim.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 * See that file for a protocol specification.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 * You can make a test program with a Makefile like:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 * xcmdsrv_client: xcmdsrv_client.c
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 * cc -o $@ -g -DMAIN -I/usr/X11R6/include -L/usr/X11R6/lib $< -lX11
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 #include <stdio.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 #include <string.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 #ifdef HAVE_SELECT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 #include <sys/time.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 #include <sys/types.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 #include <unistd.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 #include <sys/poll.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 #include <X11/Intrinsic.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 #include <X11/Xatom.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32 #define __ARGS(x) x
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 /* Client API */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 char * sendToVim __ARGS((Display *dpy, char *name, char *cmd, int asKeys, int *code));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 #ifdef MAIN
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 /* A sample program */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 main(int argc, char **argv)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 char *res;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 int code;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 if (argc == 4)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 if ((res = sendToVim(XOpenDisplay(NULL), argv[2], argv[3],
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 argv[1][0] != 'e', &code)) != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 if (code)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 printf("Error code returned: %d\n", code);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 puts(res);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 exit(0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 fprintf(stderr, "Usage: %s {k|e} <server> <command>", argv[0]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 exit(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 * Maximum size property that can be read at one time by
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 * this module:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 #define MAX_PROP_WORDS 100000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 * Forward declarations for procedures defined later in this file:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 static int x_error_check __ARGS((Display *dpy, XErrorEvent *error_event));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 static int AppendPropCarefully __ARGS((Display *display,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 Window window, Atom property, char *value, int length));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 static Window LookupName __ARGS((Display *dpy, char *name,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 int delete, char **loose));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 static int SendInit __ARGS((Display *dpy));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 static char *SendEventProc __ARGS((Display *dpy, XEvent *eventPtr,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80 int expect, int *code));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 static int IsSerialName __ARGS((char *name));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 /* Private variables */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 static Atom registryProperty = None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 static Atom commProperty = None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 static Window commWindow = None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 static int got_x_error = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 * sendToVim --
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92 * Send to an instance of Vim via the X display.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 * Results:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 * A string with the result or NULL. Caller must free if non-NULL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 char *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 sendToVim(dpy, name, cmd, asKeys, code)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100 Display *dpy; /* Where to send. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 char *name; /* Where to send. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102 char *cmd; /* What to send. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103 int asKeys; /* Interpret as keystrokes or expr ? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104 int *code; /* Return code. 0 => OK */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 Window w;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 Atom *plist;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 XErrorHandler old_handler;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 #define STATIC_SPACE 500
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 char *property, staticSpace[STATIC_SPACE];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111 int length;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112 int res;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 static int serial = 0; /* Running count of sent commands.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 * Used to give each command a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 * different serial number. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 XEvent event;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 XPropertyEvent *e = (XPropertyEvent *)&event;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 time_t start;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 char *result;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 char *loosename = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 if (commProperty == None && dpy != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 if (SendInit(dpy) < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 * Bind the server name to a communication window.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131 * Find any survivor with a serialno attached to the name if the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 * original registrant of the wanted name is no longer present.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 * Delete any lingering names from dead editors.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137 old_handler = XSetErrorHandler(x_error_check);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138 while (TRUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140 got_x_error = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141 w = LookupName(dpy, name, 0, &loosename);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 /* Check that the window is hot */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143 if (w != None)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145 plist = XListProperties(dpy, w, &res);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 XSync(dpy, False);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147 if (plist != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148 XFree(plist);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149 if (got_x_error)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151 LookupName(dpy, loosename ? loosename : name,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 /*DELETE=*/TRUE, NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153 continue;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
154 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158 if (w == None)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
160 fprintf(stderr, "no registered server named %s\n", name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
162 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
163 else if (loosename != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
164 name = loosename;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
165
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
166 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
167 * Send the command to target interpreter by appending it to the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
168 * comm window in the communication window.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
169 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
170
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
171 length = strlen(name) + strlen(cmd) + 10;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
172 if (length <= STATIC_SPACE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
173 property = staticSpace;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
174 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
175 property = (char *) malloc((unsigned) length);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
176
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
177 serial++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
178 sprintf(property, "%c%c%c-n %s%c-s %s",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
179 0, asKeys ? 'k' : 'c', 0, name, 0, cmd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
180 if (name == loosename)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
181 free(loosename);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
182 if (!asKeys)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
183 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
184 /* Add a back reference to our comm window */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
185 sprintf(property + length, "%c-r %x %d", 0, (uint) commWindow, serial);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
186 length += strlen(property + length + 1) + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
187 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
188
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
189 res = AppendPropCarefully(dpy, w, commProperty, property, length + 1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
190 if (length > STATIC_SPACE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
191 free(property);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
192 if (res < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
193 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
194 fprintf(stderr, "Failed to send command to the destination program\n");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
195 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
196 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
197
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
198 if (asKeys) /* There is no answer for this - Keys are sent async */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
199 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
200
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
201
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
202 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
203 * Enter a loop processing X events & pooling chars until we see the result
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
204 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
205
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
206 #define SEND_MSEC_POLL 50
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
207
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
208 time(&start);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
209 while ((time((time_t *) 0) - start) < 60)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
210 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
211 /* Look out for the answer */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
212 #ifndef HAVE_SELECT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
213 struct pollfd fds;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
214
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
215 fds.fd = ConnectionNumber(dpy);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
216 fds.events = POLLIN;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
217 if (poll(&fds, 1, SEND_MSEC_POLL) < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
218 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
219 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
220 fd_set fds;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
221 struct timeval tv;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
222
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
223 tv.tv_sec = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
224 tv.tv_usec = SEND_MSEC_POLL * 1000;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
225 FD_ZERO(&fds);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
226 FD_SET(ConnectionNumber(dpy), &fds);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
227 if (select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &tv) < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
228 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
229 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
230 while (XEventsQueued(dpy, QueuedAfterReading) > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
231 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
232 XNextEvent(dpy, &event);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
233 if (event.type == PropertyNotify && e->window == commWindow)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
234 if ((result = SendEventProc(dpy, &event, serial, code)) != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
235 return result;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
236 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
237 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
238 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
239 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
240
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
241
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
242 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
243 * SendInit --
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
244 * This procedure is called to initialize the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
245 * communication channels for sending commands and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
246 * receiving results.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
247 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
248
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
249 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
250 SendInit(dpy)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
251 Display *dpy;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
252 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
253 XErrorHandler old_handler;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
254
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
255 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
256 * Create the window used for communication, and set up an
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
257 * event handler for it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
258 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
259 old_handler = XSetErrorHandler(x_error_check);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
260 got_x_error = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
261
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
262 commProperty = XInternAtom(dpy, "Comm", False);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
263 /* Change this back to "InterpRegistry" to talk to tk processes */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
264 registryProperty = XInternAtom(dpy, "VimRegistry", False);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
265
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
266 if (commWindow == None)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
267 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
268 commWindow =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
269 XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
270 getpid(), 0, 10, 10, 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
271 WhitePixel(dpy, DefaultScreen(dpy)),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
272 WhitePixel(dpy, DefaultScreen(dpy)));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
273 XSelectInput(dpy, commWindow, PropertyChangeMask);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
274 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
275
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
276 XSync(dpy, False);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
277 (void) XSetErrorHandler(old_handler);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
278
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
279 return got_x_error ? -1 : 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
280 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
281
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
282 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
283 * LookupName --
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
284 * Given an interpreter name, see if the name exists in
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
285 * the interpreter registry for a particular display.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
286 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
287 * Results:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
288 * If the given name is registered, return the ID of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
289 * the window associated with the name. If the name
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
290 * isn't registered, then return 0.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
291 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
292
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
293 static Window
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
294 LookupName(dpy, name, delete, loose)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
295 Display *dpy; /* Display whose registry to check. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
296 char *name; /* Name of an interpreter. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
297 int delete; /* If non-zero, delete info about name. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
298 char **loose; /* Do another search matching -999 if not found
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
299 Return result here if a match is found */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
300 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
301 unsigned char *regProp, *entry;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
302 unsigned char *p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
303 int result, actualFormat;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
304 unsigned long numItems, bytesAfter;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
305 Atom actualType;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
306 Window returnValue;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
307
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
308 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
309 * Read the registry property.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
310 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
311
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
312 regProp = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
313 result = XGetWindowProperty(dpy, RootWindow(dpy, 0), registryProperty, 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
314 MAX_PROP_WORDS, False, XA_STRING, &actualType,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
315 &actualFormat, &numItems, &bytesAfter,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
316 &regProp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
317
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
318 if (actualType == None)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
319 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
320
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
321 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
322 * If the property is improperly formed, then delete it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
323 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
324
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
325 if ((result != Success) || (actualFormat != 8) || (actualType != XA_STRING))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
326 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
327 if (regProp != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
328 XFree(regProp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
329 XDeleteProperty(dpy, RootWindow(dpy, 0), registryProperty);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
330 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
331 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
332
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
333 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
334 * Scan the property for the desired name.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
335 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
336
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
337 returnValue = None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
338 entry = NULL; /* Not needed, but eliminates compiler warning. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
339 for (p = regProp; (p - regProp) < numItems; )
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
340 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
341 entry = p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
342 while ((*p != 0) && (!isspace(*p)))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
343 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
344 if ((*p != 0) && (strcasecmp(name, p + 1) == 0))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
345 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
346 sscanf(entry, "%x", (uint*) &returnValue);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
347 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
348 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
349 while (*p != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
350 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
351 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
352 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
353
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
354 if (loose != NULL && returnValue == None && !IsSerialName(name))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
355 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
356 for (p = regProp; (p - regProp) < numItems; )
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
357 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
358 entry = p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
359 while ((*p != 0) && (!isspace(*p)))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
360 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
361 if ((*p != 0) && IsSerialName(p + 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
362 && (strncmp(name, p + 1, strlen(name)) == 0))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
363 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
364 sscanf(entry, "%x", (uint*) &returnValue);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
365 *loose = strdup(p + 1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
366 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
367 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
368 while (*p != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
369 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
370 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
371 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
372 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
373
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
374 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
375 * Delete the property, if that is desired (copy down the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
376 * remainder of the registry property to overlay the deleted
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
377 * info, then rewrite the property).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
378 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
379
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
380 if ((delete) && (returnValue != None))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
381 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
382 int count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
383
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
384 while (*p != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
385 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
386 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
387 count = numItems - (p-regProp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
388 if (count > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
389 memcpy(entry, p, count);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
390 XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
391 8, PropModeReplace, regProp,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
392 (int) (numItems - (p-entry)));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
393 XSync(dpy, False);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
394 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
395
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
396 XFree(regProp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
397 return returnValue;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
398 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
399
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
400 static char *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
401 SendEventProc(dpy, eventPtr, expected, code)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
402 Display *dpy;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
403 XEvent *eventPtr; /* Information about event. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
404 int expected; /* The one were waiting for */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
405 int *code; /* Return code. 0 => OK */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
406 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
407 unsigned char *propInfo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
408 unsigned char *p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
409 int result, actualFormat;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
410 int retCode;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
411 unsigned long numItems, bytesAfter;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
412 Atom actualType;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
413
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
414 if ((eventPtr->xproperty.atom != commProperty)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
415 || (eventPtr->xproperty.state != PropertyNewValue))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
416 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
417 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
418 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
419
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
420 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
421 * Read the comm property and delete it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
422 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
423
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
424 propInfo = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
425 result = XGetWindowProperty(dpy, commWindow, commProperty, 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
426 MAX_PROP_WORDS, True, XA_STRING, &actualType,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
427 &actualFormat, &numItems, &bytesAfter,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
428 &propInfo);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
429
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
430 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
431 * If the property doesn't exist or is improperly formed
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
432 * then ignore it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
433 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
434
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
435 if ((result != Success) || (actualType != XA_STRING)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
436 || (actualFormat != 8))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
437 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
438 if (propInfo != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
439 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
440 XFree(propInfo);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
441 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
442 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
443 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
444
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
445 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
446 * Several commands and results could arrive in the property at
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
447 * one time; each iteration through the outer loop handles a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
448 * single command or result.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
449 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
450
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
451 for (p = propInfo; (p - propInfo) < numItems; )
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
452 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
453 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
454 * Ignore leading NULs; each command or result starts with a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
455 * NUL so that no matter how badly formed a preceding command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
456 * is, we'll be able to tell that a new command/result is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
457 * starting.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
458 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
459
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
460 if (*p == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
461 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
462 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
463 continue;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
464 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
465
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
466 if ((*p == 'r') && (p[1] == 0))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
467 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
468 int serial, gotSerial;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
469 char *res;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
470
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
471 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
472 * This is a reply to some command that we sent out. Iterate
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
473 * over all of its options. Stop when we reach the end of the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
474 * property or something that doesn't look like an option.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
475 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
476
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
477 p += 2;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
478 gotSerial = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
479 res = "";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
480 retCode = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
481 while (((p-propInfo) < numItems) && (*p == '-'))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
482 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
483 switch (p[1])
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
484 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
485 case 'r':
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
486 if (p[2] == ' ')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
487 res = p + 3;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
488 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
489 case 's':
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
490 if (sscanf(p + 2, " %d", &serial) == 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
491 gotSerial = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
492 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
493 case 'c':
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
494 if (sscanf(p + 2, " %d", &retCode) != 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
495 retCode = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
496 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
497 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
498 while (*p != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
499 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
500 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
501 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
502
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
503 if (!gotSerial)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
504 continue;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
505
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
506 if (code != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
507 *code = retCode;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
508 return serial == expected ? strdup(res) : NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
509 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
510 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
511 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
512 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
513 * Didn't recognize this thing. Just skip through the next
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
514 * null character and try again.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
515 * Also, throw away commands that we cant process anyway.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
516 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
517
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
518 while (*p != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
519 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
520 p++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
521 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
522 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
523 XFree(propInfo);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
524 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
525
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
526 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
527 * AppendPropCarefully --
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
528 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
529 * Append a given property to a given window, but set up
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
530 * an X error handler so that if the append fails this
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
531 * procedure can return an error code rather than having
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
532 * Xlib panic.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
533 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
534 * Return:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
535 * 0 on OK - -1 on error
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
536 *--------------------------------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
537 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
538
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
539 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
540 AppendPropCarefully(dpy, window, property, value, length)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
541 Display *dpy; /* Display on which to operate. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
542 Window window; /* Window whose property is to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
543 * be modified. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
544 Atom property; /* Name of property. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
545 char *value; /* Characters to append to property. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
546 int length; /* How much to append */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
547 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
548 XErrorHandler old_handler;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
549
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
550 old_handler = XSetErrorHandler(x_error_check);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
551 got_x_error = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
552 XChangeProperty(dpy, window, property, XA_STRING, 8,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
553 PropModeAppend, value, length);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
554 XSync(dpy, False);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
555 (void) XSetErrorHandler(old_handler);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
556 return got_x_error ? -1 : 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
557 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
558
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
559
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
560 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
561 * Another X Error handler, just used to check for errors.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
562 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
563 /* ARGSUSED */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
564 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
565 x_error_check(dpy, error_event)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
566 Display *dpy;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
567 XErrorEvent *error_event;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
568 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
569 got_x_error = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
570 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
571 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
572
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
573 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
574 * Check if "str" looks like it had a serial number appended.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
575 * Actually just checks if the name ends in a digit.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
576 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
577 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
578 IsSerialName(str)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
579 char *str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
580 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
581 int len = strlen(str);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
582
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
583 return (len > 1 && isdigit(str[len - 1]));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
584 }