1

I'm trying to write a clone of ghostscript and I can't figure out how they make it so you can type into the graphics window, and the keystrokes show up in the terminal window as though you'd typed them there to begin with. So having received the KeyRelease Event, can I stuff the char into stdin somehow, to be read with normal filereading code? Or do I have to make my own internal buffer in front of stdin so I can hack new chars into it? Or is ther some simple way to map keyboard events from my application window to Xterm?

I'm willing to do the work, but I don't even know what I'm looking for here. Help?!!

luser droog
  • 18,988
  • 3
  • 53
  • 105
  • Does ghostscript really redirect the keypress events to its own `stdin` or does it just catch keypresses and handle them as if they came from `stdin`? – mu is too short Jun 25 '11 at 06:18
  • 1
    Not sure. I suppose that would've been the thing to do before asking. :( – luser droog Jun 25 '11 at 06:23
  • The "send them both to the same place" approach is how I'd do it, trying to feed things into your own `stdin` seems like a bunch of pointless busy work. Anyway, it is easy to get locked into the wrong path, asking a question is a quick way to get fresh eyes to look at your problem and point out the obvious things that you miss. – mu is too short Jun 25 '11 at 06:29
  • 1
    I just spent an hour looking though the gs source. Haven't found it yet. – luser droog Jun 25 '11 at 07:43
  • Have you found the X event loop? I'm pretty sure ghostscript uses raw XLib for the UI so there should be a big ugly `switch` statement somewhere. OTOH, I haven't done any X programming in over a decade so take what I say with a grain of salt. – mu is too short Jun 25 '11 at 07:47
  • I'm fairly certain there isn't going to be an "X Event loop" in the traditional sense. X11 is just one of many output devices that can be selected from the command line. I found one file that looks promising: [http://git.ghostscript.com/?p=ghostpdl.git;a=blob;f=gs/base/gdevx.c;h=d8b91ad44ff2920ed32aadf4693da27d0c24cf67;hb=HEAD] but I only see Expose events. – luser droog Jun 26 '11 at 07:06

3 Answers3

3

I don't think gs does this (at least on linux).

I tried it running from to my linux box from a SSH session and switched focus to the X11 windows that pops up with the rendered image (tiger) and the keys I pressed there did NOT go to the application on the remote host.

The (end of the) strace shows GS waiting for stdin -- the read with fd=0

read(3, "  } if\n  psuserparams readonly p"..., 4096) = 3258
brk(0x1124000)                          = 0x1124000
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7f8ccaee5000, 4096)            = 0
poll([{fd=4, events=POLLIN|POLLOUT}], 1, -1) = 1 ([{fd=4, revents=POLLOUT}])
writev(4, [{"+\2\1\0", 4}, {NULL, 0}, {"", 0}], 3) = 4
poll([{fd=4, events=POLLIN}], 1, -1)    = 1 ([{fd=4, revents=POLLIN}])
read(4, "\1\1'\0\0\0\0\0\1\0\200\0\0\0\0\0\1\0\0\0\264\2\0\0008\0A\2\4\0\0\0", 4096) = 32
read(4, 0xc9bd54, 4096)                 = -1 EAGAIN (Resource temporarily unavailable)
fstat(1, {st_mode=S_IFREG|0644, st_size=143204, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8ccaee5000
write(1, "GS>", 3GS>)                      = 3
read(0,  ...unfinished ...

I had to switch focus back to the ssh window in order for the key press (Ctrl-C) to take effect. I had tried "quit" as well as ctrl-C when the focus was the image X11 window.

Ray Johnston
  • 613
  • 4
  • 3
  • My god! You're right! It must be a feature of the window manager. With KDE, I guess, since ghostscript doesn't request any input events, the graphics window cannot accept focus. By going into the window specific options I was able to force accept focus, and sure enough, typing has no effect. Mystery solved. Thank you! – luser droog Jun 29 '11 at 04:58
2

I don't know the answer, but I know the way to find it. Run ghostscript under strace and watch what it's doing. This is usually a lot easier and more informative than trying to read source.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
2

Eureka!

in the file gdevxini.c

435             wm_hints.flags = InputHint;
436             wm_hints.input = False;
437             XSetWMHints(xdev->dpy, xdev->win, &wm_hints);       /* avoid input focus */

Edit: Now that I know what it looks like, I was able to find some documentation:

The input member is used to communicate to the window manager the input focus model used by the application.... Applications that never expect any keyboard input ... should set this member to False. --X Window System: C Library and Protocol Reference, p.282

luser droog
  • 18,988
  • 3
  • 53
  • 105