PieDock 1.6.0

src/WindowManager.h

00001 /*
00002  *   O         ,-
00003  *  ° o    . -´  '     ,-
00004  *   °  .´        ` . ´,´
00005  *     ( °   ))     . (
00006  *      `-;_    . -´ `.`.
00007  *          `._'       ´
00008  *
00009  * Copyright (c) 2007-2010 Markus Fisch <mf@markusfisch.de>
00010  *
00011  * Licensed under the MIT license:
00012  * http://www.opensource.org/licenses/mit-license.php
00013  */
00014 #ifndef _PieDock_WindowManager_
00015 #define _PieDock_WindowManager_
00016 
00017 #include <X11/Xlib.h>
00018 #include <X11/Xatom.h>
00019 
00020 #include <string>
00021 #include <vector>
00022 #include <map>
00023 
00024 namespace PieDock
00025 {
00026     // forward declaration
00027     class ArgbSurface;
00028 
00034     class WindowManager
00035     {
00036         public:
00040             class WindowList : public std::vector<Window>
00041             {
00042                 public:
00043                     WindowList( Display *d ) { addClientsOf( d ); }
00044                     virtual ~WindowList() {}
00045                     void addClientsOf( Display * );
00046             };
00047 
00048             virtual ~WindowManager() {}
00049             static void activate( Display *, Window );
00050             static void iconify( Display *, Window );
00051             static void close( Display *, Window );
00052             static Window getActive( Display * );
00053             static Window getClientWindow( Display *, Window );
00054             static std::string getTitle( Display *, Window );
00055             static ArgbSurface *getIcon( Display *, Window );
00056             static unsigned long getWorkspace( Display *, Window );
00057             static unsigned long getNumberOfWorkspaces( Display * );
00058             static unsigned long getCurrentWorkspace( Display * );
00059             static bool getWorkspacePosition( Display *,
00060                 unsigned long &,
00061                 unsigned long & );
00062             static bool getDesktopGeometry( Display *,
00063                 unsigned long &,
00064                 unsigned long & );
00065             static bool isNormalWindow( Display *, Window );
00066             static void setWindowType( Display *, Window, const char * );
00067             static void sendClientMessage( Display *, Window, const char *,
00068                 unsigned long = 0, unsigned long = 0, unsigned long = 0,
00069                 unsigned long = 0, unsigned long = 0 );
00070             static Atom getAtom( Display *, const char * );
00071 
00072         private:
00076             template <class T> class Property
00077             {
00078                 public:
00079                     Property( Display *d, Window w ) :
00080                         display( d ),
00081                         window( w ),
00082                         data( 0 ),
00083                         items( 0 ) {}
00084                     Property( Display *d, Window w, Atom type,
00085                         const char *name ) :
00086                         Property( d, w ) { fetch( type, name ); }
00087                     virtual ~Property() { freeData(); }
00088                     inline T *getData() const { return data; }
00089                     inline unsigned long getItems() const { return items; }
00090                     bool fetch( Atom type, const char *name,
00091                         long length = 1024, long offset = 0,
00092                         Bool remove = False )
00093                     {
00094                         freeData();
00095 
00096                         Atom returnedType;
00097                         int format;
00098                         unsigned long items;
00099                         unsigned long bytesAfter;
00100                         unsigned char *data;
00101 
00102                         if( XGetWindowProperty(
00103                                 display,
00104                                 window,
00105                                 getAtom( display, name ),
00106                                 offset,
00107                                 length,
00108                                 remove,
00109                                 type,
00110                                 &returnedType,
00111                                 &format,
00112                                 &items,
00113                                 &bytesAfter,
00114                                 &data ) != Success )
00115                             return false;
00116 
00117                         if( returnedType != type )
00118                         {
00119                             freeData();
00120                             return false;
00121                         }
00122 
00123                         this->data = reinterpret_cast<T *>( data );
00124                         this->items = items;
00125 
00126                         return true;
00127                     }
00128 
00129                 private:
00130                     Display *display;
00131                     Window window;
00132                     T *data;
00133                     unsigned long items;
00134 
00135                     void freeData()
00136                     {
00137                         if( !data )
00138                             return;
00139 
00140                         XFree( reinterpret_cast<unsigned char *>( data ) );
00141 
00142                         data = 0;
00143                         items = 0;
00144                     }
00145             };
00146 
00147             typedef std::map<std::string, Atom> StringToAtom;
00148             static StringToAtom stringToAtom;
00149 
00150             WindowManager() {}
00151             WindowManager &operator=( const WindowManager & ) { return *this; }
00152     };
00153 }
00154 
00155 #endif