5

I've got an X11 program (Mathematica/Linux) running which contains unsaved data (calculated after I left; unfortunately I didn't have the foresight to programmatically save the data). Now I've gotten an email that the power will be switched off, unfortunately before I'm back. Therefore I want to save the data, which I could do by simply sending a Ctrl-S to the right window (I know how to find the window ID). Unfortunately there's no xdotool or autokey installed, and I don't have root rights to install one of them. So is there a way to do it?

Of course one way would be to write a C program to do it (since the functionality must be there, or those other programs couldn't work), but I've never written anything for X11, so I don't think I'll get it written in time ...

I've got access to the machine with ssh, and can access the display (I can do a screenshot - which shows the lock screen - and I can obtain a window list using xwininfo). So all I need is a way to send a single Ctrl-S to a specific window without previously installing something.

  • 1
    Maybe this will help you? doctort.org/adam/nerd-notes/x11-fake-keypress-event.html – Ernest A Mar 25 '15 at 19:11
  • @ErnestA: Thank you; that looks like a good start, however I don't see a way to focus the window (I'm not even sure if this would be possible while the screen is locked) and can't find out how to get the window (i.e. whatever is stored in a Window variable for use in the program; in the linked code, it's filled by XGetInputFocus) from the window ID. – celtschk Mar 25 '15 at 21:07
  • OK, I've found it out: The value to store there is the window id. I've got the code to work (xev tells me it receives the artificial Ctrl-S). So my question is effectively answered, thanks for that. Unfortunately Mathematica doesn't react to the simulated keypress, so my actual problem remains unsolved ... I guess I'll have to either think of another way, or to accept that the data is lost. – celtschk Mar 25 '15 at 23:10
  • @celtschk What did you use to send the event? Some programs ignore fake (“synthetic”) keypress events, but xdotool uses the XTEST extension (a debugging facility) to generate a genuine event (as far as the receiving application is concerned), so it should work. Even if it isn't installed, you can grab a binary from somewhere and copy it. – Gilles 'SO- stop being evil' Mar 26 '15 at 0:59
  • 1
    Ah, good point, I hadn't thought of the screensaver. Glad you solved it. Could you write this as an answer for the sake of future visitors? Thanks. – Gilles 'SO- stop being evil' Mar 26 '15 at 9:48
5

I solved the problem.

The first part of the solution was the information by Gilles that xdotool is just a binary, so there's no need to install it. Just copying the executable to the remote machine (on which I have permission to run executables stored in my own directories) was sufficient.

Note that when below I mention any command, I assume that the DISPLAY variable is set to the display where Mathematica runs (which in my case was :0.0). This is what I did first after logging into the machine using ssh, before doing any of the commands below.

The next step was recognizing that when the screen is locked, the Gnome screensaver grabs the keyboard events, so any keypresses sent to the Mathematica window ended up at the screensaver instead. Fortunately the Gnome screensaver is easy to unlock from the command line. The command is

gnome-screensaver-command -d

which I found here.

After that, I identified the Mathematica window using the command

xwininfo -root -tree | grep Mathematica

which I found here. The correct Mathematica window was easy to identify because it contained in the title the notebook's file name ("notebook" is the Mathematica name for a specific type of Mathematica document, the one you usually use for calculations) and a star to indicate that it was unsaved. Each line starts with the corresponding window ID (a hex number like 0x13371d)

After having identified the window, I then set focus to it using

xdotool windowactivate 0x13371d

(where the hex number was of course the window ID obtained previously) and finally sent it the Control-S using

xdotool key ctrl+s

Then I used xwininfo again to check that the star indeed disappeared in the window title, indicating that the notebook had really been saved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.