imtk

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