summaryrefslogtreecommitdiff
path: root/mupen64plus-input-sdl.patch
blob: 20e1f91c663a3c5728de79f9a05602e91e465541 (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
diff -ru mupen64plus-bundle-src-1.99.4-orig/source/mupen64plus-input-sdl/src/plugin.c mupen64plus-bundle-src-1.99.4/source/mupen64plus-input-sdl/src/plugin.c
--- mupen64plus-bundle-src-1.99.4-orig/source/mupen64plus-input-sdl/src/plugin.c	2012-11-20 20:38:20.000000000 +0100
+++ mupen64plus-bundle-src-1.99.4/source/mupen64plus-input-sdl/src/plugin.c	2012-11-26 18:17:51.000000000 +0100
@@ -24,6 +24,12 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
 
 #include <SDL.h>
 
@@ -133,6 +139,151 @@
 
 static CONTROL temp_core_controlinfo[4];
 
+/* FIFO-Control functions by flo */
+static BUTTONS unpack_fifo_data(const char* buf)
+{
+	BUTTONS buttons;
+	
+	buttons.A_BUTTON     = (buf[0] &  1) ? 1 : 0;
+	buttons.B_BUTTON     = (buf[0] &  2) ? 1 : 0;
+	buttons.L_TRIG       = (buf[0] &  4) ? 1 : 0;
+	buttons.R_TRIG       = (buf[0] &  8) ? 1 : 0;
+	buttons.Z_TRIG       = (buf[0] & 16) ? 1 : 0;
+	buttons.START_BUTTON = (buf[0] & 32) ? 1 : 0;
+	buttons.R_DPAD       = (buf[1] &  1) ? 1 : 0;
+	buttons.L_DPAD       = (buf[1] &  2) ? 1 : 0;
+	buttons.U_DPAD       = (buf[1] &  4) ? 1 : 0;
+	buttons.D_DPAD       = (buf[1] &  8) ? 1 : 0;
+	buttons.R_CBUTTON    = (buf[2] &  2) ? 1 : 0;
+	buttons.L_CBUTTON    = (buf[2] &  4) ? 1 : 0;
+	buttons.U_CBUTTON    = (buf[2] &  8) ? 1 : 0;
+	buttons.D_CBUTTON    = (buf[2] & 16) ? 1 : 0;
+	
+	buttons.X_AXIS = buf[3] | ((buf[1] & 16) ? 128 : 0);
+	buttons.Y_AXIS = buf[4] | ((buf[1] & 32) ? 128 : 0);
+	
+	return buttons;
+}
+
+typedef struct
+{
+	int fd;
+	int good;
+	char buf[5];
+	int bufptr;
+	int recovering;
+	BUTTONS buttons;
+	
+} ctl_fifo_t;
+
+static ctl_fifo_t init_ctl_fifo(const char* path)
+{
+	ctl_fifo_t ctl_fifo;
+	ctl_fifo.fd=-1;
+	ctl_fifo.good=0;
+	ctl_fifo.bufptr=0;
+	ctl_fifo.recovering=0;
+
+	if (unlink(path) < 0)
+		if (errno!=ENOENT)
+		{
+			fprintf(stderr, "The control fifo (%s) could not be unlinked, continuing without remote control support: %s", path, strerror(errno));
+			ctl_fifo.good=0;
+			return ctl_fifo;
+		}
+
+
+	if (mkfifo(path, S_IRWXU)!=0)
+	{
+		fprintf(stderr, "The control fifo (%s) could not be created, continuing without remote control support: %s", path, strerror(errno));
+		ctl_fifo.good=0;
+		return ctl_fifo;
+	}
+
+	if ((ctl_fifo.fd=open(path, O_RDONLY | O_NONBLOCK))<0)
+	{
+		fprintf(stderr, "The control fifo (%s) could not be opened, continuing without remote control support: %s", path, strerror(errno));
+		ctl_fifo.good=0;
+		return ctl_fifo;
+	}
+
+	if (fcntl(ctl_fifo.fd, F_SETFL, O_NONBLOCK)==-1)
+	{
+		fprintf(stderr, "Could not set nonblocking io mode on control fifo, continuing without remote control support: %s", strerror(errno));
+		close(ctl_fifo.fd);
+		ctl_fifo.fd=-1;
+		ctl_fifo.good=0;
+		return ctl_fifo;
+	}
+	
+	ctl_fifo.good=1;
+	
+	return ctl_fifo;
+}
+
+static BUTTONS read_buttons_from_fifo(ctl_fifo_t* ctl_fifo)
+{
+	ssize_t bytes;
+	
+	if (ctl_fifo->recovering)
+	{
+		do
+		{
+			ctl_fifo->bufptr=0;
+			if ((bytes = read(ctl_fifo->fd, ctl_fifo->buf, 1)) == -1 && errno!=EAGAIN)
+			{
+				printf("read error!\n");
+			}
+			
+			//if (bytes==1) printf("\tread %c\n", ctl_fifo->buf[0]);
+		} while ((ctl_fifo->buf[0] & 128)==0 && bytes>0);
+		
+		if (ctl_fifo->buf[0] & 128)
+		{
+			printf("successfully recovered :)\n");
+			ctl_fifo->recovering=0;
+			ctl_fifo->bufptr=1;
+		}
+	}
+	
+	
+	if (!ctl_fifo->recovering)
+		do
+		{
+			if ((bytes = read(ctl_fifo->fd, ctl_fifo->buf+ctl_fifo->bufptr, sizeof(ctl_fifo->buf)-ctl_fifo->bufptr)) == -1
+				&& (errno!=EAGAIN))
+			{
+				printf("read error!\n");
+			}
+			else if (bytes>0)
+			{
+				ctl_fifo->bufptr+=bytes;
+				if (ctl_fifo->bufptr==sizeof(ctl_fifo->buf))
+				{
+					ctl_fifo->bufptr=0;
+					
+					if (ctl_fifo->buf[0] & 128)
+					{
+						ctl_fifo->buttons = unpack_fifo_data(ctl_fifo->buf);
+					}
+					else
+					{
+						printf("fifo data corrupt, entering recovery mode\n");
+						ctl_fifo->recovering=1;
+						break;
+					}					
+				}
+			}
+		} while (bytes>0);
+	
+	return ctl_fifo->buttons;
+}
+
+static ctl_fifo_t ctl_fifo;
+static int ctl_fifo_inited=0;
+static int ctl_fifo_takeover=0;
+
+
 /* Mupen64Plus plugin functions */
 EXPORT m64p_error CALL PluginStartup(m64p_dynlib_handle CoreLibHandle, void *Context,
                                    void (*DebugCallback)(void *, int, const char *))
@@ -454,6 +605,12 @@
     SDL_Event event;
     unsigned char mstate;
 
+	if (ctl_fifo_inited==0)
+	{
+		ctl_fifo=init_ctl_fifo("/var/tmp/mupen64plus_ctl");
+		ctl_fifo_inited=1;
+	}
+
     // Handle keyboard input first
     doSdlKeys(SDL_GetKeyState(NULL));
     doSdlKeys(myKeyState);
@@ -596,7 +753,21 @@
 #ifdef _DEBUG
     DebugMessage(M64MSG_VERBOSE, "Controller #%d value: 0x%8.8X\n", Control, *(int *)&controller[Control].buttons );
 #endif
-    *Keys = controller[Control].buttons;
+    
+    if (Control==0)
+    {
+		BUTTONS fromfifo = read_buttons_from_fifo(&ctl_fifo);
+		if (fromfifo.A_BUTTON)
+			ctl_fifo_takeover=1;
+		
+		if (ctl_fifo_takeover)
+			*Keys = fromfifo;
+		else
+			*Keys = controller[Control].buttons;
+		
+	}
+	else
+		*Keys = controller[Control].buttons;
 
     /* handle mempack / rumblepak switching (only if rumble is active on joystick) */
 #ifdef __linux__