~festival-core-team/festival-os/trunk

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
/*  festival-the multimedia operating system
 *  file: kb.cpp
 *  By:   jtlb
 *  Desc: implement the kernel's keyboard driver
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.*/

#include <kb.h>

keyboard* keyboard::inst=0;

//layouts
const char qwerty[4*93] = {
    //Simple
     0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 45, 61, '\b', '\t', 113, 119,
     101, 114, 116, 121, 117, 105, 111, 112, 91, 93, '\n', 0, 97, 115, 100, 102,
     103, 104, 106, 107, 108, 59, 39, 146, 0, 92, 122, 120, 99, 118, 98, 110,
     109, 44, 46, 47, 0, 42, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 45, 0, 0, 0, 43, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

    //Shift
     0, 0, 33, 64, 35, 36, 37, 94, 38, 42, 40, 95, 41, 43, '\b', '\t', 81, 87, 69,
     82, 84, 89, 85, 73, 79, 80, 123, 125, '\n', 0, 65, 83, 68, 70, 71, 72, 74,
     75, 76, 58, 34, 126, 0, 124, 90, 88, 67, 86, 66, 78, 77, 60, 62, 63, 0, 42,
     0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55, 56, 57, 45, 52, 53, 54,
     43, 49, 50, 51, 48, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0,

    //Alt Gr
     0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 45, 61, '\b', '\t', 113, 119,
     128, 114, 116, 121, 117, 105, 111, 112, 91, 93, '\n', 0, 97, 115, 100, 102,
     103, 104, 106, 107, 108, 59, 39, 146, 0, 92, 122, 120, 99, 118, 98, 110,
     109, 44, 46, 47, 0, 42, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55,
     56, 57, 45, 52, 53, 54, 43, 49, 50, 51, 48, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0,

    //Verr num
     0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 45, 61, '\b', '\t', 113, 119,
     101, 114, 116, 121, 117, 105, 111, 112, 91, 93, '\n', 0, 97, 115, 100, 102,
     103, 104, 106, 107, 108, 59, 39, 146, 0, 92, 122, 120, 99, 118, 98, 110,
     109, 44, 46, 47, 0, 42, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55,
     56, 57, 45, 52, 53, 54, 43, 49, 50, 51, 48, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

    ///          AZERTY

const char azerty[4*93] = {

    //Simple
    0, 0, 38, 233, 34, 39, 40, 45, 232, 95,
    231, 224, 41, 61, '\b', '\t', 97, 122, 101, 114,
    116, 121, 117, 105, 111, 112, 94, 36, '\n', 0,
    113, 115, 100, 102, 103, 104, 106, 107, 108, 109,
    249, 253, 0, 42, 119, 120, 99, 118, 98, 110,
    44, 59, 58, 33, 0, 0, 0, 32, 0, 0,
    0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 60, 0, 0, 0, 0, 0, 0,

    //Shift
     0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 41, 43, '\b', '\t',
     65, 90, 69, 82, 84, 89, 85, 73, 79, 80, 168, 163, '\n', 0, 81, 83,
     68, 70, 71, 72, 74, 75, 76, 77, 37, 253, 0, 181, 87, 88, 67, 86,
     66, 78, 63, 46, 47, 36, 0, 42, 0, 32, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 55, 56, 57, 45, 52, 53, 54, 43, 49,
     50, 51, 48, 46, 0, 0, 62, 0, 0, 0, 0, 0, 0,

    //Alt Gr
     0, 0, 0, 126, 35, 123, 91, 124, 96, 92, 136, 64, 93, 125, '\b', '\t', 97,
     122, 128, 114, 116, 121, 117, 105, 111, 112, 94, 164, '\n', 0, 113, 115,
     100, 102, 103, 104, 106, 107, 108, 109, 249, 253, 0, 60, 119, 120, 99, 118,
     98, 110, 44, 59, 58, 33, 0, 42, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 45, 0, 0, 0, 43, 0, 0, 0, 0, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0,

    //Verr num
     0, 0, 38, 233, 34, 39, 40, 45, 232, 95, 231, 224, 41, 61, '\b', '\t',
     97, 122, 101, 114, 116, 121, 117, 105, 111, 112, 94, 36, '\n', 0, 113, 115,
     100, 102, 103, 104, 106, 107, 108, 109, 249, 253, 0, 42, 119, 120, 99, 118,
     98, 110, 44, 59, 58, 33, 0, 42, 0, 32, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 55, 56, 57, 45, 52, 53, 54, 43, 49,
     50, 51, 48, 46, 0, 0, 60, 0, 0, 0, 0, 0, 0
};

/* Handles the keyboard interrupt */

void keyboard::handler(struct regs *r)
{
    uint8_t scancode;
    bool released  = false;
    uint16_t msg;

    scancode = ::inportb(0x60);
    if(scancode == 0x1)
        debugging = false;
    if (scancode & 0x80)//key released
        released = true;
    if (scancode == 0xe0)
    {
    	extended = true;
    	return;
    }
    scancode &= 0x7f;
    if(debugging && !released)
    {
        printf(" => Key pressed: scancode = |%#x|; ", scancode);
        if(extended)
            printf("extended = true\n");
        else
            printf("extended = false\n");
        return;
    }

    switch(scancode)
    {
        case 0x1:
        {
            msg = KB_EVT_ESCAPE;
            break;
        }
        case 0x4b://<-
        {
            msg = KB_EVT_ARROW_RIGHT;
            break;
        }
        case 0x4d://->
        {
            msg = KB_EVT_ARROW_RIGHT;
            break;
        }
        case 0x50://
        {
            msg = KB_EVT_ARROW_DOWN;
            break;
        }
        case 0x48://
        {
            msg = KB_EVT_ARROW_UP;
            break;
        }
        case 0x1c://
        {
            msg = KB_EVT_ENTER;
            if(!released)
                add_char_to_ring('\n');//fixme
            break;
        }
        case 0xe://
        {
            msg = KB_EVT_BACKSPACE;
            if(!released)
                add_char_to_ring('\b');
            break;
        }
        case 0x52://
        {
            msg = KB_EVT_INSER;
            break;
        }
        case 0x47://
        {
            msg = KB_EVT_HOME;
            break;
        }
        case 0x49://
        {
            msg = KB_EVT_PAGE_UP;
            break;
        }
        case 0x51://
        {
            msg = KB_EVT_PAGE_DOWN;
            break;
        }
        case 0x4f://
        {
            msg = KB_EVT_END;
            break;
        }
        case 0x53://
        {
            msg = KB_EVT_SUPPR;
            break;
        }
        case 0xf://
        {
            msg = KB_EVT_TAB;
            if(!released)
                add_char_to_ring('\t');
            break;
        }
        case 0x37://
        {
            msg = KB_EVT_PRINT_SCREEN;
            break;
        }
        case 0x1d://ctrl
        {
            msg = KB_EVT_CTRL;
            ctrl = !released;
            break;
        }
        case 0x2a://left shift
        {
            msg = KB_EVT_SHIFT;
            shift = !released;
            break;
        }
        case 0x36://right shift
        {
            msg = KB_EVT_SHIFT;
            msg |= KB_EVT_RIGHT;
            shift = !released;
            break;
        }
        case 0x38://alt ou altgr
        {
            msg = KB_EVT_ALT;
            if (extended)
            {
                altgr = !released;
            }
            else
            {
                msg |= KB_EVT_RIGHT;
                alt = !released;
            }
            break;
        }
        case 0x45://verrnum
        {
            if(extended)//TODO: handle the pause key
            {
                //msg = KB_EVT_NUM_PAUSE;
            }
            else
            {
                msg = KB_EVT_NUM_LOCK;
                ver_num = !ver_num;
                update_leds();
            }
            break;
        }
        case 0x3a://caps lock
        {
            msg = KB_EVT_CAPS_LOCK;
            caps_lock = !caps_lock;
            update_leds();
            break;
        }
        case 0x46://scroll lock
        {
            msg = KB_EVT_SCROLL_LOCK;
            scroll_lock = !scroll_lock;
            update_leds();
            break;
        }
        case 0x3b:
        {
            msg = KB_EVT_F1;
            break;
        }
        case 0x3c:
        {
            msg = KB_EVT_F2;
            break;
        }
        case 0x3d:
        {
            msg = KB_EVT_F3;
            break;
        }
        case 0x3e:
        {
            msg = KB_EVT_F4;
            break;
        }
        case 0x3f:
        {
            msg = KB_EVT_F5;
            break;
        }
        case 0x40:
        {
            msg = KB_EVT_F6;
            break;
        }
        case 0x41:
        {
            msg = KB_EVT_F7;
            break;
        }
        case 0x42:
        {
            msg = KB_EVT_F8;
            break;
        }
        case 0x43:
        {
            msg = KB_EVT_F9;
            break;
        }
        case 0x44:
        {
            msg = KB_EVT_F10;
            break;
        }
        case 0x57:
        {
            msg = KB_EVT_F11;
            break;
        }
        case 0x58:
        {
            msg = KB_EVT_F12;
            break;
        }
        default://fixme: handle num pad corectly
        {
            msg = scan_code_to_ascii(scancode,extended);
            msg |= KB_EVT_KEY_ASCII;
            if(!released)
                add_char_to_ring(msg);//fixme: to be removed
        }

    }
//    if (scancode == 110)//escape key
//    {
//        return;
//    }

    //let us build the message
    if(!released)
        msg |= KB_EVT_KEY_PRESSED;
    if(ver_num)
        msg |= KB_EVT_NUM_LOCK_ON;
    if(caps_lock)
        msg |= KB_EVT_CAPS_LOCK_ON;
    if(scroll_lock)
        msg |= KB_EVT_SCROLL_LOCK_ON;

    gabriel::getinstance()->send_message(message(addr,msg),address(1,1));

    if(scancode == 0x53)//suppr
    {
        if(ctrl && (alt || altgr))
            reboot();
    }

}

char keyboard::scan_code_to_ascii(uint8_t scan, bool ext)
{
    scan &= 0x7f;//clear last bit
    if(ext)
        return 0;//key with extended sancodes are NEVER a caracter!!
    if (altgr)
        return layout[2*93 + scan];
    if (shift && !caps_lock || !shift && caps_lock)
        return layout[1*93 + scan];
    if (ver_num)
        return layout[3*93 + scan];
    return layout[3*93 + scan];
}

void keyboard::update_leds()
{
    uint8_t tmp = 0;
    if(scroll_lock)
        tmp |= 1;
    if(ver_num)
        tmp |= 2;
    if(caps_lock)
        tmp |= 4;
    while ((inportb(0x64) & 2) != 0){}//wait for the controller to be ready
    outportb(0x60,0xed);//send command "update leds" to command/data port
    while ((inportb(0x64) & 2) != 0){}//wait for the controller to be ready
    outportb(0x60,tmp);//send the new state
}

bool keyboard::add_char_to_ring(char c)
{
    if(!c)
        return false;
    if(debugging)
    {
        printf("%c", c);
        return true;//do not add it to the ring
    }
    if ((last+1 == first) || (last == 1023 && first == 0))
	{
		printf("ring overflow!!!\n");
		return false;
	}
	if (c == 0)
	{
	    return false;
	}
	else
	{
		ring[last] = c;
		if (last == 1023)
            last = 0;
        else
            last++;
		return true;
	}
}

char keyboard::getchar()
{
	while(first == last)
	{
		irq_inst->wait(1);//attend l'interruption clavier
	}

    char tmp = ring[first];
    if(tmp != '\b')
		printf("%c", tmp);
	if (first == 1023)
        first = 0;
    else
        first++;
	return tmp;
}

void keyboard::getline(char* dest, size_t len, char escape)
{
    uint32_t i = 0;
	for(; i < len;)
	{
		dest[i] = getchar();
		if(dest[i] == escape)
		{
			if(dest[i-1] == '\\')//si == \, mais en cpp il en faut deux
			{//on ne prend pas en compte l'info(ni le \)
				i--;
				continue;
			}
			else
			{
				dest[i] = 0;//conclue la ligne
				return;
			}
		}
		if(dest[i] == '\b')//si retours arrière
		{
			if(i>0)
			{
				if(dest[i-1] == '\t')//si on avais une tabulation
					printf("\b\b\b");//on efface
				printf("\b");//on efface
				i--;
			}
			continue;
		}
		i++;
	}
	dest[i] = 0;
	return;
}

/* Installs the keyboard handler into IRQ1 */
keyboard::keyboard()
{
    gab_inst = gabriel::getinstance();
    addr = gab_inst->register_device(DEVICE_FAMILY_KEYBOARD);
    irq_inst = irq::getinstance();
    irq_inst->register_handler(1, this);

    layout=(const char*)qwerty;

    ctrl  = false;
    shift = false;
    alt   = false;
    altgr = false;

    ver_num     = false;
    caps_lock   = false;
    scroll_lock = false;

    extended    = false;
    debugging   = false;

    first = 0;
    last  = 0;

    update_leds();

    inst = this;
}

void keyboard::debug_mode()
{
    debugging = true;
    while(debugging)
    {
        sleep(10);
    }
}

void keyboard::chooselayout(int32_t num)
{
    switch( num)
    {
        case 1:
            layout=(const char*)azerty;
            break;
        case 2:
            layout=(const char*)qwerty;
            break;
    }
}

void pause()
{
    irq* irq_inst = irq::getinstance();
    irq_inst->reset_fired_flag(1);
	printf("\nPress any key to continue...\n");
	if(!irq_inst)
		return;
	irq_inst->wait(1);//attend l'interruption clavier
	keyboard::getinstance()->getchar();//retire le dernier caractère....
}