-static void
-subproc_cb (XtPointer closure, int *source, XtInputId *id)
-{
- p_state *state = (p_state *) closure;
- state->input_available_p = True;
-}
-
-
-static void
-launch_text_generator (p_state *state)
-{
- XtAppContext app = XtDisplayToApplicationContext (state->dpy);
- Display *dpy = state->dpy;
- char buf[255];
- char *oprogram = get_string_resource (dpy, "program", "Program");
- char *program = (char *) malloc (strlen (oprogram) + 50);
-
- strcpy (program, "( ");
- strcat (program, oprogram);
-
- /* Kludge! Special-case "xscreensaver-text" to tell it how wide
- the screen is. We used to do this by just always feeding
- `program' through sprintf() and setting the default value to
- "xscreensaver-text --cols %d", but that makes things blow up
- if someone ever uses a --program that includes a % anywhere.
- */
- if (!strcmp (oprogram, "xscreensaver-text"))
- sprintf (program + strlen(program), " --cols %d", state->grid_width-1);
-
- strcat (program, " ) 2>&1");
-
-#ifdef HAVE_FORKPTY
- if(state->mode == 1)
- {
- int fd;
- struct winsize ws;
-
- ws.ws_row = state->grid_height - 1;
- ws.ws_col = state->grid_width - 2;
- ws.ws_xpixel = state->xgwa.width;
- ws.ws_ypixel = state->xgwa.height;
-
- state->pipe = NULL;
- if((state->pid = forkpty(&fd, NULL, NULL, &ws)) < 0)
- {
- /* Unable to fork */
- sprintf (buf, "%.100s: forkpty", progname);
- perror(buf);
- }
- else if(!state->pid)
- {
- /* This is the child fork. */
- char *av[10];
- int i = 0;
- if (putenv("TERM=vt100"))
- abort();
- av[i++] = "/bin/sh";
- av[i++] = "-c";
- av[i++] = program;
- av[i] = 0;
- execvp (av[0], av);
- sprintf (buf, "%.100s: %.100s", progname, oprogram);
- perror(buf);
- exit(1);
- }
- else
- {
- /* This is the parent fork. */
- state->pipe = fdopen(fd, "r+");
- state->pipe_id =
- XtAppAddInput (app, fileno (state->pipe),
- (XtPointer) (XtInputReadMask | XtInputExceptMask),
- subproc_cb, (XtPointer) state);
- }
- }
- else
-#endif /* HAVE_FORKPTY */
- {
- /* don't mess up controlling terminal if someone dumbly does
- "-pipe -program tcsh". */
- static int protected_stdin_p = 0;
- if (! protected_stdin_p) {
- fclose (stdin);
- open ("/dev/null", O_RDWR); /* re-allocate fd 0 */
- protected_stdin_p = 1;
- }
-
- if ((state->pipe = popen (program, "r")))
- {
- state->pipe_id =
- XtAppAddInput (app, fileno (state->pipe),
- (XtPointer) (XtInputReadMask | XtInputExceptMask),
- subproc_cb, (XtPointer) state);
- }
- else
- {
- sprintf (buf, "%.100s: %.100s", progname, program);
- perror (buf);
- }
- }
-
- free (program);
-}
-
-
-static void
-relaunch_generator_timer (XtPointer closure, XtIntervalId *id)
-{
- p_state *state = (p_state *) closure;
- if (!state->pipe_timer) abort();
- state->pipe_timer = 0;
- launch_text_generator (state);
-}
-
-
-static void
-drain_input (p_state *state)
-{
- XtAppContext app = XtDisplayToApplicationContext (state->dpy);
- if (state->input_available_p && state->pipe)
- {
- unsigned char s[2];
- int n = read (fileno (state->pipe), (void *) s, 1);
- if (n == 1)
- {
- print_char (state, s[0]);
- }
- else
- {
- XtRemoveInput (state->pipe_id);
- state->pipe_id = 0;
- if (state->pid)
- {
- waitpid(state->pid, NULL, 0);
- fclose (state->pipe);
- state->pid = 0;
- }
- else
- {
- pclose (state->pipe);
- }
- state->pipe = 0;
-
- if (state->cursor_x != 0) { /* break line if unbroken */
- print_char (state, '\r');
- print_char (state, '\n');
- }
- print_char (state, '\r'); /* blank line */
- print_char (state, '\n');
-
- /* Set up a timer to re-launch the subproc in a bit. */
- state->pipe_timer =
- XtAppAddTimeOut (app, state->subproc_relaunch_delay,
- relaunch_generator_timer,
- (XtPointer) state);
- }
-
- state->input_available_p = False;
- }
-}
-
-
-/* The interpretation of the ModN modifiers is dependent on what keys
- are bound to them: Mod1 does not necessarily mean "meta". It only
- means "meta" if Meta_L or Meta_R are bound to it. If Meta_L is on
- Mod5, then Mod5 is the one that means Meta. Oh, and Meta and Alt
- aren't necessarily the same thing. Icepicks in my forehead!
- */
-static unsigned int
-do_icccm_meta_key_stupidity (Display *dpy)
-{
- unsigned int modbits = 0;
-# ifndef HAVE_COCOA
- int i, j, k;
- XModifierKeymap *modmap = XGetModifierMapping (dpy);
- for (i = 3; i < 8; i++)
- for (j = 0; j < modmap->max_keypermod; j++)
- {
- int code = modmap->modifiermap[i * modmap->max_keypermod + j];
- KeySym *syms;
- int nsyms = 0;
- if (code == 0) continue;
- syms = XGetKeyboardMapping (dpy, code, 1, &nsyms);
- for (k = 0; k < nsyms; k++)
- if (syms[k] == XK_Meta_L || syms[k] == XK_Meta_R ||
- syms[k] == XK_Alt_L || syms[k] == XK_Alt_R)
- modbits |= (1 << i);
- XFree (syms);
- }
- XFreeModifiermap (modmap);
-# endif /* HAVE_COCOA */
- return modbits;
-}
-
-/* Returns a mask of the bit or bits of a KeyPress event that mean "meta".
- */
-static unsigned int
-meta_modifier (p_state *state)
-{
- if (!state->meta_done_once)
- {
- /* Really, we are supposed to recompute this if a KeymapNotify
- event comes in, but fuck it. */
- state->meta_done_once = True;
- state->meta_mask = do_icccm_meta_key_stupidity (state->dpy);
- }
- return state->meta_mask;