gbasys

view src/term.c @ 2:e3dc7705ad9c

communications stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 07 Mar 2012 06:11:51 +0200
parents 875ef6085efc
children 06726f0b8cd3
line source
1 /*
2 Copyright 2004-2012 John Tsiombikas <nuclear@member.fsf.org>
4 This file is part of gbasys, a library for GameBoy Advance development.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 #include <stdlib.h>
22 #include "term.h"
23 #include "input.h"
24 #include "signal.h"
25 #include "intr.h"
27 static struct key_node *key_queue, *key_queue_tail;
29 static void key_handler(void);
31 void term_init(void) {
32 mask(INTR_KEY);
33 interrupt(INTR_KEY, key_handler);
34 unmask(INTR_KEY);
36 key_queue = malloc(sizeof *key_queue);
37 key_queue->next = 0;
38 key_queue_tail = key_queue;
39 }
41 static void noop(int sig) {}
43 int gba_getc(FILE *fp) {
44 struct key_node *tmp;
45 int c;
47 if(fp != stdin) panic("getc: only stdin valid");
49 save_signal(SIGIO);
50 signal(SIGIO, noop);
51 while(!key_queue->next) pause();
52 restore_signal(SIGIO);
54 tmp = key_queue;
55 key_queue = key_queue->next;
56 free(tmp);
57 return key_queue->key;
58 }
60 static void key_handler(void) {
61 int i, state;
62 static unsigned long prev;
63 unsigned long time;
65 time = get_millisec();
66 if(time - prev < 100) return;
67 prev = time;
69 state = get_key_state(KEY_ALL);
70 for(i=0; i<KEY_COUNT; i++) {
71 int bit = (1 << i);
72 if(state & bit) {
73 struct key_node *key = malloc(sizeof *key);
74 key->key = bit;
75 key->next = 0;
76 key_queue_tail->next = key;
77 key_queue_tail = key;
78 }
79 }
80 raise(SIGIO);
81 }