Initial Import
[glAntsMech.git] / glants_mech / linux / src / keys.c
blobf881c0d7c8a022423df53882ada59797faf50f8b
1 //
2 // keys.c
3 //
4 // a typing interface
5 // for chat or other purposes
6 // - mostly used with the networking settings
7 //
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <time.h>
12 #include <stdlib.h>
13 #include <float.h> // used for _control
15 #include <GL/glx.h>
16 #include <GL/gl.h> // Header File For The OpenGL32 Library
17 #include <GL/glu.h> // Header File For The GLu32 Library
18 #include <X11/extensions/xf86vmode.h>
19 #include <X11/keysym.h>
21 #include "globals.h"
22 #include "keys.h"
23 #include "menu.h"
24 #include "network/include/connect.h"
27 // bad, bad! using globals
28 extern char network_str_[22][80];
30 static int current_mode = SEL_CLIENT_MODE;
31 static int current_sel = START_SEL;
32 static int current_char[4][2];
34 // the different key entry types
35 static int client_sel[6] = {
36 SEL_TYPE_TEXT,
37 SEL_TYPE_IP,
38 SEL_TYPE_BOOL,
39 SEL_TYPE_NUMERIC,
40 SEL_TYPE_APPLY,
41 SEL_TYPE_APPLY };
43 static int client_str[6] = {
44 CLIENT_NET_MENU,
45 CLIENT_NET_MENU+1,
46 CLIENT_NET_MENU+2,
47 CLIENT_NET_MENU+3,
48 _TEXT_CONNECT_,
49 _TEXT_DISCONNECT_ };
51 // server variables //
52 static int server_str[6] = {
53 SERVER_NET_MENU,
54 SERVER_NET_MENU+1,
55 SERVER_NET_MENU+2,
56 SERVER_NET_MENU+3,
57 _TEXT_START_,
58 _TEXT_SHUTDOWN_};
61 // final_str
62 static char final_str[4][80];
65 // The string has to begin at random points
67 static int client_start[4] = {
68 15,
69 14,
70 18,
71 16 };
73 static int server_start[4] = {
74 15,
75 14,
76 17,
77 13 };
80 static int server_sel[6] = {
81 SEL_TYPE_TEXT,
82 SEL_TYPE_NOCHANGE,
83 SEL_TYPE_BOOL,
84 SEL_TYPE_NUMERIC,
85 SEL_TYPE_APPLY,
86 SEL_TYPE_APPLY
89 static void Dec_Selection(void);
90 static void Inc_Selection(void);
92 static void Reset_StartChar(void)
94 int i = 0;
96 for (i = 0; i < 4; i++) {
97 current_char[i][0] = 0;
98 current_char[i][1] = 0;
99 } // end of the for
101 } // end of func
105 // Finalize_Selection
106 // - connect/disconnect/etc
108 static void Finalize_Selection(void)
110 int text_start = 0;
111 int tmp_selection = 0;
112 int tmp_mode = 0;
113 int i = 0,j=0;
114 char c = ' ';
116 // the mode effects how to handle input characters
117 if (current_mode == SEL_CLIENT_MODE)
120 tmp_mode = 0;
122 // collect the current user strings
123 for (i = 0; i < 4; i++)
125 tmp_selection = client_str[i];
126 text_start = client_start[i];
128 c = ' ';
129 for (j = 0; c != '\0'; j++) {
131 c = network_str_[tmp_selection][text_start+j];
132 final_str[i][j] = c;
134 } // end of the inner for
136 // make sure the string is null terminated
137 final_str[i][j] = '\0';
139 } // end of the for
141 } else {
143 tmp_mode = 1;
145 // collect the current user strings
146 for (i = 0; i < 4; i++)
148 tmp_selection = server_str[i];
149 text_start = server_start[i];
151 c = ' ';
152 for (j = 0; c != '\0'; j++) {
154 c = network_str_[tmp_selection][text_start+j];
155 final_str[i][j] = c;
157 } // end of the inner for
159 // make sure the string is null terminated
160 final_str[i][j] = '\0';
162 } // end of the for
164 } // end of th if - else
166 } // end of the function
169 // Char_Keys(
170 // - handle normal keys
171 void Alpha_Keys(char *buffer)
173 char c;
174 int input_type=0;
175 int text_start = 0;
176 int char_start = 0;
177 int char_i;
178 int tmp_mode = 0;
179 int tmp_selection = 0;
180 int i =0;
182 c = buffer[0];
184 switch(c)
186 case '\n':
187 case '\r':
188 if ((current_sel >= 0) && (current_sel < 4)) {
189 // simple I think
190 Inc_Selection();
191 } // end of teh if
194 if (current_sel == 4) {
196 // process the selection
197 if (current_mode == SEL_CLIENT_MODE)
199 Finalize_Selection();
201 // connect to server, the wheels are in motion now
202 Start_Service(final_str, SEL_CLIENT_MODE);
204 } else if (current_mode == SEL_SERVER_MODE) {
205 Finalize_Selection();
207 // start the server, watch the messages at bottom screen
208 Start_Service(final_str, SEL_SERVER_MODE);
209 } // end of if-else
211 } // end of if selection = 4
214 break;
216 default:
217 char_i = 0;
219 // the mode effects how to handle input characters
220 if (current_mode == SEL_CLIENT_MODE)
222 input_type = client_sel[current_sel];
224 if ((current_sel >= 0) && (current_sel < 4)) {
225 text_start = client_start[current_sel];
226 char_i = current_sel;
227 tmp_selection = client_str[current_sel];
228 } // end o if
230 tmp_mode = 0;
232 } else {
234 input_type = server_sel[current_sel];
235 if ((current_sel >= 0) && (current_sel < 4)) {
236 text_start = server_start[current_sel];
237 char_i = current_sel;
238 tmp_selection = server_str[current_sel];
239 } // end of if
241 tmp_mode = 1;
243 } // end of the if - else
246 switch(input_type)
248 case SEL_TYPE_TEXT:
250 if (((c >= 'a') && (c <= 'z')) ||
251 ((c >= 'A') && (c <= 'Z')))
253 // confusing yet?
254 char_start = current_char[char_i][tmp_mode];
255 network_str_[tmp_selection][text_start+char_start] = c;
257 current_char[char_i][tmp_mode]++;
258 if (current_char[char_i][tmp_mode] >= MAX_INPUT_STR)
259 current_char[char_i][tmp_mode] = MAX_INPUT_STR;
261 // and dont forget to add a null terminator
262 network_str_[tmp_selection][text_start+char_start+1] = '\0';
264 } // end of if
265 break;
268 case SEL_TYPE_IP:
270 if (((c >= '0') && (c <= '9')) || (c == '.'))
272 // confusing yet?
273 char_start = current_char[char_i][tmp_mode];
274 network_str_[tmp_selection][text_start+char_start] = c;
276 current_char[char_i][tmp_mode]++;
277 if (current_char[char_i][tmp_mode] >= MAX_INPUT_STR)
278 current_char[char_i][tmp_mode] = MAX_INPUT_STR;
280 // and dont forget to add a null terminator
281 network_str_[tmp_selection][text_start+char_start+1] = '\0';
283 } // end of if
284 break;
287 break;
289 case SEL_TYPE_NOCHANGE:
291 break;
293 case SEL_TYPE_BOOL:
295 // clear everything that is already there
297 if (((c >= 'a') && (c <= 'z')) ||
298 ((c >= 'A') && (c <= 'Z')))
300 // this is easy
301 // we have only two values
302 // rotate them
303 // the letters used are random
305 char_start = 0;
307 Super_Printf("%c", network_str_[tmp_selection][text_start+char_start]);
309 if (network_str_[tmp_selection][text_start+char_start] == 'f') {
311 for (i = 0; i < 10; i++)
312 network_str_[tmp_selection][text_start+i] = '\0';
314 network_str_[tmp_selection][text_start+char_start+0] = 't';
315 network_str_[tmp_selection][text_start+char_start+1] = 'r';
316 network_str_[tmp_selection][text_start+char_start+2] = 'u';
317 network_str_[tmp_selection][text_start+char_start+3] = 'e';
318 network_str_[tmp_selection][text_start+char_start+4] = '\0';
319 } else {
321 for (i = 0; i < 10; i++)
322 network_str_[tmp_selection][text_start+i] = '\0';
324 network_str_[tmp_selection][text_start+char_start] = 'f';
325 network_str_[tmp_selection][text_start+char_start+1] = 'a';
326 network_str_[tmp_selection][text_start+char_start+2] = 'l';
327 network_str_[tmp_selection][text_start+char_start+3] = 's';
328 network_str_[tmp_selection][text_start+char_start+4] = 'e';
329 network_str_[tmp_selection][text_start+char_start+5] = '\0';
330 } // end of if else
332 } // end of teh if
335 break;
337 case SEL_TYPE_NUMERIC:
339 if ((c >= '0') && (c <= '9'))
341 // confusing yet?
342 char_start = current_char[char_i][tmp_mode];
343 network_str_[tmp_selection][text_start+char_start] = c;
345 current_char[char_i][tmp_mode]++;
346 if (current_char[char_i][tmp_mode] >= MAX_INPUT_STR)
347 current_char[char_i][tmp_mode] = MAX_INPUT_STR;
349 // and dont forget to add a null terminator
350 network_str_[tmp_selection][text_start+char_start+1] = '\0';
352 } // end of if
355 break;
357 case SEL_TYPE_APPLY:
358 break;
360 default: break;
363 break;
366 } // end of teh functino
369 // Display_NetSel
370 // - display the selected network text
372 void Display_NetSel(char str[26][80])
374 int i;
375 // reset back to the normal setting
376 // reset the other client strings
377 for (i = 0; i < 4; i++)
379 str[client_str[i] ][0] = ':';
380 str[client_str[i] ][1] = ':';
381 } // end of the for
383 for (i = 0; i < 4; i++)
385 str[server_str[i] ][0] = ':';
386 str[server_str[i] ][1] = ':';
388 } // end of the for
390 // clear the connect strings
391 for (i = 4; i < 6; i++)
393 str[client_str[i] ][0] = ' ';
394 str[client_str[i] ][1] = ' ';
396 str[server_str[i] ][0] = ' ';
397 str[server_str[i] ][1] = ' ';
399 } // end of the for
402 if (current_mode == SEL_CLIENT_MODE)
405 // change the string to signify selection
406 if ((current_sel >= 0) && (current_sel < 4)) {
407 str[client_str[current_sel] ][0] = '>';
408 str[client_str[current_sel] ][1] = '>';
409 } // end of the if
411 if ((current_sel >= 4) && (current_sel < 6)) {
412 str[client_str[current_sel] ][0] = '>';
413 str[client_str[current_sel] ][1] = '>';
414 } // end of the if
417 } else if (current_mode == SEL_SERVER_MODE) {
419 if ((current_sel >= 0) && (current_sel < 4)) {
420 str[server_str[current_sel] ][0] = '>';
421 str[server_str[current_sel] ][1] = '>';
422 } // end of the if
424 if ((current_sel >= 4) && (current_sel < 6)) {
425 str[server_str[current_sel] ][0] = '>';
426 str[server_str[current_sel] ][1] = '>';
427 } // end of the if
429 } // end of if-else
431 } // end of the function
434 // Inc_Selection
436 static void Inc_Selection(void)
438 int max1 = 0;
440 Reset_StartChar();
442 if (current_mode == SEL_SERVER_MODE)
445 max1 = SEL_SERVER_MAX + 1;
447 } else if (current_mode == SEL_CLIENT_MODE) {
449 max1 = SEL_CLIENT_MAX + 1;
451 } // end of if-else
453 current_sel++;
454 if (current_sel >= max1)
456 current_sel = START_SEL;
457 } // end of the if
459 } // end of the function
462 // Dec_Selection
464 static void Dec_Selection(void)
466 int max1 = 0;
468 Reset_StartChar();
470 if (current_mode == SEL_SERVER_MODE)
473 max1 = SEL_SERVER_MAX;
475 } else if (current_mode == SEL_CLIENT_MODE) {
477 max1 = SEL_CLIENT_MAX;
479 } // end of if-else
481 current_sel--;
482 if (current_sel < 0)
484 current_sel = max1;
485 } // end of the if
487 } // end of the func
490 // Cmd_Keys
492 void Cmd_Keys(KeySym key)
495 // make sure we are in the correct mode
496 if (ant_globals->menu_mode == MENU_SETTINGS_MODE)
498 switch(key)
500 case XK_Down:
502 Inc_Selection();
504 break;
506 case XK_Up:
508 Dec_Selection();
510 break;
512 case XK_Tab:
513 // client mode
514 if (current_mode == SEL_SERVER_MODE)
515 current_mode = SEL_CLIENT_MODE;
516 else
517 current_mode = SEL_SERVER_MODE;
518 break;
520 case XK_Return:
521 case XK_space:
522 break;
524 default: break;
528 } // end of the if
532 } // end of the function