wmi

git clone git://oldgit.suckless.org/wmi/
Log | Files | Refs | LICENSE

wmiremote.cpp (5015B)


      1 // Copyright (c) 2003 - 2009 Anselm R Garbe <anselm@garbe.us>
      2 // See LICENSE for license details.
      3 
      4 extern "C" {
      5 #include <stdlib.h>
      6 #include <unistd.h> // getopt stuff
      7 #include <X11/Xatom.h>
      8 #include <X11/Xlib.h>
      9 #include <X11/Xutil.h>
     10 }
     11 
     12 #include <iostream>
     13 #include <string>
     14 
     15 using namespace std;
     16 
     17 static void version() {
     18     cout << "wmiremote - window manager improved - " << __WMI_VERSION
     19          << endl << " (C)opyright 2003 - 2004 by Anselm R. Garbe"
     20          << endl;
     21     exit(0);
     22 }
     23 
     24 static void usage() {
     25     cout << "usage: " << endl << endl
     26          << " wmiremote [-d <display>] [-t <text>] [-m <percentage>#<text>,...]"
     27          << endl
     28          << "           [-a <action>[+arg1[+...]]] [-p] [-v]"
     29          << endl << endl
     30          << "  -a invokes an WMI action remotely" << endl
     31          << "  -d specifies the display to use" << endl
     32          << "  -m sets meters info (see manual page for details)" << endl
     33          << "  -p prints current key bindings of running WMI" << endl
     34          << "  -t displays bartext in the wmi" << endl
     35          << "  -v show version info" << endl << endl;
     36     exit(1);
     37 }
     38 
     39 int main(int argc, char *argv[]) {
     40 
     41     string *displayName = 0;
     42     string *msgText = 0;
     43     string *actionCmd = 0;
     44     string *meterText = 0;
     45     bool prettyPrint = false;
     46 
     47     int c;
     48 
     49     // command line args
     50     while ((c = getopt(argc, argv, "a:d:t:m:pv")) != -1) {
     51         switch (c) {
     52         case 'a':
     53             actionCmd = new string(optarg);
     54             break;
     55         case 'd':
     56             displayName = new string(optarg);
     57             break;
     58         case 't':
     59             msgText = new string(optarg);
     60             break;
     61         case 'm':
     62             meterText = new string(optarg);
     63             break;
     64         case 'p':
     65             prettyPrint = true;
     66             break;
     67         case '?':
     68         case 'h':
     69             usage();
     70             break;
     71         case 'v':
     72             version();
     73             break;
     74         }
     75     }
     76 
     77     if (!msgText && !actionCmd && !prettyPrint && !meterText) {
     78         usage();
     79         exit(1);
     80     }
     81 
     82     Display *display = XOpenDisplay(displayName ? displayName->c_str() : 0);
     83 
     84     if (display == 0) {
     85         if (displayName) {
     86             cerr << "wmiremote: error, cannot open display '" <<
     87                 displayName << "'" << endl;
     88         }
     89         else {
     90             cerr << "wmiremote: error, cannot open display '0'" << endl;
     91         }
     92         exit (1);
     93     }
     94 
     95 
     96     XTextProperty property;
     97 
     98     if (msgText) {
     99         Atom WMI_STATUSTEXT = XInternAtom(display, "_WMI_STATUSTEXT", False);
    100         char *text = (char *)msgText->c_str();
    101         XStringListToTextProperty(&text, 1, &property);
    102         XSetTextProperty(display, DefaultRootWindow(display), &property, WMI_STATUSTEXT);
    103     }
    104 
    105     if (meterText) {
    106         Atom WMI_METERTEXT = XInternAtom(display, "_WMI_METERTEXT", False);
    107         char *text = (char *)meterText->c_str();
    108         XStringListToTextProperty(&text, 1, &property);
    109         XSetTextProperty(display, DefaultRootWindow(display), &property, WMI_METERTEXT);
    110     }
    111 
    112     if (actionCmd) {
    113         Atom WMI_ACTIONCMD = XInternAtom(display, "_WMI_ACTIONCMD", False);
    114         char *action = (char *)actionCmd->c_str();
    115         XStringListToTextProperty(&action, 1, &property);
    116         XSetTextProperty(display, DefaultRootWindow(display), &property, WMI_ACTIONCMD);
    117     }
    118 
    119     if (prettyPrint) {
    120         Atom WMI_PRETTYPRINT_REQUEST = XInternAtom(display,
    121                 "_WMI_PRETTYPRINT_REQUEST", False);
    122         Atom WMI_PRETTYPRINT_RESPONSE = XInternAtom(display,
    123                 "_WMI_PRETTYPRINT_RESPONSE", False);
    124         string value = "";
    125         char *val = (char *)value.c_str();
    126         XStringListToTextProperty(&val, 1, &property);
    127         XSetTextProperty(display, DefaultRootWindow(display), &property,
    128                          WMI_PRETTYPRINT_REQUEST);
    129         // init's response value
    130         XSetTextProperty(display, DefaultRootWindow(display), &property,
    131                          WMI_PRETTYPRINT_RESPONSE);
    132         bool success;
    133         while ((success = XGetTextProperty(display, DefaultRootWindow(display),
    134                              &property, WMI_PRETTYPRINT_RESPONSE)))
    135         {
    136             if (property.nitems > 0) {
    137                 value = (const char *)property.value;
    138                 if (value != "") {
    139                     cout << value;
    140                     // resets value back to empty
    141                     value = "";
    142                     val = (char *)value.c_str();
    143                     XStringListToTextProperty(&val, 1, &property);
    144                     XSetTextProperty(display, DefaultRootWindow(display), &property,
    145                             WMI_PRETTYPRINT_RESPONSE);
    146                     break;
    147                 }
    148             }
    149             usleep(500000); // 0.5 seconds
    150         }
    151         if (!success) {
    152             cerr << "wmiremote: error, it seems that WMI is not running"
    153                  << endl;
    154             exit(1);
    155         }
    156     }
    157     XSync(display, False);
    158     return 0;
    159 }