imtk

view src/state.c @ 27:48e708baa7be

added predicate imtk_layout_contains(x, y)
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 12 Dec 2020 17:20:00 +0200
parents 38609a9f7586
children
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_set_viewport(int x, int y)
61 {
62 st.scr_width = x;
63 st.scr_height = y;
64 }
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 }
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 }
81 int imtk_is_active(int id)
82 {
83 return st.active == id;
84 }
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 }
95 int imtk_is_hot(int id)
96 {
97 return st.hot == id;
98 }
100 void imtk_set_focus(int id)
101 {
102 st.input = id;
103 }
105 int imtk_has_focus(int id)
106 {
107 return st.input == id;
108 }
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 }
116 void imtk_get_mouse(int *xptr, int *yptr)
117 {
118 if(xptr) *xptr = st.mousex;
119 if(yptr) *yptr = st.mousey;
120 }
122 void imtk_set_prev_mouse(int x, int y)
123 {
124 st.prevx = x;
125 st.prevy = y;
126 }
128 void imtk_get_prev_mouse(int *xptr, int *yptr)
129 {
130 if(xptr) *xptr = st.prevx;
131 if(yptr) *yptr = st.prevy;
132 }
134 int imtk_button_state(int bn)
135 {
136 return st.mouse_bnmask & (1 << bn);
137 }
140 int imtk_get_key(void)
141 {
142 int key = -1;
143 struct key_node *node = key_list;
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 }