1 /* xscreensaver, Copyright (c) 2012-2014 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;
66 XtIntervalId pipe_timer;
70 Bool input_available_p;
71 Time subproc_relaunch_delay;
72 XComposeStatus compose;
74 Bool meta_sends_esc_p;
77 unsigned int meta_mask;
79 const char *out_buffer;
85 subproc_cb (XtPointer closure, int *source, XtInputId *id)
87 text_data *d = (text_data *) closure;
89 if (! d->input_available_p)
90 fprintf (stderr, "%s: textclient: input available\n", progname);
92 d->input_available_p = True;
96 # define BACKSLASH(c) \
97 (! ((c >= 'a' && c <= 'z') || \
98 (c >= 'A' && c <= 'Z') || \
99 (c >= '0' && c <= '9') || \
100 c == '.' || c == '_' || c == '-' || c == '+' || c == '/'))
103 launch_text_generator (text_data *d)
105 XtAppContext app = XtDisplayToApplicationContext (d->dpy);
107 const char *oprogram = d->program;
111 /* /bin/sh on OS X 10.10 wipes out the PATH. */
112 const char *path = getenv("PATH");
113 char *cmd = s = malloc ((strlen(oprogram) + strlen(path)) * 2 + 100);
114 strcpy (s, "export PATH=");
118 if (BACKSLASH(c)) *s++ = '\\';
124 char *cmd = s = malloc ((strlen(oprogram)) * 2 + 100);
128 strcat (s, oprogram);
131 /* Kludge! Special-case "xscreensaver-text" to tell it how wide
132 the screen is. We used to do this by just always feeding
133 `program' through sprintf() and setting the default value to
134 "xscreensaver-text --cols %d", but that makes things blow up
135 if someone ever uses a --program that includes a % anywhere.
137 if (!strcmp (oprogram, "xscreensaver-text"))
140 sprintf (s, " --cols %d", d->char_w);
142 sprintf (s, " --lines %d", d->max_lines);
146 strcpy (s, " ) 2>&1");
149 fprintf (stderr, "%s: textclient: launch %s: %s\n", cmd,
150 (d->pty_p ? "pty" : "pipe"), program);
159 ws.ws_col = d->char_w;
160 ws.ws_row = d->char_h;
161 ws.ws_xpixel = d->pix_w;
162 ws.ws_ypixel = d->pix_h;
165 if ((d->pid = forkpty(&fd, NULL, NULL, &ws)) < 0)
168 sprintf (buf, "%.100s: forkpty", progname);
173 /* This is the child fork. */
176 if (putenv ("TERM=vt100"))
183 sprintf (buf, "%.100s: %.100s", progname, oprogram);
189 /* This is the parent fork. */
190 if (d->pipe) abort();
191 d->pipe = fdopen (fd, "r+");
192 if (d->pipe_id) abort();
194 XtAppAddInput (app, fileno (d->pipe),
195 (XtPointer) (XtInputReadMask | XtInputExceptMask),
196 subproc_cb, (XtPointer) d);
198 fprintf (stderr, "%s: textclient: pid = %d\n", progname, d->pid);
203 #endif /* HAVE_FORKPTY */
205 /* don't mess up controlling terminal on "-pipe -program tcsh". */
206 static int protected_stdin_p = 0;
207 if (! protected_stdin_p) {
209 open ("/dev/null", O_RDWR); /* re-allocate fd 0 */
210 protected_stdin_p = 1;
213 if (d->pipe) abort();
214 if ((d->pipe = popen (cmd, "r")))
216 if (d->pipe_id) abort();
218 XtAppAddInput (app, fileno (d->pipe),
219 (XtPointer) (XtInputReadMask | XtInputExceptMask),
220 subproc_cb, (XtPointer) d);
222 fprintf (stderr, "%s: textclient: popen\n", progname);
227 sprintf (buf, "%.100s: %.100s", progname, cmd);
237 relaunch_generator_timer (XtPointer closure, XtIntervalId *id)
239 text_data *d = (text_data *) closure;
240 /* if (!d->pipe_timer) abort(); */
243 fprintf (stderr, "%s: textclient: launch timer fired\n", progname);
245 launch_text_generator (d);
250 start_timer (text_data *d)
252 XtAppContext app = XtDisplayToApplicationContext (d->dpy);
255 fprintf (stderr, "%s: textclient: relaunching in %d\n", progname,
256 (int) d->subproc_relaunch_delay);
259 XtRemoveTimeOut (d->pipe_timer);
261 XtAppAddTimeOut (app, d->subproc_relaunch_delay,
262 relaunch_generator_timer,
268 close_pipe (text_data *d)
273 fprintf (stderr, "%s: textclient: kill %d\n", progname, d->pid);
275 kill (d->pid, SIGTERM);
280 XtRemoveInput (d->pipe_id);
286 fprintf (stderr, "%s: textclient: pclose\n", progname);
297 textclient_reshape (text_data *d,
298 int pix_w, int pix_h,
299 int char_w, int char_h,
302 # if defined(HAVE_FORKPTY) && defined(TIOCSWINSZ)
308 d->max_lines = max_lines;
311 fprintf (stderr, "%s: textclient: reshape: %dx%d, %dx%d\n", progname,
312 pix_w, pix_h, char_w, char_h);
315 if (d->pid && d->pipe)
317 /* Tell the sub-process that the screen size has changed. */
321 ws.ws_xpixel = pix_w;
322 ws.ws_ypixel = pix_h;
323 ioctl (fileno (d->pipe), TIOCSWINSZ, &ws);
324 kill (d->pid, SIGWINCH);
326 # endif /* HAVE_FORKPTY && TIOCSWINSZ */
329 /* If we're running xscreensaver-text, then kill and restart it any
330 time the window is resized so that it gets an updated --cols arg
331 right away. But if we're running something else, leave it alone.
333 if (!strcmp (d->program, "xscreensaver-text"))
336 d->input_available_p = False;
343 textclient_open (Display *dpy)
345 text_data *d = (text_data *) calloc (1, sizeof (*d));
348 fprintf (stderr, "%s: textclient: init\n", progname);
353 if (get_boolean_resource (dpy, "usePty", "UsePty"))
359 "%s: no pty support on this system; using a pipe instead.\n",
364 d->subproc_relaunch_delay =
365 get_integer_resource (dpy, "relaunchDelay", "Time");
366 if (d->subproc_relaunch_delay < 1)
367 d->subproc_relaunch_delay = 1;
368 d->subproc_relaunch_delay *= 1000;
371 d->meta_sends_esc_p = get_boolean_resource (dpy, "metaSendsESC", "Boolean");
372 d->swap_bs_del_p = get_boolean_resource (dpy, "swapBSDEL", "Boolean");
374 d->program = get_string_resource (dpy, "program", "Program");
378 /* Kludge for MacOS standalone mode: see OSX/SaverRunner.m. */
380 const char *s = getenv ("XSCREENSAVER_STANDALONE");
381 if (s && *s && strcmp(s, "0"))
384 d->program = strdup (getenv ("SHELL"));
396 textclient_close (text_data *d)
399 fprintf (stderr, "%s: textclient: free\n", progname);
406 XtRemoveTimeOut (d->pipe_timer);
408 memset (d, 0, sizeof (*d));
413 textclient_getc (text_data *d)
415 XtAppContext app = XtDisplayToApplicationContext (d->dpy);
418 if (XtAppPending (app) & (XtIMTimer|XtIMAlternateInput))
419 XtAppProcessEvent (app, XtIMTimer|XtIMAlternateInput);
421 if (d->out_buffer && *d->out_buffer)
423 ret = *d->out_buffer;
426 else if (d->input_available_p && d->pipe)
429 int n = read (fileno (d->pipe), (void *) s, 1);
437 fprintf (stderr, "%s: textclient: waitpid %d\n",
440 waitpid (d->pid, NULL, 0);
446 if (d->out_column > 0)
449 fprintf (stderr, "%s: textclient: adding blank line at EOF\n",
452 d->out_buffer = "\r\n\r\n";
457 d->input_available_p = False;
460 if (ret == '\r' || ret == '\n')
467 fprintf (stderr, "%s: textclient: getc: %d\n", progname, ret);
469 fprintf (stderr, "%s: textclient: getc: %03o\n", progname, ret);
471 fprintf (stderr, "%s: textclient: getc: '%c'\n", progname, (char) ret);
478 /* The interpretation of the ModN modifiers is dependent on what keys
479 are bound to them: Mod1 does not necessarily mean "meta". It only
480 means "meta" if Meta_L or Meta_R are bound to it. If Meta_L is on
481 Mod5, then Mod5 is the one that means Meta. Oh, and Meta and Alt
482 aren't necessarily the same thing. Icepicks in my forehead!
485 do_icccm_meta_key_stupidity (Display *dpy)
487 unsigned int modbits = 0;
490 XModifierKeymap *modmap = XGetModifierMapping (dpy);
491 for (i = 3; i < 8; i++)
492 for (j = 0; j < modmap->max_keypermod; j++)
494 int code = modmap->modifiermap[i * modmap->max_keypermod + j];
497 if (code == 0) continue;
498 syms = XGetKeyboardMapping (dpy, code, 1, &nsyms);
499 for (k = 0; k < nsyms; k++)
500 if (syms[k] == XK_Meta_L || syms[k] == XK_Meta_R ||
501 syms[k] == XK_Alt_L || syms[k] == XK_Alt_R)
505 XFreeModifiermap (modmap);
506 # endif /* HAVE_COCOA */
511 /* Returns a mask of the bit or bits of a KeyPress event that mean "meta".
514 meta_modifier (text_data *d)
516 if (!d->meta_done_once)
518 /* Really, we are supposed to recompute this if a KeymapNotify
519 event comes in, but fuck it. */
520 d->meta_done_once = True;
521 d->meta_mask = do_icccm_meta_key_stupidity (d->dpy);
523 fprintf (stderr, "%s: textclient: ICCCM Meta is 0x%08X\n",
524 progname, d->meta_mask);
532 textclient_putc (text_data *d, XKeyEvent *k)
536 XLookupString (k, (char *) &c, 1, &keysym, &d->compose);
537 if (c != 0 && d->pipe)
539 if (!d->swap_bs_del_p) ;
540 else if (c == 127) c = 8;
541 else if (c == 8) c = 127;
543 /* If meta was held down, send ESC, or turn on the high bit. */
544 if (k->state & meta_modifier (d))
546 if (d->meta_sends_esc_p)
547 fputc ('\033', d->pipe);
554 k->type = 0; /* don't interpret this event defaultly. */
557 fprintf (stderr, "%s: textclient: putc '%c'\n", progname, (char) c);
565 #endif /* !USE_IPHONE -- whole file */