3

I Often run a program to open a file from terminal, and I just want to take a quick look at the file in the program without clicking the program's window, and then close the program by Ctrl-F4 which requires the program's window is in the front i.e. in the focus by default.

  • If the program is native Linux's one, the program's window is always in front i.e. in focus by default, so I can just close the program by Ctrl-F4.

  • If the program is a Windows' one running under Wine, the program's window is not in the front, i.e. not in focus by default. The program which is in the front is the terminal from which the program is started. I often forget that, and accidentally close the terminal instead of the program by Ctrl-F4. Closing the terminal will close the program as well, but does more than what I want to.

So my question is: how can I make a Windows' program to run in the front by default?

Note:

  1. You can test the above by opening a pdf file with Windows' program PDFXCview.exe under Wine 2.0.1, and with native linux's program evince. I am running them under Ubuntu 16.04.

  2. I run the Windows' program to open a file under wine in terminal by

    wine /home/tim/path/to/PDFXCview.exe myfile.pdf
    

    and the program's window is not in the front by default.

    Strangely, if I run the program without opening any file by

    wine /home/tim/path/to/PDFXCview.exe 
    

    the window of the program will be in the front by default.

  3. One thing that might or might not be related is that I have set up to use PDFXCview.exe to open pdf files from file manager PcManFM in LXDE, by adding to ~/.local/share/applications/mimeinfo.cache:

    application/pdf=wine-extension-pdf.desktop;userapp-PDFXCview.exe-NHNW1Y.desktop;
    

    where the content of the file ~/.local/share/applications/userapp-PDFXCview.exe-NHNW1Y.desktop is

    [Desktop Entry]
    Type=Application
    Name=PDFXCview.exe
    Exec=wine /home/tim/path/to/PDFXCview.exe %U
    Categories=Other;
    NoDisplay=true
    MimeType=application/pdf
    Terminal=false
    

    It might not be related because it is a setup for running programs from desktop environment, while my question is about running programs from terminal.

    When I open a pdf file using PDFXCview.exe under wine from PcManFM, by double clicking the file in PcManFM, the window of PDFXCview.exe is not in the front either. See https://askubuntu.com/q/925540/1471

Thanks.

2 Answers 2

5

First note that foreground-window and window-with-keyboard-focus are not the same thing. They are for Mircosoft's Windows, and for a lot of window managers, but not necessarily. For example KDE can manage these two properties separately.

Now about your question, like the point I made above, it will depend on the window manager. In Kde there are setting that allow you to control keyboard focus, even settings that depend on the application (but still part of the window manager). enter image description here The above image shows how to do the opposite (set accept focus to yes to do what you are asking for). I tested this configured for one application.


Do not mix up the role of window-manager, launcher etc. Both KDE, Gnome, LXDE and others do all of these. The window manager manages which windows are where, there size, whether they are minimise, which one are behind which, which has keyboard focus. The application can also control this, and so can another application, but ultimately it is the process that is registered as the window manager, that is in charge. It can block other processes from doing these things, and decides where a window is mapped, and whether it gets keyboard focus, when first mapped (or ever). The only visible part of the window manager is the title bar(+close, maximise, minimise etc icons) of all of the other windows.

5
  • Thanks. (1) I am using LXDE, and LXDE's default window manager is OpenBox. Does OpenBox work similarly? (2) My question here is about starting a Windows program from terminal, instead of from LXDE (which is my question in askubuntu site linked at the end). Is my question related to my window manager?
    – Tim
    Jul 16, 2017 at 15:53
  • I have no idea about LXDE. But what I am talking about has nothing to do with how the program is started. It is not the launcher that does the work, but the window manager. Jul 16, 2017 at 16:00
  • Do you mean that the window manager determines how a program is started from a terminal?
    – Tim
    Jul 16, 2017 at 16:33
  • @Tim, no. It has nothing to do with how it is started. Also do not mix up the role of window-manager, launcher etc. Both KDE, Gnome, LXDE and others do all of these. The window manager manages which windows are where, there size, weather they are minimise, which one are behind which, which has keyboard focus. The application can also control this, and so can another application, but ultimately it is the process that is registered as the window manager, that is in charge. It can block other processes from doing these things, and decides where a window is mapped, and weather it gets kb focus. Jul 16, 2017 at 19:22
  • I see. LXDE's default window manager is OpenBox. If there is a solution to my post, should the solution be provided by OpenBox or LXDE?
    – Tim
    Jul 16, 2017 at 20:03
2

You can give focus to the newly started program using xdotool.

I am using the following short script to open PDFs which makes sure that the PDF viewer is focused right after opening the file (also works if multiple files are being opened at once):

#!/bin/bash

# Filename expected by wine ("z:" being "/", the linux root directory)
Filename="z:${1//\//\\}"

# Start PDF-XChange Viewer, open the PDF file(s) in it, and continue with the script
WINEPREFIX=~/.wine/pdfxchange/ wine ~/.wine/pdfxchange/drive_c/Program\ Files/Tracker\ Software/PDF\ Viewer/PDFXCview.exe "$Filename" "$@" &

# Wait until the viewer has started so that xdotool can identify the window
while [ -z "$WID" ]; do WID=$(xdotool search --name "PDF-XChange Viewer"); sleep 0.1; done

# Wait until the viewer is really open and ready for input
sleep 0.5

# Activate the PDF-XChange Viewer window
xdotool windowactivate "$WID"

exit 0

Of course you would have to adapt the wine file locations.

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.