summaryrefslogtreecommitdiff
path: root/connect.c
blob: 27ddce1e9a2183bcfa3ca22829d8e8a35ca9870f (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
 *
 * Compiz Nintendo(R) Wii(TM) Remote Interface Plugin
 *
 * Copyright : (C) 2008 by Sam Spilsbury
 * E-mail    : smspillaz@gmail.com
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include "compiz-wiimote.h"
#include "wiimote_options.h"

int wiimoteDisplayPrivateIndex;

/* Wii Remote Connection / Handling --------------------------------------------------- */

/* Allocate a callback function */

static Bool
allocateCallbackFunction (cwiid_wiimote_t *wiimote, int nWiimote)
{
	if (nWiimote > MAX_WIIMOTES)
		return FALSE;

	if (cwiid_set_mesg_callback (wiimote, wiimoteCWiiDCallback))
	{
		fprintf(stderr, "Could not set mesg callback\n");
		return FALSE;
	}
	return TRUE;
}

/* Interface to find a Wii Remote
 * so that cwiid_open won't crash
 * when we can't find one */

Bool find_wiimote(bdaddr_t *bdaddr, int timeout)
{
	struct cwiid_bdinfo *bdinfo;
	int bdinfo_count;

	if (timeout == -1) {
		while ((bdinfo_count = cwiid_get_bdinfo_array(-1, 2, 1, &bdinfo, 0))
		       == 0);
		if (bdinfo_count == -1) {
			return FALSE;
		}
	}
	else {
		bdinfo_count = cwiid_get_bdinfo_array(-1, timeout, 1, &bdinfo, 0);
		if (bdinfo_count == -1) {
			return FALSE;
		}
		else if (bdinfo_count == 0) {
			return FALSE;
		}
	}

	free(bdinfo);
	return TRUE;
}

/* Second thread.
 * This is the actual bluetooth connection
 * phase for the Wii Remote. Because
 * control is not returned to compiz while
 * connection is taking place, the desktop
 * will appear to 'lock up' - which
 * is certainly not what we want, especially
 * if we want to do fancy stuff like display
 * messages. When a wiimote is successfully
 * connected to the host via bluetooth, a 
 * variable 'connected' is set to indicate that
 * the CompWiimote can be used.
 */

void* connectWiimote (void *vd)
{
        bdaddr_t bdaddr;	/* bluetooth device address */
        CompDisplay *d = (CompDisplay *) vd;
        bdaddr = *BDADDR_ANY;
        CompScreen *s;
	CompWiimote *cmpwiimote;
        cwiid_wiimote_t *wiimote;
        
        WIIMOTE_DISPLAY (d);
        
        if (!find_wiimote(&bdaddr, 2))
        {
	        compLogMessage ("wiimote", CompLogLevelError,
			"Wii Remote not found");

	        for (s = d->screens; s; s = s->next)
	        {
	           // WIIMOTE_SCREEN (s);
	            CompOption arg[4];
		    int nArg = 0;

                    arg[nArg].name = "window";
                    arg[nArg].type = CompOptionTypeInt;
                    arg[nArg].value.i = d->activeWindow;
                    nArg++;

                    arg[nArg].name = "root";
                    arg[nArg].type = CompOptionTypeInt;
                    arg[nArg].value.i = s->root;
                    nArg++;

                    arg[nArg].name = "string";
                    arg[nArg].type = CompOptionTypeString;
                    arg[nArg].value.s = "Wii Remote not found";
                    nArg++;

                    arg[nArg].name = "timeout";
                    arg[nArg].type = CompOptionTypeInt;
                    arg[nArg].value.i = 500;

		    sendInfoToPlugin (d, arg, nArg,
		    "prompt",
		    "display_text");
	        }
	        compRemoveTimeout(ad->checkingTimeoutHandle);
	    }
	    else
	    {
	        for (s = d->screens; s; s = s->next)
	        {
	            CompOption arg[4];
		    int nArg = 0;

                    arg[nArg].name = "window";
                    arg[nArg].type = CompOptionTypeInt;
                    arg[nArg].value.i = d->activeWindow;
                    nArg++;

                    arg[nArg].name = "root";
                    arg[nArg].type = CompOptionTypeInt;
                    arg[nArg].value.i = s->root;
                    nArg++;

                    arg[nArg].name = "string";
                    arg[nArg].type = CompOptionTypeString;
                    arg[nArg].value.s = "Wii Remote(s) found. Continue to\n"\
	                         " hold (1) and (2) to finalize connection sequence";
                    nArg++;

                    arg[nArg].name = "timeout";
                    arg[nArg].type = CompOptionTypeInt;
                    arg[nArg].value.i = 500;

		    sendInfoToPlugin (d, arg, nArg,
		    "prompt",
		    "display_text");
	        }
        	if (!(wiimote = cwiid_open(&bdaddr, 0))) {
		        compLogMessage ("wiimote", CompLogLevelError,
			"Could not connect to Wii Remote");

		        return NULL;
	        }

		/* Should be safe to assume that the most recent wiimote is the last one on the list */
		for (cmpwiimote = ad->wiimotes; cmpwiimote->next; cmpwiimote = cmpwiimote->next) ;

	        cmpwiimote->wiimote = wiimote;
	        cmpwiimote->connected = TRUE;
	        for (s = d->screens; s; s = s->next)
	        {
	            CompOption arg[4];
		    int nArg = 0;

                    arg[nArg].name = "window";
                    arg[nArg].type = CompOptionTypeInt;
                    arg[nArg].value.i = d->activeWindow;
                    nArg++;

                    arg[nArg].name = "root";
                    arg[nArg].type = CompOptionTypeInt;
                    arg[nArg].value.i = s->root;
                    nArg++;

                    arg[nArg].name = "string";
                    arg[nArg].type = CompOptionTypeString;
                    arg[nArg].value.s = "Wii Remote successfully connected";
                    nArg++;

                    arg[nArg].name = "timeout";
                    arg[nArg].type = CompOptionTypeInt;
                    arg[nArg].value.i = 500;

		    sendInfoToPlugin (d, arg, nArg,
		    "prompt",
		    "display_text");
	        }
	    }
	    return NULL; // This is required for pthread
}

/* Second Thread Poller.
 * This is a poller interface
 * to ensure that the Wii Remote is connected
 * before we do anything that requires initialisation.
 * When the wii remote is connected it does things
 * such as setup and then returns FALSE
 * so that it is not called again.
 */

Bool checkConnected (void *vd)
{
        CompDisplay *d = vd;
	CompWiimote *cmpwiimote;
        WIIMOTE_DISPLAY (d);
	struct cwiid_state state;	/* wiimote state */
	unsigned char mesg = 0; /* Mesg Flag */
	unsigned char rpt_mode = 0; /* Reporting Flag */
	unsigned char rumble = 0; /* Rumble Flag */
	int led_state = 0; /* LED State Flag */

        /* FIXME:
         * Remove me as all I do
         * is bring down host programs
         * not good for compiz!
         */

	cwiid_set_err(err); /* Error Handling */

	/* Should be safe to assume that the most recent wiimote is the last one on the list */
	for (cmpwiimote = ad->wiimotes; cmpwiimote->next; cmpwiimote = cmpwiimote->next) ;

        if (cmpwiimote->connected)
        {
	    /* Connect to the wiimote */
	    compLogMessage ("wiimote", CompLogLevelInfo,
			"Wii Remote Connection started");

            if (allocateCallbackFunction
            (cmpwiimote->wiimote, ad->nWiimote)) // Allocate a c
            {
	            compLogMessage ("wiimote", CompLogLevelInfo,
			"Wii Remote is now connected");


	            if (cwiid_set_rumble(cmpwiimote->wiimote, rumble)) {
		        compLogMessage ("wiimote", CompLogLevelError,
			"Couldn't set rumble");
	            }
	            
	            /* Toggle Accellerometor Reporting */
	            
                    toggle_bit(rpt_mode, CWIID_RPT_ACC);
	            set_rpt_mode(cmpwiimote->wiimote, rpt_mode);
	            
	            /* Toggle Button Reporting */
	            
	            toggle_bit(rpt_mode, CWIID_RPT_BTN);
	            set_rpt_mode(cmpwiimote->wiimote, rpt_mode);
	            
	            
	            /* CWIID_RPT_EXT is actually
	             * CWIID_RPT_NUNCHUK | CWIID_RPT_CLASSIC */
	            
	            /* Toggle Extension Reporting */ 
	            
	            toggle_bit(rpt_mode, CWIID_RPT_EXT);
	            set_rpt_mode(cmpwiimote->wiimote, rpt_mode);
	            
	            
	            /* libwiimote picks the highest quality IR mode available with the
	             * other options selected (not including as-yet-undeciphered
	             * interleaved mode */
	             
	            /* Toggle IR reporting */
	             
	            toggle_bit(rpt_mode, CWIID_RPT_IR);
	            set_rpt_mode(cmpwiimote->wiimote, rpt_mode);
	            
	            /* Toggle Status (Battery etc) reporting */
	            
	            toggle_bit(rpt_mode, CWIID_RPT_STATUS);
	            set_rpt_mode(cmpwiimote->wiimote, rpt_mode);
	            
	            /* Check to see if there is a mesg reporting
	               handler (Should be wiimoteCWiiDCallbackx, where X is
	               the Wii Remote iter
	             */
		    /* FIXME !mesg is here unconditionally true */
	            if (!mesg) {
	                if (cwiid_enable(cmpwiimote->wiimote, CWIID_FLAG_MESG_IFC)) {
				compLogMessage ("wiimote", CompLogLevelError,
				"Couldn't set messages. Wii remote will connect, but reporting and other things won't work!");
	                }
	                else {
	                    mesg = 1;
	                }
	            }
	            else {
		    if (cwiid_disable(cmpwiimote->wiimote, CWIID_FLAG_MESG_IFC)) {
			compLogMessage ("wiimote", CompLogLevelError,
			"Couldn't disable messages");
	            }
		        else {
			        mesg = 0;
		        }
	            }
	            
	            /* Request Status */
	
	            if (cwiid_request_status(cmpwiimote->wiimote)) {
		        compLogMessage ("wiimote", CompLogLevelError,
			"Error requesting status. Battery info will not work!");
	            }
	            
	            /* Request State */
	            
	            if (cwiid_get_state(cmpwiimote->wiimote, &state)) {
		        compLogMessage ("wiimote", CompLogLevelError,
			"Error requesting state. Calibration will not work!");
	            }
	            
	            /* TODO:
	             * Toggle certain LED's for certain
	             * Wii Remote 'Types'
	             */
	            
	            toggle_bit(led_state, CWIID_LED1_ON);
	            set_led_state(cmpwiimote->wiimote, led_state);
	            
	            /* Do a fancy thing with the LED's. Doesn't
	             * quite work right yet :(
	             */

		    cmpwiimote->count = 0;

	            cmpwiimote->lightsTimeoutHandle =
		    compAddTimeout (650, 650 * 1.2 /* I'll do the math later */, wiimoteChangeLights, cmpwiimote);
	            
	            /* Set some variables using the state interface
	             * FIXME: For some reason, over-querying the 
	             * state interface can result in a Mesg Pipe
	             * overflow which in turn causes a cwiid_err.
	             * This is no good because it brings down compiz!
	             * Of course, we only poll the value once, but it 
	             * _should_ be fixed in CWiiD
	             */
	             
	            cmpwiimote->acc.initAccX = state.acc[CWIID_X];
	            cmpwiimote->acc.initAccY = state.acc[CWIID_Y];

		    /* Set the id */

		    cmpwiimote->id = cwiid_get_id (cmpwiimote->wiimote);

		    reloadOptionsForWiimote (d, cmpwiimote);
		    reloadGesturesForWiimote (d, cmpwiimote);
		    reloadReportersForWiimote (d, cmpwiimote);             
	            
	            /* Increment the Wiimote iter to
	             * allow another CompWiimote to initialise
	             */
	            
	            ad->nWiimote++;
	        }
	        else
	        {
		        compLogMessage ("wiimote", CompLogLevelError,
			"Maximum number of Wii Remotes reached. If you want to enable more,"\
			"change the macro MAX_WIIMOTES in wiimote.c. This will be fixed in "\
			"the future with actual memory allocation");
	        }
	        
	        /* No more checking */
	        return FALSE;
	  }
	    
	  return TRUE;
}