Software/wmctrl

wmctrl

From the wmctrl homepage:

Installing wmctrl

A source tarball is available on the homepage; alternately, wmctrl is available from the RPMforge repository for Red Hat Linux and clones, or as part of the Debian distribution.

Additional documentation

From the Spiral of Hope blog:

wmctrl is one of very few programs which give control over your various windows within X. kde (with a GUI) and openbox (via rc.xml) have a way, and some other applications include Devil’s Pie, and some tools which come with X; xdpyinfo; xprop; xlsclients; xlsatoms; and xwininfo. Please let me know if you find any other interesting tools.

Notice that in this list there are two third-party programs. Two. Of all of these, wmctrl seems to be the only tool which can actually do things to an existing window from the commandline. It was last updated in 2005. Seriously, we all use X daily but the tools are .. I don’t even have the words; welcome to the world of Linux.

I’m not even going to go into the rant of users needing creating the documentation for the software they use. I’ve already covered that in my previous post.

1. Introduction 2. Show information about the window manager and about the environment. 3. List windows managed by the window manager. 4. List desktops. 5. Switch to the specified desktop. 6. Activate a window. 7. Close the window gracefully. 8. Move the window to the current desktop and activate it. 9. Move the window to the specified desktop. 10. Resize and move the window around the desktop. 11. Change the state of the window. 12. Set the long title of the window. 13. Set the short title of the window. 14. Set both the long and short title of the window. 15. Activate or deactivate the window manager’s “showing the desktop” mode. 16. Change the viewport for the current desktop. 17. Change the number of desktops. 18. Change the geometry of all desktops. 19. Print help. 20. Options and misc.

21. real-world examples

Introduction

Sometime in 2007 I did a lot of work to figure wmctrl out. I essentially cleaned up its man page and created examples. I also came up with quite a number of questions along the way. Just the other day I was prompted to find those old notes and post them. I intended to get wmctrl and re-test things, but I have issues compiling it, probably because of my 64bit awesomeness and newer x libraries. .. and that damned type declaration. I don’t feel like hacking around in the wmctrl source code (right now?) or getting help for it, but I’ll get those notes for you anyway. The notes ought to still be good since the “latest” wmctrl version has not changed. However, the transition from one format to another might mean there are odd mistakes (missing spaces, incorrect characters) so please let me know if you have any issues or you’d like an update. This is probably the best piece of documentation on wmctrl and I demand perfection out of my docs. I’m the kind of guy who will put two spaces after a period in HTML (where the second is discarded).

There are two reasons I was hesitant to just copy-and-paste my notes. One is that I intended to put them up on my CMS when it got built. The other is that I really wanted to re-test everything and provide even better examples. I’ve learned in the few years that I’ve played with wmctrl, and I’m positive I could build some cool scripts to really help people out. But until things come together, I suppose this’ll get trapped in a blog post.

Blogs are SO inappropriate for resources like this which ought to be kept up-to-date. Wikis ftw.

Show information about the window manager and about the environment.

List windows managed by the window manager.

The columns:

1. The window ID. This is used for the -i switch. 2. The desktop ID. It begins counting at 0. -1 means that window is on all desktops. Used with -d and more. 3. The client machine 4. The name (long title) of the window. Used with -r mostly, and can be renamed with -N and -T.

List desktops.

List desktops. The current desktop is marked with an asterisk.

The columns

1. the desktop number. It begins counting at 0. 2. ‘*’ means it’s the current desktop 3. geometry 4. viewport 5. workarea 6. TODO: title — the man page describes this, but it’s missing from the command. File a bug report.

Switch to the specified desktop.

notes

* The programmer starts counting at 0. So 1 means the second desktop. * Your window manager must be configured to provide multiple desktops for this to mean anything.

Activate a window.

Activate the window by switching to its desktop and raising it.

notes

* TODO: With a tray-enabled application like KTorrent, activating it while it’s minimized as a tray icon is a Bad Idea and won’t display it properly. You’d have to ‘restore’ it from its tray state – by right-clicking its tray icon and selecting ‘restore’. Explore a wmctrl-only example.

