d2c8b5f81c828c2012d1903e507040f84b3972da
[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 Client class maintains the state of a client window by handling
7   property changes on the window and some client messages
8 */
9
10 #include "widgetbase.hh"
11 #include "otk/point.hh"
12 #include "otk/strut.hh"
13 #include "otk/rect.hh"
14 #include "otk/eventhandler.hh"
15 #include "otk/ustring.hh"
16
17 extern "C" {
18 #include <X11/Xlib.h>
19
20 #ifdef    SHAPE
21 #include <X11/extensions/shape.h>
22 #endif // SHAPE
23 }
24
25 #include <string>
26 #include <list>
27
28 namespace ob {
29
30 class Frame;
31
32 //! The MWM Hints as retrieved from the window property
33 /*!
34   This structure only contains 3 elements, even though the Motif 2.0
35   structure contains 5. We only use the first 3, so that is all gets defined.
36 */
37 struct MwmHints {
38   unsigned long flags;      //!< A bitmask of Client::MwmFlags values
39   unsigned long functions;  //!< A bitmask of Client::MwmFunctions values
40   unsigned long decorations;//!< A bitmask of Client::MwmDecorations values
41   //! The number of elements in the Client::MwmHints struct
42   static const unsigned int elements = 3;
43 };
44
45 //! Maintains the state of a client window.
46 /*!
47   Client maintains the state of a client window. The state consists of the
48   hints that the application sets on the window, such as the title, or window
49   gravity.
50   <p>
51   Client also manages client messages for the client window. When the
52   application (or any application) requests something to be changed for the
53   client, it will call the ActionHandler (for client messages) or update the
54   class' member variables and call whatever is nessary to complete the
55   change (such as causing a redraw of the titlebar after the title is changed).
56 */
57 class Client : public otk::EventHandler, public WidgetBase {
58 public:
59
60   //! The frame window which decorates around the client window
61   /*!
62     NOTE: This should NEVER be used inside the client class's constructor!
63   */
64   Frame *frame;
65
66   //! Holds a list of Clients
67   typedef std::list<Client*> List;
68
69   //! The possible stacking layers a client window can be a part of
70   enum StackLayer {
71     Layer_Icon,       //!< 0 - iconified windows, in any order at all
72     Layer_Desktop,    //!< 1 - desktop windows
73     Layer_Below,      //!< 2 - normal windows w/ below
74     Layer_Normal,     //!< 3 - normal windows
75     Layer_Above,      //!< 4 - normal windows w/ above
76     Layer_Top,        //!< 5 - always-on-top-windows (docks?)
77     Layer_Fullscreen, //!< 6 - fullscreeen windows
78     Layer_Internal,   //!< 7 - openbox windows/menus
79     NUM_LAYERS
80   };
81
82   //! Corners of the client window, used for anchor positions
83   enum Corner { TopLeft,
84                 TopRight,
85                 BottomLeft,
86                 BottomRight };
87
88   //! Possible window types
89   enum WindowType { Type_Desktop, //!< A desktop (bottom-most window)
90                     Type_Dock,    //!< A dock bar/panel window
91                     Type_Toolbar, //!< A toolbar window, pulled off an app
92                     Type_Menu,    //!< A sticky menu from an app
93                     Type_Utility, //!< A small utility window such as a palette
94                     Type_Splash,  //!< A splash screen window
95                     Type_Dialog,  //!< A dialog window
96                     Type_Normal   //!< A normal application window
97   };
98
99   //! Possible flags for MWM Hints (defined by Motif 2.0)
100   enum MwmFlags { MwmFlag_Functions   = 1 << 0, //!< The MMW Hints define funcs
101                   MwmFlag_Decorations = 1 << 1  //!< The MWM Hints define decor
102   };
103
104   //! Possible functions for MWM Hints (defined by Motif 2.0)
105   enum MwmFunctions { MwmFunc_All      = 1 << 0, //!< All functions
106                       MwmFunc_Resize   = 1 << 1, //!< Allow resizing
107                       MwmFunc_Move     = 1 << 2, //!< Allow moving
108                       MwmFunc_Iconify  = 1 << 3, //!< Allow to be iconfied
109                       MwmFunc_Maximize = 1 << 4  //!< Allow to be maximized
110                       //MwmFunc_Close    = 1 << 5 //!< Allow to be closed
111   };
112
113   //! Possible decorations for MWM Hints (defined by Motif 2.0)
114   enum MemDecorations { MwmDecor_All      = 1 << 0, //!< All decorations
115                         MwmDecor_Border   = 1 << 1, //!< Show a border
116                         MwmDecor_Handle   = 1 << 2, //!< Show a handle (bottom)
117                         MwmDecor_Title    = 1 << 3, //!< Show a titlebar
118                         //MwmDecor_Menu     = 1 << 4, //!< Show a menu
119                         MwmDecor_Iconify  = 1 << 5, //!< Show an iconify button
120                         MwmDecor_Maximize = 1 << 6  //!< Show a maximize button
121   };
122
123   //! The things the user can do to the client window
124   enum Function { Func_Resize   = 1 << 0, //!< Allow resizing
125                   Func_Move     = 1 << 1, //!< Allow moving
126                   Func_Iconify  = 1 << 2, //!< Allow to be iconified
127                   Func_Maximize = 1 << 3, //!< Allow to be maximized
128                   Func_Close    = 1 << 4  //!< Allow to be closed
129   };
130   //! Holds a bitmask of Client::Function values
131   typedef unsigned char FunctionFlags;
132
133   //! The decorations the client window wants to be displayed on it
134   enum Decoration { Decor_Titlebar = 1 << 0, //!< Display a titlebar
135                     Decor_Handle   = 1 << 1, //!< Display a handle (bottom)
136                     Decor_Border   = 1 << 2, //!< Display a border
137                     Decor_Iconify  = 1 << 3, //!< Display an iconify button
138                     Decor_Maximize = 1 << 4, //!< Display a maximize button
139                     Decor_Sticky   = 1 << 5, //!< Display a sticky button
140                     Decor_Close    = 1 << 6  //!< Display a close button
141   };
142   //! Holds a bitmask of Client::Decoration values
143   typedef unsigned char DecorationFlags;
144
145   //! Possible actions that can be made with the _NET_WM_STATE client message
146   enum StateAction { State_Remove = 0, //!< _NET_WM_STATE_REMOVE
147                      State_Add,        //!< _NET_WM_STATE_ADD
148                      State_Toggle      //!< _NET_WM_STATE_TOGGLE
149   };
150
151   //! The event mask to grab on client windows
152   static const long event_mask = PropertyChangeMask | FocusChangeMask |
153                                  StructureNotifyMask;
154
155   //! The mask of events to not let propogate past the client
156   /*!
157     This makes things like xprop work on the client window, but means we have
158     to explicitly grab clicks that we want.
159   */
160   static const long no_propagate_mask = ButtonPressMask | ButtonReleaseMask |
161                                         ButtonMotionMask;
162
163   //! The number of unmap events to ignore on the window
164   int ignore_unmaps;
165   
166 private:
167   //! The screen number on which the client resides
168   int      _screen;
169   
170   //! The actual window that this class is wrapping up
171   Window   _window;
172
173   //! The id of the group the window belongs to
174   Window   _group;
175
176   //! The client which this client is a transient (child) for
177   Client *_transient_for;
178
179   //! The clients which are transients (children) of this client
180   Client::List _transients;
181
182   //! The desktop on which the window resides (0xffffffff for all desktops)
183   long _desktop;
184
185   //! Normal window title
186   otk::ustring  _title;
187   //! Window title when iconifiged
188   otk::ustring  _icon_title;
189
190   //! The application that created the window
191   std::string  _app_name;
192   //! The class of the window, can used for grouping
193   std::string  _app_class;
194   //! The specified role of the window, used for identification
195   std::string  _role;
196
197   //! The type of window (what its function is)
198   WindowType   _type;
199
200   //! Position and size of the window
201   /*!
202     This will not always be the actual position of the window on screen, it is
203     rather, the position requested by the client, to which the window's gravity
204     is applied.
205   */
206   otk::Rect    _area;
207
208   //! The window's strut
209   /*!
210     The strut defines areas of the screen that are marked off-bounds for window
211     placement. In theory, where this window exists.
212   */
213   otk::Strut   _strut;
214
215   //! The logical size of the window
216   /*!
217     The "logical" size of the window is refers to the user's perception of the
218     size of the window, and is the value that should be displayed to the user.
219     For example, with xterms, this value it the number of characters being
220     displayed in the terminal, instead of the number of pixels.
221   */
222   otk::Point   _logical_size;
223
224   //! Width of the border on the window.
225   /*!
226     The window manager will set this to 0 while the window is being managed,
227     but needs to restore it afterwards, so it is saved here.
228   */
229   int _border_width;
230
231   //! The minimum size of the client window
232   /*!
233     If the min is > the max, then the window is not resizable
234   */
235   otk::Point _min_size;
236   //! The maximum size of the client window
237   /*!
238     If the min is > the max, then the window is not resizable
239   */
240   otk::Point _max_size;
241   //! The size of increments to resize the client window by
242   otk::Point _size_inc;
243   //! The base size of the client window
244   /*!
245     This value should be subtracted from the window's actual size when
246     displaying its size to the user, or working with its min/max size
247   */
248   otk::Point _base_size;
249
250   //! Window decoration and functionality hints
251   MwmHints _mwmhints;
252   
253   //! Where to place the decorated window in relation to the undecorated window
254   int _gravity;
255
256   //! The state of the window, one of WithdrawnState, IconicState, or
257   //! NormalState
258   long _wmstate;
259
260   //! Was the window's position requested by the application? if not, we should
261   //! place the window ourselves when it first appears
262   bool _positioned;
263   
264   //! Can the window receive input focus?
265   bool _can_focus;
266   //! Urgency flag
267   bool _urgent;
268   //! Notify the window when it receives focus?
269   bool _focus_notify;
270   //! Does the client window have the input focus?
271   bool _focused;
272
273   //! The window uses shape extension to be non-rectangular?
274   bool _shaped;
275
276   //! The window is modal, so it must be processed before any windows it is
277   //! related to can be focused
278   bool _modal;
279   //! Only the window's titlebar is displayed
280   bool _shaded;
281   //! The window is iconified
282   bool _iconic;
283   //! The window is maximized to fill the screen vertically
284   bool _max_vert;
285   //! The window is maximized to fill the screen horizontally
286   bool _max_horz;
287   //! The window should not be displayed by pagers
288   bool _skip_pager;
289   //! The window should not be displayed by taskbars
290   bool _skip_taskbar;
291   //! The window is a 'fullscreen' window, and should be on top of all others
292   bool _fullscreen;
293   //! The window should be on top of other windows of the same type
294   bool _above;
295   //! The window should be underneath other windows of the same type
296   bool _below;
297
298   StackLayer _layer;
299
300   //! A bitmask of values in the Client::Decoration enum
301   /*!
302     The values in the variable are the decorations that the client wants to be
303     displayed around it.
304   */
305   DecorationFlags _decorations;
306
307   //! A bitmask of values in the Client::Function enum
308   /*!
309     The values in the variable specify the ways in which the user is allowed to
310     modify this window.
311   */
312   FunctionFlags _functions;
313
314   //! Retrieves the window's initial gravity
315   void getGravity();
316   //! Retrieves the desktop hint's value and sets Client::_desktop
317   void getDesktop();
318   //! Retrieves the window's type and sets Client::_type
319   void getType();
320   //! Gets the MWM Hints and adjusts Client::_functions and
321   //! Client::_decorations
322   void getMwmHints();
323   //! Gets the position and size of the window and sets Client::_area
324   void getArea();
325   //! Gets the net_state hint and sets the boolean flags for any states set in
326   //! the hint
327   void getState();
328   //! Determines if the window uses the Shape extension and sets
329   //! Client::_shaped
330   void getShaped();
331
332   //! Set up what decor should be shown on the window and what functions should
333   //! be allowed (Client::_decorations and Client::_functions).
334   /*!
335     This also updates the NET_WM_ALLOWED_ACTIONS hint.
336   */
337   void setupDecorAndFunctions();
338   
339   //! Sets the wm_state to the specified value
340   void setWMState(long state);
341   //! Adjusts the window's net_state
342   void setState(StateAction action, long data1, long data2);
343
344   //! Sends the window to the specified desktop
345   void setDesktop(long desktop);
346   
347   //! Calculates the stacking layer for the client window
348   void calcLayer();
349
350   //! Update the protocols that the window supports and adjusts things if they
351   //! change
352   void updateProtocols();
353   //! Updates the WMNormalHints and adjusts things if they change
354   void updateNormalHints();
355   //! Updates the WMHints and adjusts things if they change
356   void updateWMHints();
357   //! Updates the window's title
358   void updateTitle();
359   //! Updates the window's icon title
360   void updateIconTitle();
361   //! Updates the window's application name and class
362   void updateClass();
363   //! Updates the strut for the client
364   void updateStrut();
365   //! Updates the window's transient status, and any parents of it
366   void updateTransientFor();
367
368   //! Change the client's state hints to match the class' data
369   void changeState();
370   //! Change the allowed actions set on the client
371   void changeAllowedActions();
372
373   //! Request the client to close its window.
374   void close();
375
376   //! Shades or unshades the client window
377   /*!
378     @param shade true if the window should be shaded; false if it should be
379                  unshaded.
380   */
381   void shade(bool shade);
382
383   //! Internal version of the Client::move function
384   /*!
385     @param x The X coordinate to move to.
386     @param y The Y coordinate to move to.
387   */
388   void internal_move(int x, int y);
389   //! Internal version of the Client::resize function
390   /*!
391     This also maintains things like the client's minsize, and size increments.
392     @param anchor The corner to keep in the same position when resizing.
393     @param w The width component of the new size for the client.
394     @param h The height component of the new size for the client.
395     @param x An optional X coordinate to which the window will be moved
396              after resizing.
397     @param y An optional Y coordinate to which the window will be moved
398              after resizing.
399     The x and y coordinates must both be sepcified together, or they will have
400     no effect. When they are specified, the anchor is ignored.
401   */
402   void internal_resize(Corner anchor, int w, int h,
403                        int x = INT_MIN, int y = INT_MIN);
404
405 public:
406 #ifndef SWIG
407   //! Constructs a new Client object around a specified window id
408   /*!
409 BB    @param window The window id that the Client class should handle
410     @param screen The screen on which the window resides
411   */
412   Client(int screen, Window window);
413   //! Destroys the Client object
414   virtual ~Client();
415 #endif
416
417   //! Returns the screen on which the clien resides
418   inline int screen() const { return _screen; }
419   
420   //! Returns the window id that the Client object is handling
421   inline Window window() const { return _window; }
422
423   //! Returns the type of the window, one of the Client::WindowType values
424   inline WindowType type() const { return _type; }
425   //! Returns if the window should be treated as a normal window.
426   /*!
427     Some windows (desktops, docks, splash screens) have special rules applied
428     to them in a number of places regarding focus or user interaction.
429   */
430   inline bool normal() const {
431     return ! (_type == Type_Desktop || _type == Type_Dock ||
432               _type == Type_Splash);
433   }
434   
435   //! Returns the desktop on which the window resides
436   /*!
437     This value is a 0-based index.<br>
438     A value of 0xffffffff indicates that the window exists on all desktops.
439   */
440   inline long desktop() const { return _desktop; }
441   //! Returns the window's title
442   inline const otk::ustring &title() const { return _title; }
443   //! Returns the window's title when it is iconified
444   inline const otk::ustring &iconTitle() const { return _title; }
445   //! Returns the application's name to whom the window belongs
446   inline const std::string &appName() const { return _app_name; }
447   //! Returns the class of the window
448   inline const std::string &appClass() const { return _app_class; }
449   //! Returns the program-specified role of the window
450   inline const std::string &role() const { return _role; }
451   //! Returns if the window can be focused
452   /*!
453     @return true if the window can receive focus; otherwise, false
454   */
455   inline bool canFocus() const { return _can_focus; }
456   //! Returns if the window has indicated that it needs urgent attention
457   inline bool urgent() const { return _urgent; }
458   //! Returns if the window wants to be notified when it receives focus
459   inline bool focusNotify() const { return _focus_notify; }
460   //! Returns if the window uses the Shape extension
461   inline bool shaped() const { return _shaped; }
462   //! Returns the window's gravity
463   /*!
464     This value determines where to place the decorated window in relation to
465     its position without decorations.<br>
466     One of: NorthWestGravity, SouthWestGravity, EastGravity, ...,
467     SouthGravity, StaticGravity, ForgetGravity
468   */
469   inline int gravity() const { return _gravity; }
470   //! Returns if the application requested the initial position for the window
471   /*!
472     If the application did not request a position (this function returns false)
473     then the window should be placed intelligently by the window manager
474     initially
475   */
476   inline bool positionRequested() const { return _positioned; }
477   //! Returns the decorations that the client window wishes to be displayed on
478   //! it
479   inline DecorationFlags decorations() const { return _decorations; }
480   //! Returns the functions that the user can perform on the window
481   inline FunctionFlags funtions() const { return _functions; }
482
483   //! Return the client this window is transient for
484   inline Client *transientFor() const { return _transient_for; }
485
486   //! Returns if the window is modal
487   /*!
488     If the window is modal, then no other windows that it is related to can get
489     focus while it exists/remains modal.
490   */
491   inline bool modal() const { return _modal; }
492   //! Returns if the window is shaded
493   /*!
494     When the window is shaded, only its titlebar is visible.
495   */
496   inline bool shaded() const { return _shaded; }
497   //! Returns if the window is iconified
498   /*!
499     When the window is iconified, it is not visible at all (except in iconbars/
500     panels/etc that want to show lists of iconified windows
501   */
502   inline bool iconic() const { return _iconic; }
503   //! Returns if the window is maximized vertically
504   inline bool maxVert() const { return _max_vert; }
505   //! Returns if the window is maximized horizontally
506   inline bool maxHorz() const { return _max_horz; }
507   //! Returns the window's stacking layer
508   inline StackLayer layer() const { return _layer; }
509
510   //! Removes or reapplies the client's border to its window
511   /*!
512     Used when managing and unmanaging a window.
513     @param addborder true if adding the border to the client; false if removing
514                      from the client
515   */
516   void toggleClientBorder(bool addborder);
517
518   //! Returns the position and size of the client relative to the root window
519   inline const otk::Rect &area() const { return _area; }
520
521   //! Returns the client's strut definition
522   inline const otk::Strut &strut() const { return _strut; }
523
524   //! Move the client window
525   /*!
526     @param x The X coordinate to move to.
527     @param y The Y coordinate to move to.
528   */
529   void move(int x, int y);
530   
531   //! Resizes the client window, anchoring it in a given corner
532   /*!
533     This also maintains things like the client's minsize, and size increments.
534     @param anchor The corner to keep in the same position when resizing.
535     @param w The width component of the new size for the client.
536     @param h The height component of the new size for the client.
537   */
538   void resize(Corner anchor, int w, int h);
539
540   //! Attempt to focus the client window
541   bool focus() const;
542
543   //! Remove focus from the client window
544   void unfocus() const;
545
546   virtual void focusHandler(const XFocusChangeEvent &e);
547   virtual void unfocusHandler(const XFocusChangeEvent &e);
548   virtual void propertyHandler(const XPropertyEvent &e);
549   virtual void clientMessageHandler(const XClientMessageEvent &e);
550   virtual void configureRequestHandler(const XConfigureRequestEvent &e);
551   virtual void unmapHandler(const XUnmapEvent &e);
552   virtual void destroyHandler(const XDestroyWindowEvent &e);
553   virtual void reparentHandler(const XReparentEvent &e);
554 #if defined(SHAPE)
555   virtual void shapeHandler(const XShapeEvent &e);
556 #endif // SHAPE 
557 };
558
559 }
560
561 #endif // __client_hh