/[pearpc]/src/system/keyboard.cc
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /src/system/keyboard.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Wed Sep 5 17:11:21 2007 UTC (13 years, 3 months ago) by dpavlin
File size: 5754 byte(s)
import upstream CVS
1 /*
2 * PearPC
3 * keyboard.cc
4 *
5 * Copyright (C) 2004 Stefan Weyergraf
6 * Copyright (C) 2003,2004 Sebastian Biallas (sb@biallas.net)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
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.
20 */
21
22 #include "tools/snprintf.h"
23 #include "keyboard.h"
24 #include "display.h"
25
26 SystemKeyboard *gKeyboard = NULL;
27
28 static char *key_names[] = {
29 "A","S","D","F","H","G","Z","X","C","V",
30 NULL,"B","Q","W","E","R","Y","T","1","2",
31 "3","4","6","5","=","9","7","-","8","0","]",
32 "O","U","[","I","P","Return","L","J","'","K",
33 ";","\\",",","/","N","M",".","Tab","Space",
34 "`", "Backspace",NULL,"Escape","Ctrl","Alt","Shift","Caps-Lock","Right-Alt","Left",
35 "Right","Down","Up",NULL,NULL,"Keypad-.",NULL,"Keypad-*",NULL,"Keypad-+",
36 NULL,"Numlock",NULL,NULL,NULL,"Keypad-/","Keypad-Enter",NULL,"Keypad--",NULL,
37 NULL,NULL,"Keypad-0","Keypad-1","Keypad-2","Keypad-3","Keypad-4","Keypad-5","Keypad-6","Keypad-7",
38 NULL,"Keypad-8","Keypad-9",NULL,NULL,NULL,"F5","F6","F7","F3",
39 "F8","F9",NULL,"F11",NULL,"F13",NULL,"Scrolllock",NULL,"F10",
40 NULL,"F12",NULL,"Pause","Insert","Home","Pageup","Delete","F4","End",
41 "F2","Pagedown","F1",
42 };
43
44 SystemKeyboard::SystemKeyboard()
45 {
46 reset();
47 }
48
49 bool SystemKeyboard::handleEvent(const SystemEvent &ev)
50 {
51 if (ev.type != sysevKey) return false;
52 if (ev.key.keycode == KEY_CONTROL) mCtrl = ev.key.pressed ? KEYCODE_CTRL : 0;
53 if (ev.key.keycode == KEY_ALT) mLAlt = ev.key.pressed ? KEYCODE_LALT : 0;
54 if (ev.key.keycode == KEY_ALTGR) mRAlt = ev.key.pressed ? KEYCODE_RALT : 0;
55 if (ev.key.keycode == KEY_SHIFT) mShift = ev.key.pressed ? KEYCODE_SHIFT : 0;
56 int keycode = ev.key.keycode | mCtrl | mRAlt | mLAlt | mShift;
57 if (keycode == keyConfig.key_toggle_mouse_grab) {
58 if (ev.key.pressed) gDisplay->setMouseGrab(!gDisplay->isMouseGrabbed());
59 return true;
60 } else if (keycode == keyConfig.key_toggle_full_screen) {
61 if (ev.key.pressed) gDisplay->setFullscreenMode(!gDisplay->mFullscreen);
62 /*
63 * Changing fullscreen/windowed mode confuses
64 * some well-known OS (key release events are dropped).
65 */
66 reset();
67 return true;
68 } else if (keycode == keyConfig.key_compose_dialog) {
69 if (ev.key.pressed) gDisplay->composeKeyDialog();
70 return true;
71 } else {
72 return SystemDevice::handleEvent(ev);
73 }
74 }
75
76 void SystemKeyboard::reset()
77 {
78 mCtrl = false;
79 mLAlt = false;
80 mRAlt = false;
81 mShift = false;
82 }
83
84 bool SystemKeyboard::convertKeycodeToString(String &result, int keycode)
85 {
86 if (!key_names[keycode & 0xff]) return false;
87 if (keycode & ~(0xff|KEYCODE_CTRL|KEYCODE_LALT|KEYCODE_RALT|KEYCODE_SHIFT)) return false;
88 result = "";
89 if (keycode & KEYCODE_CTRL) {
90 result += key_names[KEY_CONTROL];
91 result += "+";
92 }
93 if (keycode & KEYCODE_LALT) {
94 result += key_names[KEY_ALT];
95 result += "+";
96 }
97 if (keycode & KEYCODE_RALT) {
98 result += key_names[KEY_ALTGR];
99 result += "+";
100 }
101 if (keycode & KEYCODE_SHIFT) {
102 result += key_names[KEY_SHIFT];
103 result += "+";
104 }
105 result += key_names[keycode & 0xff];
106 return true;
107 }
108
109 bool SystemKeyboard::convertStringToKeycode(int &keycode, const String &s)
110 {
111 if (s == "none") {
112 keycode = 0xff;
113 return true;
114 }
115 String k = s;
116 bool cont = true;
117 keycode = 0;
118 while (cont) {
119 String key, rem;
120 cont = k.leftSplit('+', key, rem);
121 k = rem;
122
123 int found = -1;
124 for (uint i=0; i < (sizeof key_names / sizeof key_names[0]); i++) {
125 if (key_names[i] && key == (String)key_names[i]) found = i;
126 }
127 switch (found) {
128 case -1:
129 ht_printf("%y not found\n", &key);
130 return false;
131 case KEY_CONTROL:
132 if (keycode & KEYCODE_CTRL) return false;
133 keycode |= KEYCODE_CTRL;
134 break;
135 case KEY_ALT:
136 if (keycode & KEYCODE_LALT) return false;
137 keycode |= KEYCODE_LALT;
138 break;
139 case KEY_ALTGR:
140 if (keycode & KEYCODE_RALT) return false;
141 keycode |= KEYCODE_RALT;
142 break;
143 case KEY_SHIFT:
144 if (keycode & KEYCODE_SHIFT) return false;
145 keycode |= KEYCODE_SHIFT;
146 break;
147 default:
148 if (keycode & 0xff) return false;
149 keycode |= found;
150 }
151 }
152 return true;
153 }
154
155 /* FIXME: need proper keymaps */
156 static char lchrs[0x7f] = {
157 'a','s','d','f','h','g','z','x','c','v',
158 0,'b','q','w','e','r','y','t','1','2',
159 '3','4','6','5','=','9','7','-','8','0',']',
160 'o','u','[','i','p',13,'l','j','\'','k',
161 ';','\\',',','/','n','m','.',9,32,
162 '`',8,0,27,0,0,0,0,0,0,
163 0,0,0,0,0,'.',0,'*',0,'+',
164 0,0,0,0,0,'/',13,0,'-',0,
165 0,0,'0','1','2','3','4','5','6','7',
166 0,'8','9',0,0,0,0,0,0,0,
167 0,0,0,0,0,0,0,0,0,0,
168 0,0,0,0,0,0,0,0,0,0,
169 0,0,0,
170 };
171
172 static char uchrs[0x7f] = {
173 'A','S','D','F','H','G','Z','X','C','V',
174 0,'B','Q','W','E','R','Y','T','!','@',
175 '#','$','^','%','+','(','&','_','*',')','}',
176 'O','U','{','I','P',13,'L','J','"','K',
177 ':','|','<','?','N','M','>',9,32,
178 '~',8,0,27,0,0,0,0,0,0,
179 0,0,0,0,0,'.',0,'*',0,'+',
180 0,0,0,0,0,'/',13,0,'-',0,
181 0,0,'0','1','2','3','4','5','6','7',
182 0,'8','9',0,0,0,0,0,0,0,
183 0,0,0,0,0,0,0,0,0,0,
184 0,0,0,0,0,0,0,0,0,0,
185 0,0,0,
186 };
187
188 bool SystemKeyboard::adbKeyToAscii(char &chr, int adbcode)
189 {
190 adbcode &= 0x7f;
191
192 if (mShift)
193 chr = uchrs[adbcode];
194 else
195 chr = lchrs[adbcode];
196
197 return chr != 0;
198 }
199
200 bool SystemKeyboard::setKeyConfig(KeyboardCharacteristics keycon)
201 {
202 keyConfig=keycon;
203 return true;
204 }
205
206 KeyboardCharacteristics &SystemKeyboard::getKeyConfig()
207 {
208 return keyConfig;
209 }

  ViewVC Help
Powered by ViewVC 1.1.26