annotate runtime/tools/xcmdsrv_client.c @ 10617:9a75c8a1b8b1

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