Some cleanups in the mapping procedure. Apply requested states during the process.
[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_Shade      = 1 << 4, //!< Allow to be shaded
129                   Func_Fullscreen = 1 << 5, //!< Allow to be made fullscreen
130                   Func_Close      = 1 << 6  //!< Allow to be closed
131   };
132   //! Holds a bitmask of Client::Function values
133   typedef unsigned char FunctionFlags;
134
135   //! The decorations the client window wants to be displayed on it
136   enum Decoration { Decor_Titlebar = 1 << 0, //!< Display a titlebar
137                     Decor_Handle   = 1 << 1, //!< Display a handle (bottom)
138                     Decor_Border   = 1 << 2, //!< Display a border
139                     Decor_Iconify  = 1 << 3, //!< Display an iconify button
140                     Decor_Maximize = 1 << 4, //!< Display a maximize button
141                     Decor_Sticky   = 1 << 5, //!< Display a sticky button
142                     Decor_Close    = 1 << 6  //!< Display a close button
143   };
144   //! Holds a bitmask of Client::Decoration values
145   typedef unsigned char DecorationFlags;
146
147   //! Possible actions that can be made with the _NET_WM_STATE client message
148   enum StateAction { State_Remove = 0, //!< _NET_WM_STATE_REMOVE
149                      State_Add,        //!< _NET_WM_STATE_ADD
150                      State_Toggle      //!< _NET_WM_STATE_TOGGLE
151   };
152
153   //! The event mask to grab on client windows
154   static const long event_mask = PropertyChangeMask | FocusChangeMask |
155                                  StructureNotifyMask;
156
157   //! The mask of events to not let propogate past the client
158   /*!
159     This makes things like xprop work on the client window, but means we have
160     to explicitly grab clicks that we want.
161   */
162   static const long no_propagate_mask = ButtonPressMask | ButtonReleaseMask |
163                                         ButtonMotionMask;
164
165   //! The number of unmap events to ignore on the window
166   int ignore_unmaps;
167   
168 private:
169   //! The screen number on which the client resides
170   int      _screen;
171   
172   //! The actual window that this class is wrapping up
173   Window   _window;
174
175   //! The id of the group the window belongs to
176   Window   _group;
177
178   //! The client which this client is a transient (child) for
179   Client *_transient_for;
180
181   //! The clients which are transients (children) of this client
182   Client::List _transients;
183
184   //! The desktop on which the window resides (0xffffffff for all desktops)
185   long _desktop;
186
187   //! Normal window title
188   otk::ustring  _title;
189   //! Window title when iconifiged
190   otk::ustring  _icon_title;
191
192   //! The application that created the window
193   std::string  _app_name;
194   //! The class of the window, can used for grouping
195   std::string  _app_class;
196   //! The specified role of the window, used for identification
197   std::string  _role;
198
199   //! The type of window (what its function is)
200   WindowType   _type;
201
202   //! Position and size of the window
203   /*!
204     This will not always be the actual position of the window on screen, it is
205     rather, the position requested by the client, to which the window's gravity
206     is applied.
207   */
208   otk::Rect    _area;
209
210   //! The window's strut
211   /*!
212     The strut defines areas of the screen that are marked off-bounds for window
213     placement. In theory, where this window exists.
214   */
215   otk::Strut   _strut;
216
217   //! The logical size of the window
218   /*!
219     The "logical" size of the window is refers to the user's perception of the
220     size of the window, and is the value that should be displayed to the user.
221     For example, with xterms, this value it the number of characters being
222     displayed in the terminal, instead of the number of pixels.
223   */
224   otk::Point   _logical_size;
225
226   //! Width of the border on the window.
227   /*!
228     The window manager will set this to 0 while the window is being managed,
229     but needs to restore it afterwards, so it is saved here.
230   */
231   int _border_width;
232
233   //! The minimum size of the client window
234   /*!
235     If the min is > the max, then the window is not resizable
236   */
237   otk::Point _min_size;
238   //! The maximum size of the client window
239   /*!
240     If the min is > the max, then the window is not resizable
241   */
242   otk::Point _max_size;
243   //! The size of increments to resize the client window by
244   otk::Point _size_inc;
245   //! The base size of the client window
246   /*!
247     This value should be subtracted from the window's actual size when
248     displaying its size to the user, or working with its min/max size
249   */
250   otk::Point _base_size;
251
252   //! Window decoration and functionality hints
253   MwmHints _mwmhints;
254   
255   //! Where to place the decorated window in relation to the undecorated window
256   int _gravity;
257
258   //! The state of the window, one of WithdrawnState, IconicState, or
259   //! NormalState
260   long _wmstate;
261
262   //! Was the window's position requested by the application? if not, we should
263   //! place the window ourselves when it first appears
264   bool _positioned;
265   
266   //! Can the window receive input focus?
267   bool _can_focus;
268   //! Urgency flag
269   bool _urgent;
270   //! Notify the window when it receives focus?
271   bool _focus_notify;
272   //! Does the client window have the input focus?
273   bool _focused;
274
275   //! The window uses shape extension to be non-rectangular?
276   bool _shaped;
277
278   //! The window is modal, so it must be processed before any windows it is
279   //! related to can be focused
280   bool _modal;
281   //! Only the window's titlebar is displayed
282   bool _shaded;
283   //! The window is iconified
284   bool _iconic;
285   //! The window is maximized to fill the screen vertically
286   bool _max_vert;
287   //! The window is maximized to fill the screen horizontally
288   bool _max_horz;
289   //! The window should not be displayed by pagers
290   bool _skip_pager;
291   //! The window should not be displayed by taskbars
292   bool _skip_taskbar;
293   //! The window is a 'fullscreen' window, and should be on top of all others
294   bool _fullscreen;
295   //! The window should be on top of other windows of the same type
296   bool _above;
297   //! The window should be underneath other windows of the same type
298   bool _below;
299
300   StackLayer _layer;
301
302   //! A bitmask of values in the Client::Decoration enum
303   /*!
304     The values in the variable are the decorations that the client wants to be
305     displayed around it.
306   */
307   DecorationFlags _decorations;
308
309   //! A bitmask of values in the Client::Function enum
310   /*!
311     The values in the variable specify the ways in which the user is allowed to
312     modify this window.
313   */
314   FunctionFlags _functions;
315
316   //! Retrieves the window's initial gravity
317   void getGravity();
318   //! Retrieves the desktop hint's value and sets Client::_desktop
319   void getDesktop();
320   //! Retrieves the window's type and sets Client::_type
321   void getType();
322   //! Gets the MWM Hints and adjusts Client::_functions and
323   //! Client::_decorations
324   void getMwmHints();
325   //! Gets the position and size of the window and sets Client::_area
326   void getArea();
327   //! Gets the net_state hint and sets the boolean flags for any states set in
328   //! the hint
329   void getState();
330   //! Determines if the window uses the Shape extension and sets
331   //! Client::_shaped
332   void getShaped();
333
334   //! Set up what decor should be shown on the window and what functions should
335   //! be allowed (Client::_decorations and Client::_functions).
336   /*!
337     This also updates the NET_WM_ALLOWED_ACTIONS hint.
338   */
339   void setupDecorAndFunctions();
340   
341   //! Sets the wm_state to the specified value
342   void setWMState(long state);
343   //! Adjusts the window's net_state
344   /*!
345     This should not be called as part of the window mapping process! It is for
346     use when updating the state post-mapping.<br>
347     Client::applyStartupState is used to do the same things during the mapping
348     process.
349   */
350   void setState(StateAction action, long data1, long data2);
351
352   //! Sends the window to the specified desktop
353   void setDesktop(long desktop);
354   
355   //! Calculates the stacking layer for the client window
356   void calcLayer();
357
358   //! Update the protocols that the window supports and adjusts things if they
359   //! change
360   void updateProtocols();
361   //! Updates the WMNormalHints and adjusts things if they change
362   void updateNormalHints();
363   //! Updates the WMHints and adjusts things if they change
364   void updateWMHints();
365   //! Updates the window's title
366   void updateTitle();
367   //! Updates the window's icon title
368   void updateIconTitle();
369   //! Updates the window's application name and class
370   void updateClass();
371   //! Updates the strut for the client
372   void updateStrut();
373   //! Updates the window's transient status, and any parents of it
374   void updateTransientFor();
375
376   //! Change the client's state hints to match the class' data
377   void changeState();
378   //! Change the allowed actions set on the client
379   void changeAllowedActions();
380
381   //! Request the client to close its window.
382   void close();
383
384   //! Shades or unshades the client window
385   /*!
386     @param shade true if the window should be shaded; false if it should be
387                  unshaded.
388   */
389   void shade(bool shade);
390
391   //! Fullscreen's or unfullscreen's the client window
392   /*!
393     @param fs true if the window should be made fullscreen; false if it should
394               be returned to normal state.
395   */
396   void fullscreen(bool fs);
397
398   //! Internal version of the Client::move function
399   /*!
400     @param x The X coordinate to move to.
401     @param y The Y coordinate to move to.
402   */
403   void internal_move(int x, int y);
404   //! Internal version of the Client::resize function
405   /*!
406     This also maintains things like the client's minsize, and size increments.
407     @param anchor The corner to keep in the same position when resizing.
408     @param w The width component of the new size for the client.
409     @param h The height component of the new size for the client.
410     @param x An optional X coordinate to which the window will be moved
411              after resizing.
412     @param y An optional Y coordinate to which the window will be moved
413              after resizing.
414     The x and y coordinates must both be sepcified together, or they will have
415     no effect. When they are specified, the anchor is ignored.
416   */
417   void internal_resize(Corner anchor, int w, int h,
418                        int x = INT_MIN, int y = INT_MIN);
419
420 public:
421 #ifndef SWIG
422   //! Constructs a new Client object around a specified window id
423   /*!
424 BB    @param window The window id that the Client class should handle
425     @param screen The screen on which the window resides
426   */
427   Client(int screen, Window window);
428   //! Destroys the Client object
429   virtual ~Client();
430 #endif
431
432   //! Returns the screen on which the clien resides
433   inline int screen() const { return _screen; }
434   
435   //! Returns the window id that the Client object is handling
436   inline Window window() const { return _window; }
437
438   //! Returns the type of the window, one of the Client::WindowType values
439   inline WindowType type() const { return _type; }
440   //! Returns if the window should be treated as a normal window.
441   /*!
442     Some windows (desktops, docks, splash screens) have special rules applied
443     to them in a number of places regarding focus or user interaction.
444   */
445   inline bool normal() const {
446     return ! (_type == Type_Desktop || _type == Type_Dock ||
447               _type == Type_Splash);
448   }
449   
450   //! Returns the desktop on which the window resides
451   /*!
452     This value is a 0-based index.<br>
453     A value of 0xffffffff indicates that the window exists on all desktops.
454   */
455   inline long desktop() const { return _desktop; }
456   //! Returns the window's title
457   inline const otk::ustring &title() const { return _title; }
458   //! Returns the window's title when it is iconified
459   inline const otk::ustring &iconTitle() const { return _title; }
460   //! Returns the application's name to whom the window belongs
461   inline const std::string &appName() const { return _app_name; }
462   //! Returns the class of the window
463   inline const std::string &appClass() const { return _app_class; }
464   //! Returns the program-specified role of the window
465   inline const std::string &role() const { return _role; }
466   //! Returns if the window can be focused
467   /*!
468     @return true if the window can receive focus; otherwise, false
469   */
470   inline bool canFocus() const { return _can_focus; }
471   //! Returns if the window has indicated that it needs urgent attention
472   inline bool urgent() const { return _urgent; }
473   //! Returns if the window wants to be notified when it receives focus
474   inline bool focusNotify() const { return _focus_notify; }
475   //! Returns if the window uses the Shape extension
476   inline bool shaped() const { return _shaped; }
477   //! Returns the window's gravity
478   /*!
479     This value determines where to place the decorated window in relation to
480     its position without decorations.<br>
481     One of: NorthWestGravity, SouthWestGravity, EastGravity, ...,
482     SouthGravity, StaticGravity, ForgetGravity
483   */
484   inline int gravity() const { return _gravity; }
485   //! Returns if the application requested the initial position for the window
486   /*!
487     If the application did not request a position (this function returns false)
488     then the window should be placed intelligently by the window manager
489     initially
490   */
491   inline bool positionRequested() const { return _positioned; }
492   //! Returns the decorations that the client window wishes to be displayed on
493   //! it
494   inline DecorationFlags decorations() const { return _decorations; }
495   //! Returns the functions that the user can perform on the window
496   inline FunctionFlags funtions() const { return _functions; }
497
498   //! Return the client this window is transient for
499   inline Client *transientFor() const { return _transient_for; }
500
501   //! Returns if the window is modal
502   /*!
503     If the window is modal, then no other windows that it is related to can get
504     focus while it exists/remains modal.
505   */
506   inline bool modal() const { return _modal; }
507   //! Returns if the window is shaded
508   /*!
509     When the window is shaded, only its titlebar is visible.
510   */
511   inline bool shaded() const { return _shaded; }
512   //! Returns if the window is in fullscreen mode
513   inline bool fullscreen() const { return _fullscreen; }
514   //! Returns if the window is iconified
515   /*!
516     When the window is iconified, it is not visible at all (except in iconbars/
517     panels/etc that want to show lists of iconified windows
518   */
519   inline bool iconic() const { return _iconic; }
520   //! Returns if the window is maximized vertically
521   inline bool maxVert() const { return _max_vert; }
522   //! Returns if the window is maximized horizontally
523   inline bool maxHorz() const { return _max_horz; }
524   //! Returns the window's stacking layer
525   inline StackLayer layer() const { return _layer; }
526
527   //! Applies the states requested when the window mapped
528   /*!
529     This should be called only once, during the window mapping process. It
530     applies things like maximized, and fullscreen.
531   */
532   void applyStartupState();
533   
534   //! Removes or reapplies the client's border to its window
535   /*!
536     Used when managing and unmanaging a window.
537     @param addborder true if adding the border to the client; false if removing
538                      from the client
539   */
540   void toggleClientBorder(bool addborder);
541
542   //! Returns the position and size of the client relative to the root window
543   inline const otk::Rect &area() const { return _area; }
544
545   //! Returns the client's strut definition
546   inline const otk::Strut &strut() const { return _strut; }
547
548   //! Move the client window
549   /*!
550     @param x The X coordinate to move to.
551     @param y The Y coordinate to move to.
552   */
553   void move(int x, int y);
554   
555   //! Resizes the client window, anchoring it in a given corner
556   /*!
557     This also maintains things like the client's minsize, and size increments.
558     @param anchor The corner to keep in the same position when resizing.
559     @param w The width component of the new size for the client.
560     @param h The height component of the new size for the client.
561   */
562   void resize(Corner anchor, int w, int h);
563
564   //! Attempt to focus the client window
565   bool focus() const;
566
567   //! Remove focus from the client window
568   void unfocus() const;
569
570   virtual void focusHandler(const XFocusChangeEvent &e);
571   virtual void unfocusHandler(const XFocusChangeEvent &e);
572   virtual void propertyHandler(const XPropertyEvent &e);
573   virtual void clientMessageHandler(const XClientMessageEvent &e);
574   virtual void configureRequestHandler(const XConfigureRequestEvent &e);
575   virtual void unmapHandler(const XUnmapEvent &e);
576   virtual void destroyHandler(const XDestroyWindowEvent &e);
577   virtual void reparentHandler(const XReparentEvent &e);
578 #if defined(SHAPE)
579   virtual void shapeHandler(const XShapeEvent &e);
580 #endif // SHAPE 
581 };
582
583 }
584
585 #endif // __client_hh