- added libdrawtext
[demo_prior] / src / imtk / state.c
1 #include "state.h"
2 #include "imtk.h"
3
4 struct key_node {
5         int key;
6         struct key_node *next;
7 };
8
9
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 };
15
16 static struct key_node *key_list, *key_tail;
17
18
19
20 void imtk_inp_key(int key, int state)
21 {
22         if(state == IMTK_DOWN) {
23                 struct key_node *node;
24
25                 if(!(node = malloc(sizeof *node))) {
26                         return;
27                 }
28                 node->key = key;
29                 node->next = 0;
30
31                 if(key_list) {
32                         key_tail->next = node;
33                         key_tail = node;
34                 } else {
35                         key_list = key_tail = node;
36                 }
37         }
38
39         imtk_post_redisplay();
40 }
41
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 }
51
52 void imtk_inp_motion(int x, int y)
53 {
54         st.mousex = x;
55         st.mousey = y;
56
57         imtk_post_redisplay();
58 }
59
60 void imtk_set_viewport(int x, int y)
61 {
62         st.scr_width = x;
63         st.scr_height = y;
64 }
65
66 void imtk_get_viewport(int *width, int *height)
67 {
68         if(width) *width = st.scr_width;
69         if(height) *height = st.scr_height;
70 }
71
72
73 void imtk_set_active(int id)
74 {
75         if(id == -1 || st.hot == id) {
76                 st.prev_active = st.active;
77                 st.active = id;
78         }
79 }
80
81 int imtk_is_active(int id)
82 {
83         return st.active == id;
84 }
85
86 int imtk_set_hot(int id)
87 {
88         if(st.active == -1) {
89                 st.hot = id;
90                 return 1;
91         }
92         return 0;
93 }
94
95 int imtk_is_hot(int id)
96 {
97         return st.hot == id;
98 }
99
100 void imtk_set_focus(int id)
101 {
102         st.input = id;
103 }
104
105 int imtk_has_focus(int id)
106 {
107         return st.input == id;
108 }
109
110 int imtk_hit_test(int x, int y, int w, int h)
111 {
112         return st.mousex >= x && st.mousex < (x + w) &&
113                 st.mousey >= y && st.mousey < (y + h);
114 }
115
116 void imtk_get_mouse(int *xptr, int *yptr)
117 {
118         if(xptr) *xptr = st.mousex;
119         if(yptr) *yptr = st.mousey;
120 }
121
122 void imtk_set_prev_mouse(int x, int y)
123 {
124         st.prevx = x;
125         st.prevy = y;
126 }
127
128 void imtk_get_prev_mouse(int *xptr, int *yptr)
129 {
130         if(xptr) *xptr = st.prevx;
131         if(yptr) *yptr = st.prevy;
132 }
133
134 int imtk_button_state(int bn)
135 {
136         return st.mouse_bnmask & (1 << bn);
137 }
138
139
140 int imtk_get_key(void)
141 {
142         int key = -1;
143         struct key_node *node = key_list;
144
145         if(node) {
146                 key = node->key;
147                 key_list = node->next;
148                 if(!key_list) {
149                         key_tail = 0;
150                 }
151                 free(node);
152         }
153         return key;
154 }