Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 8295fce438044fd557c0346ce0a3f74ac1885e14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
   client/keyboard.cc
   This file is part of the Osirion project and is distributed under
   the terms and conditions of the GNU General Public License version 2
*/

#include <iostream>
#include <iomanip>
#include <fstream>

#include "auxiliary/functions.h"
#include "client/keyboard.h"
#include "core/application.h"
#include "core/commandbuffer.h"
#include "filesystem/filesystem.h"
#include "sys/sys.h"

namespace client {

/*
Notes:
http://docs.mandragor.org/files/Common_libs_documentation/SDL/SDL_Documentation_project_en/sdlkey.html
*/

Keyboard::Keyboard()
{
	numlock = false;
	capslock = false;

	add_key("backspace", SDLK_BACKSPACE);
	add_key("tab", SDLK_TAB);
   	add_key("clear", SDLK_CLEAR);
   	add_key("enter", SDLK_RETURN);
   	add_key("pause", SDLK_PAUSE);
   	add_key("esc", SDLK_ESCAPE);
   	add_key("space", SDLK_SPACE, ' ', "ui_control");
   	add_key("!", SDLK_EXCLAIM, '!');
   	add_key("\"", SDLK_QUOTEDBL, '"');
   	add_key("#", SDLK_HASH, '#');
   	add_key("$", SDLK_DOLLAR, '$');
   	add_key("&", SDLK_AMPERSAND, '&');
   	add_key("'", SDLK_QUOTE, '\'');
	add_key("(", SDLK_LEFTPAREN, '(');
   	add_key(")", SDLK_RIGHTPAREN, ')');
   	add_key("*", SDLK_ASTERISK, '*');
   	add_key("+", SDLK_PLUS, '+');
   	add_key(",", SDLK_COMMA, ',');
   	add_key("-", SDLK_MINUS, '-');
   	add_key(".", SDLK_PERIOD, '.');
   	add_key("/", SDLK_SLASH, '/');

	add_key("0", SDLK_0, '0');
	add_key("1", SDLK_1, '1');
	add_key("2", SDLK_2, '2');
	add_key("3", SDLK_3, '3');
	add_key("4", SDLK_4, '4');
	add_key("5", SDLK_5, '5');
	add_key("6", SDLK_6, '6');
	add_key("7", SDLK_7, '7');
	add_key("8", SDLK_8, '8');
	add_key("9", SDLK_9, '9');

	add_key(":", SDLK_COLON, ':');
   	add_key(";", SDLK_SEMICOLON, ';');
   	add_key("<", SDLK_LESS, '<');
   	add_key("=", SDLK_EQUALS, '=');
   	add_key(">", SDLK_GREATER, '>');
   	add_key("?", SDLK_QUESTION, '?');
  	add_key("@", SDLK_AT, '@');
   	add_key("[", SDLK_LEFTBRACKET, '[');
   	add_key("\\", SDLK_BACKSLASH, '\\');
   	add_key("]", SDLK_RIGHTBRACKET, ']');
   	add_key("^", SDLK_CARET, '^');
   	add_key("_", SDLK_UNDERSCORE, '_');
   	add_key("`", SDLK_BACKQUOTE, '`', "ui_console");

	add_key("a", SDLK_a, 'a');
	add_key("b", SDLK_b, 'b');
	add_key("c", SDLK_c, 'c');
	add_key("d", SDLK_d, 'd');
	add_key("e", SDLK_e, 'e');
	add_key("f", SDLK_f, 'f');
	add_key("g", SDLK_g, 'g');
	add_key("h", SDLK_h, 'h');
	add_key("i", SDLK_i, 'i');
	add_key("j", SDLK_j, 'j');
	add_key("k", SDLK_k, 'k');
	add_key("l", SDLK_l, 'l');
	add_key("m", SDLK_m, 'm');
	add_key("n", SDLK_n, 'n', "target_next");
	add_key("o", SDLK_o, 'o');
	add_key("p", SDLK_p, 'p', "target_prev");
	add_key("q", SDLK_q, 'q');
	add_key("r", SDLK_r, 'r');
	add_key("s", SDLK_s, 's');
	add_key("t", SDLK_t, 't', "ui_chat");
	add_key("u", SDLK_u, 'u');
	add_key("v", SDLK_v, 'v', "ui_view");
	add_key("w", SDLK_w, 'w');
	add_key("x", SDLK_x, 'x');
	add_key("y", SDLK_y, 'y');
	add_key("z", SDLK_z, 'z');

	add_key("del", SDLK_DELETE);
	add_key("kp0", SDLK_KP0);
	add_key("kp1", SDLK_KP1);
	add_key("kp2", SDLK_KP2, 0, "+up");
	add_key("kp3", SDLK_KP3);
	add_key("kp4", SDLK_KP4, 0, "+left");
	add_key("kp5", SDLK_KP5);
	add_key("kp6", SDLK_KP6, 0, "+right");
	add_key("kp7", SDLK_KP7);
	add_key("kp8", SDLK_KP8, 0, "+down");
	add_key("kp9", SDLK_KP9);
 
	add_key("kpperiod", SDLK_KP_PERIOD, '.');
	add_key("kpdiv", SDLK_KP_DIVIDE, '/', "+rollleft");
	add_key("kpmul", SDLK_KP_MULTIPLY, '*', "+rollright");
	add_key("kpmin", SDLK_KP_MINUS, '-', "-thrust");
	add_key("kpplus", SDLK_KP_PLUS, '+', "+thrust");
	add_key("kpenter", SDLK_KP_ENTER, '\n');
	add_key("kpequal", SDLK_KP_EQUALS, '=');
	
	add_key("up", SDLK_UP, 0, "+camup");
	add_key("down", SDLK_DOWN, 0, "+camdown");
	add_key("right", SDLK_RIGHT, 0, "+camright");
	add_key("left", SDLK_LEFT, 0, "+camleft");

	add_key("insert", SDLK_INSERT);
	add_key("home", SDLK_HOME);
	add_key("end", SDLK_END);
	add_key("pageup", SDLK_PAGEUP);
	add_key("pagedown", SDLK_PAGEDOWN);

	add_key("f1", SDLK_F1);
	add_key("f2", SDLK_F2);
	add_key("f3", SDLK_F3);
	add_key("f4", SDLK_F4);
	add_key("f5", SDLK_F5);
	add_key("f6", SDLK_F6);
	add_key("f7", SDLK_F7);
	add_key("f8", SDLK_F8);
	add_key("f9", SDLK_F9);
	add_key("f10", SDLK_F11);
	add_key("f11", SDLK_F11);
	add_key("f12", SDLK_F12);
	add_key("f13", SDLK_F13);
	add_key("f14", SDLK_F14);
	add_key("f15", SDLK_F15);
	
	add_key("numlock", SDLK_NUMLOCK);
	add_key("capslock", SDLK_CAPSLOCK);
	add_key("scrollock", SDLK_SCROLLOCK);

	add_key("rshift", SDLK_RSHIFT);
	add_key("lshift", SDLK_LSHIFT);

	add_key("rctrl", SDLK_RCTRL);
	add_key("lctrl", SDLK_LCTRL);

	add_key("ralt", SDLK_RALT);
	add_key("lalt", SDLK_LALT);

	add_key("rmeta", SDLK_RMETA);
	add_key("lmeta", SDLK_LMETA);
	add_key("lwin", SDLK_LSUPER);
	add_key("rwin", SDLK_RSUPER);
	add_key("mode", SDLK_MODE);

	add_key("help", SDLK_HELP);
	add_key("print", SDLK_PRINT, 0, "screenshot");
	add_key("sysrq", SDLK_SYSREQ);
	add_key("breal", SDLK_BREAK);
	add_key("menu", SDLK_MENU);
	add_key("power", SDLK_POWER);
	add_key("euro", SDLK_EURO);

	// mouse key aliases
	add_key("mouse1", 512 + SDL_BUTTON_LEFT, 0, "+control");
	add_key("mouse2", 512 + SDL_BUTTON_RIGHT);
	add_key("mouse3", 512 + SDL_BUTTON_MIDDLE);

	add_key("mouse4", 512 + SDL_BUTTON_WHEELUP, 0, "+thrust");
	add_key("mouse5", 512 + SDL_BUTTON_WHEELDOWN, 0, "-thrust");
}

Keyboard::~Keyboard()
{
	for(iterator it = begin(); it != end(); it++)
		delete (*it).second;

	keys.clear();
}

void Keyboard::save_binds()
{
	std::string filename(filesystem::writedir);
	filename.append("binds.cfg");
	std::ofstream ofs(filename.c_str());

	if (!ofs.is_open()) {
		con_warn << "Could not write " << filename << std::endl;
		return;
	}

	con_print << "  writing keyboard binds to " << filename << std::endl;

	ofs << "# binds.cfg - osirion keyboard binds" << std::endl;
	ofs << "# this file is automaticly generated" << std::endl;

	iterator it;
	for (it = begin(); it != end(); it++) {
		Key *key = (*it).second;
		if (key->bind().size()) {
			ofs << "bind " << key->name() << " " << key->bind()  << std::endl;
		}
		/*
		} else {
			ofs << "unbind " << key->name() << std::endl;
		}
		*/
	}
	ofs.close();
}

void Keyboard::load_binds()
{
	std::string filename(filesystem::writedir);
	filename.append("binds.cfg");
	std::ifstream ifs(filename.c_str(), std::ifstream::in);

	if (!ifs.is_open()) {
		con_warn << "Could not read " << filename << std::endl;
		return;
	}

	con_print << "  reading keyboard binds from " << filename << std::endl;

	char line[MAXCMDSIZE];
	while (ifs.getline(line, MAXCMDSIZE-1)) {
		if (line[0] && line[0] != '#' && line[0] != ';')
			core::cmd() << line << '\n';
	}
	
	// execute commands in the buffer
	core::CommandBuffer::exec();
}
Key * Keyboard::release(unsigned int sym)
{
	Key *key = find(sym);
	if (!key) {
		return 0;
	}
	
	key->pressed() = 0;
	return key;
}

Key * Keyboard::press(unsigned int sym)
{
	Key *key = find(sym);
	if (!key) {
		return 0;
	}
	
	if (!key->pressed())
		key->pressed() = core::application()->time();

	return key;
}

Key *Keyboard::find(std::string const & name)
{
	Key *key = 0;
	for (iterator it = begin(); it != end() && !key; it++) {
		if ((*it).second->name().compare(name) == 0) {
			key = (*it).second;
		}
	}
	return key;
}

Key *Keyboard::find(unsigned int keysym)
{
	iterator it = keys.find(keysym);
	if (it == end())
		return 0;
	else
		return (*it).second;
}

void Keyboard::bind(std::string const &name, const std::string str)
{
	Key *key = find(name);
	if (key) {
		key->bind().assign(str);
		aux::trim(key->bind());
	} else {
		con_print << "Key '" << name << "' not found." << std::endl;
	}
}

void Keyboard::unbind(std::string const &name)
{
	Key *key = find(name);
	if (key) {
		key->bind().clear();
	} else {
		con_print << "Key '" << name << "' not found." << std::endl;
	}
}

void Keyboard::add_key(const char *name, const unsigned int keysym, const char ascii, const char *bind)
{
	keys[keysym] = new Key(name, keysym, ascii, bind);
}

void Keyboard::list_keys()
{
	for (iterator it = begin(); it != end(); it++) {
		con_print << "  " << aux::pad_left((*it).second->name(), 6) << " " << (*it).second->bind() << std::endl;
	}
	con_print  << keys.size() << " keys" << std::endl;
}

void Keyboard::list_binds()
{
	size_t n =0;
	for (iterator it = begin(); it != end(); it++) {
		if ((*it).second->bind().size()) {
			con_print << "  " << aux::pad_left((*it).second->name(), 6) << " " << (*it).second->bind() << std::endl;
			n++;
		}
	}
	con_print  << n << " binds" << std::endl;
}

void setkeyboardmode(bool input)
{
/*	if(input)
		SDL_EnableKeyRepeat(250,  SDL_DEFAULT_REPEAT_INTERVAL);
	else
		SDL_EnableKeyRepeat(10,  SDL_DEFAULT_REPEAT_INTERVAL);
*/
}

unsigned int translate_keysym(const SDL_keysym &keysym)
{
	
	int key = keysym.sym;
	bool shift = false;	
	bool numlock = false;

	

	// keypad keys
	if (keysym.mod & KMOD_NUM) {
		numlock = true;
		switch(key) {
			case SDLK_KP0:
				return '0';
				break;
			case SDLK_KP1:
				return '1';
				break;
			case SDLK_KP2:
				return '2';
				break;
			case SDLK_KP3:
				return '3';
				break;
			case SDLK_KP4:
				return '4';
				break;
			case SDLK_KP5:
				return '5';
				break;
			case SDLK_KP6:
				return '6';
				break;
			case SDLK_KP7:
				return '7';
				break;
			case SDLK_KP8:
				return '8';
				break;
			case SDLK_KP9:
				return '9';
				break;
		}
	} else {
		switch(key) {
		case SDLK_KP0:
			return SDLK_INSERT;
			break;
		case SDLK_KP1:
			return SDLK_END;
			break;
		case SDLK_KP2:
			return SDLK_DOWN;
			break;
		case SDLK_KP3:
			return SDLK_PAGEDOWN;
			break;
		case SDLK_KP4:
			return SDLK_LEFT;
			break;
		case SDLK_KP6:
			return SDLK_RIGHT;
			break;
		case SDLK_KP7:
			return SDLK_HOME;
			break;
		case SDLK_KP8:
			return SDLK_UP;
			break;
		case SDLK_KP9:
			return SDLK_PAGEUP;
			break;
		}
	}

	// special keys
	switch(key) {
		case SDLK_ESCAPE:
			return SDLK_ESCAPE;
			break;
		case SDLK_KP_ENTER:
			return SDLK_RETURN;
			break;
		case SDLK_KP_PERIOD:
			return '.';
			break;
   		case SDLK_KP_DIVIDE:
			return '/';
			break;
   		case SDLK_KP_MULTIPLY:
			return '*';
			break;
   		case SDLK_KP_MINUS:
			return '-';
			break;
   		case SDLK_KP_PLUS:
			return '+';
			break;
   		case SDLK_KP_EQUALS:
			return '=';
			break;
	}

	// caps lock
	if (keysym.mod & KMOD_CAPS)
		shift = true;

	// left/right shift
	if ((keysym.mod & KMOD_LSHIFT) || (keysym.mod & KMOD_RSHIFT)) {
		shift = !shift;
	}

	if (shift) {
		if ((key >= 'a' && key <= 'z')) {
			key = key + 'A' - 'a';
			return key;
		} 

		switch (key) {
			case '`':
				key = '~';
				break;
			case '1':
				key = '!';
				break;
			case '2':
				key = '@';
				break;
			case '3':
				key = '#';
				break;
			case '4':
				key = '$';
				break;
			case '5':
				key = '%';
				break;
			case '6':
				key = '^';
				break;
			case '7':
				key = '&';
				break;
			case '8':
				key = '*';
				break;
			case '9':
				key = '(';
				break;
			case '0':
				key = ')';
				break;
			case '-':
				key = '_';
				break;
			case '=':
				key = '+';
				break;
			// second row
			case '[':
				key = '{';
				break;
			case ']':
				key = '}';
				break;
			case '|':
				key = '\\';
				break;
			// third row
			case ';':
				key = ':';
				break;
			case '\'':
				key = '"';
				break;
			// fourth row
			case ',':
				key = '<';
				break;
			case '.':
				key = '>';
				break;
			case '/':
				key = '?';
				break;
		}
	}

	return key;
}

} // namespace client