nuclear@6: #include "state.h" nuclear@6: #include "imtk.h" nuclear@6: nuclear@6: struct key_node { nuclear@6: int key; nuclear@6: struct key_node *next; nuclear@6: }; nuclear@6: nuclear@6: nuclear@6: struct imtk_state st = { nuclear@6: 1, 1, /* scr_width/scr_height */ nuclear@6: 0, 0, 0, 0, 0, /* mousex/mousey, prevx, prevy, mouse_bnmask */ nuclear@6: -1, -1, -1, -1 /* active, hot, input, prev_active */ nuclear@6: }; nuclear@6: nuclear@6: static struct key_node *key_list, *key_tail; nuclear@6: nuclear@6: nuclear@6: nuclear@6: void imtk_inp_key(int key, int state) nuclear@6: { nuclear@6: if(state == IMTK_DOWN) { nuclear@6: struct key_node *node; nuclear@6: nuclear@6: if(!(node = malloc(sizeof *node))) { nuclear@6: return; nuclear@6: } nuclear@6: node->key = key; nuclear@6: node->next = 0; nuclear@6: nuclear@6: if(key_list) { nuclear@6: key_tail->next = node; nuclear@6: key_tail = node; nuclear@6: } else { nuclear@6: key_list = key_tail = node; nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: imtk_post_redisplay(); nuclear@6: } nuclear@6: nuclear@6: void imtk_inp_mouse(int bn, int state) nuclear@6: { nuclear@6: if(state == IMTK_DOWN) { nuclear@6: st.mouse_bnmask |= 1 << bn; nuclear@6: } else { nuclear@6: st.mouse_bnmask &= ~(1 << bn); nuclear@6: } nuclear@6: imtk_post_redisplay(); nuclear@6: } nuclear@6: nuclear@6: void imtk_inp_motion(int x, int y) nuclear@6: { nuclear@6: st.mousex = x; nuclear@6: st.mousey = y; nuclear@6: nuclear@6: imtk_post_redisplay(); nuclear@6: } nuclear@6: nuclear@7: void imtk_set_viewport(int x, int y) nuclear@6: { nuclear@6: st.scr_width = x; nuclear@6: st.scr_height = y; nuclear@6: } nuclear@6: nuclear@7: void imtk_get_viewport(int *width, int *height) nuclear@7: { nuclear@7: if(width) *width = st.scr_width; nuclear@7: if(height) *height = st.scr_height; nuclear@7: } nuclear@7: nuclear@6: nuclear@6: void imtk_set_active(int id) nuclear@6: { nuclear@6: if(id == -1 || st.hot == id) { nuclear@6: st.prev_active = st.active; nuclear@6: st.active = id; nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: int imtk_is_active(int id) nuclear@6: { nuclear@6: return st.active == id; nuclear@6: } nuclear@6: nuclear@6: int imtk_set_hot(int id) nuclear@6: { nuclear@6: if(st.active == -1) { nuclear@6: st.hot = id; nuclear@6: return 1; nuclear@6: } nuclear@6: return 0; nuclear@6: } nuclear@6: nuclear@6: int imtk_is_hot(int id) nuclear@6: { nuclear@6: return st.hot == id; nuclear@6: } nuclear@6: nuclear@6: void imtk_set_focus(int id) nuclear@6: { nuclear@6: st.input = id; nuclear@6: } nuclear@6: nuclear@6: int imtk_has_focus(int id) nuclear@6: { nuclear@6: return st.input == id; nuclear@6: } nuclear@6: nuclear@6: int imtk_hit_test(int x, int y, int w, int h) nuclear@6: { nuclear@6: return st.mousex >= x && st.mousex < (x + w) && nuclear@6: st.mousey >= y && st.mousey < (y + h); nuclear@6: } nuclear@6: nuclear@6: void imtk_get_mouse(int *xptr, int *yptr) nuclear@6: { nuclear@6: if(xptr) *xptr = st.mousex; nuclear@6: if(yptr) *yptr = st.mousey; nuclear@6: } nuclear@6: nuclear@6: void imtk_set_prev_mouse(int x, int y) nuclear@6: { nuclear@6: st.prevx = x; nuclear@6: st.prevy = y; nuclear@6: } nuclear@6: nuclear@6: void imtk_get_prev_mouse(int *xptr, int *yptr) nuclear@6: { nuclear@6: if(xptr) *xptr = st.prevx; nuclear@6: if(yptr) *yptr = st.prevy; nuclear@6: } nuclear@6: nuclear@6: int imtk_button_state(int bn) nuclear@6: { nuclear@6: return st.mouse_bnmask & (1 << bn); nuclear@6: } nuclear@6: nuclear@6: nuclear@6: int imtk_get_key(void) nuclear@6: { nuclear@6: int key = -1; nuclear@6: struct key_node *node = key_list; nuclear@6: nuclear@6: if(node) { nuclear@6: key = node->key; nuclear@6: key_list = node->next; nuclear@6: if(!key_list) { nuclear@6: key_tail = 0; nuclear@6: } nuclear@6: free(node); nuclear@6: } nuclear@6: return key; nuclear@6: }