]> icculus.org git repositories - mikachu/openbox.git/blob - src/openbox.hh
dont unset focused client if it is still/already set, that causes bugs!
[mikachu/openbox.git] / src / openbox.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 #ifndef   __openbox_hh
3 #define   __openbox_hh
4
5 /*! @file openbox.hh
6   @brief The main class for the Openbox window manager
7 */
8
9 extern "C" {
10 #include <X11/Xlib.h>
11 }
12
13 #include <string>
14 #include <vector>
15 #include <map>
16
17 #include "otk/screeninfo.hh"
18 #include "otk/timerqueuemanager.hh"
19 #include "otk/property.hh"
20 #include "otk/configuration.hh"
21 #include "otk/eventdispatcher.hh"
22 #include "otk/eventhandler.hh"
23
24 namespace ob {
25
26 class OBScreen;
27 class OBClient;
28 class OBActions;
29 class OBBindings;
30
31 //! Mouse cursors used throughout Openbox
32 struct Cursors {
33   Cursor session;  //!< The default mouse cursor
34   Cursor move;     //!< For moving a window
35   Cursor ll_angle; //!< For resizing the bottom left corner of a window
36   Cursor lr_angle; //!< For resizing the bottom right corner of a window
37   Cursor ul_angle; //!< For resizing the top left corner of a window
38   Cursor ur_angle; //!< For resizing the right corner of a window
39 };
40
41
42 //! The main class for the Openbox window manager
43 /*!
44   Only a single instance of the Openbox class may be used in the application. A
45   pointer to this instance is held in the Openbox::instance static member
46   variable.
47   Instantiation of this class begins the window manager. After instantiation,
48   the Openbox::eventLoop function should be called. The eventLoop method does
49   not exit until the window manager is ready to be destroyed. Destruction of
50   the Openbox class instance will shutdown the window manager.
51 */
52 class Openbox : public otk::OtkEventDispatcher, public otk::OtkEventHandler
53 {
54 public:
55   //! The single instance of the Openbox class for the application
56   /*!
57     Since this variable is globally available in the application, the Openbox
58     class does not need to be passed around to any of the other classes.
59   */
60   static Openbox *instance;
61
62   //! The posible running states of the window manager
63   enum RunState {
64     State_Starting, //!< The window manager is starting up (being created)
65     State_Normal,   //!< The window manager is running in its normal state
66     State_Exiting   //!< The window manager is exiting (being destroyed)
67   };
68
69   //! A map for looking up a specific client class from the window id
70   typedef std::map<Window, OBClient *> ClientMap;
71
72   //! A list of OBScreen classes
73   typedef std::vector<OBScreen *> ScreenList;
74   
75 private:
76   // stuff that can be passed on the command line
77   //! Path to the config file to use/in use
78   /*!
79     Defaults to $(HOME)/.openbox/rc3
80   */
81   std::string _rcfilepath;
82   //! Path to the menu file to use/in use
83   /*!
84     Defaults to $(HOME)/.openbox/menu3
85   */
86   std::string _menufilepath;
87   //! Path to the script file to execute on startup
88   /*!
89     Defaults to $(HOME)/.openbox/user.py
90   */
91   std::string _scriptfilepath;
92   //! The display requested by the user, or null to use the DISPLAY env var
93   char *_displayreq;
94   //! The value of argv, i.e. how this application was executed
95   char **_argv;
96
97   //! A list of all managed clients
98   ClientMap _clients;
99
100   //! A list of all the managed screens
101   ScreenList _screens;
102   
103   //! Manages all timers for the application
104   /*!
105     Use of the otk::OBTimerQueueManager::fire funtion in this object ensures
106     that all timers fire when their times elapse.
107   */
108   otk::OBTimerQueueManager _timermanager;
109
110   //! Cached atoms on the display
111   /*!
112     This is a pointer because the OBProperty class uses otk::OBDisplay::display
113     in its constructor, so, it needs to be initialized <b>after</b> the display
114     is initialized in this class' constructor.
115   */
116   otk::OBProperty *_property;
117
118   //! The action interface through which all user-available actions occur
119   OBActions *_actions;
120
121   //! The interface through which keys/buttons are grabbed and handled
122   OBBindings *_bindings;
123
124   //! Run the application in synchronous mode? (for debugging)
125   bool _sync;
126
127   //! The running state of the window manager
128   RunState _state;
129
130   //! Mouse cursors used throughout Openbox
131   Cursors _cursors;
132
133   //! When set to true, the Openbox::eventLoop function will stop and return
134   bool _shutdown;
135
136   //! When set to true, and Openbox is about to exit, it will spawn a new
137   //! process
138   bool _restart;
139
140   //! If this contains anything, a restart will try to execute the program in
141   //! this variable, and will fallback to reexec'ing itself if that fails
142   std::string _restart_prog;
143
144   //! The client with input focus
145   /*!
146     Updated by the clients themselves.
147   */
148   OBClient *_focused_client;
149
150   //! The screen with input focus
151   /*!
152     Updated by the clients when they update the Openbox::focused_client
153     property.
154   */
155   OBScreen *_focused_screen;
156   
157   //! Parses the command line used when executing this application
158   void parseCommandLine(int argv, char **argv);
159   //! Displays the version string to stdout
160   void showVersion();
161   //! Displays usage information and help to stdout
162   void showHelp();
163
164   //! Handles signal events for the application
165   static void signalHandler(int signal);
166
167 public:
168 #ifndef SWIG
169   //! Openbox constructor.
170   /*!
171     \param argc Number of command line arguments, as received in main()
172     \param argv The command line arguments, as received in main()
173   */
174   Openbox(int argc, char **argv);
175   //! Openbox destructor.
176   virtual ~Openbox();
177 #endif
178
179   //! Returns the state of the window manager (starting, exiting, etc)
180   inline RunState state() const { return _state; }
181
182   //! Returns the otk::OBTimerQueueManager for the application
183   /*!
184     All otk::OBTimer objects used in the application should be made to use this
185     otk::OBTimerQueueManager.
186   */
187   inline otk::OBTimerQueueManager *timerManager() { return &_timermanager; }
188
189   //! Returns the otk::OBProperty instance for the window manager
190   inline const otk::OBProperty *property() const { return _property; }
191
192   //! Returns the OBActions instance for the window manager
193   inline OBActions *actions() const { return _actions; }
194
195   //! Returns the OBBindings instance for the window manager
196   inline OBBindings *bindings() const { return _bindings; }
197
198   //! Returns a managed screen
199   inline OBScreen *screen(int num) {
200     assert(num >= 0); assert(num < (signed)_screens.size());
201     if (num >= screenCount())
202       return NULL;
203     return _screens[num];
204   }
205
206   //! Returns the number of managed screens
207   inline int screenCount() const {
208     return (signed)_screens.size();
209   }
210
211   //! Returns the mouse cursors used throughout Openbox
212   inline const Cursors &cursors() const { return _cursors; }
213
214 #ifndef SWIG
215   //! The main function of the Openbox class
216   /*!
217     This function should be called after instantiating the Openbox class.
218     It loops indefinately while handling all events for the application.
219     The Openbox::shutdown method will cause this function to exit.
220   */
221   void eventLoop();
222 #endif
223
224   //! Adds an OBClient to the client list for lookups
225   void addClient(Window window, OBClient *client);
226
227   //! Removes an OBClient from the client list for lookups
228   void removeClient(Window window);
229
230   //! Finds an OBClient based on its window id
231   OBClient *findClient(Window window);
232
233   //! The client with input focus
234   inline OBClient *focusedClient() { return _focused_client; }
235
236   //! Change the client which has focus.
237   /*!
238     This is called by the clients themselves when their focus state changes.
239   */
240   void setFocusedClient(OBClient *c);
241
242   //! The screen with input focus
243   inline OBScreen *focusedScreen() { return _focused_screen; }
244   
245   //! Requests that the window manager exit
246   /*!
247     Causes the Openbox::eventLoop function to stop looping, so that the window
248     manager can be destroyed.
249   */
250   inline void shutdown() { _shutdown = true; }
251
252   inline void restart(const std::string &bin = "") {
253     _shutdown = true; _restart = true; _restart_prog = bin;
254   }
255
256   //! Executes a command on a screen
257   void execute(int screen, const std::string &bin);
258 };
259
260 }
261
262 #endif // __openbox_hh