PieDock  1.6.3
WindowManager.h
1 /*
2  * O ,-
3  * o . - ' ,-
4  * . ` . ,
5  * ( )) . (
6  * `-;_ . - `.`.
7  * `._'
8  *
9  * Copyright (c) 2007-2010 Markus Fisch <mf@markusfisch.de>
10  *
11  * Licensed under the MIT license:
12  * http://www.opensource.org/licenses/mit-license.php
13  */
14 #ifndef _PieDock_WindowManager_
15 #define _PieDock_WindowManager_
16 
17 #include <X11/Xlib.h>
18 #include <X11/Xatom.h>
19 
20 #include <string>
21 #include <vector>
22 #include <map>
23 
24 static int ignoreHandler( Display *, XErrorEvent * ) { return True; }
25 
26 namespace PieDock
27 {
28  // forward declaration
29  class ArgbSurface;
30 
37  {
38  public:
42  class WindowList : public std::vector<Window>
43  {
44  public:
45  WindowList( Display *d ) { addClientsOf( d ); }
46  virtual ~WindowList() {}
47  void addClientsOf( Display * );
48  };
49 
50  virtual ~WindowManager() {}
51  static void activate( Display *, Window );
52  static void iconify( Display *, Window );
53  static void close( Display *, Window );
54  static Window getActive( Display * );
55  static Window getClientWindow( Display *, Window );
56  static std::string getTitle( Display *, Window );
57  static ArgbSurface *getIcon( Display *, Window );
58  static unsigned long getWorkspace( Display *, Window );
59  static unsigned long getNumberOfWorkspaces( Display * );
60  static unsigned long getCurrentWorkspace( Display * );
61  static bool getWorkspacePosition( Display *,
62  unsigned long &,
63  unsigned long & );
64  static bool getDesktopGeometry( Display *,
65  unsigned long &,
66  unsigned long & );
67  static bool isNormalWindow( Display *, Window );
68  static void setWindowType( Display *, Window, const char * );
69  static void sendClientMessage( Display *, Window, const char *,
70  unsigned long = 0, unsigned long = 0, unsigned long = 0,
71  unsigned long = 0, unsigned long = 0 );
72  static Atom getAtom( Display *, const char * );
73 
74  private:
78  template <class T> class Property
79  {
80  public:
81  Property( Display *d, Window w ) :
82  display( d ),
83  window( w ),
84  data( 0 ),
85  items( 0 ) {}
86  Property( Display *d, Window w, Atom type,
87  const char *name ) :
88  Property( d, w ) { fetch( type, name ); }
89  virtual ~Property() { freeData(); }
90  inline T *getData() const { return data; }
91  inline unsigned long getItems() const { return items; }
92  bool fetch( Atom type, const char *name,
93  long length = 1024, long offset = 0,
94  Bool remove = False )
95  {
96  freeData();
97 
98  Atom returnedType;
99  int format;
100  unsigned long items;
101  unsigned long bytesAfter;
102  unsigned char *data;
103 
104  XErrorHandler defaultHandler =
105  XSetErrorHandler( ignoreHandler );
106 
107  if( XGetWindowProperty(
108  display,
109  window,
110  getAtom( display, name ),
111  offset,
112  length,
113  remove,
114  type,
115  &returnedType,
116  &format,
117  &items,
118  &bytesAfter,
119  &data ) != Success )
120  return false;
121 
122  XSetErrorHandler( defaultHandler );
123 
124  if( returnedType != type )
125  {
126  freeData();
127  return false;
128  }
129 
130  this->data = reinterpret_cast<T *>( data );
131  this->items = items;
132 
133  return true;
134  }
135 
136  private:
137  Display *display;
138  Window window;
139  T *data;
140  unsigned long items;
141 
142  void freeData()
143  {
144  if( !data )
145  return;
146 
147  XFree( reinterpret_cast<unsigned char *>( data ) );
148 
149  data = 0;
150  items = 0;
151  }
152  };
153 
154  typedef std::map<std::string, Atom> StringToAtom;
155  static StringToAtom stringToAtom;
156 
157  WindowManager() {}
158  WindowManager &operator=( const WindowManager & ) { return *this; }
159  };
160 }
161 
162 #endif