This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Graphical Environments

Take charge of your window manager with WMCTRL and Devil's Pie

By Shashank Sharma on December 21, 2007 (9:00:00 AM)

Share    Print    Comments   

There are literally dozens of window managers that you can use with your favorite desktop environment to get a beautiful and appealing desktop. If you want to fine-tune your window manager, here are two programs that can help you control everything from application window size to pinning an application to all workspaces to fixing a position for your application windows to resizing desktops. One, wmctrl, works with any window managers that adheres to the Extended Window Manager Hints (EWMH), while Devil's Pie is a window-matching utility, which means it can configure application windows based on defined rules.

wmctrl is a command-line utility that can be used to manipulate or control various aspects of application windows and the desktop environment. Fedora and Ubuntu users can use yum or apt-get respectively to install wmctrl on their machines, or you can grab the compressed tarball from the software's Web site. According to the site, wmctrl is known to work with IceWM, Blackbox, Metacity, Openbox, Kwin, Enlightenment, and several other window managers.

There isn't much documentation for wmctrl, but the detailed man page is enough to start you in the right direction. The command wmctrl -m will print out details about the desktop environment and the window manager. You can use the -l switch to get a list of all the active windows (applications) in space-separated columns. The first column is a hexadecimal number, which is unique for each window. The second is the desktop number, or the workspace -- 0 means the first workspace, 1 means the second, and so on. The machine name is listed in the third column, and the last column contains the window title:

wmctrl -l
0x03400175  0 localhost.localdomain XChat: linuxlala @ FreeNode (formerly OpenProjects.net) / #fedora (+tncrRL)
0x0240007e  0 localhost.localdomain Gmail - Inbox (33) - Mozilla Firefox
0x03c00035  0 localhost.localdomain wmctrl_devilspie_article (~/Desktop) - gedit
0x03e0001e  0 localhost.localdomain linuxlala@localhost:~

To manipulate any window, you need to know its title. The command, wmctrl -a gedit will make the Gedit window active. This is most useful when you have several windows open in various workspaces. But what happens if you are simultaneously working with several Gedit windows? You can easily retitle any window with the command wmctrl -r :SELECT: -T "cool feature". The :SELECT: option enables you to select the application window whose title you wish to change by clicking on it -- though not by using the Alt-Tab key combination. When you click on any window, its title immediately changes to the one specified. You can specify the current title of the window instead of using the :SELECT: option. For example, wmctrl -r XChat -T "IRC" would retitle my XChat window to IRC.

You can also resize windows with wmctrl using the -r and the -e switches. The -r switch specifies a window and the -e switch resizes a window already specified. wmctrl -r gedit -e 1,20,30,750,1200 would resize the Gedit application window. For this to work, the application window must not be in a maximized state. The -e swtich expects five values: gravity, X-position, Y-position, width, and height. The X and Y positions respectively describe the position of the window from the left and top of the screen. Gravity is used to specify the position of the window's frame and it can be any value from 1 to 9. Each value maps to a position on the screen. The values it can take are northwest (1), north (2), northeast (3), west (4), center (5), east (6), southwest (7), south (8), and southeast (9).

Working the Devil

Devil's Pie can perform all of the same functions as wmctrl, but it's actually a more powerful tool for manipulating windows. Being a window matching utility, Devil's Pie works on a set of defined rules. As new application windows are created, Devil's Pie looks through the defined rules to see if any are defined for the window being created and then, based on the defined rules, it models the window.

Devil's Pie was created to incorporate window matching into Metacity, the default window manager on the GNOME desktop environment. The rules for each window are defined as a set of S-Expressions. To install Devil's Pie, simply grab the compressed tarball from its Web site, or use yum or apt-get depending on the distribution running on your machine. Rules for each window are defined in individual files with a .ds extension and stored in the ~/.devilspie directory.

The first file you should create is the debug.ds file. Open a text editor and type debug. Save this file as ~/.devilspie/debug.ds. When you now run Devil's Pie, it will display on the screen the application name, window name, window class, and various other properties of all the open windows. You should always refer to your applications with the correct names to make Devil's Pie perform correctly.

Let's create a rule that forces the Gedit text editor to always open in the second workspace:

(if (is (application_name) "gedit") (begin (set_workspace 2) ) )

