Get current Virtual Desktop – number/name/tag/id

kdewindow-managerxorg

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).

Best Answer

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.

Related Question