-//
-// Map of all known GLUT cursor names
-//
-struct {
- const char * name;
- int cursor;
-} mouse_cursor_map[] = {
- { "right-arrow", GLUT_CURSOR_RIGHT_ARROW },
- { "left-arrow", GLUT_CURSOR_LEFT_ARROW },
- { "info", GLUT_CURSOR_INFO },
- { "destroy", GLUT_CURSOR_DESTROY },
- { "help", GLUT_CURSOR_HELP },
- { "cycle", GLUT_CURSOR_CYCLE },
- { "spray", GLUT_CURSOR_SPRAY },
- { "wait", GLUT_CURSOR_WAIT },
- { "text", GLUT_CURSOR_TEXT },
- { "crosshair", GLUT_CURSOR_CROSSHAIR },
- { "up-down", GLUT_CURSOR_UP_DOWN },
- { "left-right", GLUT_CURSOR_LEFT_RIGHT },
- { "top-side", GLUT_CURSOR_TOP_SIDE },
- { "bottom-side", GLUT_CURSOR_BOTTOM_SIDE },
- { "left-side", GLUT_CURSOR_LEFT_SIDE },
- { "right-side", GLUT_CURSOR_RIGHT_SIDE },
- { "top-left-corner", GLUT_CURSOR_TOP_LEFT_CORNER },
- { "top-right-corner", GLUT_CURSOR_TOP_RIGHT_CORNER },
- { "bottom-right-corner", GLUT_CURSOR_BOTTOM_RIGHT_CORNER },
- { "bottom-left-corner", GLUT_CURSOR_BOTTOM_LEFT_CORNER },
- { "inherit", GLUT_CURSOR_INHERIT },
- { "none", GLUT_CURSOR_NONE },
- { "full-crosshair", GLUT_CURSOR_FULL_CROSSHAIR },
- { 0, 0 }
-};
-
-
-
-void
-FGInput::_init_mouse ()
-{
- SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
-
- SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
- if (mouse_nodes == 0) {
- SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
- mouse_nodes = fgGetNode("/input/mice", true);
- }
-
- int j;
- for (int i = 0; i < MAX_MICE; i++) {
- SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
- mouse &m = _mouse_bindings[i];
-
- // Grab node pointers
- char buf[64];
- sprintf(buf, "/devices/status/mice/mouse[%d]/mode", i);
- m.mode_node = fgGetNode(buf);
- if (m.mode_node == NULL) {
- m.mode_node = fgGetNode(buf, true);
- m.mode_node->setIntValue(0);
- }
- for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
- sprintf(buf, "/devices/status/mice/mouse[%d]/button[%d]", i, j);
- m.mouse_button_nodes[j] = fgGetNode(buf, true);
- m.mouse_button_nodes[j]->setBoolValue(false);
- }
-
- // Read all the modes
- m.nModes = mouse_node->getIntValue("mode-count", 1);
- m.modes = new mouse_mode[m.nModes];
-
- for (int j = 0; j < m.nModes; j++) {
- int k;
-
- // Read the mouse cursor for this mode
- SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
- const char * cursor_name =
- mode_node->getStringValue("cursor", "inherit");
- m.modes[j].cursor = GLUT_CURSOR_INHERIT;
- for (k = 0; mouse_cursor_map[k].name != 0; k++) {
- if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
- m.modes[j].cursor = mouse_cursor_map[k].cursor;
- break;
- }
- }
-
- // Read other properties for this mode
- m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
- m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
-
- // Read the button bindings for this mode
- m.modes[j].buttons = new button[MAX_MOUSE_BUTTONS];
- char buf[32];
- for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
- sprintf(buf, "mouse button %d", k);
- SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
- _init_button(mode_node->getChild("button", k),
- m.modes[j].buttons[k],
- buf);
- }
-
- // Read the axis bindings for this mode
- _read_bindings(mode_node->getChild("x-axis", 0, true),
- m.modes[j].x_bindings,
- FG_MOD_NONE);
- _read_bindings(mode_node->getChild("y-axis", 0, true),
- m.modes[j].y_bindings,
- FG_MOD_NONE);
- }
- }
-}
-
-
-void
-FGInput::_init_button (const SGPropertyNode * node,
- button &b,
- const string name)
-{
- if (node == 0) {
- SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name);
- } else {
- b.is_repeatable = node->getBoolValue("repeatable", b.is_repeatable);
-
- // Get the bindings for the button
- _read_bindings(node, b.bindings, FG_MOD_NONE);
- }
-}
-
-
-void
-FGInput::_update_keyboard ()
-{
- // no-op
-}
-
-
-void
-FGInput::_update_joystick (double dt)
-{
- int modifiers = FG_MOD_NONE; // FIXME: any way to get the real ones?
- int buttons;
- // float js_val, diff;
- float axis_values[MAX_JOYSTICK_AXES];
-
- int i;
- int j;
-
- for ( i = 0; i < MAX_JOYSTICKS; i++) {
-
- jsJoystick * js = _joystick_bindings[i].js;
- if (js == 0 || js->notWorking())
- continue;
-
- js->read(&buttons, axis_values);
-
-
- // Fire bindings for the axes.
- for ( j = 0; j < _joystick_bindings[i].naxes; j++) {
- axis &a = _joystick_bindings[i].axes[j];
-
- // Do nothing if the axis position
- // is unchanged; only a change in
- // position fires the bindings.
- if (fabs(axis_values[j] - a.last_value) > a.tolerance) {
-// SG_LOG(SG_INPUT, SG_DEBUG, "Axis " << j << " has moved");
- SGPropertyNode node;
- a.last_value = axis_values[j];
-// SG_LOG(SG_INPUT, SG_DEBUG, "There are "
-// << a.bindings[modifiers].size() << " bindings");
- for (unsigned int k = 0; k < a.bindings[modifiers].size(); k++)
- a.bindings[modifiers][k]->fire(axis_values[j]);
- }
-
- // do we have to emulate axis buttons?
- a.last_dt += dt;
- if(a.last_dt >= a.interval_sec) {
- if (a.low.bindings[modifiers].size())
- _update_button(_joystick_bindings[i].axes[j].low,
- modifiers,
- axis_values[j] < a.low_threshold,
- -1, -1);
-
- if (a.high.bindings[modifiers].size())
- _update_button(_joystick_bindings[i].axes[j].high,
- modifiers,
- axis_values[j] > a.high_threshold,
- -1, -1);
- a.last_dt -= a.interval_sec;
- }
- }
-
- // Fire bindings for the buttons.
- for (j = 0; j < _joystick_bindings[i].nbuttons; j++) {
- button &b = _joystick_bindings[i].buttons[j];
- b.last_dt += dt;
- if(b.last_dt >= b.interval_sec) {
- _update_button(_joystick_bindings[i].buttons[j],
- modifiers,
- (buttons & (1 << j)) > 0,
- -1, -1);
- b.last_dt -= b.interval_sec;
- }
- }
- }
-}
-
-void
-FGInput::_update_mouse ()
-{
- mouse &m = _mouse_bindings[0];
- int mode = m.mode_node->getIntValue();
- if (mode != m.current_mode) {
- m.current_mode = mode;
- if (mode >= 0 && mode < m.nModes) {
- glutSetCursor(m.modes[mode].cursor);
- m.x = fgGetInt("/sim/startup/xsize", 800) / 2;
- m.y = fgGetInt("/sim/startup/ysize", 600) / 2;
- glutWarpPointer(m.x, m.y);
- } else {
- SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
- glutSetCursor(GLUT_CURSOR_INHERIT);
- }
- }
-}
-
-void
-FGInput::_update_button (button &b, int modifiers, bool pressed,
- int x, int y)
-{
- if (pressed) {
- // The press event may be repeated.
- if (!b.last_state || b.is_repeatable) {
- SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
- for (unsigned int k = 0; k < b.bindings[modifiers].size(); k++)
- b.bindings[modifiers][k]->fire(x, y);
- }
- } else {
- // The release event is never repeated.
- if (b.last_state) {
- SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
- for (unsigned int k = 0; k < b.bindings[modifiers|FG_MOD_UP].size(); k++)
- b.bindings[modifiers|FG_MOD_UP][k]->fire(x, y);
- }
- }
-
- b.last_state = pressed;
-}
-
-
-void
-FGInput::_read_bindings (const SGPropertyNode * node,
- binding_list_t * binding_list,
- int modifiers)
-{
- SG_LOG(SG_INPUT, SG_DEBUG, "Reading all bindings");
- vector<SGPropertyNode_ptr> bindings = node->getChildren("binding");
- for (unsigned int i = 0; i < bindings.size(); i++) {
- SG_LOG(SG_INPUT, SG_DEBUG, "Reading binding "
- << bindings[i]->getStringValue("command"));
- binding_list[modifiers].push_back(new FGBinding(bindings[i]));
- }
-
- // Read nested bindings for modifiers
- if (node->getChild("mod-up") != 0)
- _read_bindings(node->getChild("mod-up"), binding_list,
- modifiers|FG_MOD_UP);
-
- if (node->getChild("mod-shift") != 0)
- _read_bindings(node->getChild("mod-shift"), binding_list,
- modifiers|FG_MOD_SHIFT);
-
- if (node->getChild("mod-ctrl") != 0)
- _read_bindings(node->getChild("mod-ctrl"), binding_list,
- modifiers|FG_MOD_CTRL);
-
- if (node->getChild("mod-alt") != 0)
- _read_bindings(node->getChild("mod-alt"), binding_list,
- modifiers|FG_MOD_ALT);
-}
-
-
-const vector<FGBinding *> &
-FGInput::_find_key_bindings (unsigned int k, int modifiers)
-{
- unsigned char kc = (unsigned char)k;
- button &b = _key_bindings[k];
-
- // Try it straight, first.
- if (b.bindings[modifiers].size() > 0)
- return b.bindings[modifiers];
-
- // Alt-Gr is CTRL+ALT
- else if (modifiers&(FG_MOD_CTRL|FG_MOD_ALT))
- return _find_key_bindings(k, modifiers&~(FG_MOD_CTRL|FG_MOD_ALT));
-
- // Try removing the control modifier
- // for control keys.
- else if ((modifiers&FG_MOD_CTRL) && iscntrl(kc))
- return _find_key_bindings(k, modifiers&~FG_MOD_CTRL);
-
- // Try removing shift modifier
- // for upper case or any punctuation
- // (since different keyboards will
- // shift different punctuation types)
- else if ((modifiers&FG_MOD_SHIFT) && (isupper(kc) || ispunct(kc)))
- return _find_key_bindings(k, modifiers&~FG_MOD_SHIFT);
-
- // Try removing alt modifier for
- // high-bit characters.
- else if ((modifiers&FG_MOD_ALT) && k >= 128 && k < 256)
- return _find_key_bindings(k, modifiers&~FG_MOD_ALT);
-
- // Give up and return the empty vector.
- else
- return b.bindings[modifiers];
-}
-
-
-\f
-////////////////////////////////////////////////////////////////////////
-// Implementation of FGInput::button.
-////////////////////////////////////////////////////////////////////////
-
-FGInput::button::button ()
- : is_repeatable(false),
- last_state(-1)
-{
-}
-
-FGInput::button::~button ()
-{
- // FIXME: memory leak
-// for (int i = 0; i < FG_MOD_MAX; i++)
-// for (int j = 0; i < bindings[i].size(); j++)
-// delete bindings[i][j];
-}
-
-
-\f
-////////////////////////////////////////////////////////////////////////
-// Implementation of FGInput::axis.
-////////////////////////////////////////////////////////////////////////
-
-FGInput::axis::axis ()
- : last_value(9999999),
- tolerance(0.002),
- low_threshold(-0.9),
- high_threshold(0.9)
-{
-}
-
-FGInput::axis::~axis ()
-{
-// for (int i = 0; i < FG_MOD_MAX; i++)
-// for (int j = 0; i < bindings[i].size(); j++)
-// delete bindings[i][j];
-}
-
-
-\f
-////////////////////////////////////////////////////////////////////////
-// Implementation of FGInput::joystick.
-////////////////////////////////////////////////////////////////////////
-
-FGInput::joystick::joystick ()
-{
-}
-
-FGInput::joystick::~joystick ()
-{
-// delete js;
- delete[] axes;
- delete[] buttons;
-}
-
-
-\f
-////////////////////////////////////////////////////////////////////////
-// Implementation of FGInput::mouse_mode
-////////////////////////////////////////////////////////////////////////
-
-FGInput::mouse_mode::mouse_mode ()
- : cursor(GLUT_CURSOR_INHERIT),
- constrained(false),
- pass_through(false),
- buttons(0)
-{
-}
-
-FGInput::mouse_mode::~mouse_mode ()
-{
- // FIXME: memory leak
-// for (int i = 0; i < FG_MOD_MAX; i++) {
-// int j;
-// for (j = 0; i < x_bindings[i].size(); j++)
-// delete bindings[i][j];
-// for (j = 0; j < y_bindings[i].size(); j++)
-// delete bindings[i][j];
-// }
- delete [] buttons;
-}
-
-
-\f
-////////////////////////////////////////////////////////////////////////
-// Implementation of FGInput::mouse
-////////////////////////////////////////////////////////////////////////
-
-FGInput::mouse::mouse ()
- : x(-1),
- y(-1),
- nModes(1),
- current_mode(0),
- modes(0)
-{
-}
-
-FGInput::mouse::~mouse ()
-{
- delete [] modes;
-}
-
-
-\f
-////////////////////////////////////////////////////////////////////////
-// Implementation of GLUT callbacks.
-////////////////////////////////////////////////////////////////////////
-
-
-/**
- * Construct the modifiers.
- */
-static inline int get_mods ()
-{
- int glut_modifiers = glutGetModifiers();
- int modifiers = 0;
-
- if (glut_modifiers & GLUT_ACTIVE_SHIFT)
- modifiers |= FGInput::FG_MOD_SHIFT;
- if (glut_modifiers & GLUT_ACTIVE_CTRL)
- modifiers |= FGInput::FG_MOD_CTRL;
- if (glut_modifiers & GLUT_ACTIVE_ALT)
- modifiers |= FGInput::FG_MOD_ALT;
-
- return modifiers;
-}
-
-
-\f
-////////////////////////////////////////////////////////////////////////
-// GLUT C callbacks.
-////////////////////////////////////////////////////////////////////////
-
-void
-GLUTkey(unsigned char k, int x, int y)
-{
- // Give PUI a chance to grab it first.
- if (!puKeyboard(k, PU_DOWN)) {
- if (default_input != 0)
- default_input->doKey(k, get_mods(), x, y);
- }
-}
-
-void
-GLUTkeyup(unsigned char k, int x, int y)
-{
- if (default_input != 0)
- default_input->doKey(k, get_mods()|FGInput::FG_MOD_UP, x, y);
-}
-
-void
-GLUTspecialkey(int k, int x, int y)
-{
- // Give PUI a chance to grab it first.
- if (!puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN)) {
- if (default_input != 0)
- default_input->doKey(k + 256, get_mods(), x, y);
- }
-}
-
-void
-GLUTspecialkeyup(int k, int x, int y)
-{
- if (default_input != 0)
- default_input->doKey(k + 256, get_mods()|FGInput::FG_MOD_UP, x, y);
-}
-
-void
-GLUTmouse (int button, int updown, int x, int y)
-{
- if (default_input != 0)
- default_input->doMouseClick(button, updown, x, y);
-}
-
-void
-GLUTmotion (int x, int y)