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, bool mainWindow, const osgGA::GUIEventAdapter*);
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);
155 MAX_JOYSTICK_AXES = _JS_MAX_AXES,
156 MAX_JOYSTICK_BUTTONS = 32,
159 MAX_MOUSE_BUTTONS = 8
164 typedef vector<SGSharedPtr<SGBinding> > binding_list_t;
167 * Settings for a key or button.
176 binding_list_t bindings[KEYMOD_MAX];
181 * Settings for a single joystick axis.
188 binding_list_t bindings[KEYMOD_MAX];
190 float high_threshold;
199 * Settings for a joystick.
203 virtual ~joystick ();
214 * Settings for a mouse mode.
218 virtual ~mouse_mode ();
223 binding_list_t x_bindings[KEYMOD_MAX];
224 binding_list_t y_bindings[KEYMOD_MAX];
229 * Settings for a mouse.
238 SGPropertyNode_ptr mode_node;
239 SGPropertyNode_ptr mouse_button_nodes[MAX_MOUSE_BUTTONS];
248 * Initialize joystick bindings.
250 void _init_joystick ();
254 * Scan directory recursively for "named joystick" configuration files,
255 * and read them into /input/joysticks/js-named[index]++.
257 void _scan_joystick_dir (SGPath *path, SGPropertyNode* node, int *index);
261 * Initialize mouse bindings.
267 * Initialize a single button.
269 inline void _init_button (const SGPropertyNode * node,
274 * Initialize key bindings, as well as those joystick parts that
275 * depend on a working Nasal subsystem.
277 void _postinit_keyboard ();
278 void _postinit_joystick ();
281 * Update the keyboard.
283 void _update_keyboard ();
287 * Update the joystick.
289 void _update_joystick (double dt);
295 void _update_mouse (double dt);
299 * Update a single button.
301 inline void _update_button (button &b, int modifiers, bool pressed,
306 * Read bindings and modifiers.
308 void _read_bindings (const SGPropertyNode * node,
309 binding_list_t * binding_list,
313 * Look up the bindings for a key code.
315 const binding_list_t& _find_key_bindings (unsigned int k,
318 button _key_bindings[MAX_KEYS];
319 joystick _joystick_bindings[MAX_JOYSTICKS];
320 mouse _mouse_bindings[MAX_MICE];
323 * Nasal module name/namespace.
328 * List of currently pressed mouse button events
330 std::map<int, std::list<SGSharedPtr<SGPickCallback> > > _activePickCallbacks;
333 * Key listener interface.
335 SGPropertyNode_ptr _key_event;