]> icculus.org git repositories - mikachu/openbox.git/blob - src/client.hh
update to cleaned up otk api
[mikachu/openbox.git] / src / client.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 #ifndef   __client_hh
3 #define   __client_hh
4
5 /*! @file client.hh
6   @brief The OBClient class maintains the state of a client window by handling
7   property changes on the window and some client messages
8 */
9
10 extern "C" {
11 #include <X11/Xlib.h>
12
13 #ifdef    SHAPE
14 #include <X11/extensions/shape.h>
15 #endif // SHAPE
16 }
17
18 #include <string>
19
20 #include "otk/point.hh"
21 #include "otk/strut.hh"
22 #include "otk/rect.hh"
23 #include "otk/eventhandler.hh"
24 #include "widget.hh"
25 #include "python.hh"
26
27 namespace ob {
28
29 class OBFrame;
30
31 extern PyTypeObject OBClient_Type;
32
33 //! Maintains the state of a client window.
34 /*!
35   OBClient maintains the state of a client window. The state consists of the
36   hints that the application sets on the window, such as the title, or window
37   gravity.
38   <p>
39   OBClient also manages client messages for the client window. When the
40   application (or any application) requests something to be changed for the
41   client, it will call the ActionHandler (for client messages) or update the
42   class' member variables and call whatever is nessary to complete the
43   change (such as causing a redraw of the titlebar after the title is changed).
44 */
45 class OBClient : public otk::OtkEventHandler, public OBWidget {
46   PyObject_HEAD
47 public:
48
49   //! The frame window which decorates around the client window
50   /*!
51     NOTE: This should NEVER be used inside the client class's constructor!
52   */
53   OBFrame *frame;
54
55   //! Corners of the client window, used for anchor positions
56   enum Corner { TopLeft,
57                 TopRight,
58                 BottomLeft,
59                 BottomRight };
60
61   //! Possible window types
62   enum WindowType { Type_Desktop, //!< A desktop (bottom-most window)
63                     Type_Dock,    //!< A dock bar/panel window
64                     Type_Toolbar, //!< A toolbar window, pulled off an app
65                     Type_Menu,    //!< A sticky menu from an app
66                     Type_Utility, //!< A small utility window such as a palette
67                     Type_Splash,  //!< A splash screen window
68                     Type_Dialog,  //!< A dialog window
69                     Type_Normal   //!< A normal application window
70   };
71
72   //! Possible flags for MWM Hints (defined by Motif 2.0)
73   enum MwmFlags { MwmFlag_Functions   = 1 << 0, //!< The MMW Hints define funcs
74                   MwmFlag_Decorations = 1 << 1  //!< The MWM Hints define decor
75   };
76
77   //! Possible functions for MWM Hints (defined by Motif 2.0)
78   enum MwmFunctions { MwmFunc_All      = 1 << 0, //!< All functions
79                       MwmFunc_Resize   = 1 << 1, //!< Allow resizing
80                       MwmFunc_Move     = 1 << 2, //!< Allow moving
81                       MwmFunc_Iconify  = 1 << 3, //!< Allow to be iconfied
82                       MwmFunc_Maximize = 1 << 4  //!< Allow to be maximized
83                       //MwmFunc_Close    = 1 << 5 //!< Allow to be closed
84   };
85
86   //! Possible decorations for MWM Hints (defined by Motif 2.0)
87   enum MemDecorations { MwmDecor_All      = 1 << 0, //!< All decorations
88                         MwmDecor_Border   = 1 << 1, //!< Show a border
89                         MwmDecor_Handle   = 1 << 2, //!< Show a handle (bottom)
90                         MwmDecor_Title    = 1 << 3, //!< Show a titlebar
91                         //MwmDecor_Menu     = 1 << 4, //!< Show a menu
92                         MwmDecor_Iconify  = 1 << 5, //!< Show an iconify button
93                         MwmDecor_Maximize = 1 << 6  //!< Show a maximize button
94   };
95
96   //! The things the user can do to the client window
97   enum Function { Func_Resize   = 1 << 0, //!< Allow resizing
98                   Func_Move     = 1 << 1, //!< Allow moving
99                   Func_Iconify  = 1 << 2, //!< Allow to be iconified
100                   Func_Maximize = 1 << 3, //!< Allow to be maximized
101                   Func_Close    = 1 << 4  //!< Allow to be closed
102   };
103   //! Holds a bitmask of OBClient::Function values
104   typedef unsigned char FunctionFlags;
105
106   //! The decorations the client window wants to be displayed on it
107   enum Decoration { Decor_Titlebar = 1 << 0, //!< Display a titlebar
108                     Decor_Handle   = 1 << 1, //!< Display a handle (bottom)
109                     Decor_Border   = 1 << 2, //!< Display a border
110                     Decor_Iconify  = 1 << 3, //!< Display an iconify button
111                     Decor_Maximize = 1 << 4, //!< Display a maximize button
112                     Decor_Sticky   = 1 << 5, //!< Display a sticky button
113                     Decor_Close    = 1 << 6  //!< Display a close button
114   };
115   //! Holds a bitmask of OBClient::Decoration values
116   typedef unsigned char DecorationFlags;
117
118   //! The MWM Hints as retrieved from the window property
119   /*!
120     This structure only contains 3 elements, even though the Motif 2.0
121     structure contains 5. We only use the first 3, so that is all gets defined.
122   */
123   typedef struct MwmHints {
124     //! The number of elements in the OBClient::MwmHints struct
125     static const unsigned int elements = 3;
126     unsigned long flags;      //!< A bitmask of OBClient::MwmFlags values
127     unsigned long functions;  //!< A bitmask of OBClient::MwmFunctions values
128     unsigned long decorations;//!< A bitmask of OBClient::MwmDecorations values
129   };
130
131   //! Possible actions that can be made with the _NET_WM_STATE client message
132   enum StateAction { State_Remove = 0, //!< _NET_WM_STATE_REMOVE
133                      State_Add,        //!< _NET_WM_STATE_ADD
134                      State_Toggle      //!< _NET_WM_STATE_TOGGLE
135   };
136
137   //! The event mask to grab on client windows
138   static const long event_mask = PropertyChangeMask | FocusChangeMask |
139                                  StructureNotifyMask;
140
141   //! The mask of events to not let propogate past the client
142   /*!
143     This makes things like xprop work on the client window, but means we have
144     to explicitly grab clicks that we want.
145   */
146   static const long no_propagate_mask = ButtonPressMask | ButtonReleaseMask |
147                                         ButtonMotionMask;
148
149   //! The number of unmap events to ignore on the window
150   int ignore_unmaps;
151   
152 private:
153   //! The screen number on which the client resides
154   int      _screen;
155   
156   //! The actual window that this class is wrapping up
157   Window   _window;
158
159   //! The id of the group the window belongs to
160   Window   _group;
161
162   // XXX: transient_for, transients
163
164   //! The desktop on which the window resides (0xffffffff for all desktops)
165   unsigned long _desktop;
166
167   //! Normal window title
168   std::string  _title; // XXX: Have to keep track if this string is Utf8 or not
169   //! Window title when iconifiged
170   std::string  _icon_title;
171
172   //! The application that created the window
173   std::string  _app_name;
174   //! The class of the window, can used for grouping
175   std::string  _app_class;
176
177   //! The type of window (what its function is)
178   WindowType   _type;
179
180   //! Position and size of the window
181   /*!
182     This will not always be the actual position of the window on screen, it is
183     rather, the position requested by the client, to which the window's gravity
184     is applied.
185   */
186   otk::Rect    _area;
187
188   //! The logical size of the window
189   /*!
190     The "logical" size of the window is refers to the user's perception of the
191     size of the window, and is the value that should be displayed to the user.
192     For example, with xterms, this value it the number of characters being
193     displayed in the terminal, instead of the number of pixels.
194   */
195   otk::Point   _logical_size;
196
197   //! Width of the border on the window.
198   /*!
199     The window manager will set this to 0 while the window is being managed,
200     but needs to restore it afterwards, so it is saved here.
201   */
202   int _border_width;
203
204   //! The minimum size of the client window
205   /*!
206     If the min is > the max, then the window is not resizable
207   */
208   otk::Point _min_size;
209   //! The maximum size of the client window
210   /*!
211     If the min is > the max, then the window is not resizable
212   */
213   otk::Point _max_size;
214   //! The size of increments to resize the client window by
215   otk::Point _size_inc;
216   //! The base size of the client window
217   /*!
218     This value should be subtracted from the window's actual size when
219     displaying its size to the user, or working with its min/max size
220   */
221   otk::Point _base_size;
222
223   //! Where to place the decorated window in relation to the undecorated window
224   int _gravity;
225
226   //! The state of the window, one of WithdrawnState, IconicState, or
227   //! NormalState
228   long _wmstate;
229
230   //! Was the window's position requested by the application? if not, we should
231   //! place the window ourselves when it first appears
232   bool _positioned;
233   
234   //! Can the window receive input focus?
235   bool _can_focus;
236   //! Urgency flag
237   bool _urgent;
238   //! Notify the window when it receives focus?
239   bool _focus_notify;
240
241   //! The window uses shape extension to be non-rectangular?
242   bool _shaped;
243
244   //! The window is modal, so it must be processed before any windows it is
245   //! related to can be focused
246   bool _modal;
247   //! Only the window's titlebar is displayed
248   bool _shaded;
249   //! The window is iconified
250   bool _iconic;
251   //! The window is maximized to fill the screen vertically
252   bool _max_vert;
253   //! The window is maximized to fill the screen horizontally
254   bool _max_horz;
255   //! The window is a 'fullscreen' window, and should be on top of all others
256   bool _fullscreen;
257   //! The window should be on top of other windows of the same type
258   bool _floating;
259
260   //! A bitmask of values in the OBClient::Decoration enum
261   /*!
262     The values in the variable are the decorations that the client wants to be
263     displayed around it.
264   */
265   DecorationFlags _decorations;
266
267   //! A bitmask of values in the OBClient::Function enum
268   /*!
269     The values in the variable specify the ways in which the user is allowed to
270     modify this window.
271   */
272   FunctionFlags _functions;
273
274   //! Retrieves the desktop hint's value and sets OBClient::_desktop
275   void getDesktop();
276   //! Retrieves the window's type and sets OBClient::_type
277   void getType();
278   //! Gets the MWM Hints and adjusts OBClient::_functions and
279   //! OBClient::_decorations
280   void getMwmHints();
281   //! Gets the position and size of the window and sets OBClient::_area
282   void getArea();
283   //! Gets the net_state hint and sets the boolean flags for any states set in
284   //! the hint
285   void getState();
286   //! Determines if the window uses the Shape extension and sets
287   //! OBClient::_shaped
288   void getShaped();
289
290   //! Sets the wm_state to the specified value
291   void setWMState(long state);
292   //! Sends the window to the specified desktop
293   void setDesktop(long desktop);
294   //! Adjusts the window's net_state
295   void setState(StateAction action, long data1, long data2);
296
297   //! Update the protocols that the window supports and adjusts things if they
298   //! change
299   void updateProtocols();
300   //! Updates the WMNormalHints and adjusts things if they change
301   void updateNormalHints();
302   //! Updates the WMHints and adjusts things if they change
303   void updateWMHints();
304   //! Updates the window's title
305   void updateTitle();
306   //! Updates the window's icon title
307   void updateIconTitle();
308   //! Updates the window's application name and class
309   void updateClass();
310   // XXX: updateTransientFor();
311
312 public:
313   //! Constructs a new OBClient object around a specified window id
314   /*!
315     @param window The window id that the OBClient class should handle
316     @param screen The screen on which the window resides
317   */
318   OBClient(int screen, Window window);
319   //! Destroys the OBClient object
320   virtual ~OBClient();
321
322   //! Returns the screen on which the clien resides
323   inline int screen() const { return _screen; }
324   
325   //! Returns the window id that the OBClient object is handling
326   inline Window window() const { return _window; }
327
328   //! Returns the type of the window, one of the OBClient::WindowType values
329   inline WindowType type() const { return _type; }
330   //! Returns the desktop on which the window resides
331   /*!
332     This value is a 0-based index.<br>
333     A value of 0xffffffff indicates that the window exists on all desktops.
334   */
335   inline unsigned long desktop() const { return _desktop; }
336   //! Returns the window's title
337   inline const std::string &title() const { return _title; }
338   //! Returns the window's title when it is iconified
339   inline const std::string &iconTitle() const { return _title; }
340   //! Returns the application's name to whom the window belongs
341   inline const std::string &appName() const { return _app_name; }
342   //! Returns the class of the window
343   inline const std::string &appClass() const { return _app_class; }
344   //! Returns if the window can be focused
345   /*!
346     @return true if the window can receive focusl otherwise, false
347   */
348   inline bool canFocus() const { return _can_focus; }
349   //! Returns if the window has indicated that it needs urgent attention
350   inline bool urgent() const { return _urgent; }
351   //! Returns if the window wants to be notified when it receives focus
352   inline bool focusNotify() const { return _focus_notify; }
353   //! Returns if the window uses the Shape extension
354   inline bool shaped() const { return _shaped; }
355   //! Returns the window's gravity
356   /*!
357     This value determines where to place the decorated window in relation to
358     its position without decorations.<br>
359     One of: NorthWestGravity, SouthWestGravity, EastGravity, ...,
360     SouthGravity, StaticGravity, ForgetGravity
361   */
362   inline int gravity() const { return _gravity; }
363   //! Returns if the application requested the initial position for the window
364   /*!
365     If the application did not request a position (this function returns false)
366     then the window should be placed intelligently by the window manager
367     initially
368   */
369   inline bool positionRequested() const { return _positioned; }
370   //! Returns the decorations that the client window wishes to be displayed on
371   //! it
372   inline DecorationFlags decorations() const { return _decorations; }
373   //! Returns the functions that the user can perform on the window
374   inline FunctionFlags funtions() const { return _functions; }
375
376   //! Returns if the window is modal
377   /*!
378     If the window is modal, then no other windows that it is related to can get
379     focus while it exists/remains modal.
380   */
381   inline bool modal() const { return _modal; }
382   //! Returns if the window is shaded
383   /*!
384     When the window is shaded, only its titlebar is visible, the client itself
385     is not mapped
386   */
387   inline bool shaded() const { return _shaded; }
388   //! Returns if the window is iconified
389   /*!
390     When the window is iconified, it is not visible at all (except in iconbars/
391     panels/etc that want to show lists of iconified windows
392   */
393   inline bool iconic() const { return _iconic; }
394   //! Returns if the window is maximized vertically
395   inline bool maxVert() const { return _max_vert; }
396   //! Returns if the window is maximized horizontally
397   inline bool maxHorz() const { return _max_horz; }
398   //! Returns if the window is fullscreen
399   /*!
400     When the window is fullscreen, it is kept above all others
401   */
402   inline bool fullscreen() const { return _fullscreen; }
403   //! Returns if the window is floating
404   /*!
405     When the window is floating, it is kept above all others in the same
406     stacking layer as it
407   */
408   inline bool floating() const { return _floating; }
409
410   //! Removes or reapplies the client's border to its window
411   /*!
412     Used when managing and unmanaging a window.
413     @param addborder true if adding the border to the client; false if removing
414                      from the client
415   */
416   void toggleClientBorder(bool addborder);
417
418   //! Returns the position and size of the client relative to the root window
419   inline const otk::Rect &area() const { return _area; }
420
421   //! Move the client window
422   void move(int x, int y);
423   
424   //! Resizes the client window, anchoring it in a given corner
425   /*!
426     This also maintains things like the client's minsize, and size increments.
427     @param anchor The corner to keep in the same position when resizing
428     @param x The X component of the new size for the client
429     @param y The Y component of the new size for the client
430   */
431   void resize(Corner anchor, int x, int y);
432   
433   virtual void propertyHandler(const XPropertyEvent &e);
434   virtual void clientMessageHandler(const XClientMessageEvent &e);
435   virtual void shapeHandler(const XShapeEvent &e);
436   virtual void configureRequestHandler(const XConfigureRequestEvent &e);
437   virtual void unmapHandler(const XUnmapEvent &e);
438   virtual void destroyHandler(const XDestroyWindowEvent &e);
439 };
440
441 }
442
443 #endif // __client_hh