Close the window gracefully.

notes

* TODO: With a tray-enabled application like KTorrent, closing it will minimize it to the tray. If it’s already in the tray, then -c won’t do anything. wmctrl doesn’t have an alternate close or a ‘kill’ command for this case. Request this feature. * Does not work with mousepad.

Move the window to the current desktop and activate it.

notes

* With a tray-enabled application like KTorrent, activating it while it’s minimized as a tray icon is a Bad Idea and won’t display it properly. You’d have to ‘restore’ it from its tray state – by right-clicking its tray icon and selecting ‘restore’.

Move the window to the specified desktop.

notes

* Tray-enabled applications like KTorrent won’t go anywhere if they’re minimized to the tray.

Resize and move the window around the desktop.

This example will move the window to the top-left of the screen (0,0) and resize it to 800×600 pixels. The five options are: “gravity,X,Y,width,height”:

1. gravity — I’m not sure what this

2. X — The X coordinates. How far from the left the window will begin. Use -1 to leave this value as-is. 3. Y — The Y coordinates. How far from the top the window will begin. Use -1 to leave this value as-is. 4. width — The new window width in pixels Use -1 to leave this value as-is. 5. height — The new window hight in pixels. Use -1 to leave this value as-is.

notes

* “gravity,X,Y,width,height” follows the EWMH specification * width and height are NOT like using the mouse to resize a window. With KTorrent, some of the elements are ‘chopped off’ because of the resize. A slight adjustment using the mouse corrects this. * Tray-enabled applications like KTorrent won’t go anywhere or do anything if they’re minimized to the tray.

The EWMH specification describe ‘gravity’ thusly:

But wmctrl doesn’t seem to respect these. It expects an integer.

Change the state of the window.

Change the state of the window. Using this option it’s possible for example to make the window maximized, minimized or fullscreen.

The EWMH specification defines a _NET_WM_STATE request. wmctrl supports these properties:

* modal — doesn’t seem to do anything, but I don’t really understand it anyway. * sticky — doesn’t work * maximized_vert — works * maximized_horz — works * shaded — works * skip_taskbar — works, but I can’t add it back. =/ * skip_pager — works * hidden — does not work, and really shouldn’t since the ‘minimize’ action would be responsible for setting this. Except wmctrl doesn’t have that. =/ * fullscreen — works. Removes the window title and border and forces fullscreen.

* above — works * below — works * TODO: demands_attention — this is defined in the EWMH specs but was not listed in the wmctrl manual and does not appear to be supported. Request it.

Examples:

Multiple commands at once are done like this:

Set the long title of the window.

Set the name (long title) of the window.

notes

* Changing it back doesn’t seem to work. * Warning: This influences the use of the -r switch! * This is great for testing on an xterm shell if your environment automatically changes the title for you. Then you can just change directories to get things restored nicely. * For KTorrent after the first time I use this wmctrl feature I have to minimize it to the tray and restore it to view the change.

Their example shows ‘zenity’ but I prefer kdialog. In a terminal emulator window, do:

complex stuff

It would be nice if I could have regular expressions which could match a title. I can partially understand how it could be done, but I don’t want to bother implementing the feature myself. =/ Some nonsense like this:

Where the othergrep thing matches the 10 characters at the beginning of each line. I don’t know how to do that though. Something like (……….) or (.{10}) I think.

Set the short title of the window.

Set the icon name (short title) of the window.

notes

* See -N for more info. * I’m not sure what this is supposed to do.

Set both the long and short title of the window.

notes

* See -N for more info. * I can’t change the title back. This is the same problem as -N.

Activate or deactivate the window manager’s “showing the desktop” mode.

Activate or deactivate window manager’s “showing the desktop” mode. Many window managers do not implement this mode.

notes

* doesn’t work for me.

Change the viewport for the current desktop.

