How to manipulate a window in Linux - linux

I've been programming in Linux for years but don't have too much knowledge in the perspective of display. Now I'm asked to write a separate process that will monitor which window (probably not the window created by my process) is focused by the user and do some simple manipulations such as move and resize.
Is it possible? Is there a general way to achieve that? Can someone give any hint I can look into? Thanks!

Use xdotool. It can emulate keystrokes, mouse-actions, find active windows etc.
Example: Run this command in your (non maximized) terminal window and it will jump 10 pixels down and to the right:
xdotool windowmove --relative $(xdotool getwindowfocus) 10 10
It should be available in most distributions, ie sudo apt-get install xdotool or similar for other package managers.

Essentially its the XWindow system that does it all. However you can control a lot of it using simple utilities. For example if I wan to change the title of a VLC window, I can do it like this:
# this will ask me to point to a window and I will point to VLC player
$ xwininfo -all | grep 'xwininfo: Window id:'
xwininfo: Window id: 0x2000011 (has no name)
Here 0x2000011 is the window id of VLC Player running on my system. We can use this window to get or set properties. To do that we can use xprop utility:
for p in _NET_WM_VISIBLE_NAME _NET_WM_NAME; \
do xprop -id 0x2000011 -format $p 8s -set $p "MyVLC"; done
This will change the window title of VLC Player to MyVLC.
Although this is what xdotool also does behind the scenes, these commands are fairly low level, and are very likely be installed by default.
References:
https://github.com/ChickenProp/set-window-title/blob/master/set-window-title

Related

how to bring application to front with xdg-open

On linux, xdg-open can launch an application, but in most cases the desired behavior is to only launch the application if it's not already running, and to bring it to the front if it is already running.
Can xdg-open do this? Is there some other standard way to provide this functionality?
As you can see xdg-open is nothing more then the wrapper which handles the file type and opens it in the preferred application. It's up to the application if it does or doesn't support "bring to front" option.
$ file /usr/bin/xdg-open
/usr/bin/xdg-open: POSIX shell script, ASCII text executable
In order to bring to front the application window you might use other tools like wmctrl, xdotool or qdbus.
Details:
NAME
wmctrl - interact with a EWMH/NetWM compatible X Window Manager.
SYNOPSIS
wmctrl [ options | actions ]...
DESCRIPTION
wmctrl is a command that can be used to interact with an X Window man‐
ager that is compatible with the EWMH/NetWM specification. wmctrl can
query the window manager for information, and it can request that cer‐
tain window management actions be taken.
[...]
-a <WIN>
Switch to the desktop containing the window <WIN>, raise the
window, and give it focus.
NAME
xdotool - command-line X11 automation tool
SYNOPSIS
xdotool cmd args...
Notation: Some documentation uses [window] to denote an optional window
argument. This case means that the argument, if not present, will
default to "%1". See "WINDOW STACK" for what "%1" means.
DESCRIPTION
xdotool lets you programatically (or manually) simulate keyboard input
and mouse activity, move and resize windows, etc. It does this using
X11's XTEST extension and other Xlib functions.
There is some support for Extended Window Manager Hints (aka EWMH or
NetWM). See the "EXTENDED WINDOW MANAGER HINTS" section for more
information.

kde 'window shortcuts' are temporary

