- 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;