Change the viewport for the current desktop. The X and Y values are separated with a comma. They define the top left corner of the viewport. The window manager may ignore the request.

notes

* This is ignored.

Change the number of desktops.

Change number of desktops. The window manager may ignore the request.

Learn how many desktops you have:

Now check again:

notes

* This programmer begins counting at 0. So ‘1′ means ‘2 desktops’ * Blackbox 0.70.1 and bbkeys 0.9.0 won’t really honour this. It’ll set it so that -d displays the ‘correct’ number of desktops, but my hotkeys to change desktops will still work. So it looks like bbkeys doesn’t respect wmctrl’s setting or Blackbox isn’t honouring wmctrl’s setting.

Change the geometry of all desktops.

Change geometry (common size) of all desktops. The window manager may ignore the request.

notes

* Blackbox ignores this request.

Print help.

(basically prints the man page)

Options and misc.

Interpret <WIN> as a numerical window ID.

The -i option may be used to interpret the argument as a numerical window ID

notes

* This is an alternate to using -r. * Defaults to use a decimal number. * If it starts with “0x”, then it’s assumed to be a hexadecimal number. * TODO: <WIN> as a numerical window ID doesn’t seem to work consistently. Perhaps not all options support it or something crazy is going on. It used to work very well for me. Re-test.

Include PIDs in the window list.

Include PIDs in the window list. Very few X applications support this feature.

notes

* blackbox supports this. =)

Include geometry in the window list.

Include WM_CLASS in the window list or interpret <WIN> as the WM_CLASS name.

notes

* TODO: describe interpreting <WIN> as the WM_CLASS name. Useful for: -a, -c, -R and -r.

Override auto-detection and force UTF-8 mode.

Match the full window title and be case-sensitive

Modifies the behavior of the window title matching algorithm. It will match only the full window title instead of a substring, when this option is used. Furthermore it makes the matching case sensitive.

Be verbose.

Be verbose. Useful for debugging.

Use a workaround.

Use a workaround. The option may appear multiple times

list of options as of 1.07

* DESKTOP_TITLES_INVALID_UTF8 — Print non-ASCII desktop titles correctly when using Window Maker.

-r

wmctrl -r is used to specify the window name (long title) to act on. It is used for a number of features:

* Move the window to the specified desktop. * Resize and move the window around the desktop. * Change the state of the window. * Set the long title of the window. * Set the short title of the window. * Set both the long and short title of the window.

notes;

* It’s interpreted as a string. * It’s matched against the window name (long title) * Only the first matching window is used. * The matching isn’t case sensitive * The string may appear in any position of the title. * If you don’t want to use the name of the window, then try the -i switch.

acting on a mouse-selected window

Select the window by clicking on it.

acting on the current window

Use the currently active window for the action.

real-world examples

force window dimensions when launching an app

A bbkeys hotkey can be set up to use xtoolwait and wmctrl to force a specific size. If a second occurrence is launched, I can relocate that one and resize it.

It’s not hard to have this as a simple script like this:

I’d bet that anyone decent with bash could whip up something that’s nice and reusable too. (TODO)

Also, I’m going to post a user addition here. I’m unable to do any real testing on it without wmctrl working.

I also just bumped into some patches for wmctrl. I haven’t read any further and haven’t tested them out.

* Removing decorations in Metacity * Move and Hide

Fullscreen issue

Proof that wmctrl usually works

(Blackbox 0.70.1)

1. Start xine windowed

2. alt-tab back to your term

3. Prove that wmctrl can communicate to xine and fullscreen works:

(fullscreen now)

4. alt-tab back to your term (you might need to do it twice)

5. Prove that wmctrl can remove fullscreen:

(windowed now)

6. Another proof:

(fullscreen now)

(windowed now)

Proof of an issue when the app starts fullscreen

1. Start xine fullscreen

2. Now alt-tab back out to your term. You might have to click on the xine window for alt-tab to work.

3. try to remove fullscreen

This does not do anything.

This does not do anything.