简体   繁体   中英

How to route X11 Keyboard events into stdin like ghostscript does?

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?!!

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM