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;
97 launch_text_generator (text_data *d)
99 XtAppContext app = XtDisplayToApplicationContext (d->dpy);
101 const char *oprogram = d->program;
102 char *program = (char *) malloc (strlen (oprogram) + 50);
104 strcpy (program, "( ");
105 strcat (program, oprogram);
107 /* Kludge! Special-case "xscreensaver-text" to tell it how wide
108 the screen is. We used to do this by just always feeding
109 `program' through sprintf() and setting the default value to
110 "xscreensaver-text --cols %d", but that makes things blow up
111 if someone ever uses a --program that includes a % anywhere.
113 if (!strcmp (oprogram, "xscreensaver-text"))
116 sprintf (program + strlen(program), " --cols %d", d->char_w);
118 sprintf (program + strlen(program), " --lines %d", d->max_lines);
121 strcat (program, " ) 2>&1");
124 fprintf (stderr, "%s: textclient: launch %s: %s\n", progname,
125 (d->pty_p ? "pty" : "pipe"), program);
134 ws.ws_col = d->char_w;
135 ws.ws_row = d->char_h;
136 ws.ws_xpixel = d->pix_w;
137 ws.ws_ypixel = d->pix_h;
140 if ((d->pid = forkpty(&fd, NULL, NULL, &ws)) < 0)
143 sprintf (buf, "%.100s: forkpty", progname);
148 /* This is the child fork. */
151 if (putenv ("TERM=vt100"))
158 sprintf (buf, "%.100s: %.100s", progname, oprogram);
164 /* This is the parent fork. */
165 if (d->pipe) abort();
166 d->pipe = fdopen (fd, "r+");
167 if (d->pipe_id) abort();
169 XtAppAddInput (app, fileno (d->pipe),
170 (XtPointer) (XtInputReadMask | XtInputExceptMask),
171 subproc_cb, (XtPointer) d);
173 fprintf (stderr, "%s: textclient: pid = %d\n", progname, d->pid);
178 #endif /* HAVE_FORKPTY */
180 /* don't mess up controlling terminal on "-pipe -program tcsh". */
181 static int protected_stdin_p = 0;
182 if (! protected_stdin_p) {
184 open ("/dev/null", O_RDWR); /* re-allocate fd 0 */
185 protected_stdin_p = 1;
188 if (d->pipe) abort();
189 if ((d->pipe = popen (program, "r")))
191 if (d->pipe_id) abort();
193 XtAppAddInput (app, fileno (d->pipe),
194 (XtPointer) (XtInputReadMask | XtInputExceptMask),
195 subproc_cb, (XtPointer) d);
197 fprintf (stderr, "%s: textclient: popen\n", progname);
202 sprintf (buf, "%.100s: %.100s", progname, program);
212 relaunch_generator_timer (XtPointer closure, XtIntervalId *id)
214 text_data *d = (text_data *) closure;
215 /* if (!d->pipe_timer) abort(); */
218 fprintf (stderr, "%s: textclient: launch timer fired\n", progname);
220 launch_text_generator (d);
225 start_timer (text_data *d)
227 XtAppContext app = XtDisplayToApplicationContext (d->dpy);
230 fprintf (stderr, "%s: textclient: relaunching in %d\n", progname,
231 (int) d->subproc_relaunch_delay);
234 XtRemoveTimeOut (d->pipe_timer);
236 XtAppAddTimeOut (app, d->subproc_relaunch_delay,
237 relaunch_generator_timer,
243 close_pipe (text_data *d)
248 fprintf (stderr, "%s: textclient: kill %d\n", progname, d->pid);
250 kill (d->pid, SIGTERM);
255 XtRemoveInput (d->pipe_id);
261 fprintf (stderr, "%s: textclient: pclose\n", progname);
272 textclient_reshape (text_data *d,
273 int pix_w, int pix_h,
274 int char_w, int char_h,
277 # if defined(HAVE_FORKPTY) && defined(TIOCSWINSZ)
283 d->max_lines = max_lines;
286 fprintf (stderr, "%s: textclient: reshape: %dx%d, %dx%d\n", progname,
287 pix_w, pix_h, char_w, char_h);
290 if (d->pid && d->pipe)
292 /* Tell the sub-process that the screen size has changed. */
296 ws.ws_xpixel = pix_w;
297 ws.ws_ypixel = pix_h;
298 ioctl (fileno (d->pipe), TIOCSWINSZ, &ws);
299 kill (d->pid, SIGWINCH);
301 # endif /* HAVE_FORKPTY && TIOCSWINSZ */
304 /* If we're running xscreensaver-text, then kill and restart it any
305 time the window is resized so that it gets an updated --cols arg
306 right away. But if we're running something else, leave it alone.
308 if (!strcmp (d->program, "xscreensaver-text"))
311 d->input_available_p = False;
318 textclient_open (Display *dpy)
320 text_data *d = (text_data *) calloc (1, sizeof (*d));
323 fprintf (stderr, "%s: textclient: init\n", progname);
328 if (get_boolean_resource (dpy, "usePty", "UsePty"))
334 "%s: no pty support on this system; using a pipe instead.\n",
339 d->subproc_relaunch_delay =
340 get_integer_resource (dpy, "relaunchDelay", "Time");
341 if (d->subproc_relaunch_delay < 1)
342 d->subproc_relaunch_delay = 1;
343 d->subproc_relaunch_delay *= 1000;
346 d->meta_sends_esc_p = get_boolean_resource (dpy, "metaSendsESC", "Boolean");
347 d->swap_bs_del_p = get_boolean_resource (dpy, "swapBSDEL", "Boolean");
349 d->program = get_string_resource (dpy, "program", "Program");
353 /* Kludge for MacOS standalone mode: see OSX/SaverRunner.m. */
355 const char *s = getenv ("XSCREENSAVER_STANDALONE");
356 if (s && *s && strcmp(s, "0"))
359 d->program = strdup (getenv ("SHELL"));
371 textclient_close (text_data *d)
374 fprintf (stderr, "%s: textclient: free\n", progname);
381 XtRemoveTimeOut (d->pipe_timer);
383 memset (d, 0, sizeof (*d));
388 textclient_getc (text_data *d)
390 XtAppContext app = XtDisplayToApplicationContext (d->dpy);
393 if (XtAppPending (app) & (XtIMTimer|XtIMAlternateInput))
394 XtAppProcessEvent (app, XtIMTimer|XtIMAlternateInput);
396 if (d->out_buffer && *d->out_buffer)
398 ret = *d->out_buffer;
401 else if (d->input_available_p && d->pipe)
404 int n = read (fileno (d->pipe), (void *) s, 1);
412 fprintf (stderr, "%s: textclient: waitpid %d\n",
415 waitpid (d->pid, NULL, 0);
421 if (d->out_column > 0)
424 fprintf (stderr, "%s: textclient: adding blank line at EOF\n",
427 d->out_buffer = "\r\n\r\n";
432 d->input_available_p = False;
435 if (ret == '\r' || ret == '\n')
442 fprintf (stderr, "%s: textclient: getc: %d\n", progname, ret);
444 fprintf (stderr, "%s: textclient: getc: %03o\n", progname, ret);
446 fprintf (stderr, "%s: textclient: getc: '%c'\n", progname, (char) ret);
453 /* The interpretation of the ModN modifiers is dependent on what keys
454 are bound to them: Mod1 does not necessarily mean "meta". It only
455 means "meta" if Meta_L or Meta_R are bound to it. If Meta_L is on
456 Mod5, then Mod5 is the one that means Meta. Oh, and Meta and Alt
457 aren't necessarily the same thing. Icepicks in my forehead!
460 do_icccm_meta_key_stupidity (Display *dpy)
462 unsigned int modbits = 0;
465 XModifierKeymap *modmap = XGetModifierMapping (dpy);
466 for (i = 3; i < 8; i++)
467 for (j = 0; j < modmap->max_keypermod; j++)
469 int code = modmap->modifiermap[i * modmap->max_keypermod + j];
472 if (code == 0) continue;
473 syms = XGetKeyboardMapping (dpy, code, 1, &nsyms);
474 for (k = 0; k < nsyms; k++)
475 if (syms[k] == XK_Meta_L || syms[k] == XK_Meta_R ||
476 syms[k] == XK_Alt_L || syms[k] == XK_Alt_R)
480 XFreeModifiermap (modmap);
481 # endif /* HAVE_COCOA */
486 /* Returns a mask of the bit or bits of a KeyPress event that mean "meta".
489 meta_modifier (text_data *d)
491 if (!d->meta_done_once)
493 /* Really, we are supposed to recompute this if a KeymapNotify
494 event comes in, but fuck it. */
495 d->meta_done_once = True;
496 d->meta_mask = do_icccm_meta_key_stupidity (d->dpy);
498 fprintf (stderr, "%s: textclient: ICCCM Meta is 0x%08X\n",
499 progname, d->meta_mask);
507 textclient_putc (text_data *d, XKeyEvent *k)
511 XLookupString (k, (char *) &c, 1, &keysym, &d->compose);
512 if (c != 0 && d->pipe)
514 if (!d->swap_bs_del_p) ;
515 else if (c == 127) c = 8;
516 else if (c == 8) c = 127;
518 /* If meta was held down, send ESC, or turn on the high bit. */
519 if (k->state & meta_modifier (d))
521 if (d->meta_sends_esc_p)
522 fputc ('\033', d->pipe);
529 k->type = 0; /* don't interpret this event defaultly. */
532 fprintf (stderr, "%s: textclient: putc '%c'\n", progname, (char) c);
540 #endif /* !USE_IPHONE -- whole file */