1 /* xscreensaver, Copyright (c) 2012-2016 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.
20 * On iOS and Android, textclient-mobile.c is used instead.
25 #if !defined(USE_IPHONE) && !defined(HAVE_ANDROID) /* whole file */
27 #include "textclient.h"
28 #include "resources.h"
31 # define XK_MISCELLANY
32 # include <X11/keysymdef.h>
33 # include <X11/Xatom.h>
34 # include <X11/Intrinsic.h>
44 # include <fcntl.h> /* for O_RDWR */
48 # include <sys/ioctl.h>
55 # ifdef HAVE_SYS_TERMIOS_H
56 # include <sys/termios.h>
58 #endif /* HAVE_FORKPTY */
62 extern const char *progname;
67 int pix_w, pix_h, char_w, char_h;
71 XtIntervalId pipe_timer;
75 Bool input_available_p;
76 Time subproc_relaunch_delay;
77 XComposeStatus compose;
79 Bool meta_sends_esc_p;
82 unsigned int meta_mask;
84 const char *out_buffer;
90 subproc_cb (XtPointer closure, int *source, XtInputId *id)
92 text_data *d = (text_data *) closure;
94 if (! d->input_available_p)
95 fprintf (stderr, "%s: textclient: input available\n", progname);
97 d->input_available_p = True;
101 # define BACKSLASH(c) \
102 (! ((c >= 'a' && c <= 'z') || \
103 (c >= 'A' && c <= 'Z') || \
104 (c >= '0' && c <= '9') || \
105 c == '.' || c == '_' || c == '-' || c == '+' || c == '/'))
108 launch_text_generator (text_data *d)
110 XtAppContext app = XtDisplayToApplicationContext (d->dpy);
112 const char *oprogram = d->program;
116 /* /bin/sh on OS X 10.10 wipes out the PATH. */
117 const char *path = getenv("PATH");
118 char *cmd = s = malloc ((strlen(oprogram) + strlen(path)) * 2 + 100);
119 strcpy (s, "export PATH=");
123 if (BACKSLASH(c)) *s++ = '\\';
129 char *cmd = s = malloc ((strlen(oprogram)) * 2 + 100);
133 strcat (s, oprogram);
136 /* Kludge! Special-case "xscreensaver-text" to tell it how wide
137 the screen is. We used to do this by just always feeding
138 `program' through sprintf() and setting the default value to
139 "xscreensaver-text --cols %d", but that makes things blow up
140 if someone ever uses a --program that includes a % anywhere.
142 if (!strcmp (oprogram, "xscreensaver-text"))
145 sprintf (s, " --cols %d", d->char_w);
147 sprintf (s, " --lines %d", d->max_lines);
151 strcpy (s, " ) 2>&1");
154 fprintf (stderr, "%s: textclient: launch %s: %s\n", cmd,
155 (d->pty_p ? "pty" : "pipe"), program);
164 ws.ws_col = d->char_w;
165 ws.ws_row = d->char_h;
166 ws.ws_xpixel = d->pix_w;
167 ws.ws_ypixel = d->pix_h;
170 if ((d->pid = forkpty(&fd, NULL, NULL, &ws)) < 0)
173 sprintf (buf, "%.100s: forkpty", progname);
178 /* This is the child fork. */
181 if (putenv ("TERM=vt100"))
188 sprintf (buf, "%.100s: %.100s", progname, oprogram);
194 /* This is the parent fork. */
195 if (d->pipe) abort();
196 d->pipe = fdopen (fd, "r+");
197 if (d->pipe_id) abort();
199 XtAppAddInput (app, fileno (d->pipe),
200 (XtPointer) (XtInputReadMask | XtInputExceptMask),
201 subproc_cb, (XtPointer) d);
203 fprintf (stderr, "%s: textclient: pid = %d\n", progname, d->pid);
208 #endif /* HAVE_FORKPTY */
210 /* don't mess up controlling terminal on "-pipe -program tcsh". */
211 static int protected_stdin_p = 0;
212 if (! protected_stdin_p) {
214 open ("/dev/null", O_RDWR); /* re-allocate fd 0 */
215 protected_stdin_p = 1;
218 if (d->pipe) abort();
219 if ((d->pipe = popen (cmd, "r")))
221 if (d->pipe_id) abort();
223 XtAppAddInput (app, fileno (d->pipe),
224 (XtPointer) (XtInputReadMask | XtInputExceptMask),
225 subproc_cb, (XtPointer) d);
227 fprintf (stderr, "%s: textclient: popen\n", progname);
232 sprintf (buf, "%.100s: %.100s", progname, cmd);
242 relaunch_generator_timer (XtPointer closure, XtIntervalId *id)
244 text_data *d = (text_data *) closure;
245 /* if (!d->pipe_timer) abort(); */
248 fprintf (stderr, "%s: textclient: launch timer fired\n", progname);
250 launch_text_generator (d);
255 start_timer (text_data *d)
257 XtAppContext app = XtDisplayToApplicationContext (d->dpy);
260 fprintf (stderr, "%s: textclient: relaunching in %d\n", progname,
261 (int) d->subproc_relaunch_delay);
264 XtRemoveTimeOut (d->pipe_timer);
266 XtAppAddTimeOut (app, d->subproc_relaunch_delay,
267 relaunch_generator_timer,
273 close_pipe (text_data *d)
278 fprintf (stderr, "%s: textclient: kill %d\n", progname, d->pid);
280 kill (d->pid, SIGTERM);
285 XtRemoveInput (d->pipe_id);
291 fprintf (stderr, "%s: textclient: pclose\n", progname);
302 textclient_reshape (text_data *d,
303 int pix_w, int pix_h,
304 int char_w, int char_h,
307 # if defined(HAVE_FORKPTY) && defined(TIOCSWINSZ)
313 d->max_lines = max_lines;
316 fprintf (stderr, "%s: textclient: reshape: %dx%d, %dx%d\n", progname,
317 pix_w, pix_h, char_w, char_h);
320 if (d->pid && d->pipe)
322 /* Tell the sub-process that the screen size has changed. */
326 ws.ws_xpixel = pix_w;
327 ws.ws_ypixel = pix_h;
328 ioctl (fileno (d->pipe), TIOCSWINSZ, &ws);
329 kill (d->pid, SIGWINCH);
331 # endif /* HAVE_FORKPTY && TIOCSWINSZ */
334 /* If we're running xscreensaver-text, then kill and restart it any
335 time the window is resized so that it gets an updated --cols arg
336 right away. But if we're running something else, leave it alone.
338 if (!strcmp (d->program, "xscreensaver-text"))
341 d->input_available_p = False;
348 textclient_open (Display *dpy)
350 text_data *d = (text_data *) calloc (1, sizeof (*d));
353 fprintf (stderr, "%s: textclient: init\n", progname);
358 if (get_boolean_resource (dpy, "usePty", "UsePty"))
364 "%s: no pty support on this system; using a pipe instead.\n",
369 d->subproc_relaunch_delay =
370 get_integer_resource (dpy, "relaunchDelay", "Time");
371 if (d->subproc_relaunch_delay < 1)
372 d->subproc_relaunch_delay = 1;
373 d->subproc_relaunch_delay *= 1000;
376 d->meta_sends_esc_p = get_boolean_resource (dpy, "metaSendsESC", "Boolean");
377 d->swap_bs_del_p = get_boolean_resource (dpy, "swapBSDEL", "Boolean");
379 d->program = get_string_resource (dpy, "program", "Program");
383 /* Kludge for MacOS standalone mode: see OSX/SaverRunner.m. */
385 const char *s = getenv ("XSCREENSAVER_STANDALONE");
386 if (s && *s && strcmp(s, "0"))
389 d->program = strdup (getenv ("SHELL"));
401 textclient_close (text_data *d)
404 fprintf (stderr, "%s: textclient: free\n", progname);
411 XtRemoveTimeOut (d->pipe_timer);
413 memset (d, 0, sizeof (*d));
418 textclient_getc (text_data *d)
420 XtAppContext app = XtDisplayToApplicationContext (d->dpy);
423 if (XtAppPending (app) & (XtIMTimer|XtIMAlternateInput))
424 XtAppProcessEvent (app, XtIMTimer|XtIMAlternateInput);
426 if (d->out_buffer && *d->out_buffer)
428 ret = *d->out_buffer;
431 else if (d->input_available_p && d->pipe)
434 int n = read (fileno (d->pipe), (void *) s, 1);
442 fprintf (stderr, "%s: textclient: waitpid %d\n",
445 waitpid (d->pid, NULL, 0);
451 if (d->out_column > 0)
454 fprintf (stderr, "%s: textclient: adding blank line at EOF\n",
457 d->out_buffer = "\r\n\r\n";
462 d->input_available_p = False;
465 if (ret == '\r' || ret == '\n')
472 fprintf (stderr, "%s: textclient: getc: %d\n", progname, ret);
474 fprintf (stderr, "%s: textclient: getc: %03o\n", progname, ret);
476 fprintf (stderr, "%s: textclient: getc: '%c'\n", progname, (char) ret);
483 /* The interpretation of the ModN modifiers is dependent on what keys
484 are bound to them: Mod1 does not necessarily mean "meta". It only
485 means "meta" if Meta_L or Meta_R are bound to it. If Meta_L is on
486 Mod5, then Mod5 is the one that means Meta. Oh, and Meta and Alt
487 aren't necessarily the same thing. Icepicks in my forehead!
490 do_icccm_meta_key_stupidity (Display *dpy)
492 unsigned int modbits = 0;
495 XModifierKeymap *modmap = XGetModifierMapping (dpy);
496 for (i = 3; i < 8; i++)
497 for (j = 0; j < modmap->max_keypermod; j++)
499 int code = modmap->modifiermap[i * modmap->max_keypermod + j];
502 if (code == 0) continue;
503 syms = XGetKeyboardMapping (dpy, code, 1, &nsyms);
504 for (k = 0; k < nsyms; k++)
505 if (syms[k] == XK_Meta_L || syms[k] == XK_Meta_R ||
506 syms[k] == XK_Alt_L || syms[k] == XK_Alt_R)
510 XFreeModifiermap (modmap);
511 # endif /* HAVE_COCOA */
516 /* Returns a mask of the bit or bits of a KeyPress event that mean "meta".
519 meta_modifier (text_data *d)
521 if (!d->meta_done_once)
523 /* Really, we are supposed to recompute this if a KeymapNotify
524 event comes in, but fuck it. */
525 d->meta_done_once = True;
526 d->meta_mask = do_icccm_meta_key_stupidity (d->dpy);
528 fprintf (stderr, "%s: textclient: ICCCM Meta is 0x%08X\n",
529 progname, d->meta_mask);
537 textclient_putc (text_data *d, XKeyEvent *k)
541 XLookupString (k, (char *) &c, 1, &keysym, &d->compose);
542 if (c != 0 && d->pipe)
544 if (!d->swap_bs_del_p) ;
545 else if (c == 127) c = 8;
546 else if (c == 8) c = 127;
548 /* If meta was held down, send ESC, or turn on the high bit. */
549 if (k->state & meta_modifier (d))
551 if (d->meta_sends_esc_p)
552 fputc ('\033', d->pipe);
559 k->type = 0; /* don't interpret this event defaultly. */
562 fprintf (stderr, "%s: textclient: putc '%c'\n", progname, (char) c);
570 #endif /* !USE_IPHONE -- whole file */