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., 675 Mass Ave, Cambridge, MA 02139, 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/commands.hxx>
39 #include <simgear/props/condition.hxx>
40 #include <simgear/props/props.hxx>
42 #include <Main/fg_os.hxx>
43 #include <Main/fg_props.hxx>
44 #include <Main/globals.hxx>
55 #if defined( UL_WIN32 )
56 #define TGT_PLATFORM "windows"
57 #elif defined ( UL_MAC_OSX )
58 #define TGT_PLATFORM "mac"
60 #define TGT_PLATFORM "unix"
65 ////////////////////////////////////////////////////////////////////////
66 // General binding support.
67 ////////////////////////////////////////////////////////////////////////
71 * An input binding of some sort.
73 * <p>This class represents a binding that can be assigned to a
74 * keyboard key, a joystick button or axis, or even a panel
77 class FGBinding : public SGConditional
82 * Default constructor.
88 * Convenience constructor.
90 * @param node The binding will be built from this node.
92 FGBinding (const SGPropertyNode * node);
98 virtual ~FGBinding () {}
102 * Get the command name.
104 * @return The string name of the command for this binding.
106 virtual const string &getCommandName () const { return _command_name; }
110 * Get the command itself.
112 * @return The command associated with this binding, or 0 if none
115 virtual SGCommandMgr::command_t getCommand () const { return _command; }
119 * Get the argument that will be passed to the command.
121 * @return A property node that will be passed to the command as its
122 * argument, or 0 if none was supplied.
124 virtual const SGPropertyNode * getArg () { return _arg; }
128 * Read a binding from a property node.
130 * @param node The property node containing the binding.
132 virtual void read (const SGPropertyNode * node);
138 virtual void fire () const;
142 * Fire a binding with a scaled movement (rather than absolute position).
144 virtual void fire (double offset, double max) const;
148 * Fire a binding with a setting (i.e. joystick axis).
150 * A double 'setting' property will be added to the arguments.
152 * @param setting The input setting, usually between -1.0 and 1.0.
154 virtual void fire (double setting) const;
159 FGBinding (const FGBinding &binding);
161 string _command_name;
162 mutable SGCommandMgr::command_t _command;
163 mutable SGPropertyNode_ptr _arg;
164 mutable SGPropertyNode_ptr _setting;
169 ////////////////////////////////////////////////////////////////////////
170 // General input mapping support.
171 ////////////////////////////////////////////////////////////////////////
175 * Generic input module.
177 * <p>This module is designed to handle input from multiple sources --
178 * keyboard, joystick, mouse, or even panel switches -- in a consistent
179 * way, and to allow users to rebind any of the actions at runtime.</p>
181 class FGInput : public SGSubsystem
185 * Default constructor.
195 // Implementation of SGSubsystem.
197 virtual void init ();
198 virtual void reinit ();
199 virtual void postinit ();
200 virtual void update (double dt);
201 virtual void suspend ();
202 virtual void resume ();
203 virtual bool is_suspended () const;
207 * Control whether this is the default module to receive events.
209 * The first input module created will set itself as the default
212 * @param status true if this should be the default module for
213 * events, false otherwise.
215 virtual void makeDefault (bool status = true);
219 * Handle a single keystroke.
221 * @param k The integer key code, see Main/fg_os.hxx
222 * @param modifiers Modifier keys pressed (bitfield).
223 * @param x The mouse x position at the time of keypress.
224 * @param y The mouse y position at the time of keypress.
226 virtual void doKey (int k, int modifiers, int x, int y);
230 * Handle a mouse click event.
232 * @param button The mouse button selected.
233 * @param updown Button status.
234 * @param x The X position of the mouse event, in screen coordinates.
235 * @param y The Y position of the mouse event, in screen coordinates.
237 virtual void doMouseClick (int button, int updown, int x, int y);
241 * Handle mouse motion.
243 * @param x The new mouse x position, in screen coordinates.
244 * @param y The new mouse y position, in screen coordinates.
246 virtual void doMouseMotion (int x, int y);
260 MAX_JOYSTICK_AXES = _JS_MAX_AXES,
261 MAX_JOYSTICK_BUTTONS = 32,
264 MAX_MOUSE_BUTTONS = 8
269 typedef vector<FGBinding *> binding_list_t;
272 * Settings for a key or button.
281 binding_list_t bindings[KEYMOD_MAX];
286 * Settings for a single joystick axis.
293 binding_list_t bindings[KEYMOD_MAX];
295 float high_threshold;
304 * Settings for a joystick.
308 virtual ~joystick ();
319 * Settings for a mouse mode.
323 virtual ~mouse_mode ();
328 binding_list_t x_bindings[KEYMOD_MAX];
329 binding_list_t y_bindings[KEYMOD_MAX];
334 * Settings for a mouse.
341 SGPropertyNode * mode_node;
342 SGPropertyNode * mouse_button_nodes[MAX_MOUSE_BUTTONS];
353 * Initialize key bindings.
355 void _init_keyboard ();
359 * Initialize joystick bindings.
361 void _init_joystick ();
365 * Scan directory recursively for "named joystick" configuration files,
366 * and read them into /input/joysticks/js-named[index]++.
368 void _scan_joystick_dir (SGPath *path, SGPropertyNode* node, int *index);
372 * Initialize mouse bindings.
378 * Initialize a single button.
380 inline void _init_button (const SGPropertyNode * node,
385 * Initialize nasal parts that had to wait for the nasal to get
388 void _postinit_joystick ();
391 * Update the keyboard.
393 void _update_keyboard ();
397 * Update the joystick.
399 void _update_joystick (double dt);
405 void _update_mouse (double dt);
409 * Update a single button.
411 inline void _update_button (button &b, int modifiers, bool pressed,
416 * Read bindings and modifiers.
418 void _read_bindings (const SGPropertyNode * node,
419 binding_list_t * binding_list,
423 * Look up the bindings for a key code.
425 const vector<FGBinding *> &_find_key_bindings (unsigned int k,
428 button _key_bindings[MAX_KEYS];
429 joystick _joystick_bindings[MAX_JOYSTICKS];
430 mouse _mouse_bindings[MAX_MICE];
432 SGPropertyNode *_which_joystick;