I'm running the KDE desktop, and I'd like to associate hotkeys with a set of windows and use those hotkeys to activate those windows from anywhere. Ideally, this would work like in Windows - where the key launches the app the first time you press it, and thereafter just brings it to the front. But I don't think that's possible in KDE (why not???).
Anyway, the kwin window menu has a "More Actions/Window Shortcut" option that does let you set a key combination that will bring that window to the front. Except that it only works for the current session. Is there no way to make that association permanent?
In my normal Windows workspace, I have 2 PuTty sessions logged on to a unix host under different user ids. Each of these has an associated hotkey. In addition, I have the app I work in (a browser of sorts) with its own hotkey, and a programming editor with its own key. I am constantly switching between these 4 windows, and I do it all with keystrokes. And I use the same keystrokes to start those sessions up when I first need them. It's great - the only Windows feature that I seriously miss when running Linux. How can I come closest to having that in KDE? Or some other Linux desktop?
Well, I tried xdotool, and combined in a script and attached to a KDE custom shortcut, it actually works. Launches the app if xdotool doesn't find it, and activates the window if it does. Painful, but it does work. The app in question is WIN32 code - hence launching it via wine.
Here's the script:
#!/bin/bash
pid=`xdotool search --name Medialine`
if [ "$pid" == "" ]; then
wine /home/rob/wem.exe&
else
xdotool windowactivate $pid
fi

Ubuntu Always On Top Over Fullscreen

I am running into a problem setting up some automation on Ubuntu and was hoping for some help. I need to be able to start chrome, set full screen, start another window, set that to always on top, and then set focus back to chrome. This should end up with a small window on top of chrome, but chrome receiving all user inputs.
I've done a lot of digging and the best I can come up with is using wmctrl:
// Set chrome as fullscreen<br/>
wmctrl -r [ChromeWindowTitle] -b add,fullscreen
// Set the app as always on top<br/>
wmctrl -r [AppWindowTitle] -b add,above
// Activate chrome<br/>
xdotool search --name [ChromeWindowTitle] | awk '{ system("xdotool windowactivate --sync " $1) }'
The always on top and full screen work, but when an app is in full screen mode it seems to supersede the always on top stack. I've been able to verify this through a bunch of experiments. Basically what I have been able to verify is:
1) I can force an app full screen
2) I can force an app always on top
3) I cannot do both and have the always on top app on top of the full screen app. The full screen app is always displayed on top.
So the question is, is it possible (and how is it possible) to have a window floating over a full screen window? I'm running a fairly vanilla Ubuntu 14.04 so there shouldn't be any big surprises there.
Thanks in advance.
-Mike

Ubuntu BASH Script Background Job Hide Windows

I have a seemingly simple problem that I can't figure out how to solve.
I have a bash script which launches a program in the background within a loop. However, each time the program opens up it launches a window and focuses on it. Is there a way to launch a process and have all of the windows which it launches be minimized or completely suppressed?
Here is my code:
#!/bin/bash
while true; do
process1 & P=$!; #I need to hide all of the windows in this process
process2;
kill $P;
wait;
sleep 0.1; done
Thank you for your help.
Use xdotool, replace name_in_titlebar with the name from your titlebar :D
xdotool search --name name_in_titlebar windowactivate
xdotool key ctrl+super+Up
This is up to the Window Manager. Try looking up how to prevent focus stealing in your wm documentation.
For a more general approach, you can start a second X server (startx -- :1 and then Ctrl-Alt-F8 to switch to it), run an instance of Xnest to get an X-server-in-a-window where subwindows won't steal focus, or run a vncserver/nxserver that the windows can spam and you can occasionally connect to and look at if you want. With any of these set up on e.g. display :1, you can redirect your process's window with DISPLAY=:1 process1.

unix shell command that make the virtual desktop spin

I'm looking for a way to make my virtual desktop change without using keyboard shortcuts.
I explain: I want to do a java program that will change my current desktop to the desktop at the left for exemple, let's say by clicking on a button. I've done it using keyboard shortcuts sent by the java program, but i want it more universal (i mean, that can work for everyone). Hope I'm clear enough!
Thanks.
This will give you available workspace (virtual desktop) names:
wmctrl -d
then
wmctrl -s <workspace name>
will move to the workspace.
Take a look at here http://tomas.styblo.name/wmctrl/ to see what window managers are supported.
My previous answer was:
xrandr is probably what you want. Take a look at this wikipedia page here too.
I thought xrandr will work but couldn't figure out how. I hope someone will comment and give the definite answer whether xrandr will work or not.

Resources