linuxvirtual-desktop

How to get virtual desktop name/number on Linux?


I'm developing an icon manager app for openbox, and need to know the specific virtual desktop where an icon is created to restore it to same on restart.

Is there some standard way in which the current virtual desktop's info can be determined?


Solution

  • If your window manager is EWMH compliant, you can use the properties listed here:

    http://standards.freedesktop.org/wm-spec/1.4/ar01s03.html

    in particular, _NET_NUMBER_OF_DESKTOPS and _NET_DESKTOP_NAMES.


    Modified code from this site will list all available virtual desktops. It opens a pipe to wmctrl -d which returns the list with the current virt.desktop indicated with an *:

    #include <stdio.h>
    #include <stdlib.h>
    
    #ifdef WIN32
    FILE *popen ( const char* command, const char* flags) {return _popen(command,flags);}
    int pclose ( FILE* fd) { return _pclose(fd);}
    #endif
    
    int main(int argc, char* argv[])
    {
        char psBuffer[4096];
        FILE *iopipe;
    
        if( (iopipe = popen( "wmctrl -d", "r" )) == NULL )
            exit( 1 );
    
        while( !feof( iopipe ) )
        {
            if( fgets( psBuffer, 4095, iopipe ) != NULL )
                printf( psBuffer );
        }
    
        printf( "\nProcess returned %d\n", pclose( iopipe ) );
        return 0;
    }
    

    The captured output will look something like this: (man wmctrl for explanation)

    0  * DG: 1680x1050  VP: 0,0  WA: 36,36 3564x1044  (Unnamed desktop)
    1  - DG: 1680x1050  VP: 0,0  WA: 36,36 3564x1044  desktop 2
    2  - DG: 1680x1050  VP: 0,0  WA: 36,36 3564x1044  desktop 3
    3  - DG: 1680x1050  VP: 0,0  WA: 36,36 3564x1044  desktop 4
    4  - DG: 1680x1050  VP: 0,0  WA: 36,36 3564x1044  desktop 5
    5  - DG: 1680x1050  VP: 0,0  WA: 36,36 3564x1044  desktop 6