annotate src/gtk/joypadconfig.cpp @ 2:3549bbe597ed

adding makefile.am
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:47 -0600
parents f9f4f1b99eed
children
rev   line source
rlm@1 1 // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
rlm@1 2 // Copyright (C) 1999-2003 Forgotten
rlm@1 3 // Copyright (C) 2004 Forgotten and the VBA development team
rlm@1 4
rlm@1 5 // This program is free software; you can redistribute it and/or modify
rlm@1 6 // it under the terms of the GNU General Public License as published by
rlm@1 7 // the Free Software Foundation; either version 2, or(at your option)
rlm@1 8 // any later version.
rlm@1 9 //
rlm@1 10 // This program is distributed in the hope that it will be useful,
rlm@1 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
rlm@1 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
rlm@1 13 // GNU General Public License for more details.
rlm@1 14 //
rlm@1 15 // You should have received a copy of the GNU General Public License
rlm@1 16 // along with this program; if not, write to the Free Software Foundation,
rlm@1 17 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
rlm@1 18
rlm@1 19 #include "joypadconfig.h"
rlm@1 20
rlm@1 21 #include <string.h>
rlm@1 22
rlm@1 23 #include "intl.h"
rlm@1 24
rlm@1 25 namespace VBA
rlm@1 26 {
rlm@1 27
rlm@1 28 guint * JoypadConfig::puiAt(int _iIndex)
rlm@1 29 {
rlm@1 30 guint * puiMember;
rlm@1 31
rlm@1 32 switch (_iIndex)
rlm@1 33 {
rlm@1 34 case 0:
rlm@1 35 puiMember = &m_uiUp;
rlm@1 36 break;
rlm@1 37 case 1:
rlm@1 38 puiMember = &m_uiDown;
rlm@1 39 break;
rlm@1 40 case 2:
rlm@1 41 puiMember = &m_uiLeft;
rlm@1 42 break;
rlm@1 43 case 3:
rlm@1 44 puiMember = &m_uiRight;
rlm@1 45 break;
rlm@1 46 case 4:
rlm@1 47 puiMember = &m_uiA;
rlm@1 48 break;
rlm@1 49 case 5:
rlm@1 50 puiMember = &m_uiB;
rlm@1 51 break;
rlm@1 52 case 6:
rlm@1 53 puiMember = &m_uiL;
rlm@1 54 break;
rlm@1 55 case 7:
rlm@1 56 puiMember = &m_uiR;
rlm@1 57 break;
rlm@1 58 case 8:
rlm@1 59 puiMember = &m_uiSelect;
rlm@1 60 break;
rlm@1 61 case 9:
rlm@1 62 puiMember = &m_uiStart;
rlm@1 63 break;
rlm@1 64 case 10:
rlm@1 65 puiMember = &m_uiSpeed;
rlm@1 66 break;
rlm@1 67 case 11:
rlm@1 68 puiMember = &m_uiCapture;
rlm@1 69 break;
rlm@1 70 default:
rlm@1 71 puiMember = NULL;
rlm@1 72 }
rlm@1 73
rlm@1 74 return puiMember;
rlm@1 75 }
rlm@1 76
rlm@1 77 int JoypadConfig::iFind(guint _uiKeycode)
rlm@1 78 {
rlm@1 79 for (guint i = 0; i < 12; i++)
rlm@1 80 {
rlm@1 81 if (*puiAt(i) == _uiKeycode)
rlm@1 82 {
rlm@1 83 return i;
rlm@1 84 }
rlm@1 85 }
rlm@1 86
rlm@1 87 return -1;
rlm@1 88 }
rlm@1 89
rlm@1 90 void JoypadConfig::vSetDefault()
rlm@1 91 {
rlm@1 92 guint auiKeyval[] =
rlm@1 93 {
rlm@1 94 GDK_Up, GDK_Down, GDK_Left, GDK_Right,
rlm@1 95 GDK_z, GDK_x, GDK_a, GDK_s,
rlm@1 96 GDK_BackSpace, GDK_Return,
rlm@1 97 GDK_space, GDK_F12
rlm@1 98 };
rlm@1 99
rlm@1 100 for (guint i = 0; i < G_N_ELEMENTS(auiKeyval); i++)
rlm@1 101 {
rlm@1 102 GdkKeymapKey * pstKeys;
rlm@1 103 int iKeys;
rlm@1 104
rlm@1 105 if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(),
rlm@1 106 auiKeyval[i],
rlm@1 107 &pstKeys,
rlm@1 108 &iKeys))
rlm@1 109 {
rlm@1 110 *puiAt(i) = pstKeys[0].keycode;
rlm@1 111 g_free(pstKeys);
rlm@1 112 }
rlm@1 113 else
rlm@1 114 {
rlm@1 115 *puiAt(i) = 0;
rlm@1 116 }
rlm@1 117 }
rlm@1 118 }
rlm@1 119
rlm@1 120 Keymap * JoypadConfig::poCreateKeymap() const
rlm@1 121 {
rlm@1 122 Keymap * poKeymap = new Keymap();
rlm@1 123
rlm@1 124 poKeymap->vRegister(m_uiUp, KeyUp );
rlm@1 125 poKeymap->vRegister(m_uiDown, KeyDown );
rlm@1 126 poKeymap->vRegister(m_uiLeft, KeyLeft );
rlm@1 127 poKeymap->vRegister(m_uiRight, KeyRight );
rlm@1 128 poKeymap->vRegister(m_uiA, KeyA );
rlm@1 129 poKeymap->vRegister(m_uiB, KeyB );
rlm@1 130 poKeymap->vRegister(m_uiL, KeyL );
rlm@1 131 poKeymap->vRegister(m_uiR, KeyR );
rlm@1 132 poKeymap->vRegister(m_uiSelect, KeySelect );
rlm@1 133 poKeymap->vRegister(m_uiStart, KeyStart );
rlm@1 134 poKeymap->vRegister(m_uiSpeed, KeySpeed );
rlm@1 135 poKeymap->vRegister(m_uiCapture, KeyCapture );
rlm@1 136
rlm@1 137 return poKeymap;
rlm@1 138 }
rlm@1 139
rlm@1 140 JoypadConfigDialog::JoypadConfigDialog(GtkDialog * _pstDialog,
rlm@1 141 const Glib::RefPtr<Gnome::Glade::Xml> & _poXml) :
rlm@1 142 Gtk::Dialog(_pstDialog)
rlm@1 143 {
rlm@1 144 m_puiCurrentKeyCode = NULL;
rlm@1 145
rlm@1 146 memset(&m_oConfig, 0, sizeof(m_oConfig));
rlm@1 147
rlm@1 148 m_poOkButton = dynamic_cast<Gtk::Button *>(_poXml->get_widget("JoypadOkButton"));
rlm@1 149
rlm@1 150 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadUpEntry")));
rlm@1 151 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadDownEntry")));
rlm@1 152 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadLeftEntry")));
rlm@1 153 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadRightEntry")));
rlm@1 154 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadAEntry")));
rlm@1 155 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadBEntry")));
rlm@1 156 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadLEntry")));
rlm@1 157 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadREntry")));
rlm@1 158 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadSelectEntry")));
rlm@1 159 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadStartEntry")));
rlm@1 160 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadSpeedEntry")));
rlm@1 161 m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadCaptureEntry")));
rlm@1 162
rlm@1 163 for (guint i = 0; i < m_oEntries.size(); i++)
rlm@1 164 {
rlm@1 165 Gtk::Entry * poEntry = m_oEntries[i];
rlm@1 166
rlm@1 167 poEntry->signal_focus_in_event().connect(SigC::bind<guint>(
rlm@1 168 SigC::slot(*this, &JoypadConfigDialog::bOnEntryFocusIn),
rlm@1 169 i));
rlm@1 170 poEntry->signal_focus_out_event().connect(SigC::slot(*this, &JoypadConfigDialog::bOnEntryFocusOut));
rlm@1 171 }
rlm@1 172
rlm@1 173 vUpdateEntries();
rlm@1 174 }
rlm@1 175
rlm@1 176 JoypadConfigDialog::~JoypadConfigDialog()
rlm@1 177 {
rlm@1 178 }
rlm@1 179
rlm@1 180 void JoypadConfigDialog::vSetConfig(const JoypadConfig & _roConfig)
rlm@1 181 {
rlm@1 182 m_oConfig = _roConfig;
rlm@1 183 vUpdateEntries();
rlm@1 184 }
rlm@1 185
rlm@1 186 void JoypadConfigDialog::vUpdateEntries()
rlm@1 187 {
rlm@1 188 for (guint i = 0; i < m_oEntries.size(); i++)
rlm@1 189 {
rlm@1 190 guint uiKeyval = 0;
rlm@1 191 gdk_keymap_translate_keyboard_state(gdk_keymap_get_default(),
rlm@1 192 *m_oConfig.puiAt(i),
rlm@1 193 (GdkModifierType)0,
rlm@1 194 0,
rlm@1 195 &uiKeyval,
rlm@1 196 NULL,
rlm@1 197 NULL,
rlm@1 198 NULL);
rlm@1 199 const char * csName = gdk_keyval_name(uiKeyval);
rlm@1 200 if (csName == NULL)
rlm@1 201 {
rlm@1 202 m_oEntries[i]->set_text(_("<Undefined>"));
rlm@1 203 }
rlm@1 204 else
rlm@1 205 {
rlm@1 206 m_oEntries[i]->set_text(csName);
rlm@1 207 }
rlm@1 208 }
rlm@1 209 }
rlm@1 210
rlm@1 211 bool JoypadConfigDialog::bOnEntryFocusIn(GdkEventFocus * _pstEvent,
rlm@1 212 guint _uiEntry)
rlm@1 213 {
rlm@1 214 m_uiCurrentEntry = _uiEntry;
rlm@1 215 m_puiCurrentKeyCode = m_oConfig.puiAt(_uiEntry);
rlm@1 216
rlm@1 217 return false;
rlm@1 218 }
rlm@1 219
rlm@1 220 bool JoypadConfigDialog::bOnEntryFocusOut(GdkEventFocus * _pstEvent)
rlm@1 221 {
rlm@1 222 m_puiCurrentKeyCode = NULL;
rlm@1 223
rlm@1 224 return false;
rlm@1 225 }
rlm@1 226
rlm@1 227 bool JoypadConfigDialog::on_key_press_event(GdkEventKey * _pstEvent)
rlm@1 228 {
rlm@1 229 if (m_puiCurrentKeyCode == NULL)
rlm@1 230 {
rlm@1 231 return Gtk::Dialog::on_key_press_event(_pstEvent);
rlm@1 232 }
rlm@1 233
rlm@1 234 *m_puiCurrentKeyCode = 0;
rlm@1 235 int iFound = m_oConfig.iFind(_pstEvent->hardware_keycode);
rlm@1 236 if (iFound >= 0)
rlm@1 237 {
rlm@1 238 *m_oConfig.puiAt(iFound) = 0;
rlm@1 239 m_oEntries[iFound]->set_text(_("<Undefined>"));
rlm@1 240 }
rlm@1 241
rlm@1 242 *m_puiCurrentKeyCode = _pstEvent->hardware_keycode;
rlm@1 243
rlm@1 244 guint uiKeyval = 0;
rlm@1 245 gdk_keymap_translate_keyboard_state(gdk_keymap_get_default(),
rlm@1 246 _pstEvent->hardware_keycode,
rlm@1 247 (GdkModifierType)0,
rlm@1 248 0,
rlm@1 249 &uiKeyval,
rlm@1 250 NULL,
rlm@1 251 NULL,
rlm@1 252 NULL);
rlm@1 253
rlm@1 254 const char * csName = gdk_keyval_name(uiKeyval);
rlm@1 255 if (csName == NULL)
rlm@1 256 {
rlm@1 257 m_oEntries[m_uiCurrentEntry]->set_text(_("<Undefined>"));
rlm@1 258 }
rlm@1 259 else
rlm@1 260 {
rlm@1 261 m_oEntries[m_uiCurrentEntry]->set_text(csName);
rlm@1 262 }
rlm@1 263
rlm@1 264 if (m_uiCurrentEntry + 1 < m_oEntries.size())
rlm@1 265 {
rlm@1 266 m_oEntries[m_uiCurrentEntry + 1]->grab_focus();
rlm@1 267 }
rlm@1 268 else
rlm@1 269 {
rlm@1 270 m_poOkButton->grab_focus();
rlm@1 271 }
rlm@1 272
rlm@1 273 return true;
rlm@1 274 }
rlm@1 275
rlm@1 276 } // namespace VBA