gbasys

diff src/term.c @ 0:875ef6085efc

gbasys mercurial repository
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 04 Mar 2012 04:04:25 +0200
parents
children e3dc7705ad9c
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/term.c	Sun Mar 04 04:04:25 2012 +0200
     1.3 @@ -0,0 +1,81 @@
     1.4 +/*
     1.5 +Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
     1.6 +
     1.7 +This file is part of libgba, a library for GameBoy Advance development.
     1.8 +
     1.9 +This program is free software; you can redistribute it and/or modify
    1.10 +it under the terms of the GNU General Public License as published by
    1.11 +the Free Software Foundation; either version 2 of the License, or
    1.12 +(at your option) any later version.
    1.13 +
    1.14 +This program is distributed in the hope that it will be useful,
    1.15 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 +GNU General Public License for more details.
    1.18 +
    1.19 +You should have received a copy of the GNU General Public License
    1.20 +along with this program; if not, write to the Free Software
    1.21 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.22 +*/
    1.23 +
    1.24 +#include <stdlib.h>
    1.25 +#include "term.h"
    1.26 +#include "input.h"
    1.27 +#include "signal.h"
    1.28 +#include "intr.h"
    1.29 +
    1.30 +static struct key_node *key_queue, *key_queue_tail;
    1.31 +
    1.32 +static void key_handler(void);
    1.33 +
    1.34 +void term_init(void) {
    1.35 +	mask(INTR_KEY);
    1.36 +	interrupt(INTR_KEY, key_handler);
    1.37 +	unmask(INTR_KEY);
    1.38 +
    1.39 +	key_queue = malloc(sizeof *key_queue);
    1.40 +	key_queue->next = 0;
    1.41 +	key_queue_tail = key_queue;
    1.42 +}
    1.43 +
    1.44 +static void noop(int sig) {}
    1.45 +
    1.46 +int gba_getc(FILE *fp) {
    1.47 +	struct key_node *tmp;
    1.48 +	int c;
    1.49 +
    1.50 +	if(fp != stdin) panic("getc: only stdin valid");
    1.51 +	
    1.52 +	save_signal(SIGIO);
    1.53 +	signal(SIGIO, noop);
    1.54 +	while(!key_queue->next) pause();
    1.55 +	restore_signal(SIGIO);
    1.56 +
    1.57 +	tmp = key_queue;
    1.58 +	key_queue = key_queue->next;
    1.59 +	free(tmp);
    1.60 +	return key_queue->key;
    1.61 +}
    1.62 +
    1.63 +static void key_handler(void) {
    1.64 +	int i, state;
    1.65 +	static unsigned long prev;
    1.66 +	unsigned long time;
    1.67 +
    1.68 +	time = get_millisec();
    1.69 +	if(time - prev < 100) return;
    1.70 +	prev = time;
    1.71 +
    1.72 +	state = get_key_state(KEY_ALL);
    1.73 +	for(i=0; i<KEY_COUNT; i++) {
    1.74 +		int bit = (1 << i);
    1.75 +		if(state & bit) {
    1.76 +			struct key_node *key = malloc(sizeof *key);
    1.77 +			key->key = bit;
    1.78 +			key->next = 0;
    1.79 +			key_queue_tail->next = key;
    1.80 +			key_queue_tail = key;
    1.81 +		}
    1.82 +	}
    1.83 +	raise(SIGIO);
    1.84 +}