src/ncwmremote.cpp

Go to the documentation of this file.
00001 // Copyright (c) 2003 - 2004 Anselm R. Garbe <anselmg at t-online.de>
00002 // See ../LICENSE.txt for license details.
00003 //
00004 // $Id: wmiremote.cpp 2 2007-05-16 10:38:27Z eg $
00005 
00006 #include "../version.h"
00007 
00008 extern "C" {
00009 #include <unistd.h> // getopt stuff
00010 #include <X11/Xatom.h>
00011 #include <X11/Xlib.h>
00012 #include <X11/Xutil.h>
00013 }
00014 
00015 #include <iostream>
00016 #include <string>
00017 
00018 using namespace std;
00019 
00020 static void version() {
00021     cout << "ncwmremote - no cheese window manager - " << __NCWM_VERSION
00022          << endl << " (C)opyright 2003 - 2004 by Anselm R. Garbe"
00023          << endl;
00024     exit(0);
00025 }
00026 
00027 static void usage() {
00028     cout << "usage: " << endl << endl
00029          << " ncwmremote [-d <display>] [-t <text>] [-m <percentage>#<text>,...]"
00030          << endl
00031          << "           [-a <action>[+arg1[+...]]] [-p] [-v]"
00032          << endl << endl
00033          << "  -a invokes an NCWM action remotely" << endl
00034          << "  -d specifies the display to use" << endl
00035          << "  -m sets meters info (see manual page for details)" << endl
00036          << "  -p prints current key bindings of running NCWM" << endl
00037          << "  -t displays bartext in the ncwm" << endl
00038          << "  -v show version info" << endl << endl;
00039     exit(1);
00040 }
00041 
00042 int main(int argc, char *argv[]) {
00043 
00044     string *displayName = 0;
00045     string *msgText = 0;
00046     string *actionCmd = 0;
00047     string *meterText = 0;
00048     bool prettyPrint = false;
00049 
00050     int c;
00051 
00052     // command line args
00053     while ((c = getopt(argc, argv, "a:d:t:m:pv")) != -1) {
00054         switch (c) {
00055         case 'a':
00056             actionCmd = new string(optarg);
00057             break;
00058         case 'd':
00059             displayName = new string(optarg);
00060             break;
00061         case 't':
00062             msgText = new string(optarg);
00063             break;
00064         case 'm':
00065             meterText = new string(optarg);
00066             break;
00067         case 'p':
00068             prettyPrint = true;
00069             break;
00070         case '?':
00071         case 'h':
00072             usage();
00073             break;
00074         case 'v':
00075             version();
00076             break;
00077         }
00078     }
00079 
00080     if (!msgText && !actionCmd && !prettyPrint && !meterText) {
00081         usage();
00082         exit(1);
00083     }
00084 
00085     Display *display = XOpenDisplay(displayName ? displayName->c_str() : 0);
00086 
00087     if (display == 0) {
00088         if (displayName) {
00089             cerr << "ncwmremote: error, cannot open display '" <<
00090                 displayName << "'" << endl;
00091         }
00092         else {
00093             cerr << "ncwmremote: error, cannot open display '0'" << endl;
00094         }
00095         exit (1);
00096     }
00097 
00098 
00099     XTextProperty property;
00100 
00101     if (msgText) {
00102         Atom NCWM_STATUSTEXT = XInternAtom(display, "_NCWM_STATUSTEXT", False);
00103         char *text = (char *)msgText->c_str();
00104         XStringListToTextProperty(&text, 1, &property);
00105         XSetTextProperty(display, DefaultRootWindow(display), &property, NCWM_STATUSTEXT);
00106     }
00107 
00108     if (meterText) {
00109         Atom NCWM_METERTEXT = XInternAtom(display, "_NCWM_METERTEXT", False);
00110         char *text = (char *)meterText->c_str();
00111         XStringListToTextProperty(&text, 1, &property);
00112         XSetTextProperty(display, DefaultRootWindow(display), &property, NCWM_METERTEXT);
00113     }
00114 
00115     if (actionCmd) {
00116         Atom NCWM_ACTIONCMD = XInternAtom(display, "_NCWM_ACTIONCMD", False);
00117         char *action = (char *)actionCmd->c_str();
00118         XStringListToTextProperty(&action, 1, &property);
00119         XSetTextProperty(display, DefaultRootWindow(display), &property, NCWM_ACTIONCMD);
00120     }
00121 
00122     if (prettyPrint) {
00123         Atom NCWM_PRETTYPRINT_REQUEST = XInternAtom(display,
00124                 "_NCWM_PRETTYPRINT_REQUEST", False);
00125         Atom NCWM_PRETTYPRINT_RESPONSE = XInternAtom(display,
00126                 "_NCWM_PRETTYPRINT_RESPONSE", False);
00127         string value = "";
00128         char *val = (char *)value.c_str();
00129         XStringListToTextProperty(&val, 1, &property);
00130         XSetTextProperty(display, DefaultRootWindow(display), &property,
00131                          NCWM_PRETTYPRINT_REQUEST);
00132         // init's response value
00133         XSetTextProperty(display, DefaultRootWindow(display), &property,
00134                          NCWM_PRETTYPRINT_RESPONSE);
00135         bool success;
00136         while ((success = XGetTextProperty(display, DefaultRootWindow(display),
00137                              &property, NCWM_PRETTYPRINT_RESPONSE)))
00138         {
00139             if (property.nitems > 0) {
00140                 value = (const char *)property.value;
00141                 if (value != "") {
00142                     cout << value;
00143                     // resets value back to empty
00144                     value = "";
00145                     val = (char *)value.c_str();
00146                     XStringListToTextProperty(&val, 1, &property);
00147                     XSetTextProperty(display, DefaultRootWindow(display), &property,
00148                             NCWM_PRETTYPRINT_RESPONSE);
00149                     break;
00150                 }
00151             }
00152             usleep(500000); // 0.5 seconds
00153         }
00154         if (!success) {
00155             cerr << "ncwmremote: error, it seems that NCWM is not running"
00156                  << endl;
00157             exit(1);
00158         }
00159     }
00160     XSync(display, False);
00161     return 0;
00162 }

Generated on Thu May 24 15:19:32 2007 for ncwm by  doxygen 1.5.1