How to get number and/or name (or tag/id... etc) of currently used virtual desktop ?

Command line, scripting, C/C++ solutions allowed (, while portable to C/C++ are preferred). ;)

I'm mostly interested in KDE and GNOME, as well as dwm, xmonad, awesome - solutions for other desktop environments are welcome (I would love to support as much as I can, in order to provide as universal solution as possible).

Btw. For those interested, To be more specific about usecase: I'm interested in timetracking oriented around desktop + windows focus + xevents + multiplescreen. Currently there exist separate solutions for this (ktimetracker tracks tasks depending on desktops, while it's app tracking is very poor - no regular expressions support, just exact window titles -> not very usefull. On the other hand, workrave tracks keyboard and mouse activity, but is unaware of desktop->task I work on.), I'd like to do something, that merges those information in order to achieve more precise picture (maybe incorporating Association rule learning or other Data mining technique) of activities I have (in order to achieve more precise information related with my habits, in order with "ant-RSI fight" ;) ).

So if your desktop follows in different paradigm, than "Virtual Desktop + Windows (with Names and Titles)" please, show code/cli snippet, that shows information about currently used window/frame/app + currently selected desktop/task/tags (eventually screen).

  • 1
    A general solution to this is not going to be possible. For example the Desktop Environment I use doesn't use VD's at all, it uses tags that can be mixed and matched at any given moment. There are ways that will span a few DE's, but don't expect full coverage. – Caleb Oct 29 '11 at 19:38
  • Thanks @Celeb , I hope you don't mind, I've changed a little bit problem statement, according to your comment. Thanks for pointing me - I hope - on right way. :) – Grzegorz Wierzowiecki Oct 29 '11 at 21:13
  • 2
    Of that list, I know awesome is going to play a different game. That's why I use and is the one that specifically handles VD's using an entirely different paradigm. I think xmonad and dwm are much like it. They are known as dynamic or framework managers and don't have the kind of desktop paradigm that Gnome/KDE based WM's typically do. You can probably come up with something that works across stock Gnome/KDE/XFCE ... however I would seriously question whether you are on the right track at all. It sounds like you are trying to work this problem backwards. – Caleb Oct 29 '11 at 22:10
  • 1
    As far as your program is concerned, Window Managers should be left to their job however they want their job to be done, meddling with the WM from inside a program is not welcome. If you are trying to add functionality / behavior to a WM or write some kind of window controller / monitor, this will need to be done specific to each different WM and use the WM's own API, whatever that is. – Caleb Oct 29 '11 at 22:12
  • 1
    A similar project that might be interesting to you is arbtt: "arbtt is a background daemon that stores which windows are open, which one has the focus and how long since your last action (and possibly more sources later), and stores this. It is also a program that will, based on expressive rules you specify, derive what you were doing, and what for." – sr_ Oct 30 '11 at 8:09

Window managers that adhere to the FreeDesktop standards store information about desktops in properties of the root window, in particular:

  • _NET_NUMBER_OF_DESKTOPS: number of virtual desktops
  • _NET_CURRENT_DESKTOP: index of the active desktop
  • _NET_DESKTOP_NAMES: list of names of the virtual desktops

You can access these with any interface to XGetWindowProperty such as xprop. From the shell, wmctrl -d provides a handy way of listing the virtual desktops.

As far as I know, only the window manager is notified when a property of the root window changes. So if you want to react to virtual desktop switching, you'll have to work with the window manager. For example, with your use case, I'd use the Lisp hooks provided by my window manager (Sawfish) when anything interesting happens.

| improve this answer | |
  • thanks for precise stuff, I need a moment to look carefully at this. All things you've mentioned looks relevant, interesting and useful. Please, give me some time before I evaluate answer :). – Grzegorz Wierzowiecki Nov 11 '11 at 23:05

Also have a look at arbtt, the Automatic Rule-Base Time Tracker written mainly by Joachim Breitner in Haskell. See the config example for a peak of what it's able to do, i.e., tracking your work time via focused window titles and time rules. (Maybe more, I have never tried it; no use case here, but it sounds similar to what you try to do.)

It's available via hackage and on Debian's repositories.

| improve this answer | |
  • arbtt looks like very similar project, I'd like to do. Thanks for it. It's always good to compare to similar existing applications, in order to check what other people implement as important :). – Grzegorz Wierzowiecki Nov 14 '11 at 21:02

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.