aboutsummaryrefslogtreecommitdiff
path: root/libnsfb/patches/200-sdl-kbdmouse.patch
blob: 660e220109a89aabf708537610be8c83aee213e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
--- a/src/surface/sdl.c	2010-04-29 01:26:13.521908000 +0200
+++ b/src/surface/sdl.c	2010-10-13 03:06:18.945784735 +0200
@@ -6,6 +6,10 @@
  *                http://www.opensource.org/licenses/mit-license.php
  */
 
+#define _POSIX_SOURCE
+#include <sys/types.h>
+#include <unistd.h>
+#include <signal.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <SDL/SDL.h>
@@ -20,6 +24,10 @@
 #include "plot.h"
 #include "cursor.h"
 
+#ifdef KBDMOUSE
+#include KBDMOUSE
+#endif
+
 enum nsfb_key_code_e sdl_nsfb_map[] = {
     NSFB_KEY_UNKNOWN,
     NSFB_KEY_UNKNOWN,
@@ -470,7 +478,11 @@
     nsfb->linelen = sdl_screen->pitch;
 
     SDL_ShowCursor(SDL_DISABLE);
+#ifdef KBDMOUSE
+    SDL_EnableKeyRepeat(KBDMOUSE_REPEAT_DELAY, KBDMOUSE_REPEAT_INTERVAL);
+#else
     SDL_EnableKeyRepeat(300, 50);
+#endif
 
     return 0;
 }
@@ -497,6 +509,116 @@
     return 0;
 }
 
+#ifdef KBDMOUSE
+
+static int keyboard_mouse(SDL_Event *event)
+{
+    static int enabled = 1;
+
+    SDL_Event new;
+    int x, y;
+
+    if ((int)event->key.keysym.sym == KBDMOUSE_TOGGLE) {
+    	if (event->type == SDL_KEYUP)
+    	    enabled = !enabled;
+        return 0;
+    }
+
+    if (!enabled)
+        return -1;
+
+    switch (event->type) {
+        case SDL_KEYDOWN:
+            switch ((int)event->key.keysym.sym) {
+                case KBDMOUSE_LEFT_CLICK:
+                case KBDMOUSE_RIGHT_CLICK:
+                    SDL_GetMouseState(&x, &y);
+
+                    new.type = SDL_MOUSEBUTTONDOWN;
+                    new.button.type = SDL_MOUSEBUTTONDOWN;
+                    new.button.which = 0;
+                    new.button.state = SDL_PRESSED;
+                    new.button.x = (Uint16)x;
+                    new.button.y = (Uint16)y;
+
+                    switch ((int)event->key.keysym.sym) {
+                        case KBDMOUSE_LEFT_CLICK:  new.button.button = SDL_BUTTON_LEFT; break;
+                        case KBDMOUSE_RIGHT_CLICK: new.button.button = SDL_BUTTON_RIGHT; break;
+                    }
+
+                    SDL_PushEvent(&new);
+                    return 0;
+
+                case KBDMOUSE_UP:
+                case KBDMOUSE_DOWN:
+                case KBDMOUSE_LEFT:
+                case KBDMOUSE_RIGHT:
+                    SDL_GetMouseState(&x, &y);
+
+                    switch ((int)event->key.keysym.sym) {
+                        case KBDMOUSE_UP:    y -= KBDMOUSE_STEP; break;
+                        case KBDMOUSE_DOWN:  y += KBDMOUSE_STEP; break;
+                        case KBDMOUSE_RIGHT: x += KBDMOUSE_STEP; break;
+                        case KBDMOUSE_LEFT:  x -= KBDMOUSE_STEP; break;
+                    }
+
+                    SDL_WarpMouse((Uint16)x, (Uint16)y);
+                    return 0;
+
+                case KBDMOUSE_QUIT_HACK:
+                    return 0;
+            }
+            break;
+
+        case SDL_KEYUP:
+            switch ((int)event->key.keysym.sym) {
+                case KBDMOUSE_LEFT_CLICK:
+                case KBDMOUSE_RIGHT_CLICK:
+                    SDL_GetMouseState(&x, &y);
+
+                    new.type = SDL_MOUSEBUTTONUP;
+                    new.button.type = SDL_MOUSEBUTTONUP;
+                    new.button.which = 0;
+                    new.button.state = SDL_RELEASED;
+                    new.button.x = (Uint16)x;
+                    new.button.y = (Uint16)y;
+
+                    switch ((int)event->key.keysym.sym) {
+                        case KBDMOUSE_LEFT_CLICK:  new.button.button = SDL_BUTTON_LEFT; break;
+                        case KBDMOUSE_RIGHT_CLICK: new.button.button = SDL_BUTTON_RIGHT; break;
+                    }
+
+                    SDL_PushEvent(&new);
+                    return 0;
+
+                case KBDMOUSE_UP:
+                case KBDMOUSE_DOWN:
+                case KBDMOUSE_LEFT:
+                case KBDMOUSE_RIGHT:
+                    return 0;
+
+                case KBDMOUSE_QUIT_HACK:
+                    kill(getpid(), SIGTERM);
+                    return 0;
+            }
+            break;
+
+        default:
+            break;
+    }
+
+    return -1;
+}
+
+#else
+
+static int keyboard_mouse(SDL_Event *event __attribute__((unused)))
+{
+    return -1;
+}
+
+#endif
+
 static bool sdl_input(nsfb_t *nsfb, nsfb_event_t *event, int timeout)
 {
     int got_event;
@@ -526,6 +648,9 @@
     if (got_event == 0)
         return false;
 
+    if (!keyboard_mouse(&sdlevent))
+        return false;
+
     event->type = NSFB_EVENT_NONE;
 
     switch (sdlevent.type) {