summaryrefslogtreecommitdiff
path: root/wiimote.c
blob: 43db5a2511c604f8fdb7a9b7aae78e5126b3048e (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
/**
 * Compiz Wiimote Plugin. Use the Nintendo Wii Remove with Compiz.
 *
 * 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.
 *
 * This plugin provides an interface that collects values from cwiid and stored them
 * in display stucts. It should be possible to extend this plugin quite easily.
 * 
 * Please note any major changes to the code in this header with who you
 * are and what you did. 
 *
 * TODO:
 * - ???
 */
#include "wiimote-internal.h"

static int corePrivateIndex; 
int wiimoteDisplayPrivateIndex;
int functionsPrivateIndex;

CompWiimote *
wiimoteForIter (CompDisplay *d,
		int         iter)
{
    WIIMOTE_DISPLAY (d);

    CompWiimote *wiimote;

    for (wiimote = ad->wiimotes; wiimote; wiimote = wiimote->next)
    {
	if (wiimote->id == iter)
	    return wiimote;
    }
    return NULL;
}

/* Wrappable Functions --------------------------------------------------- */

static void
wiimoteDisplayOptionChanged (CompDisplay             *d,
			       CompOption              *opt,
			       WiimoteDisplayOptions num)
{
    CompWiimote *wiimote;
    WIIMOTE_DISPLAY (d);
    switch (num)
    {   

	case WiimoteDisplayOptionXCalibrationMul:
	case WiimoteDisplayOptionYCalibrationMul:
	case WiimoteDisplayOptionXAdjust:
	case WiimoteDisplayOptionYAdjust:
        {
		for (wiimote = ad->wiimotes; wiimote; wiimote = wiimote->next)
		    reloadOptionsForWiimote (d, wiimote);
		break;
	}
	case WiimoteDisplayOptionGestureWiimoteNumber:
	case WiimoteDisplayOptionGestureType:
	case WiimoteDisplayOptionGesturePluginName:
	case WiimoteDisplayOptionGestureActionName:
	case WiimoteDisplayOptionGestureSensitivity:
	{
		for (wiimote = ad->wiimotes; wiimote; wiimote = wiimote->next)
		    reloadGesturesForWiimote (d, wiimote);
		break;
	}
	case WiimoteDisplayOptionReportWiimoteNumber:
	case WiimoteDisplayOptionReportType:
	case WiimoteDisplayOptionReportPluginName:
	case WiimoteDisplayOptionReportActionName:
	case WiimoteDisplayOptionReportSensitivity:
	{
		for (wiimote = ad->wiimotes; wiimote; wiimote = wiimote->next)
		    reloadReportersForWiimote (d, wiimote);
		break;
	}
	default:
		break;
    } 
}

/* Memory Management ----------------------------------------------------- */

CompWiimote *
wiimoteAddWiimote (CompDisplay *d)
{
    WIIMOTE_DISPLAY (d);
    CompWiimote *wiimote;
    
    if (!ad->wiimotes)
    {
        ad->wiimotes = calloc (1, sizeof (CompWiimote));
        if (!ad->wiimotes)
            return NULL;
	ad->wiimotes->next = NULL;
	wiimote = ad->wiimotes;
    }
    else
    {
    	for (wiimote = ad->wiimotes; wiimote->next; wiimote = wiimote->next) ;
    	
    	wiimote->next = calloc (1 , sizeof (CompWiimote));
	if (!wiimote->next)
	    return NULL;
	wiimote->next->next = NULL;
	wiimote = wiimote->next;
    }
    
    wiimote->connected = FALSE;
    wiimote->initiated = FALSE;
    wiimote->initSet = FALSE;

    return wiimote;
}

void
wiimoteRemoveWiimote (CompDisplay *d,
		      CompWiimote *wiimote)
{
    WIIMOTE_DISPLAY (d);
    CompWiimote *run;
    
    if (wiimote == ad->wiimotes)
    {
        if (ad->wiimotes->next)
            ad->wiimotes = ad->wiimotes->next;
        else
            ad->wiimotes = NULL;
            
        /* Let CWiiD handle some of the other memory management */
        
        free (wiimote);
    }
    else
    {
	for (run = ad->wiimotes; run; run = run->next)
	{
	    if (run == wiimote)
	    {
		if (run->next)
		    run = run->next;
		else
		    run = NULL;

		free (wiimote);
		break;
	    }
	}
    }
}

static WiimoteBaseFunctions wiimoteFunctions =
{
    .wiimoteForIter = wiimoteForIter
};

/* Core Initialization --------------------------------------------------- */

/* Use initCore() to find the first display (c->displays) */

static Bool
wiimoteInitCore (CompPlugin *p,
  	     CompCore   *c)
{
    if (!checkPluginABI ("core", CORE_ABIVERSION))
        return FALSE;

    wiimoteDisplayPrivateIndex = allocateDisplayPrivateIndex ();
    if (wiimoteDisplayPrivateIndex < 0)
    {
        return FALSE;
    }

    functionsPrivateIndex = allocateDisplayPrivateIndex ();
    if (functionsPrivateIndex < 0)
    {
	freeDisplayPrivateIndex (wiimoteDisplayPrivateIndex);
	return FALSE;
    }
    
    firstDisplay = c->displays;
    
    return TRUE;
}

static void
wiimoteFiniCore (CompPlugin *p,
  	     CompCore   *c)
{
    freeDisplayPrivateIndex (wiimoteDisplayPrivateIndex);
    freeDisplayPrivateIndex (functionsPrivateIndex);
}

/* Display Initialization --------------------------------------------------- */

static Bool
wiimoteInitDisplay (CompPlugin  *p,
		      CompDisplay *d)
{
    WiimoteDisplay *ad;
    CompOption     *abi, *index;

    if (!checkPluginABI ("core", CORE_ABIVERSION))
	return FALSE;
	
    compLogMessage ("wiimote", CompLogLevelInfo,
		"Please note that if you do not have the prompt plugin loaded, you won't see instructions on screen");

    ad = malloc (sizeof (WiimoteDisplay));
    if (!ad)
	return FALSE;

    ad->screenPrivateIndex = allocateScreenPrivateIndex (d);
    if (ad->screenPrivateIndex < 0)
    {
	free (ad);
	return FALSE;
    }

    d->base.privates[wiimoteDisplayPrivateIndex].ptr = ad;
    d->base.privates[functionsPrivateIndex].ptr = &wiimoteFunctions;

    abi = wiimoteGetAbiOption (d);
    abi->value.i = WIIMOTE_ABIVERSION;

    index = wiimoteGetIndexOption (d);
    index->value.i = functionsPrivateIndex;
    
    wiimoteSetToggleKeyInitiate (d, wiimoteToggle);
    wiimoteSetDisableKeyInitiate (d, wiimoteDisable);
    wiimoteSetReportKeyInitiate (d, wiimoteSendInfo);

    ad->nWiimote = 0;
    ad->wiimotes = NULL;

    wiimoteSetXCalibrationMulNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetYCalibrationMulNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetXAdjustNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetYAdjustNotify (d, wiimoteDisplayOptionChanged);

    wiimoteSetGestureWiimoteNumberNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetGestureTypeNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetGesturePluginNameNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetGestureActionNameNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetGestureSensitivityNotify (d, wiimoteDisplayOptionChanged);

    wiimoteSetReportWiimoteNumberNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetReportTypeNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetReportPluginNameNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetReportActionNameNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetReportSensitivityNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetReportDataTypeNotify (d, wiimoteDisplayOptionChanged);

    wiimoteSetReportXArgumentNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetReportYArgumentNotify (d, wiimoteDisplayOptionChanged);
    wiimoteSetReportZArgumentNotify (d, wiimoteDisplayOptionChanged);
        
    return TRUE;
}

static void
wiimoteFiniDisplay (CompPlugin  *p,
		      CompDisplay *d)
{
    WIIMOTE_DISPLAY (d);

    freeScreenPrivateIndex (d, ad->screenPrivateIndex);
    free (ad);
}

/* Screen Initialization --------------------------------------------------- */

static Bool
wiimoteInitScreen (CompPlugin *p,
		     CompScreen *s)
{
    WiimoteScreen *as;

    WIIMOTE_DISPLAY (s->display);

    as = malloc (sizeof (WiimoteScreen));
    if (!as)
	return FALSE;

    s->base.privates[ad->screenPrivateIndex].ptr = as;

    return TRUE;
}

static void
wiimoteFiniScreen (CompPlugin *p,
		     CompScreen *s)
{
    WIIMOTE_SCREEN (s);

    free (as);
}

/* Object Initialization --------------------------------------------------- */

static CompBool
wiimoteInitObject (CompPlugin *p,
		     CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
	(InitPluginObjectProc) wiimoteInitCore,
	(InitPluginObjectProc) wiimoteInitDisplay,
	(InitPluginObjectProc) wiimoteInitScreen
    };

    RETURN_DISPATCH (o, dispTab, ARRAY_SIZE (dispTab), TRUE, (p, o));
}

static void
wiimoteFiniObject (CompPlugin *p,
		     CompObject *o)
{
    static FiniPluginObjectProc dispTab[] = {
	(FiniPluginObjectProc) wiimoteFiniCore,
	(FiniPluginObjectProc) wiimoteFiniDisplay,
	(FiniPluginObjectProc) wiimoteFiniScreen
    };

    DISPATCH (o, dispTab, ARRAY_SIZE (dispTab), (p, o));
}

/* Plugin Initialization --------------------------------------------------- */

static Bool
wiimoteInit (CompPlugin *p)
{
    fprintf(stderr, " in init\n");
    corePrivateIndex = allocateCorePrivateIndex ();
    if (corePrivateIndex < 0)
	return FALSE;
    return TRUE;
}

static void
wiimoteFini (CompPlugin *p)
{
    freeCorePrivateIndex (corePrivateIndex);
}

CompPluginVTable wiimoteVTable = {
    "wiimote",
    0,
    wiimoteInit,
    wiimoteFini,
    wiimoteInitObject,
    wiimoteFiniObject,
    0,
    0
};

CompPluginVTable*
getCompPluginInfo (void)
{
    return &wiimoteVTable;
}