gbasys

view src/term.c @ 12:a6ddf338a111

line clipping
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 01 Feb 2016 04:41:27 +0200
parents 06726f0b8cd3
children
line source
1 /*
2 gbasys - a gameboy advance hardware abstraction library
3 Copyright (C) 2004-2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdlib.h>
19 #include "term.h"
20 #include "input.h"
21 #include "signal.h"
22 #include "intr.h"
24 static struct key_node *key_queue, *key_queue_tail;
26 static void key_handler(void);
28 void term_init(void) {
29 mask(INTR_KEY);
30 interrupt(INTR_KEY, key_handler);
31 unmask(INTR_KEY);
33 key_queue = malloc(sizeof *key_queue);
34 key_queue->next = 0;
35 key_queue_tail = key_queue;
36 }
38 static void noop(int sig) {}
40 int gba_getc(FILE *fp) {
41 struct key_node *tmp;
42 int c;
44 if(fp != stdin) panic("getc: only stdin valid");
46 save_signal(SIGTTIN);
47 signal(SIGTTIN, noop);
48 while(!key_queue->next) pause();
49 restore_signal(SIGTTIN);
51 tmp = key_queue;
52 key_queue = key_queue->next;
53 free(tmp);
54 return key_queue->key;
55 }
57 static void key_handler(void) {
58 int i, state;
59 static unsigned long prev;
60 unsigned long time;
62 time = get_millisec();
63 if(time - prev < 100) return;
64 prev = time;
66 state = get_key_state(KEY_ALL);
67 for(i=0; i<KEY_COUNT; i++) {
68 int bit = (1 << i);
69 if(state & bit) {
70 struct key_node *key = malloc(sizeof *key);
71 key->key = bit;
72 key->next = 0;
73 key_queue_tail->next = key;
74 key_queue_tail = key;
75 }
76 }
77 raise(SIGTTIN);
78 }