amd64 fixes
[btb/d2x.git] / ui / keypress.c
1 /* $Id: keypress.c,v 1.5 2005-02-27 03:55:46 chris Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 #ifdef RCS
16 static char rcsid[] = "$Id: keypress.c,v 1.5 2005-02-27 03:55:46 chris Exp $";
17 #endif
18
19 #ifdef HAVE_CONFIG_H
20 #include "conf.h"
21 #endif
22
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26
27 #include "fix.h"
28 #include "pstypes.h"
29 #include "gr.h"
30 #include "ui.h"
31 #include "key.h"
32
33 char * KeyDesc[256] = {         \
34 "","{Esc}","{1}","{2}","{3}","{4}","{5}","{6}","{7}","{8}","{9}","{0}","{-}",           \
35 "{=}","{Backspace}","{Tab}","{Q}","{W}","{E}","{R}","{T}","{Y}","{U}","{I}","{O}",      \
36 "{P}","{[}","{]}","{Enter}","{LeftCtrl}","{A}","{S}","{D}","{F}",        \
37 "{G}","{H}","{J}","{K}","{L}","{;}","{'}","{`}",        \
38 "{RightShift}","{\\}","{Z}","{X}","{C}","{V}","{B}","{N}","{M}","{,}",      \
39 "{.}","{/}","{LeftShift}","{Pad*}","{LeftAlt}","{Spacebar}",      \
40 "{CapsLock}","{F1}","{F2}","{F3}","{F4}","{F5}","{F6}","{F7}","{F8}","{F9}",        \
41 "{F10}","{NumLock}","{ScrollLock}","{Pad7}","{Pad8}","{Pad9}","{Pad-}",   \
42 "{Pad4}","{Pad5}","{Pad6}","{Pad+}","{Pad1}","{Pad2}","{Pad3}","{Pad0}", \
43 "{Pad.}","","","","{F11}","{F12}","","","","","","","","","",         \
44 "","","","","","","","","","","","","","","","","","","","",     \
45 "","","","","","","","","","","","","","","","","","","","",     \
46 "","","","","","","","","","","","","","","","","","",           \
47 "{PadEnter}","{RightCtrl}","","","","","","","","","","","","","", \
48 "","","","","","","","","","","{Pad/}","","","{RightAlt}","",      \
49 "","","","","","","","","","","","","","{Home}","{Up}","{PageUp}",     \
50 "","{Left}","","{Right}","","{End}","{Down}","{PageDown}","{Insert}",       \
51 "{Delete}","","","","","","","","","","","","","","","","","",     \
52 "","","","","","","","","","","","","","","","","","","","",     \
53 "","","","","","","" };
54
55
56
57
58 void GetKeyDescription( char * text, int keypress )
59 {
60         char Ctrl[10];
61         char Alt[10];
62         char Shift[10];
63
64         if (keypress & KEY_CTRLED)
65                 strcpy( Ctrl, "{Ctrl}");
66         else
67                 strcpy( Ctrl, "");
68
69         if (keypress & KEY_ALTED)
70                 strcpy( Alt, "{Alt}");
71         else
72                 strcpy( Alt, "");
73
74         if (keypress & KEY_SHIFTED)
75                 strcpy( Shift, "{Shift}");
76         else
77                 strcpy( Shift, "");
78
79         sprintf( text, "%s%s%s%s", Ctrl, Alt, Shift, KeyDesc[keypress & 255 ]  );
80 }
81
82
83 int DecodeKeyText( char * text )
84 {
85         int i, code = 0;
86
87         if (strstr(text, "{Ctrl}") )
88                 code |= KEY_CTRLED;
89         if (strstr(text, "{Alt}") )
90                 code |= KEY_ALTED;
91         if (strstr(text, "{Shift}") )
92                 code |= KEY_SHIFTED;
93
94         for (i=0; i<256; i++ )
95         {
96                 if ( strlen(KeyDesc[i])>0 && strstr(text, KeyDesc[i]) )
97                 {
98                         code |= i;
99                         return code;
100                 }
101         }
102         return -1;
103 }
104
105
106
107
108 int GetKeyCode(char * text)
109 {
110         UI_WINDOW * wnd;
111         UI_GADGET_BUTTON * DoneButton;
112         char temp_text[100];
113
114         text = text;
115
116         wnd = ui_open_window( 200, 200, 400, 200, WIN_DIALOG );
117
118         DoneButton = ui_add_gadget_button( wnd, 170, 165, 60, 25, "Ok", NULL );
119
120         ui_gadget_calc_keys(wnd);
121
122         //key_flush();
123
124         wnd->keyboard_focus_gadget = (UI_GADGET *)DoneButton;
125
126         while(1)
127         {
128                 ui_mega_process();
129                 ui_window_do_gadgets(wnd);
130
131                 if (last_keypress > 0)
132                 {
133                         GetKeyDescription( temp_text, last_keypress );
134                         ui_wprintf_at( wnd, 10, 100, "%s     ", temp_text  );
135                 }
136
137                 if (DoneButton->pressed)
138                         break;
139
140                 gr_update();
141         }
142
143         ui_close_window(wnd);
144
145         return 0;
146 }
147