1 // input.hxx -- handle user input from various sources.
3 // Written by David Megginson, started May 2001.
5 // Copyright (C) 2001 David Megginson, david@megginson.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 # error This library requires C++
34 #include <simgear/compiler.h>
36 #include <simgear/misc/sg_path.hxx>
37 #include <simgear/structure/subsystem_mgr.hxx>
38 #include <simgear/structure/SGBinding.hxx>
39 #include <simgear/props/condition.hxx>
40 #include <simgear/props/props.hxx>
41 #include <simgear/scene/util/SGSceneUserData.hxx>
43 #include <Main/fg_os.hxx>
44 #include <Main/fg_props.hxx>
45 #include <Main/globals.hxx>
57 #if defined( UL_WIN32 )
58 #define TGT_PLATFORM "windows"
59 #elif defined ( UL_MAC_OSX )
60 #define TGT_PLATFORM "mac"
62 #define TGT_PLATFORM "unix"
67 ////////////////////////////////////////////////////////////////////////
68 // General input mapping support.
69 ////////////////////////////////////////////////////////////////////////
73 * Generic input module.
75 * <p>This module is designed to handle input from multiple sources --
76 * keyboard, joystick, mouse, or even panel switches -- in a consistent
77 * way, and to allow users to rebind any of the actions at runtime.</p>
79 class FGInput : public SGSubsystem
83 * Default constructor.
93 // Implementation of SGSubsystem.
96 virtual void reinit ();
97 virtual void postinit ();
99 virtual void unbind ();
100 virtual void update (double dt);
101 virtual void suspend ();
102 virtual void resume ();
103 virtual bool is_suspended () const;
107 * Control whether this is the default module to receive events.
109 * The first input module created will set itself as the default
112 * @param status true if this should be the default module for
113 * events, false otherwise.
115 virtual void makeDefault (bool status = true);
119 * Handle a single keystroke.
121 * @param k The integer key code, see Main/fg_os.hxx
122 * @param modifiers Modifier keys pressed (bitfield).
123 * @param x The mouse x position at the time of keypress.
124 * @param y The mouse y position at the time of keypress.
126 virtual void doKey (int k, int modifiers, int x, int y);
130 * Handle a mouse click event.
132 * @param button The mouse button selected.
133 * @param updown Button status.
134 * @param x The X position of the mouse event, in screen coordinates.
135 * @param y The Y position of the mouse event, in screen coordinates.
137 virtual void doMouseClick (int button, int updown, int x, int y);
141 * Handle mouse motion.
143 * @param x The new mouse x position, in screen coordinates.
144 * @param y The new mouse y position, in screen coordinates.
146 virtual void doMouseMotion (int x, int y);
160 MAX_JOYSTICK_AXES = _JS_MAX_AXES,
161 MAX_JOYSTICK_BUTTONS = 32,
164 MAX_MOUSE_BUTTONS = 8
169 typedef vector<SGSharedPtr<SGBinding> > binding_list_t;
172 * Settings for a key or button.
181 binding_list_t bindings[KEYMOD_MAX];
186 * Settings for a single joystick axis.
193 binding_list_t bindings[KEYMOD_MAX];
195 float high_threshold;
204 * Settings for a joystick.
208 virtual ~joystick ();
219 * Settings for a mouse mode.
223 virtual ~mouse_mode ();
228 binding_list_t x_bindings[KEYMOD_MAX];
229 binding_list_t y_bindings[KEYMOD_MAX];
234 * Settings for a mouse.
241 SGPropertyNode_ptr mode_node;
242 SGPropertyNode_ptr mouse_button_nodes[MAX_MOUSE_BUTTONS];
253 * Initialize key bindings.
255 void _init_keyboard ();
259 * Initialize joystick bindings.
261 void _init_joystick ();
265 * Scan directory recursively for "named joystick" configuration files,
266 * and read them into /input/joysticks/js-named[index]++.
268 void _scan_joystick_dir (SGPath *path, SGPropertyNode* node, int *index);
272 * Initialize mouse bindings.
278 * Initialize a single button.
280 inline void _init_button (const SGPropertyNode * node,
285 * Initialize nasal parts that had to wait for the nasal to get
288 void _postinit_joystick ();
291 * Update the keyboard.
293 void _update_keyboard ();
297 * Update the joystick.
299 void _update_joystick (double dt);
305 void _update_mouse (double dt);
309 * Update a single button.
311 inline void _update_button (button &b, int modifiers, bool pressed,
316 * Read bindings and modifiers.
318 void _read_bindings (const SGPropertyNode * node,
319 binding_list_t * binding_list,
323 * Look up the bindings for a key code.
325 const binding_list_t& _find_key_bindings (unsigned int k,
328 button _key_bindings[MAX_KEYS];
329 joystick _joystick_bindings[MAX_JOYSTICKS];
330 mouse _mouse_bindings[MAX_MICE];
333 * Nasal module name/namespace.
338 * List of currently pressed mouse button events
340 std::map<int, std::list<SGSharedPtr<SGPickCallback> > > _activePickCallbacks;