summaryrefslogtreecommitdiffstats
path: root/dispatch.c
diff options
context:
space:
mode:
authorJames <git@panaceas.org>2014-05-05 17:50:20 +0100
committerJames <git@panaceas.org>2014-05-05 17:50:20 +0100
commit470457e22a1b5537013603d5e367c51e47bb61bf (patch)
tree6b72d32bfd9eaec31c8c520d18782ccaebc01759 /dispatch.c
downloadkmd_usb-470457e22a1b5537013603d5e367c51e47bb61bf.tar.gz
kmd_usb-470457e22a1b5537013603d5e367c51e47bb61bf.tar.bz2
kmd_usb-470457e22a1b5537013603d5e367c51e47bb61bf.zip
fish
Diffstat (limited to 'dispatch.c')
-rw-r--r--dispatch.c233
1 files changed, 233 insertions, 0 deletions
diff --git a/dispatch.c b/dispatch.c
new file mode 100644
index 0000000..ec8da7b
--- /dev/null
+++ b/dispatch.c
@@ -0,0 +1,233 @@
+#include "project.h"
+
+#define N_SCREENS 4
+#define GRACE 10
+
+#define W 2048
+#define H 2048
+
+static int current_computer = 0;
+static int keys_down;
+static int current_x, current_y;
+static int mouse_button[0x10];
+
+
+void
+dispatch_key (int key, int ud)
+{
+ static int keys[KEY_MAX];
+
+// ignore autorepeat
+ if (ud == 2)
+ return;
+
+ if (keys[key] != ud)
+ {
+ keys[key] = ud;
+ if (ud)
+ keys_down++;
+ else
+ keys_down--;
+ }
+
+ if (ud && keys[KEY_RIGHTALT] && (key == KEY_RIGHTMETA))
+ {
+ lcd_off();
+ output_reset ();
+ status_reset ();
+ }
+
+ if (ud && keys[KEY_LEFTALT] && (key == KEY_LEFTMETA))
+ layout_toggle_lock (current_computer);
+
+ send_keyboard_event (current_computer, key, ud);
+}
+
+void
+dispatch_mouse_button (int button, int ud)
+{
+
+ if (mouse_button[button] != ud)
+ {
+ mouse_button[button] = ud;
+ if (ud)
+ keys_down++;
+ else
+ keys_down--;
+ }
+
+ send_mouse_event (current_computer, current_x, current_y, 0,
+ mouse_button[0], mouse_button[2], mouse_button[1]);
+}
+
+
+void
+dispatch_mouse_wheel (int d)
+{
+ send_mouse_event (current_computer, current_x, current_y, d,
+ mouse_button[0], mouse_button[2], mouse_button[1]);
+}
+
+static int
+accell (int a)
+{
+
+ int m = a / 10;
+
+ if (m < 0)
+ m = -m;
+ if (m == 0)
+ m = 1;
+ if (m > 4)
+ m = 4;
+
+ return m * a;
+}
+
+
+void
+dispatch_mouse_motion (int rx, int ry)
+{
+
+ ry += ry;
+
+ rx = accell (rx);
+ ry = accell (ry);
+
+ current_x += rx;
+ current_y += ry;
+
+ if (current_y < 0)
+ current_y = 0;
+ if (current_y >= H)
+ current_y = H - 1;
+
+ if (current_x < 0)
+ {
+ if (map_grace_left (current_computer))
+ {
+ if (keys_down)
+ {
+ current_x = 0;
+ }
+ else if (current_x < -GRACE)
+ {
+ if (map_switch_left (&current_computer))
+ {
+ current_x = W - 1;
+ }
+ else
+ {
+ current_x = -GRACE;
+ }
+ }
+ }
+ else
+ {
+ if (map_switch_left (&current_computer))
+ {
+ current_x = W - 1;
+ }
+ else
+ {
+ current_x = 0;
+ }
+ }
+ }
+
+ if (current_x >= W)
+ {
+ if (map_grace_right (current_computer))
+ {
+ if (keys_down)
+ {
+ current_x = W - 1;
+ }
+ else if (current_x > (W + GRACE))
+ {
+ if (map_switch_right (&current_computer))
+ {
+ current_x = 0;
+ }
+ else
+ {
+ current_x = W + GRACE;
+ }
+ }
+ }
+ else
+ {
+ if (map_switch_right (&current_computer))
+ {
+ current_x = 0;
+ }
+ else
+ {
+ current_x = W - 1;
+ }
+ }
+ }
+
+ send_mouse_event (current_computer, current_x, current_y, 0,
+ mouse_button[0], mouse_button[2], mouse_button[1]);
+}
+
+
+void
+dispatch_event (struct input_event *ev)
+{
+ static int rel_x, rel_y, rel_wheel;
+
+ if (ev->type == EV_REL)
+ {
+
+ if (ev->code == REL_X)
+ rel_x = ev->value;
+ if (ev->code == REL_Y)
+ rel_y = ev->value;
+ if (ev->code == REL_WHEEL)
+ rel_wheel = ev->value;
+ }
+ else if (ev->type == EV_KEY)
+ {
+ switch (ev->code)
+ {
+ case BTN_LEFT:
+ case BTN_RIGHT:
+ case BTN_MIDDLE:
+ case BTN_SIDE:
+ case BTN_EXTRA:
+ case BTN_FORWARD:
+ case BTN_BACK:
+ case BTN_TASK:
+ dispatch_mouse_button (ev->code - BTN_LEFT, ev->value);
+ break;
+ default:
+ dispatch_key (ev->code, ev->value);
+ }
+ }
+ else if (ev->type == EV_SYN)
+ {
+ if (rel_x || rel_y)
+ {
+ dispatch_mouse_motion (rel_x, rel_y);
+ rel_x = 0;
+ rel_y = 0;
+ }
+ if (rel_wheel)
+ {
+ dispatch_mouse_wheel (rel_wheel);
+ rel_wheel = 0;
+ }
+
+ }
+
+//print_event(ev);
+
+}
+
+void
+dispatch_init (void)
+{
+ current_computer = map_init ();
+}