summaryrefslogtreecommitdiff
path: root/devel/SDL/patches/patch-src_video_wscons_SDL__wsconsevents.c
blob: 3fd72ad947cd273b553556114367ee0258786cf1 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
$NetBSD: patch-src_video_wscons_SDL__wsconsevents.c,v 1.10 2015/01/30 12:02:49 wiz Exp $

--- src/video/wscons/SDL_wsconsevents.c.orig	2012-01-19 06:30:06.000000000 +0000
+++ src/video/wscons/SDL_wsconsevents.c
@@ -25,6 +25,7 @@
 #include <dev/wscons/wsdisplay_usl_io.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>    
+#include <limits.h>    
 #include <unistd.h>  
 #include <termios.h>
 #include <errno.h> 
@@ -47,14 +48,31 @@ int WSCONS_InitKeyboard(_THIS)
     return -1;
   }
 
+#if defined(__NetBSD__)
+  if (private->kbdType == 0) {
+    /* wsmux with no keyboard attached, assumed USB */
+    private->kbdType = WSKBD_TYPE_USB;
+  }
+#endif
+
   if (tcgetattr(private->fd, &private->saved_tty) == -1) {
     WSCONS_ReportError("cannot get terminal attributes: %s", strerror(errno));
     return -1;
   }
+  if (ioctl(private->fd, WSKBDIO_GETKEYREPEAT, &private->saved_repeat) == -1) {
+    WSCONS_ReportError("cannot get repeat settings: %s", strerror(errno));
+    return -1;
+  }
   private->did_save_tty = 1;
+
+  if (ioctl(private->fd, KDSKBMODE, K_RAW) == -1) {
+    WSCONS_ReportError("cannot set raw keyboard mode: %s", strerror(errno));
+    return -1;
+  }
+
   tty = private->saved_tty;
   tty.c_iflag = IGNPAR | IGNBRK;
-  tty.c_oflag = 0;
+  //tty.c_oflag = 0;
   tty.c_cflag = CREAD | CS8;
   tty.c_lflag = 0;
   tty.c_cc[VTIME] = 0;
@@ -65,8 +83,12 @@ int WSCONS_InitKeyboard(_THIS)
     WSCONS_ReportError("cannot set terminal attributes: %s", strerror(errno));
     return -1;
   }
-  if (ioctl(private->fd, KDSKBMODE, K_RAW) == -1) {
-    WSCONS_ReportError("cannot set raw keyboard mode: %s", strerror(errno));
+
+  struct wskbd_keyrepeat_data repeat;
+  repeat.which = WSKBD_KEYREPEAT_DOALL;
+  repeat.del1 = repeat.delN = UINT_MAX;
+  if (ioctl(private->fd, WSKBDIO_SETKEYREPEAT, &repeat) == -1) {
+    WSCONS_ReportError("cannot set repeat settings: %s", strerror(errno));
     return -1;
   }
 
@@ -81,6 +103,10 @@ void WSCONS_ReleaseKeyboard(_THIS)
 			 strerror(errno));
     }
     if (private->did_save_tty) {
+      if (ioctl(private->fd, WSKBDIO_SETKEYREPEAT, &private->saved_repeat) == -1) {
+        WSCONS_ReportError("cannot restore repeat settings: %s",
+			   strerror(errno));
+      }
       if (tcsetattr(private->fd, TCSANOW, &private->saved_tty) < 0) {
 	WSCONS_ReportError("cannot restore keynoard attributes: %s",
 			   strerror(errno));
@@ -89,8 +115,65 @@ void WSCONS_ReleaseKeyboard(_THIS)
   }
 }
 
