revert between 56095 -> 55830 in arch
[AROS.git] / workbench / classes / zune / texteditor / mcp / KeyStorage.c
blob74cbab8602d9d760f271905bbef75c3828b43da4
1 /***************************************************************************
3 TextEditor.mcc - Textediting MUI Custom Class
4 Copyright (C) 1997-2000 Allan Odgaard
5 Copyright (C) 2005-2014 TextEditor.mcc Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library 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 GNU
15 Lesser General Public License for more details.
17 TextEditor class Support Site: http://www.sf.net/projects/texteditor-mcc
19 $Id$
21 ***************************************************************************/
23 #include <math.h>
24 #include <string.h>
26 #include <clib/alib_protos.h>
27 #include <devices/inputevent.h>
28 #include <dos/dos.h>
29 #include <dos/dostags.h>
30 #include <dos/rdargs.h>
31 #include <exec/memory.h>
32 #include <libraries/mui.h>
33 #include <mui/HotkeyString_mcc.h>
34 #include <proto/dos.h>
35 #include <proto/exec.h>
36 #include <proto/intuition.h>
37 #include <proto/keymap.h>
38 #include <proto/muimaster.h>
40 #include "private.h"
41 #include "newmouse.h"
42 #include "Debug.h"
44 enum Keys
46 key_lshift = 0,
47 key_rshift,
48 key_capslock,
49 key_control,
50 key_lalt,
51 key_ralt,
52 key_lamiga,
53 key_ramiga,
54 key_numpad,
55 key_shift,
56 key_alt,
57 key_amiga,
58 key_f1,
59 key_f2,
60 key_f3,
61 key_f4,
62 key_f5,
63 key_f6,
64 key_f7,
65 key_f8,
66 key_f9,
67 key_f10,
68 key_f11,
69 key_f12,
70 key_help,
71 key_up,
72 key_down,
73 key_right,
74 key_left,
75 key_home,
76 key_end,
77 key_page_up,
78 key_page_down,
79 key_insert,
80 key_prtscr,
81 key_pause,
82 key_numlock,
83 #if defined(__amigaos4__)
84 key_menu,
85 #elif defined(__MORPHOS__)
86 key_scrlock,
87 #endif
88 key_mm_stop,
89 key_mm_play,
90 key_mm_prev,
91 key_mm_next,
92 key_mm_rewind,
93 key_mm_forward,
94 key_wheel_up,
95 key_wheel_down,
96 key_wheel_left,
97 key_wheel_right,
98 key_wheel_button,
99 key_escape,
100 key_tab,
101 key_return,
102 key_space,
103 key_backspace,
104 key_delete,
105 key_key,
106 key_count,
109 void ConvertKeyString(STRPTR keystring, UWORD action, struct KeyAction *storage)
111 IPTR args[key_count];
112 struct RDArgs *ra_result;
113 struct RDArgs *myrdargs;
115 static const char * const ratemplate =
116 "LSHIFT/S,RSHIFT/S,CAPSLOCK/S,CONTROL=CTRL/S,LALT/S,RALT/S,LAMIGA=LCOMMAND/S,RAMIGA=RCOMMAND/S,NUMPAD=NUMERICPAD/S,SHIFT/S,ALT/S,AMIGA=COMMAND/S,"
117 "f1/S,f2/S,f3/S,f4/S,f5/S,f6/S,f7/S,f8/S,f9/S,f10/S,f11/S,f12/S,"
118 "help/S,"
119 "up/S,down/S,right/S,left/S,"
120 "home/S,end/S,page_up=pageup/S,page_down=pagedown/S,insert/S,printscreen=prtscr/S,pause=break/S,numlock/S,"
121 #if defined(__amigaos4__)
122 "menu/S,"
123 #elif defined(__MORPHOS__)
124 "scrolllock=scrlock/S,"
125 #endif
126 "media_stop/S,media_play/S,media_prev/S,media_next/S,media_rewind/S,media_forward/S,"
127 "nm_wheel_up/S,nm_wheel_down/S,nm_wheel_left/S,nm_wheel_right/S,nm_wheel_button/S,"
128 "escape=esc/S,tab/S,return=enter/S,space/S,backspace=bs/S,delete=del/S,key/F";
130 storage->vanilla = FALSE;
131 storage->key = 0;
132 storage->qualifier = 0;
133 storage->action = action;
135 // clear all args
136 memset(args, 0, sizeof(args));
138 if((myrdargs = AllocDosObject(DOS_RDARGS, NULL)) != NULL)
140 ULONG length = strlen(keystring);
141 char *buffer;
143 if((buffer = AllocVecShared(length + 2, MEMF_ANY)) != NULL)
145 strlcpy(buffer, keystring, length + 1);
146 buffer[length] = '\n';
147 buffer[length+1] = '\0';
148 myrdargs->RDA_Source.CS_Buffer = buffer;
149 myrdargs->RDA_Source.CS_Length = length+1;
150 myrdargs->RDA_Source.CS_CurChr = 0;
151 myrdargs->RDA_Flags |= RDAF_NOPROMPT;
153 if((ra_result = ReadArgs(ratemplate, (APTR)args, myrdargs)) != NULL)
155 // Scan for 12 qualifier keys
156 if(args[key_lshift])
157 storage->qualifier |= IEQUALIFIER_LSHIFT;
158 if(args[key_rshift])
159 storage->qualifier |= IEQUALIFIER_RSHIFT;
160 if(args[key_capslock])
161 storage->qualifier |= IEQUALIFIER_CAPSLOCK;
162 if(args[key_control])
163 storage->qualifier |= IEQUALIFIER_CONTROL;
164 if(args[key_lalt])
165 storage->qualifier |= IEQUALIFIER_LALT;
166 if(args[key_ralt])
167 storage->qualifier |= IEQUALIFIER_RALT;
168 if(args[key_lamiga])
169 storage->qualifier |= IEQUALIFIER_LCOMMAND;
170 if(args[key_ramiga])
171 storage->qualifier |= IEQUALIFIER_RCOMMAND;
172 if(args[key_numpad])
173 storage->qualifier |= IEQUALIFIER_NUMERICPAD;
174 if(args[key_shift])
175 storage->qualifier |= IEQUALIFIER_SHIFT;
176 if(args[key_alt])
177 storage->qualifier |= IEQUALIFIER_ALT;
178 if(args[key_amiga])
179 storage->qualifier |= IEQUALIFIER_COMMAND;
181 // Scan for the 10 standard F-keys (f1-f10)
182 if(args[key_f1])
183 storage->key = RAWKEY_F1;
184 if(args[key_f2])
185 storage->key = RAWKEY_F2;
186 if(args[key_f3])
187 storage->key = RAWKEY_F3;
188 if(args[key_f4])
189 storage->key = RAWKEY_F4;
190 if(args[key_f5])
191 storage->key = RAWKEY_F5;
192 if(args[key_f6])
193 storage->key = RAWKEY_F6;
194 if(args[key_f7])
195 storage->key = RAWKEY_F7;
196 if(args[key_f8])
197 storage->key = RAWKEY_F8;
198 if(args[key_f9])
199 storage->key = RAWKEY_F9;
200 if(args[key_f10])
201 storage->key = RAWKEY_F10;
203 // Scan for the 2 extended f-keys (f11,f12)
204 if(args[key_f11])
205 storage->key = RAWKEY_F11;
206 if(args[key_f12])
207 storage->key = RAWKEY_F12;
209 // Help
210 if(args[key_help])
211 storage->key = RAWKEY_HELP;
213 // Scan for cursor-keys
214 if(args[key_up])
215 storage->key = RAWKEY_CRSRUP;
216 if(args[key_down])
217 storage->key = RAWKEY_CRSRDOWN;
218 if(args[key_right])
219 storage->key = RAWKEY_CRSRRIGHT;
220 if(args[key_left])
221 storage->key = RAWKEY_CRSRLEFT;
223 // scan for the other extended (non-standard) keys
224 if(args[key_home])
225 storage->key = RAWKEY_HOME;
226 if(args[key_end])
227 storage->key = RAWKEY_END;
228 if(args[key_page_up])
229 storage->key = RAWKEY_PAGEUP;
230 if(args[key_page_down])
231 storage->key = RAWKEY_PAGEDOWN;
232 if(args[key_insert])
233 storage->key = RAWKEY_INSERT;
234 if(args[key_prtscr])
235 storage->key = RAWKEY_PRINTSCR;
236 if(args[key_pause])
237 storage->key = RAWKEY_BREAK;
238 if(args[key_numlock])
239 storage->key = RAWKEY_NUMLOCK;
241 // some keys are mutual excluse on some platforms
242 #if defined(__amigso4__)
243 if(args[key_menu])
244 storage->key = RAWKEY_MENU;
245 #elif defined(__MORPHOS__)
246 if(args[key_scrlock])
247 storage->key = RAWKEY_SCRLOCK;
248 #endif
250 // lets address the media/CDTV keys as well
251 #if defined(__amigaos4__)
252 if(args[key_mm_stop])
253 storage->key = RAWKEY_MEDIA_STOP;
254 if(args[key_mm_play])
255 storage->key = RAWKEY_MEDIA_PLAY_PAUSE;
256 if(args[key_mm_prev])
257 storage->key = RAWKEY_MEDIA_PREV_TRACK;
258 if(args[key_mm_next])
259 storage->key = RAWKEY_MEDIA_NEXT_TRACK;
260 if(args[key_mm_rewind])
261 storage->key = RAWKEY_MEDIA_SHUFFLE;
262 if(args[key_mm_forward])
263 storage->key = RAWKEY_MEDIA_REPEAT;
264 #else
265 if(args[key_mm_stop])
266 storage->key = RAWKEY_AUD_STOP;
267 if(args[key_mm_play])
268 storage->key = RAWKEY_AUD_PLAY_PAUSE;
269 if(args[key_mm_prev])
270 storage->key = RAWKEY_AUD_PREV_TRACK;
271 if(args[key_mm_next])
272 storage->key = RAWKEY_AUD_NEXT_TRACK;
273 if(args[key_mm_rewind])
274 storage->key = RAWKEY_AUD_SHUFFLE;
275 if(args[key_mm_forward])
276 storage->key = RAWKEY_AUD_REPEAT;
277 #endif
279 // take respect of the NEWMOUSE RAWKEY based wheel events as well
280 if(args[key_wheel_up])
281 storage->key = NM_WHEEL_UP;
282 if(args[key_wheel_down])
283 storage->key = NM_WHEEL_DOWN;
284 if(args[key_wheel_left])
285 storage->key = NM_WHEEL_LEFT;
286 if(args[key_wheel_right])
287 storage->key = NM_WHEEL_RIGHT;
288 if(args[key_wheel_button])
289 storage->key = NM_BUTTON_FOURTH;
291 if(storage->key == 0)
293 storage->vanilla = TRUE;
294 if(args[key_escape])
295 storage->key = 0x1b; /* Esc */
296 if(args[key_tab])
297 storage->key = 0x09; /* Tab */
298 if(args[key_return])
299 storage->key = 0x0d; /* CR */
300 if(args[key_space])
301 storage->key = 0x20; /* Space */
302 if(args[key_backspace])
303 storage->key = 0x08; /* Backspace */
304 if(args[key_delete])
305 storage->key = 0x7f; /* Delete */
307 if(storage->key == 0 && args[key_key])
309 STRPTR str = (STRPTR)args[key_key];
311 storage->key = str[0];
314 FreeArgs(ra_result);
316 FreeVec(buffer);
318 FreeDosObject(DOS_RDARGS, myrdargs);
322 void KeyToString(STRPTR buffer, ULONG buffer_len, struct KeyAction *ka)
324 buffer[0] = '\0';
326 // lets first put the qualifiers in our buffer string
327 if(ka->qualifier & IEQUALIFIER_LSHIFT)
328 strlcat(buffer, "lshift ", buffer_len);
329 if(ka->qualifier & IEQUALIFIER_RSHIFT)
330 strlcat(buffer, "rshift ", buffer_len);
331 if(ka->qualifier & IEQUALIFIER_CAPSLOCK)
332 strlcat(buffer, "capslock ", buffer_len);
333 if(ka->qualifier & IEQUALIFIER_CONTROL)
334 strlcat(buffer, "control ", buffer_len);
335 if(ka->qualifier & IEQUALIFIER_LALT)
336 strlcat(buffer, "lalt ", buffer_len);
337 if(ka->qualifier & IEQUALIFIER_RALT)
338 strlcat(buffer, "ralt ", buffer_len);
339 if(ka->qualifier & IEQUALIFIER_LCOMMAND)
340 strlcat(buffer, "lcommand ", buffer_len);
341 if(ka->qualifier & IEQUALIFIER_RCOMMAND)
342 strlcat(buffer, "rcommand ", buffer_len);
343 if(ka->qualifier & IEQUALIFIER_NUMERICPAD)
344 strlcat(buffer, "numpad ", buffer_len);
345 if(ka->qualifier & IEQUALIFIER_SHIFT)
346 strlcat(buffer, "shift ", buffer_len);
347 if(ka->qualifier & IEQUALIFIER_ALT)
348 strlcat(buffer, "alt ", buffer_len);
349 if(ka->qualifier & IEQUALIFIER_COMMAND)
350 strlcat(buffer, "command ", buffer_len);
352 // then we check wheter this are vanilla key codes or RAWKEY codes
353 if(ka->vanilla)
355 switch(ka->key)
357 case 0x08: strlcat(buffer, "backspace", buffer_len); break;
358 case 0x09: strlcat(buffer, "tab", buffer_len); break;
359 case 0x0d: strlcat(buffer, ((ka->qualifier & IEQUALIFIER_NUMERICPAD) ? "enter" : "return"), buffer_len); break;
360 case 0x1b: strlcat(buffer, "esc", buffer_len); break;
361 case 0x29: strlcat(buffer, "space", buffer_len); break;
362 case 0x7f: strlcat(buffer, "del", buffer_len); break;
364 default:
366 char *p = &buffer[strlen(buffer)];
368 *p++ = ka->key;
369 *p = '\0';
373 else
375 switch(ka->key)
377 case RAWKEY_CRSRUP: strlcat(buffer, "up", buffer_len); break;
378 case RAWKEY_CRSRDOWN: strlcat(buffer, "down", buffer_len); break;
379 case RAWKEY_CRSRRIGHT: strlcat(buffer, "right", buffer_len); break;
380 case RAWKEY_CRSRLEFT: strlcat(buffer, "left", buffer_len); break;
381 case RAWKEY_F1: strlcat(buffer, "f1", buffer_len); break;
382 case RAWKEY_F2: strlcat(buffer, "f2", buffer_len); break;
383 case RAWKEY_F3: strlcat(buffer, "f3", buffer_len); break;
384 case RAWKEY_F4: strlcat(buffer, "f4", buffer_len); break;
385 case RAWKEY_F5: strlcat(buffer, "f5", buffer_len); break;
386 case RAWKEY_F6: strlcat(buffer, "f6", buffer_len); break;
387 case RAWKEY_F7: strlcat(buffer, "f7", buffer_len); break;
388 case RAWKEY_F8: strlcat(buffer, "f8", buffer_len); break;
389 case RAWKEY_F9: strlcat(buffer, "f9", buffer_len); break;
390 case RAWKEY_F10: strlcat(buffer, "f10", buffer_len); break;
391 case RAWKEY_F11: strlcat(buffer, "f11", buffer_len); break;
392 case RAWKEY_F12: strlcat(buffer, "f12", buffer_len); break;
393 case RAWKEY_HELP: strlcat(buffer, "help", buffer_len); break;
394 case RAWKEY_HOME: strlcat(buffer, "home", buffer_len); break;
395 case RAWKEY_END: strlcat(buffer, "end", buffer_len); break;
396 case RAWKEY_PAGEUP: strlcat(buffer, "page_up", buffer_len); break;
397 case RAWKEY_PAGEDOWN: strlcat(buffer, "page_down", buffer_len); break;
398 case RAWKEY_INSERT: strlcat(buffer, "insert", buffer_len); break;
399 case RAWKEY_PRINTSCR: strlcat(buffer, "printscreen", buffer_len); break;
400 case RAWKEY_BREAK: strlcat(buffer, "pause", buffer_len); break;
401 case RAWKEY_NUMLOCK: strlcat(buffer, "numlock", buffer_len); break;
403 #if defined(__amigaos4__)
404 case RAWKEY_MENU: strlcat(buffer, "menu", buffer_len); break;
405 #elif defined(__MORPHOS__)
406 case RAWKEY_SCRLOCK: strlcat(buffer, "scrolllock", buffer_len); break;
407 #endif
409 #if defined(__amigaos4__)
410 case RAWKEY_MEDIA_STOP: strlcat(buffer, "media_stop", buffer_len); break;
411 case RAWKEY_MEDIA_PLAY_PAUSE: strlcat(buffer, "media_play", buffer_len); break;
412 case RAWKEY_MEDIA_PREV_TRACK: strlcat(buffer, "media_prev", buffer_len); break;
413 case RAWKEY_MEDIA_NEXT_TRACK: strlcat(buffer, "media_next", buffer_len); break;
414 case RAWKEY_MEDIA_SHUFFLE: strlcat(buffer, "media_rewind", buffer_len); break;
415 case RAWKEY_MEDIA_REPEAT: strlcat(buffer, "media_forward", buffer_len); break;
416 #else
417 case RAWKEY_AUD_STOP: strlcat(buffer, "media_stop", buffer_len); break;
418 case RAWKEY_AUD_PLAY_PAUSE: strlcat(buffer, "media_play", buffer_len); break;
419 case RAWKEY_AUD_PREV_TRACK: strlcat(buffer, "media_prev", buffer_len); break;
420 case RAWKEY_AUD_NEXT_TRACK: strlcat(buffer, "media_next", buffer_len); break;
421 case RAWKEY_AUD_SHUFFLE: strlcat(buffer, "media_rewind", buffer_len); break;
422 case RAWKEY_AUD_REPEAT: strlcat(buffer, "media_forward", buffer_len); break;
423 #endif
425 case NM_WHEEL_UP: strlcat(buffer, "nm_wheel_up", buffer_len); break;
426 case NM_WHEEL_DOWN: strlcat(buffer, "nm_wheel_down", buffer_len); break;
427 case NM_WHEEL_LEFT: strlcat(buffer, "nm_wheel_left", buffer_len); break;
428 case NM_WHEEL_RIGHT: strlcat(buffer, "nm_wheel_right", buffer_len); break;
429 case NM_BUTTON_FOURTH: strlcat(buffer, "nm_wheel_button", buffer_len); break;
431 default:
432 strlcat(buffer, "???", buffer_len);
433 break;
438 void ImportKeys(struct InstData_MCP *data, void *config)
440 void *cfg_data;
441 struct te_key *userkeys;
443 ENTER();
445 if(config != NULL && (cfg_data = (void *)DoMethod(config, MUIM_Dataspace_Find, MUICFG_TextEditor_Keybindings)))
446 userkeys = cfg_data;
447 else
448 userkeys = (struct te_key *)default_keybindings;
450 DoMethod(data->keybindings, MUIM_List_Clear);
452 set(data->keybindings, MUIA_List_Quiet, TRUE);
454 while((WORD)userkeys->code != -1)
456 DoMethod(data->keybindings, MUIM_List_InsertSingle, userkeys, MUIV_List_Insert_Bottom);
458 userkeys++;
461 set(data->keybindings, MUIA_List_Quiet, FALSE);
463 LEAVE();
466 void ExportKeys(struct InstData_MCP *data, void *config)
468 ULONG c, size;
469 struct te_key *entry;
470 struct te_key *entries;
472 c = xget(data->keybindings, MUIA_List_Entries);
473 size = (c+1) * sizeof(struct te_key);
475 if((entries = (struct te_key *)AllocVecShared(size, MEMF_ANY)))
477 struct te_key *buffer = entries+c;
479 buffer->code = -1;
480 while(c--)
482 DoMethod(data->keybindings, MUIM_List_GetEntry, c, &entry);
483 buffer--;
484 buffer->code = entry->code;
485 buffer->qual = entry->qual;
486 buffer->act = entry->act;
488 DoMethod(config, MUIM_Dataspace_Add, entries, size, MUICFG_TextEditor_Keybindings);
489 FreeVec(entries);