Save this as gedit.ds in ~/.devilspie. Open a terminal, type devilspie, and press Enter. Now when you launch Gedit, it will automatically shift to the second workspace no matter what workspace you launch it from.

You can use regular expressions when writing rules. For example, if you have two active instances of gnome-terminal, you can use regular expressions to write a rule for each:

(if (matches (application_name) "linuxlala") (begin (set_workspace 3) ) )

The above rule applies to the default gnome-terminal. With the matches keyword, you can make Devil's Pie look for partial matches. Suppose you have another gnome-terminal window within which you've browsed to the /etc directory. To refer to this window, you can use (matches (application_name) "etc". If you wish to make use of regular expressions, you must also use the matches keyword in place of is.

You can similarly write rules to make your applications open as maximized or in a certain position of the screen. You can also use pin to make sure the application is available on all workspaces. This can be helpful for IM or chat programs that you might want in all workspaces so as to not miss an important message.

You can even perform multiple actions on an application by adding them within the begin structure.

(if (matches (application_name) "linuxlala") (begin (set_workspace 3) (maximize) (undecorate) ) )

To add Devil's Pie to your startup programs list, click System -> Preferences -> Personal -> Sessions. Click New and type Devil's Pie in the Name field and /usr/bin/devilspie in the Command field. Now every time you log into your machine, Devil's Pie will run automatically and manipulate the applications as described in the rules.

wmctrl and Devil's Pie put you in charge of your windowed applications by giving you complete control over all their aspects.

Shashank Sharma specializes in writing about free and open source software for new users and moderates the Linux.com forum boards. He is the coauthor of Beginning Fedora, published by Apress.

Share    Print    Comments   

Comments

on Take charge of your window manager with WMCTRL and Devil's Pie

Note: Comments are owned by the poster. We are not responsible for their content.

Typo

Posted by: Anonymous [ip: 91.84.213.104] on December 21, 2007 10:03 AM
"Open a text editor and type debug" should be "Open a text editor and type (debug)"

#

wmctrl in action and X11::GUITest

Posted by: suso_banderas on December 21, 2007 12:41 PM
I work at a large enterprise and helped setup a four 37 inch flat panel displays for systems monitoring and environment information. I decided to use wmctrl to switch desktops back and forth so it was like a slideshow. Its a great way to use multiple desktops as "frames". Unfortunately, wmctrl doesn't work with Beryl.

Another thing you can use for automation is the perl module X11::GUITest. You can control the mouse and stuff. I used it once to automate making a large mosaic map from several Mapquest and Google satellite images. But I can also use it to do stuff like automatically scroll down a web page after I switch to a desktop. So if you need to control the mouse and keyboard, X11::GUITest is something to look into.

#

Take charge of your window manager with WMCTRL and Devil's Pie

Posted by: Anonymous [ip: 89.136.186.184] on December 21, 2007 04:35 PM
I use wmctrl do a lot of interesting stuff. Such as maximizing windows that change their size and title/class/name while they run (such as QEMU) and so Devil's Pie cannot deal with them. So I run scripts that employ wmctrl to check for these changes and maximize.

The biggest limitation in wmctrl is that it was left incomplete and cannot perform all the hints that the full spec allows for. And for both wmctrl and Devil's Pie is true that they left out clearing certain flags -- you can set the "skip the pager" flag, but you can't clear it!

For keyboard and mouse control I also recommend xmacro and Xnee, as well as irexec if you have a remote. Together with xmodmap you can do some pretty nifty stuff.

#

Take charge of your window manager with WMCTRL and Devil's Pie

Posted by: Anonymous [ip: 68.192.12.60] on December 21, 2007 05:05 PM
Devilspie desperately needs a GUI - not just for ease of use by end-users, but also as a test case for the developers. Once they try to make a GUI, the missing functionality (as mentioned above) would become obvious.

#

Python anyone?

Posted by: Anonymous [ip: 76.195.2.194] on December 23, 2007 08:05 PM
What it needs is a python plugin system. This would mean that people could write plugins, gui plugins even and control devils pie... maybe a module could looks like this....

import dp

p = dp.pie()
p.application_name = "whatever"
p.set_workspace=3
p.undecorate=True
p.tell_daemon()

# end of script

This would not in itself would make it easier for end users but guis could be written for all devils pie functions. This also means that you don't have to maintain your own interpreter, config file format etc and can rely on a fairly simple existing language.

Just a thought.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya