1 /* xscreensaver, Copyright (c) 2012 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 * Running programs under a pipe or pty and returning bytes from them.
12 * Uses these X resources:
14 * program: What to run. Usually "xscreensaver-text".
15 * relaunchDelay: secs How long after the command dies before restarting.
16 * usePty: bool Whether to run the command interactively.
17 * metaSendsESC: bool Whether to send Alt-x as ESC x in pty-mode.
18 * swapBSDEL: bool Swap Backspace and Delete in pty-mode.
23 #ifndef USE_IPHONE /* whole file -- see OSX/iostextclient.m */
25 #include "textclient.h"
26 #include "resources.h"
29 # define XK_MISCELLANY
30 # include <X11/keysymdef.h>
31 # include <X11/Xatom.h>
32 # include <X11/Intrinsic.h>
42 # include <fcntl.h> /* for O_RDWR */
46 # include <sys/ioctl.h>
53 #endif /* HAVE_FORKPTY */
57 extern const char *progname;
62 int pix_w, pix_h, char_w, char_h;
65 XtIntervalId pipe_timer;
69 Bool input_available_p;
70 Time subproc_relaunch_delay;
71 XComposeStatus compose;
73 Bool meta_sends_esc_p;
76 unsigned int meta_mask;
78 const char *out_buffer;
84 subproc_cb (XtPointer closure, int *source, XtInputId *id)
86 text_data *d = (text_data *) closure;
88 if (! d->input_available_p)
89 fprintf (stderr, "%s: textclient: input available\n", progname);
91 d->input_available_p = True;
96 launch_text_generator (text_data *d)
98 XtAppContext app = XtDisplayToApplicationContext (d->dpy);
100 const char *oprogram = d->program;
101 char *program = (char *) malloc (strlen (oprogram) + 50);
103 strcpy (program, "( ");
104 strcat (program, oprogram);
106 /* Kludge! Special-case "xscreensaver-text" to tell it how wide
107 the screen is. We used to do this by just always feeding
108 `program' through sprintf() and setting the default value to
109 "xscreensaver-text --cols %d", but that makes things blow up
110 if someone ever uses a --program that includes a % anywhere.
112 if (!strcmp (oprogram, "xscreensaver-text"))
113 sprintf (program + strlen(program), " --cols %d", d->char_w);
115 strcat (program, " ) 2>&1");
118 fprintf (stderr, "%s: textclient: launch %s: %s\n", progname,
119 (d->pty_p ? "pty" : "pipe"), program);
128 ws.ws_col = d->char_w;
129 ws.ws_row = d->char_h;
130 ws.ws_xpixel = d->pix_w;
131 ws.ws_ypixel = d->pix_h;
134 if ((d->pid = forkpty(&fd, NULL, NULL, &ws)) < 0)
137 sprintf (buf, "%.100s: forkpty", progname);
142 /* This is the child fork. */
145 if (putenv ("TERM=vt100"))
152 sprintf (buf, "%.100s: %.100s", progname, oprogram);
158 /* This is the parent fork. */
159 d->pipe = fdopen (fd, "r+");
161 XtAppAddInput (app, fileno (d->pipe),
162 (XtPointer) (XtInputReadMask | XtInputExceptMask),
163 subproc_cb, (XtPointer) d);
165 fprintf (stderr, "%s: textclient: pid = %d\n", progname, d->pid);
170 #endif /* HAVE_FORKPTY */
172 /* don't mess up controlling terminal on "-pipe -program tcsh". */
173 static int protected_stdin_p = 0;
174 if (! protected_stdin_p) {
176 open ("/dev/null", O_RDWR); /* re-allocate fd 0 */
177 protected_stdin_p = 1;
180 if ((d->pipe = popen (program, "r")))
183 XtAppAddInput (app, fileno (d->pipe),
184 (XtPointer) (XtInputReadMask | XtInputExceptMask),
185 subproc_cb, (XtPointer) d);
187 fprintf (stderr, "%s: textclient: popen\n", progname);
192 sprintf (buf, "%.100s: %.100s", progname, program);
202 relaunch_generator_timer (XtPointer closure, XtIntervalId *id)
204 text_data *d = (text_data *) closure;
205 /* if (!d->pipe_timer) abort(); */
208 fprintf (stderr, "%s: textclient: launch timer fired\n", progname);
210 launch_text_generator (d);
215 textclient_reshape (text_data *d,
216 int pix_w, int pix_h,
217 int char_w, int char_h)
219 # if defined(HAVE_FORKPTY) && defined(TIOCSWINSZ)
227 fprintf (stderr, "%s: textclient: reshape: %dx%d, %dx%d\n", progname,
228 pix_w, pix_h, char_w, char_h);
231 if (d->pid && d->pipe)
233 /* Tell the sub-process that the screen size has changed. */
237 ws.ws_xpixel = pix_w;
238 ws.ws_ypixel = pix_h;
239 ioctl (fileno (d->pipe), TIOCSWINSZ, &ws);
240 kill (d->pid, SIGWINCH);
242 # endif /* HAVE_FORKPTY && TIOCSWINSZ */
245 /* If we're running xscreensaver-text, then kill and restart it any
246 time the window is resized so that it gets an updated --cols arg
247 right away. But if we're running something else, leave it alone.
249 if (!strcmp (d->program, "xscreensaver-text"))
252 kill (d->pid, SIGTERM);
254 XtRemoveInput (d->pipe_id);
257 d->input_available_p = False;
258 relaunch_generator_timer (d, 0);
264 textclient_open (Display *dpy)
266 text_data *d = (text_data *) calloc (1, sizeof (*d));
269 fprintf (stderr, "%s: textclient: init\n", progname);
274 if (get_boolean_resource (dpy, "usePty", "UsePty"))
280 "%s: no pty support on this system; using a pipe instead.\n",
285 d->subproc_relaunch_delay =
286 (1000 * get_integer_resource (dpy, "relaunchDelay", "Time"));
288 d->meta_sends_esc_p = get_boolean_resource (dpy, "metaSendsESC", "Boolean");
289 d->swap_bs_del_p = get_boolean_resource (dpy, "swapBSDEL", "Boolean");
291 d->program = get_string_resource (dpy, "program", "Program");
295 /* Kludge for MacOS standalone mode: see OSX/SaverRunner.m. */
297 const char *s = getenv ("XSCREENSAVER_STANDALONE");
298 if (s && *s && strcmp(s, "0"))
301 d->program = strdup (getenv ("SHELL"));
306 launch_text_generator (d);
313 textclient_close (text_data *d)
316 fprintf (stderr, "%s: textclient: free\n", progname);
322 XtRemoveInput (d->pipe_id);
326 XtRemoveTimeOut (d->pipe_timer);
327 memset (d, 0, sizeof (*d));
332 textclient_getc (text_data *d)
334 XtAppContext app = XtDisplayToApplicationContext (d->dpy);
337 if (XtAppPending (app) & (XtIMTimer|XtIMAlternateInput))
338 XtAppProcessEvent (app, XtIMTimer|XtIMAlternateInput);
340 if (d->out_buffer && *d->out_buffer)
342 ret = *d->out_buffer;
345 else if (d->input_available_p && d->pipe)
348 int n = read (fileno (d->pipe), (void *) s, 1);
354 XtRemoveInput (d->pipe_id);
360 fprintf (stderr, "%s: textclient: waitpid %d\n",
363 waitpid (d->pid, NULL, 0);
370 fprintf (stderr, "%s: textclient: pclose\n", progname);
376 if (d->out_column > 0)
379 fprintf (stderr, "%s: textclient: adding blank line at EOF\n",
382 d->out_buffer = "\r\n\r\n";
386 fprintf (stderr, "%s: textclient: relaunching in %d\n", progname,
387 (int) d->subproc_relaunch_delay);
390 XtAppAddTimeOut (app, d->subproc_relaunch_delay,
391 relaunch_generator_timer,
394 d->input_available_p = False;
397 if (ret == '\r' || ret == '\n')
404 fprintf (stderr, "%s: textclient: getc: %d\n", progname, ret);
406 fprintf (stderr, "%s: textclient: getc: %03o\n", progname, ret);
408 fprintf (stderr, "%s: textclient: getc: '%c'\n", progname, (char) ret);
415 /* The interpretation of the ModN modifiers is dependent on what keys
416 are bound to them: Mod1 does not necessarily mean "meta". It only
417 means "meta" if Meta_L or Meta_R are bound to it. If Meta_L is on
418 Mod5, then Mod5 is the one that means Meta. Oh, and Meta and Alt
419 aren't necessarily the same thing. Icepicks in my forehead!
422 do_icccm_meta_key_stupidity (Display *dpy)
424 unsigned int modbits = 0;
427 XModifierKeymap *modmap = XGetModifierMapping (dpy);
428 for (i = 3; i < 8; i++)
429 for (j = 0; j < modmap->max_keypermod; j++)
431 int code = modmap->modifiermap[i * modmap->max_keypermod + j];
434 if (code == 0) continue;
435 syms = XGetKeyboardMapping (dpy, code, 1, &nsyms);
436 for (k = 0; k < nsyms; k++)
437 if (syms[k] == XK_Meta_L || syms[k] == XK_Meta_R ||
438 syms[k] == XK_Alt_L || syms[k] == XK_Alt_R)
442 XFreeModifiermap (modmap);
443 # endif /* HAVE_COCOA */
448 /* Returns a mask of the bit or bits of a KeyPress event that mean "meta".
451 meta_modifier (text_data *d)
453 if (!d->meta_done_once)
455 /* Really, we are supposed to recompute this if a KeymapNotify
456 event comes in, but fuck it. */
457 d->meta_done_once = True;
458 d->meta_mask = do_icccm_meta_key_stupidity (d->dpy);
460 fprintf (stderr, "%s: textclient: ICCCM Meta is 0x%08X\n",
461 progname, d->meta_mask);
469 textclient_putc (text_data *d, XKeyEvent *k)
473 XLookupString (k, (char *) &c, 1, &keysym, &d->compose);
474 if (c != 0 && d->pipe)
476 if (!d->swap_bs_del_p) ;
477 else if (c == 127) c = 8;
478 else if (c == 8) c = 127;
480 /* If meta was held down, send ESC, or turn on the high bit. */
481 if (k->state & meta_modifier (d))
483 if (d->meta_sends_esc_p)
484 fputc ('\033', d->pipe);
491 k->type = 0; /* don't interpret this event defaultly. */
494 fprintf (stderr, "%s: textclient: putc '%c'\n", progname, (char) c);
502 #endif /* !USE_IPHONE -- whole file */