imtk

annotate 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
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@7 60 void imtk_set_viewport(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@7 66 void imtk_get_viewport(int *width, int *height)
nuclear@7 67 {
nuclear@7 68 if(width) *width = st.scr_width;
nuclear@7 69 if(height) *height = st.scr_height;
nuclear@7 70 }
nuclear@7 71
nuclear@6 72
nuclear@6 73 void imtk_set_active(int id)
nuclear@6 74 {
nuclear@6 75 if(id == -1 || st.hot == id) {
nuclear@6 76 st.prev_active = st.active;
nuclear@6 77 st.active = id;
nuclear@6 78 }
nuclear@6 79 }
nuclear@6 80
nuclear@6 81 int imtk_is_active(int id)
nuclear@6 82 {
nuclear@6 83 return st.active == id;
nuclear@6 84 }
nuclear@6 85
nuclear@6 86 int imtk_set_hot(int id)
nuclear@6 87 {
nuclear@6 88 if(st.active == -1) {
nuclear@6 89 st.hot = id;
nuclear@6 90 return 1;
nuclear@6 91 }
nuclear@6 92 return 0;
nuclear@6 93 }
nuclear@6 94
nuclear@6 95 int imtk_is_hot(int id)
nuclear@6 96 {
nuclear@6 97 return st.hot == id;
nuclear@6 98 }
nuclear@6 99
nuclear@6 100 void imtk_set_focus(int id)
nuclear@6 101 {
nuclear@6 102 st.input = id;
nuclear@6 103 }
nuclear@6 104
nuclear@6 105 int imtk_has_focus(int id)
nuclear@6 106 {
nuclear@6 107 return st.input == id;
nuclear@6 108 }
nuclear@6 109
nuclear@6 110 int imtk_hit_test(int x, int y, int w, int h)
nuclear@6 111 {
nuclear@6 112 return st.mousex >= x && st.mousex < (x + w) &&
nuclear@6 113 st.mousey >= y && st.mousey < (y + h);
nuclear@6 114 }
nuclear@6 115
nuclear@6 116 void imtk_get_mouse(int *xptr, int *yptr)
nuclear@6 117 {
nuclear@6 118 if(xptr) *xptr = st.mousex;
nuclear@6 119 if(yptr) *yptr = st.mousey;
nuclear@6 120 }
nuclear@6 121
nuclear@6 122 void imtk_set_prev_mouse(int x, int y)
nuclear@6 123 {
nuclear@6 124 st.prevx = x;
nuclear@6 125 st.prevy = y;
nuclear@6 126 }
nuclear@6 127
nuclear@6 128 void imtk_get_prev_mouse(int *xptr, int *yptr)
nuclear@6 129 {
nuclear@6 130 if(xptr) *xptr = st.prevx;
nuclear@6 131 if(yptr) *yptr = st.prevy;
nuclear@6 132 }
nuclear@6 133
nuclear@6 134 int imtk_button_state(int bn)
nuclear@6 135 {
nuclear@6 136 return st.mouse_bnmask & (1 << bn);
nuclear@6 137 }
nuclear@6 138
nuclear@6 139
nuclear@6 140 int imtk_get_key(void)
nuclear@6 141 {
nuclear@6 142 int key = -1;
nuclear@6 143 struct key_node *node = key_list;
nuclear@6 144
nuclear@6 145 if(node) {
nuclear@6 146 key = node->key;
nuclear@6 147 key_list = node->next;
nuclear@6 148 if(!key_list) {
nuclear@6 149 key_tail = 0;
nuclear@6 150 }
nuclear@6 151 free(node);
nuclear@6 152 }
nuclear@6 153 return key;
nuclear@6 154 }