imtk

view 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 source
1 #include "state.h"
2 #include "imtk.h"
4 struct key_node {
5 int key;
6 struct key_node *next;
7 };
10 struct imtk_state st = {
11 1, 1, /* scr_width/scr_height */
12 0, 0, 0, 0, 0, /* mousex/mousey, prevx, prevy, mouse_bnmask */
13 -1, -1, -1, -1 /* active, hot, input, prev_active */
14 };
16 static struct key_node *key_list, *key_tail;
20 void imtk_inp_key(int key, int state)
21 {
22 if(state == IMTK_DOWN) {
23 struct key_node *node;
25 if(!(node = malloc(sizeof *node))) {
26 return;
27 }
28 node->key = key;
29 node->next = 0;
31 if(key_list) {
32 key_tail->next = node;
33 key_tail = node;
34 } else {
35 key_list = key_tail = node;
36 }
37 }
39 imtk_post_redisplay();
40 }
42 void imtk_inp_mouse(int bn, int state)
43 {
44 if(state == IMTK_DOWN) {
45 st.mouse_bnmask |= 1 << bn;
46 } else {
47 st.mouse_bnmask &= ~(1 << bn);
48 }
49 imtk_post_redisplay();
50 }
52 void imtk_inp_motion(int x, int y)
53 {
54 st.mousex = x;
55 st.mousey = y;
57 imtk_post_redisplay();
58 }
60 void imtk_inp_reshape(int x, int y)
61 {
62 st.scr_width = x;
63 st.scr_height = y;
64 }
67 void imtk_set_active(int id)
68 {
69 if(id == -1 || st.hot == id) {
70 st.prev_active = st.active;
71 st.active = id;
72 }
73 }
75 int imtk_is_active(int id)
76 {
77 return st.active == id;
78 }
80 int imtk_set_hot(int id)
81 {
82 if(st.active == -1) {
83 st.hot = id;
84 return 1;
85 }
86 return 0;
87 }
89 int imtk_is_hot(int id)
90 {
91 return st.hot == id;
92 }
94 void imtk_set_focus(int id)
95 {
96 st.input = id;
97 }
99 int imtk_has_focus(int id)
100 {
101 return st.input == id;
102 }
104 int imtk_hit_test(int x, int y, int w, int h)
105 {
106 return st.mousex >= x && st.mousex < (x + w) &&
107 st.mousey >= y && st.mousey < (y + h);
108 }
110 void imtk_get_mouse(int *xptr, int *yptr)
111 {
112 if(xptr) *xptr = st.mousex;
113 if(yptr) *yptr = st.mousey;
114 }
116 void imtk_set_prev_mouse(int x, int y)
117 {
118 st.prevx = x;
119 st.prevy = y;
120 }
122 void imtk_get_prev_mouse(int *xptr, int *yptr)
123 {
124 if(xptr) *xptr = st.prevx;
125 if(yptr) *yptr = st.prevy;
126 }
128 int imtk_button_state(int bn)
129 {
130 return st.mouse_bnmask & (1 << bn);
131 }
134 int imtk_get_key(void)
135 {
136 int key = -1;
137 struct key_node *node = key_list;
139 if(node) {
140 key = node->key;
141 key_list = node->next;
142 if(!key_list) {
143 key_tail = 0;
144 }
145 free(node);
146 }
147 return key;
148 }