-static void updateMouse()
+int WSCONS_InitMouse(_THIS)
 {
+  if (private->mouseFd != -1) {
+#if defined(WSMOUSEIO_SETVERSION)
+    int version = WSMOUSE_EVENT_VERSION;
+    if (ioctl(private->mouseFd, WSMOUSEIO_SETVERSION, &version) == -1) {
+      WSCONS_ReportError("cannot set mouse API version: %s", strerror(errno));
+      return -1;
+    }
+#endif
+  }
+  return 0;
+}
+
+void WSCONS_ReleaseMouse(_THIS)
+{
+}
+
+#define NUMEVENTS 64
+
+static void updateMouse(_THIS)
+{
+  struct wscons_event evlist[NUMEVENTS];
+  struct wscons_event *ev = evlist;
+  int nev, i;
+  ssize_t len;
+
+  len = read(private->mouseFd, evlist, sizeof(evlist));
+  if (len == -1) {
+    WSCONS_ReportError("Failed to read wsmouse event: %s", strerror(errno));
+    return;
+  }
+
+  nev = len / sizeof(*ev);
+
+  for (i = 0; i < nev; i++, ev++) {
+    switch (ev->type) {
+    case WSCONS_EVENT_MOUSE_UP:
+      posted += SDL_PrivateMouseButton(SDL_RELEASED, ev->value+1, 0, 0);
+      break;
+    case WSCONS_EVENT_MOUSE_DOWN:
+      posted += SDL_PrivateMouseButton(SDL_PRESSED, ev->value+1, 0, 0);
+      break;
+    case WSCONS_EVENT_MOUSE_DELTA_X:
+      posted += SDL_PrivateMouseMotion(0, 1, ev->value, 0);
+      break;
+    case WSCONS_EVENT_MOUSE_DELTA_Y:
+      posted += SDL_PrivateMouseMotion(0, 1, 0, -ev->value);
+      break;
+    case WSCONS_EVENT_MOUSE_DELTA_Z:
+      posted += SDL_PrivateMouseButton(SDL_PRESSED,
+                    ev->value > 0 ? SDL_BUTTON_WHEELUP : SDL_BUTTON_WHEELDOWN,
+                    0, 0);
+      posted += SDL_PrivateMouseButton(SDL_RELEASED,
+                    ev->value > 0 ? SDL_BUTTON_WHEELUP : SDL_BUTTON_WHEELDOWN,
+                    0, 0);
+      break;
+    }
+  }
 }
 
 static SDLKey keymap[128];
@@ -107,6 +190,11 @@ static SDL_keysym *TranslateKey(int scan
   if (keysym->sym == SDLK_UNKNOWN)
     printf("Unknown mapping for scancode %d\n", scancode);
 
+  keysym->unicode = 0;
+  if (SDL_TranslateUNICODE) {
+    keysym->unicode = keysym->sym;
+  }
+
   return keysym;
 }
 
@@ -120,19 +208,42 @@ static void updateKeyboard(_THIS)
     for (i = 0; i < n; i++) {
       unsigned char c = buf[i] & 0x7f;
       if (c == 224) // special key prefix -- what should we do with it?
-	continue;
+       continue;
       posted += SDL_PrivateKeyboard((buf[i] & 0x80) ? SDL_RELEASED : SDL_PRESSED,
-				    TranslateKey(c, &keysym));
+                                   TranslateKey(c, &keysym));
     }
   }
 }
 
 void WSCONS_PumpEvents(_THIS)
 {
+  static struct timeval zero;
+  int maxfd = 0;
+
+  if (private->fd > maxfd)
+    maxfd = private->fd;
+  if (private->mouseFd > maxfd)
+    maxfd = private->mouseFd;
+
   do {
+    fd_set fds;
+
     posted = 0;
-    updateMouse();
-    updateKeyboard(this);
+
+    FD_ZERO(&fds);
+    if (private->fd != -1)
+      FD_SET(private->fd, &fds);
+    if (private->mouseFd != -1)
+      FD_SET(private->mouseFd, &fds);
+
+    if (select(maxfd+1, &fds, NULL, NULL, &zero) > 0) {
+      if (private->mouseFd != -1 && FD_ISSET(private->mouseFd, &fds)) {
+        updateMouse(this);
+      }
+      if (private->fd != -1 && FD_ISSET(private->fd, &fds)) {
+        updateKeyboard(this);
+      }
+    }
   } while (posted);
 }
 
@@ -146,8 +257,10 @@ void WSCONS_InitOSKeymap(_THIS)
   }
 
   switch (private->kbdType) {
-#ifdef WSKBD_TYPE_ZAURUS
+#if defined(WSKBD_TYPE_ZAURUS)
   case WSKBD_TYPE_ZAURUS:
+#endif
+  case WSKBD_TYPE_USB:
     /* top row */
     keymap[2] = SDLK_1;
     keymap[3] = SDLK_2;
@@ -220,7 +333,6 @@ void WSCONS_InitOSKeymap(_THIS)
     keymap[77] = SDLK_RIGHT;
     keymap[80] = SDLK_DOWN;
     break;
-#endif /* WSKBD_TYPE_ZAURUS */
 
   default:
     WSCONS_ReportError("Unable to map keys for keyboard type %u",