imtk

diff src/state.c @ 6:38609a9f7586

reorganizing ...
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Apr 2011 14:22:42 +0300
parents
children 6d35e6c7b2ca
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/state.c	Thu Apr 14 14:22:42 2011 +0300
     1.3 @@ -0,0 +1,148 @@
     1.4 +#include "state.h"
     1.5 +#include "imtk.h"
     1.6 +
     1.7 +struct key_node {
     1.8 +	int key;
     1.9 +	struct key_node *next;
    1.10 +};
    1.11 +
    1.12 +
    1.13 +struct imtk_state st = {
    1.14 +	1, 1,			/* scr_width/scr_height */
    1.15 +	0, 0, 0, 0, 0,	/* mousex/mousey, prevx, prevy, mouse_bnmask */
    1.16 +	-1, -1, -1, -1	/* active, hot, input, prev_active */
    1.17 +};
    1.18 +
    1.19 +static struct key_node *key_list, *key_tail;
    1.20 +
    1.21 +
    1.22 +
    1.23 +void imtk_inp_key(int key, int state)
    1.24 +{
    1.25 +	if(state == IMTK_DOWN) {
    1.26 +		struct key_node *node;
    1.27 +
    1.28 +		if(!(node = malloc(sizeof *node))) {
    1.29 +			return;
    1.30 +		}
    1.31 +		node->key = key;
    1.32 +		node->next = 0;
    1.33 +
    1.34 +		if(key_list) {
    1.35 +			key_tail->next = node;
    1.36 +			key_tail = node;
    1.37 +		} else {
    1.38 +			key_list = key_tail = node;
    1.39 +		}
    1.40 +	}
    1.41 +
    1.42 +	imtk_post_redisplay();
    1.43 +}
    1.44 +
    1.45 +void imtk_inp_mouse(int bn, int state)
    1.46 +{
    1.47 +	if(state == IMTK_DOWN) {
    1.48 +		st.mouse_bnmask |= 1 << bn;
    1.49 +	} else {
    1.50 +		st.mouse_bnmask &= ~(1 << bn);
    1.51 +	}
    1.52 +	imtk_post_redisplay();
    1.53 +}
    1.54 +
    1.55 +void imtk_inp_motion(int x, int y)
    1.56 +{
    1.57 +	st.mousex = x;
    1.58 +	st.mousey = y;
    1.59 +
    1.60 +	imtk_post_redisplay();
    1.61 +}
    1.62 +
    1.63 +void imtk_inp_reshape(int x, int y)
    1.64 +{
    1.65 +	st.scr_width = x;
    1.66 +	st.scr_height = y;
    1.67 +}
    1.68 +
    1.69 +
    1.70 +void imtk_set_active(int id)
    1.71 +{
    1.72 +	if(id == -1 || st.hot == id) {
    1.73 +		st.prev_active = st.active;
    1.74 +		st.active = id;
    1.75 +	}
    1.76 +}
    1.77 +
    1.78 +int imtk_is_active(int id)
    1.79 +{
    1.80 +	return st.active == id;
    1.81 +}
    1.82 +
    1.83 +int imtk_set_hot(int id)
    1.84 +{
    1.85 +	if(st.active == -1) {
    1.86 +		st.hot = id;
    1.87 +		return 1;
    1.88 +	}
    1.89 +	return 0;
    1.90 +}
    1.91 +
    1.92 +int imtk_is_hot(int id)
    1.93 +{
    1.94 +	return st.hot == id;
    1.95 +}
    1.96 +
    1.97 +void imtk_set_focus(int id)
    1.98 +{
    1.99 +	st.input = id;
   1.100 +}
   1.101 +
   1.102 +int imtk_has_focus(int id)
   1.103 +{
   1.104 +	return st.input == id;
   1.105 +}
   1.106 +
   1.107 +int imtk_hit_test(int x, int y, int w, int h)
   1.108 +{
   1.109 +	return st.mousex >= x && st.mousex < (x + w) &&
   1.110 +		st.mousey >= y && st.mousey < (y + h);
   1.111 +}
   1.112 +
   1.113 +void imtk_get_mouse(int *xptr, int *yptr)
   1.114 +{
   1.115 +	if(xptr) *xptr = st.mousex;
   1.116 +	if(yptr) *yptr = st.mousey;
   1.117 +}
   1.118 +
   1.119 +void imtk_set_prev_mouse(int x, int y)
   1.120 +{
   1.121 +	st.prevx = x;
   1.122 +	st.prevy = y;
   1.123 +}
   1.124 +
   1.125 +void imtk_get_prev_mouse(int *xptr, int *yptr)
   1.126 +{
   1.127 +	if(xptr) *xptr = st.prevx;
   1.128 +	if(yptr) *yptr = st.prevy;
   1.129 +}
   1.130 +
   1.131 +int imtk_button_state(int bn)
   1.132 +{
   1.133 +	return st.mouse_bnmask & (1 << bn);
   1.134 +}
   1.135 +
   1.136 +
   1.137 +int imtk_get_key(void)
   1.138 +{
   1.139 +	int key = -1;
   1.140 +	struct key_node *node = key_list;
   1.141 +
   1.142 +	if(node) {
   1.143 +		key = node->key;
   1.144 +		key_list = node->next;
   1.145 +		if(!key_list) {
   1.146 +			key_tail = 0;
   1.147 +		}
   1.148 +		free(node);
   1.149 +	}
   1.150 +	return key;
   1.151 +}