summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Spilsbury <Sam@ACER-SUSE.SUSE>2008-03-08 18:07:05 +0900
committerSam Spilsbury <Sam@ACER-SUSE.SUSE>2008-03-08 18:07:05 +0900
commit26448db287e6755d3d74e3b7ad29042dd32751c2 (patch)
treeedd1b179d2dd2f97f32849bf82bfc168ebf26d73
parent80e8ae13fc1ef7e47413603253f059f2137cb391 (diff)
downloadwiimote-26448db287e6755d3d74e3b7ad29042dd32751c2.tar.gz
wiimote-26448db287e6755d3d74e3b7ad29042dd32751c2.tar.bz2
* Code and comment cleanup and ordering
-rw-r--r--compiz-wiimote.h8
-rw-r--r--wiimote.c560
2 files changed, 270 insertions, 298 deletions
diff --git a/compiz-wiimote.h b/compiz-wiimote.h
index 74df4f7..7ddfb07 100644
--- a/compiz-wiimote.h
+++ b/compiz-wiimote.h
@@ -346,15 +346,21 @@ typedef struct _WiimoteScreen
} WiimoteScreen;
cwiid_mesg_callback_t cwiid_callback;
+cwiid_err_t err;
+static int corePrivateIndex;
static int wiimoteDisplayPrivateIndex;
CompDisplay *firstDisplay;
-cwiid_err_t err;
/* Function Prototypes */
static int findMinIR (CompDisplay *, int, int, int, int, int); // No you can't have fminf
static int findMaxIR (CompDisplay *, int, int, int, int, int); // Or fmaxf
+static Bool wiimoteChangeLights(void *Wiimote);
+static void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state);
+static void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode);
static Bool sendInfoToPlugin (CompDisplay *, CompOption *, int, char *, char *);
+static void wiimoteProcessButtons (CompDisplay *, int, struct cwiid_btn_mesg *);
+static void wiimoteProcessNunchuckButtons (CompDisplay *, int, struct cwiid_nunchuk_mesg *);
#define GET_WIIMOTE_DISPLAY(d) \
((WiimoteDisplay *) (d)->base.privates[wiimoteDisplayPrivateIndex].ptr)
diff --git a/wiimote.c b/wiimote.c
index fe57b63..6556b6e 100644
--- a/wiimote.c
+++ b/wiimote.c
@@ -18,7 +18,7 @@
* are and what you did.
*
* TODO:
- * - Gesture timeout
+ * - ???
*/
#include <stdio.h>
#include <stdlib.h>
@@ -35,32 +35,18 @@
#include "wiimote_options.h"
#include "compiz-wiimote.h"
+/* Shortcut Macros --------------------------------------------------- */
+
#define toggle_bit(bf,b) \
(bf) = ((bf) & b) \
? ((bf) & ~(b)) \
: ((bf) | (b))
-#define PI 3.1415926
-#define CWIID_FALSE -1
-#define CWIID_TRUE 0
-#define PI2 3.14159265358979323846
-#define DEG2RAD2(DEG) ((DEG)*((PI2)/(180.0)))
-
-#define NEW_AMOUNT 0.3
-#define OLD_AMOUNT (1.0 - NEW_AMOUNT)
-#define X_EDGE 50
-#define Y_EDGE 50
-/* Prototyping */
-
-static int corePrivateIndex;
-
-static Bool wiimoteChangeLights(void *Wiimote);
-void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state);
-void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode);
-/*static Bool print_state(void *vd);*/
+#define PI 3.14159265358979323846
+#define DEG2RAD2(DEG) ((DEG)*((PI)/(180.0)))
-/* Wiimote Utility Functions */
+/* Utility Functions --------------------------------------------------- */
/* Error Handling */
void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap)
@@ -72,7 +58,7 @@ void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap)
}
/* Set LED Wrapper */
-void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state)
+static void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state)
{
if (cwiid_set_led(wiimote, led_state)) {
fprintf(stderr, "Error setting LEDs \n");
@@ -80,14 +66,14 @@ void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state)
}
/* Set Reporting Mode Wrapper*/
-void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode)
+static void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode)
{
if (cwiid_set_rpt_mode(wiimote, rpt_mode)) {
fprintf(stderr, "Error setting report mode\n");
}
}
-/* Light shows FTW! */
+/* Wii Remote Light Show */
static Bool wiimoteChangeLights(void *Wiimote)
{
@@ -146,9 +132,6 @@ static Bool wiimoteChangeLights(void *Wiimote)
return TRUE;
}
-
-/* Text Prompting Interface */
-
static Bool
allocateCallbackFunction (cwiid_wiimote_t *wiimote, int nWiimote)
{
@@ -163,6 +146,41 @@ allocateCallbackFunction (cwiid_wiimote_t *wiimote, int nWiimote)
return TRUE;
}
+/* Utility */
+static int findMinIR (CompDisplay *d, int wiimoteNumber, int ir1, int ir2, int ir3, int ir4)
+{
+ WIIMOTE_DISPLAY (d);
+
+ int min = ir1;
+
+ if ((ir2 < min) && ad->cWiimote[wiimoteNumber].ir[1].valid)
+ min = ir2;
+ if ((ir3 < min) && ad->cWiimote[wiimoteNumber].ir[2].valid)
+ min = ir3;
+ if ((ir4 < min) && ad->cWiimote[wiimoteNumber].ir[3].valid)
+ min = ir4;
+
+ return min;
+}
+
+static int findMaxIR (CompDisplay *d, int wiimoteNumber, int ir1, int ir2, int ir3, int ir4)
+{
+ WIIMOTE_DISPLAY (d);
+
+ int max = ir1;
+
+ if ((ir2 > max) && ad->cWiimote[wiimoteNumber].ir[1].valid)
+ max = ir2;
+ if ((ir3 > max) && ad->cWiimote[wiimoteNumber].ir[2].valid)
+ max = ir3;
+ if ((ir4 > max) && ad->cWiimote[wiimoteNumber].ir[3].valid)
+ max = ir4;
+
+ return max;
+}
+
+/* Wii Remote Connection / Handling --------------------------------------------------- */
+
/* Interface to find a Wii Remote
* so that cwiid_open won't crash
* when we can't find one */
@@ -250,13 +268,8 @@ static void* connectWiimote (void *vd)
sendInfoToPlugin (d, arg, nArg,
"prompt",
"display_text");
-
- /*stringData = "Wii Remote not found";
- wiimoteFreeWindowTitle (s);
- wiimoteRenderWindowTitle (s, stringData);
- compAddTimeout(3000, removeTitle, s);*/
}
- compRemoveTimeout(ad->checkingTimeoutHandle); // Do not continue polling
+ compRemoveTimeout(ad->checkingTimeoutHandle);
}
else
{
@@ -288,11 +301,6 @@ static void* connectWiimote (void *vd)
sendInfoToPlugin (d, arg, nArg,
"prompt",
"display_text");
-
- /*stringData = " Wii Remote(s) found. Continue to\n"\
- " hold (1) and (2) to finalize connection sequence";
- wiimoteFreeWindowTitle (s);
- wiimoteRenderWindowTitle (s, stringData);*/
}
if (!(wiimote = cwiid_open(&bdaddr, 0))) {
compLogMessage (d, "wiimote", CompLogLevelError,
@@ -329,10 +337,6 @@ static void* connectWiimote (void *vd)
sendInfoToPlugin (d, arg, nArg,
"prompt",
"display_text");
- /*stringData = "Wii Remote successfully connected";
- wiimoteFreeWindowTitle (s);
- wiimoteRenderWindowTitle (s, stringData);
- compAddTimeout(3000, removeTitle, s);*/
}
}
return NULL; // This is required for pthread
@@ -457,8 +461,6 @@ static Bool checkConnected (void *vd)
"Error requesting state. Calibration will not work!");
}
- /* Toggle LED 1 ON */
-
/* TODO:
* Toggle certain LED's for certain
* Wii Remote 'Types'
@@ -471,7 +473,8 @@ static Bool checkConnected (void *vd)
* quite work right yet :(
*/
- ad->cWiimote[ad->nWiimote].lightsTimeoutHandle = compAddTimeout (650, wiimoteChangeLights, ad->cWiimote[ad->nWiimote].wiimote);
+ ad->cWiimote[ad->nWiimote].lightsTimeoutHandle =
+ compAddTimeout (650, wiimoteChangeLights, ad->cWiimote[ad->nWiimote].wiimote);
/* Set some variables using the state interface
* FIXME: For some reason, over-querying the
@@ -499,7 +502,9 @@ static Bool checkConnected (void *vd)
else
{
compLogMessage (d, "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");
+ "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 */
@@ -509,7 +514,187 @@ static Bool checkConnected (void *vd)
return TRUE;
}
-/* Paint title on screen if neccessary */
+
+/* Prototype cwiid_callback with cwiid_callback_t, define it with the actual
+ * type - this will cause a compile error (rather than some undefined bizarre
+ * behavior) if cwiid_callback_t changes */
+/* cwiid_mesg_callback_t has undergone a few changes lately, hopefully this
+ * will be the last. Some programs need to know which messages were received
+ * simultaneously (e.g. for correlating accelerometer and IR data), and the
+ * sequence number mechanism used previously proved cumbersome, so we just
+ * pass an array of messages, all of which were received at the same time.
+ * The id is to distinguish between multiple wiimotes using the same callback.
+ * */
+
+/* Essentially what this does, is a small callback whenever cwiid_callback (The
+ * prototype to cwiid_callback_t) is 'called' by cwiid_callback_t (All the time),
+ * it will print out status reports. It is NOT a loop inside this plugin, but instead
+ * is called with a number of informations about the Wii Remote at the time.
+ */
+
+/* Since we can't actually get a CompDisplay *d into the arguements
+ * we kind of cheat and use the first display provided by CompCore *c.
+ * Obviously this is going to become depracted as soon as David releases
+ * some sort of system to have multiple diplays, but I assume that we
+ * can move this information into the core structs when that happens.
+ * (Assuming that there is only one core)
+ */
+
+void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
+ union cwiid_mesg mesg[], struct timespec *timestamp)
+{
+ int i, j;
+ int valid_source;
+ float totalX = 0.0f, totalY = 0.0f;
+ int count = 0;
+ int id = cwiid_get_id (wiimote);
+ int wiimoteNumber = 0;
+ struct cwiid_state state;
+
+ cwiid_get_state(wiimote, &state);
+ //unsigned char led_state = 0;
+
+ WIIMOTE_DISPLAY (firstDisplay);
+ ad->cWiimote[wiimoteNumber].nunchuck.connected = FALSE;
+
+ for (i = 0; i < MAX_WIIMOTES; i++)
+ {
+ if (id == ad->cWiimote[i].id)
+ {
+ wiimoteNumber = i;
+ break;
+ }
+ }
+
+ for (i=0; i < mesg_count; i++)
+ {
+ switch (mesg[i].type) {
+ case CWIID_MESG_STATUS:
+ switch (mesg[i].status_mesg.ext_type) {
+ case CWIID_EXT_NONE:
+ break;
+ case CWIID_EXT_NUNCHUK:
+ ad->cWiimote[wiimoteNumber].nunchuck.connected = TRUE;
+ ad->cWiimote[wiimoteNumber].nunchuck.initAccX =
+ state.ext.nunchuk.acc[CWIID_X];
+ ad->cWiimote[wiimoteNumber].nunchuck.initAccY =
+ state.ext.nunchuk.acc[CWIID_Y];
+ ad->cWiimote[wiimoteNumber].nunchuck.initAccZ =
+ state.ext.nunchuk.acc[CWIID_Z];
+ ad->cWiimote[wiimoteNumber].nunchuck.initStickX =
+ state.ext.nunchuk.stick[CWIID_X];
+ ad->cWiimote[wiimoteNumber].nunchuck.initStickY =
+ state.ext.nunchuk.stick[CWIID_Y];
+ wiimoteProcessNunchuckButtons(firstDisplay, wiimoteNumber, (struct cwiid_nunchuk_mesg *) &mesg[i]);
+ break;
+ case CWIID_EXT_CLASSIC:
+ break;
+ default:
+ break;
+ }
+ printf("\n");
+ break;
+ case CWIID_MESG_BTN:
+ wiimoteProcessButtons(firstDisplay, wiimoteNumber, (struct cwiid_btn_mesg *) &mesg[i]);
+ break;
+ case CWIID_MESG_ACC:
+ ad->cWiimote[wiimoteNumber].acc.accDX = ((ad->cWiimote[wiimoteNumber].acc.accX) - (ad->cWiimote[wiimoteNumber].acc.initAccX));
+ ad->cWiimote[wiimoteNumber].acc.accDY = ((ad->cWiimote[wiimoteNumber].acc.accY) - (ad->cWiimote[wiimoteNumber].acc.initAccY));
+ ad->cWiimote[wiimoteNumber].acc.accX = mesg[i].acc_mesg.acc[CWIID_X];
+ ad->cWiimote[wiimoteNumber].acc.accY = mesg[i].acc_mesg.acc[CWIID_Y];
+ ad->cWiimote[wiimoteNumber].acc.accZ = mesg[i].acc_mesg.acc[CWIID_Z];
+ break;
+ case CWIID_MESG_IR:
+ valid_source = 0;
+ for (j = 0; j < CWIID_IR_SRC_COUNT; j++) {
+ if (mesg[i].ir_mesg.src[j].valid) {
+ valid_source = 1;
+ ad->cWiimote[wiimoteNumber].ir[j].valid = TRUE;
+ ad->cWiimote[wiimoteNumber].ir[j].x = mesg[i].ir_mesg.src[j].pos[CWIID_X];
+ ad->cWiimote[wiimoteNumber].ir[j].y = mesg[i].ir_mesg.src[j].pos[CWIID_Y];
+ ad->cWiimote[wiimoteNumber].ir[j].size = mesg[i].ir_mesg.src[j].size;
+ totalX += mesg[i].ir_mesg.src[j].pos[CWIID_X];
+ totalY += mesg[i].ir_mesg.src[j].pos[CWIID_Y];
+ count++;
+ }
+ else
+ {
+ ad->cWiimote[wiimoteNumber].ir[j].valid = FALSE;
+ }
+ }
+ break;
+ case CWIID_MESG_NUNCHUK:
+ ad->cWiimote[wiimoteNumber].nunchuck.connected = TRUE;
+ ad->cWiimote[wiimoteNumber].nunchuck.accDX = ((ad->cWiimote[wiimoteNumber].nunchuck.accX) - (ad->cWiimote[wiimoteNumber].nunchuck.initAccX));
+ ad->cWiimote[wiimoteNumber].nunchuck.accDY = ((ad->cWiimote[wiimoteNumber].nunchuck.accY) - (ad->cWiimote[wiimoteNumber].nunchuck.initAccY));
+ ad->cWiimote[wiimoteNumber].nunchuck.accX = mesg[i].nunchuk_mesg.acc[CWIID_X];
+ ad->cWiimote[wiimoteNumber].nunchuck.accY = mesg[i].nunchuk_mesg.acc[CWIID_Y];
+ ad->cWiimote[wiimoteNumber].nunchuck.accZ = mesg[i].nunchuk_mesg.acc[CWIID_Z];
+ ad->cWiimote[wiimoteNumber].nunchuck.stickDX = ((ad->cWiimote[wiimoteNumber].nunchuck.stickX) - (ad->cWiimote[wiimoteNumber].nunchuck.initStickX));
+ ad->cWiimote[wiimoteNumber].nunchuck.stickDY = ((ad->cWiimote[wiimoteNumber].nunchuck.stickY) - (ad->cWiimote[wiimoteNumber].nunchuck.initStickY));
+ ad->cWiimote[wiimoteNumber].nunchuck.stickX = mesg[i].nunchuk_mesg.stick[CWIID_X];
+ ad->cWiimote[wiimoteNumber].nunchuck.stickY = mesg[i].nunchuk_mesg.stick[CWIID_Y];
+ break;
+ case CWIID_MESG_CLASSIC:
+ printf("Classic Report: btns=%.4X l_stick=(%d,%d) r_stick=(%d,%d) "
+ "l=%d r=%d\n", mesg[i].classic_mesg.buttons,
+ mesg[i].classic_mesg.l_stick[CWIID_X],
+ mesg[i].classic_mesg.l_stick[CWIID_Y],
+ mesg[i].classic_mesg.r_stick[CWIID_X],
+ mesg[i].classic_mesg.r_stick[CWIID_Y],
+ mesg[i].classic_mesg.l, mesg[i].classic_mesg.r);
+ break;
+ case CWIID_MESG_ERROR:
+ if (cwiid_close(wiimote)) {
+ fprintf(stderr, "Error on wiimote disconnect\n");
+ exit(-1);
+ }
+ exit(0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ /* Find the minimum and maximum points */
+
+ int min, max;
+ float xRange = 0.0f;
+ if (ad->cWiimote[wiimoteNumber].ir[1].valid)
+ {
+ ad->cWiimote[wiimoteNumber].irMidX = (totalX / count);
+ ad->cWiimote[wiimoteNumber].irMidY = (totalY / count);
+ }
+ else
+ {
+ ad->cWiimote[wiimoteNumber].irMidX = 1.0f;
+ ad->cWiimote[wiimoteNumber].irMidY = 1.0f;
+ }
+ if (ad->cWiimote[wiimoteNumber].ir[0].valid && ad->cWiimote[wiimoteNumber].ir[1].valid)
+ {
+ min = findMinIR (firstDisplay, wiimoteNumber,
+ ad->cWiimote[wiimoteNumber].ir[0].x,
+ ad->cWiimote[wiimoteNumber].ir[1].x,
+ ad->cWiimote[wiimoteNumber].ir[2].x,
+ ad->cWiimote[wiimoteNumber].ir[3].x);
+
+ max = findMaxIR (firstDisplay, wiimoteNumber,
+ ad->cWiimote[wiimoteNumber].ir[0].x,
+ ad->cWiimote[wiimoteNumber].ir[1].x,
+ ad->cWiimote[wiimoteNumber].ir[2].x,
+ ad->cWiimote[wiimoteNumber].ir[3].x);
+
+ xRange = max - min;
+ }
+ else
+ {
+ xRange = 1.0;
+ }
+ ad->cWiimote[wiimoteNumber].irDistance = (xRange / 100);
+
+}
+
+/* Wrappable Functions --------------------------------------------------- */
static Bool
wiimotePaintOutput (CompScreen *s,
@@ -539,10 +724,8 @@ wiimotePaintOutput (CompScreen *s,
return status;
}
-/* Report Creation */
-/* Get the data first */
+/* Inter-Plugin Communication --------------------------------------------------- */
-/* Info Send */
static Bool
sendInfoToPlugin (CompDisplay *d, CompOption *argument, int nArgument, char *pluginName, char *actionName)
{
@@ -627,6 +810,7 @@ static Bool sendGesture(CompDisplay *d, int wiimoteNumber, int type)
return TRUE;
}
+/* Value Reporting --------------------------------------------------- */
static Bool sendReports(void *vs)
{
@@ -752,7 +936,7 @@ static Bool sendReports(void *vs)
}
-/* Loosely based off wminput */
+/* Gesturing and gesture checking --------------------------------------------- */
#define CHECK_BUTTON(a,b,c,e) \
if (mesg->buttons & a) \
@@ -853,231 +1037,7 @@ wiimoteCheckForGestures (void *vs)
return TRUE;
}
-
-/* Utility */
-static int findMinIR (CompDisplay *d, int wiimoteNumber, int ir1, int ir2, int ir3, int ir4)
-{
- WIIMOTE_DISPLAY (d);
-
- int min = ir1;
-
- if ((ir2 < min) && ad->cWiimote[wiimoteNumber].ir[1].valid)
- min = ir2;
- if ((ir3 < min) && ad->cWiimote[wiimoteNumber].ir[2].valid)
- min = ir3;
- if ((ir4 < min) && ad->cWiimote[wiimoteNumber].ir[3].valid)
- min = ir4;
-
- return min;
-}
-
-static int findMaxIR (CompDisplay *d, int wiimoteNumber, int ir1, int ir2, int ir3, int ir4)
-{
- WIIMOTE_DISPLAY (d);
-
- int max = ir1;
-
- if ((ir2 > max) && ad->cWiimote[wiimoteNumber].ir[1].valid)
- max = ir2;
- if ((ir3 > max) && ad->cWiimote[wiimoteNumber].ir[2].valid)
- max = ir3;
- if ((ir4 > max) && ad->cWiimote[wiimoteNumber].ir[3].valid)
- max = ir4;
-
- return max;
-}
-
-/* Prototype cwiid_callback with cwiid_callback_t, define it with the actual
- * type - this will cause a compile error (rather than some undefined bizarre
- * behavior) if cwiid_callback_t changes */
-/* cwiid_mesg_callback_t has undergone a few changes lately, hopefully this
- * will be the last. Some programs need to know which messages were received
- * simultaneously (e.g. for correlating accelerometer and IR data), and the
- * sequence number mechanism used previously proved cumbersome, so we just
- * pass an array of messages, all of which were received at the same time.
- * The id is to distinguish between multiple wiimotes using the same callback.
- * */
-
-/* Essentially what this does, is a small callback whenever cwiid_callback (The
- * prototype to cwiid_callback_t) is 'called' by cwiid_callback_t (All the time),
- * it will print out status reports. It is NOT a loop inside this plugin, but instead
- * is called with a number of informations about the Wii Remote at the time.
- */
-
-/* Since we can't actually get a CompDisplay *d into the arguements
- * we kind of cheat and use the first display provided by CompCore *c.
- * Obviously this is going to become depracted as soon as David releases
- * some sort of system to have multiple diplays, but I assume that we
- * can move this information into the core structs when that happens.
- * (Assuming that there is only one core)
- */
-
-void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
- union cwiid_mesg mesg[], struct timespec *timestamp)
-{
- int i, j;
- int valid_source;
- float totalX = 0.0f, totalY = 0.0f;
- int count = 0;
- int id = cwiid_get_id (wiimote);
- int wiimoteNumber = 0;
- struct cwiid_state state;
-
- cwiid_get_state(wiimote, &state);
- //unsigned char led_state = 0;
-
- WIIMOTE_DISPLAY (firstDisplay);
- ad->cWiimote[wiimoteNumber].nunchuck.connected = FALSE;
-
- for (i = 0; i < MAX_WIIMOTES; i++)
- {
- if (id == ad->cWiimote[i].id)
- {
- wiimoteNumber = i;
- break;
- }
- }
-
- for (i=0; i < mesg_count; i++)
- {
- switch (mesg[i].type) {
- case CWIID_MESG_STATUS:
- /*printf("Status Report: battery=%d extension=",
- mesg[i].status_mesg.battery);*/
- switch (mesg[i].status_mesg.ext_type) {
- case CWIID_EXT_NONE:
- /*printf("none");*/
- break;
- case CWIID_EXT_NUNCHUK:
- ad->cWiimote[wiimoteNumber].nunchuck.connected = TRUE;
- ad->cWiimote[wiimoteNumber].nunchuck.initAccX =
- state.ext.nunchuk.acc[CWIID_X];
- ad->cWiimote[wiimoteNumber].nunchuck.initAccY =
- state.ext.nunchuk.acc[CWIID_Y];
- ad->cWiimote[wiimoteNumber].nunchuck.initAccZ =
- state.ext.nunchuk.acc[CWIID_Z];
- ad->cWiimote[wiimoteNumber].nunchuck.initStickX =
- state.ext.nunchuk.stick[CWIID_X];
- ad->cWiimote[wiimoteNumber].nunchuck.initStickY =
- state.ext.nunchuk.stick[CWIID_Y];
- wiimoteProcessNunchuckButtons(firstDisplay, wiimoteNumber, (struct cwiid_nunchuk_mesg *) &mesg[i]);
- break;
- case CWIID_EXT_CLASSIC:
- /*printf("Classic Controller");*/
- break;
- default:
- /*printf("Unknown Extension");*/
- break;
- }
- printf("\n");
- break;
- case CWIID_MESG_BTN:
- wiimoteProcessButtons(firstDisplay, wiimoteNumber, (struct cwiid_btn_mesg *) &mesg[i]);
- break;
- case CWIID_MESG_ACC:
- ad->cWiimote[wiimoteNumber].acc.accDX = ((ad->cWiimote[wiimoteNumber].acc.accX) - (ad->cWiimote[wiimoteNumber].acc.initAccX));
- ad->cWiimote[wiimoteNumber].acc.accDY = ((ad->cWiimote[wiimoteNumber].acc.accY) - (ad->cWiimote[wiimoteNumber].acc.initAccY));
- ad->cWiimote[wiimoteNumber].acc.accX = mesg[i].acc_mesg.acc[CWIID_X];
- ad->cWiimote[wiimoteNumber].acc.accY = mesg[i].acc_mesg.acc[CWIID_Y];
- ad->cWiimote[wiimoteNumber].acc.accZ = mesg[i].acc_mesg.acc[CWIID_Z];
- break;
- case CWIID_MESG_IR:
- //printf("IR Report: ");
- valid_source = 0;
- for (j = 0; j < CWIID_IR_SRC_COUNT; j++) {
- if (mesg[i].ir_mesg.src[j].valid) {
- valid_source = 1;
- ad->cWiimote[wiimoteNumber].ir[j].valid = TRUE;
- ad->cWiimote[wiimoteNumber].ir[j].x = mesg[i].ir_mesg.src[j].pos[CWIID_X];
- ad->cWiimote[wiimoteNumber].ir[j].y = mesg[i].ir_mesg.src[j].pos[CWIID_Y];
- ad->cWiimote[wiimoteNumber].ir[j].size = mesg[i].ir_mesg.src[j].size;
- totalX += mesg[i].ir_mesg.src[j].pos[CWIID_X];
- totalY += mesg[i].ir_mesg.src[j].pos[CWIID_Y];
- count++;
- }
- else
- {
- ad->cWiimote[wiimoteNumber].ir[j].valid = FALSE;
- }
- }
- if (!valid_source) {
- //printf("no sources detected");
- }
- //printf("\n");
- break;
- case CWIID_MESG_NUNCHUK:
- ad->cWiimote[wiimoteNumber].nunchuck.connected = TRUE;
- ad->cWiimote[wiimoteNumber].nunchuck.accDX = ((ad->cWiimote[wiimoteNumber].nunchuck.accX) - (ad->cWiimote[wiimoteNumber].nunchuck.initAccX));
- ad->cWiimote[wiimoteNumber].nunchuck.accDY = ((ad->cWiimote[wiimoteNumber].nunchuck.accY) - (ad->cWiimote[wiimoteNumber].nunchuck.initAccY));
- ad->cWiimote[wiimoteNumber].nunchuck.accX = mesg[i].nunchuk_mesg.acc[CWIID_X];
- ad->cWiimote[wiimoteNumber].nunchuck.accY = mesg[i].nunchuk_mesg.acc[CWIID_Y];
- ad->cWiimote[wiimoteNumber].nunchuck.accZ = mesg[i].nunchuk_mesg.acc[CWIID_Z];
- ad->cWiimote[wiimoteNumber].nunchuck.stickDX = ((ad->cWiimote[wiimoteNumber].nunchuck.stickX) - (ad->cWiimote[wiimoteNumber].nunchuck.initStickX));
- ad->cWiimote[wiimoteNumber].nunchuck.stickDY = ((ad->cWiimote[wiimoteNumber].nunchuck.stickY) - (ad->cWiimote[wiimoteNumber].nunchuck.initStickY));
- ad->cWiimote[wiimoteNumber].nunchuck.stickX = mesg[i].nunchuk_mesg.stick[CWIID_X];
- ad->cWiimote[wiimoteNumber].nunchuck.stickY = mesg[i].nunchuk_mesg.stick[CWIID_Y];
- break;
- case CWIID_MESG_CLASSIC:
- printf("Classic Report: btns=%.4X l_stick=(%d,%d) r_stick=(%d,%d) "
- "l=%d r=%d\n", mesg[i].classic_mesg.buttons,
- mesg[i].classic_mesg.l_stick[CWIID_X],
- mesg[i].classic_mesg.l_stick[CWIID_Y],
- mesg[i].classic_mesg.r_stick[CWIID_X],
- mesg[i].classic_mesg.r_stick[CWIID_Y],
- mesg[i].classic_mesg.l, mesg[i].classic_mesg.r);
- break;
- case CWIID_MESG_ERROR:
- if (cwiid_close(wiimote)) {
- fprintf(stderr, "Error on wiimote disconnect\n");
- exit(-1);
- }
- exit(0);
- break;
- default:
- /*printf("Unknown Report");*/
- break;
- }
- }
-
- /* Find the minimum and maximum points */
-
- int min, max;
- float xRange = 0.0f;
- if (ad->cWiimote[wiimoteNumber].ir[1].valid)
- {
- ad->cWiimote[wiimoteNumber].irMidX = (totalX / count);
- ad->cWiimote[wiimoteNumber].irMidY = (totalY / count);
- }
- else
- {
- ad->cWiimote[wiimoteNumber].irMidX = 1.0f;
- ad->cWiimote[wiimoteNumber].irMidY = 1.0f;
- }
- if (ad->cWiimote[wiimoteNumber].ir[0].valid && ad->cWiimote[wiimoteNumber].ir[1].valid)
- {
- min = findMinIR (firstDisplay, wiimoteNumber,
- ad->cWiimote[wiimoteNumber].ir[0].x,
- ad->cWiimote[wiimoteNumber].ir[1].x,
- ad->cWiimote[wiimoteNumber].ir[2].x,
- ad->cWiimote[wiimoteNumber].ir[3].x);
-
- max = findMaxIR (firstDisplay, wiimoteNumber,
- ad->cWiimote[wiimoteNumber].ir[0].x,
- ad->cWiimote[wiimoteNumber].ir[1].x,
- ad->cWiimote[wiimoteNumber].ir[2].x,
- ad->cWiimote[wiimoteNumber].ir[3].x);
-
- xRange = max - min;
- }
- else
- {
- xRange = 1.0;
- }
- ad->cWiimote[wiimoteNumber].irDistance = (xRange / 100);
-
-}
-
-/* Callable Actions */
+/* Callable Actions --------------------------------------------------- */
static Bool
wiimoteToggle (CompDisplay *d,
@@ -1089,7 +1049,8 @@ wiimoteToggle (CompDisplay *d,
int rc;
WIIMOTE_DISPLAY (d);
-
+
+ /* Create second thread attributes */
pthread_attr_t secondThreadAttr;
pthread_attr_init(&secondThreadAttr);
@@ -1104,7 +1065,8 @@ wiimoteToggle (CompDisplay *d,
CompScreen *s;
s = findScreenAtDisplay (d, getIntOptionNamed (option, nOption,
"root", 0));
- if (s) {
+ if (s)
+ {
ad->firstRoot = s->root;
/* Create Message */
@@ -1136,10 +1098,10 @@ wiimoteToggle (CompDisplay *d,
"display_text");
compLogMessage (d, "wiimote", CompLogLevelInfo,
"Hold down the 1 and 2 Buttons on your Nintendo Wii Remote now");
- // Create the second thread.
+ /* Create the second thread. */
rc = pthread_create(&ad->cWiimote[ad->nWiimote].connectWiimote, &secondThreadAttr, connectWiimote, (void *)d);
- // Clean up second thread's attribute.
+ /* Clean up second thread's attribute. */
pthread_attr_destroy(&secondThreadAttr);
if (!rc)
{
@@ -1164,8 +1126,8 @@ wiimoteDisable (CompDisplay *d,
WIIMOTE_DISPLAY (d);
/* nWiimote -1 because we increment nWiimote
- * so we need to find the most recent (Which is -1)
- */
+ * so we need to find the most recent (Which is -1)
+ */
if (ad->nWiimote < 1)
@@ -1174,13 +1136,14 @@ wiimoteDisable (CompDisplay *d,
ad->cWiimote[ad->nWiimote - 1].initiated = FALSE;
ad->cWiimote[ad->nWiimote - 1].connected = FALSE;
+ /* Wait for the second thread to return control to itself */
- rc = pthread_join(ad->cWiimote[ad->nWiimote -1].connectWiimote, NULL); // Wait for second thread to complete it's operation.
+ rc = pthread_join(ad->cWiimote[ad->nWiimote -1].connectWiimote, NULL);
if (rc)
compLogMessage (d, "wiimote", CompLogLevelError,
"Threading error occurred.\n");
- /* Remove that timeout handle... */
+ /* Remove timeout handles */
if (ad->infoTimeoutHandle)
compRemoveTimeout (ad->infoTimeoutHandle);
@@ -1192,17 +1155,21 @@ wiimoteDisable (CompDisplay *d,
if (ad->cWiimote[ad->nWiimote - 1].lightsTimeoutHandle)
compRemoveTimeout (ad->cWiimote[ad->nWiimote - 1].lightsTimeoutHandle);
+
+ /* Tell the user that we are closing the connection */
+
compLogMessage (d, "wiimote", CompLogLevelInfo,
"Closing connection to most recently connected Wii Remote\n");
if (cwiid_close(ad->cWiimote[ad->nWiimote - 1].wiimote)) {
compLogMessage (d, "wiimote", CompLogLevelError,
- "Error closing that connection\nb");
+ "Error closing that connection\n");
return -1;
}
/* Decrement Wiimote Iter */
ad->nWiimote--;
+
return TRUE;
}
@@ -1245,7 +1212,7 @@ wiimoteSendInfo (CompDisplay *d,
return TRUE;
}
-/* Option Initialisation */
+/* Option Initialisation --------------------------------------------------- */
static void
reloadWiimoteGestures (CompDisplay *d)
@@ -1405,22 +1372,12 @@ reloadWiimoteOptions (CompDisplay *d)
}
}
}
-
-/* Configuration, initialization, boring stuff. ----------------------- */
-
-/* Takes the action and toggles us.
-*/
-
-
-/* Change notify for bcop */
static void
wiimoteDisplayOptionChanged (CompDisplay *d,
CompOption *opt,
WiimoteDisplayOptions num)
{
- /* For a long process, go through all the options and process them*/
-
switch (num)
{
@@ -1455,7 +1412,9 @@ wiimoteDisplayOptionChanged (CompDisplay *d,
}
}
-/* We're cheating to find the first display :o */
+/* Core Initialization --------------------------------------------------- */
+
+/* Use initCore() to find the first display (c->displays) */
static Bool
wiimoteInitCore (CompPlugin *p,
@@ -1482,6 +1441,8 @@ wiimoteFiniCore (CompPlugin *p,
freeDisplayPrivateIndex (wiimoteDisplayPrivateIndex);
}
+/* Display Initialization --------------------------------------------------- */
+
static Bool
wiimoteInitDisplay (CompPlugin *p,
CompDisplay *d)
@@ -1554,11 +1515,12 @@ wiimoteFiniDisplay (CompPlugin *p,
{
WIIMOTE_DISPLAY (d);
-
freeScreenPrivateIndex (d, ad->screenPrivateIndex);
free (ad);
}
+/* Screen Initialization --------------------------------------------------- */
+
static Bool
wiimoteInitScreen (CompPlugin *p,
CompScreen *s)
@@ -1589,6 +1551,8 @@ wiimoteFiniScreen (CompPlugin *p,
free (as);
}
+/* Object Initialization --------------------------------------------------- */
+
static CompBool
wiimoteInitObject (CompPlugin *p,
CompObject *o)
@@ -1615,6 +1579,8 @@ wiimoteFiniObject (CompPlugin *p,
DISPATCH (o, dispTab, ARRAY_SIZE (dispTab), (p, o));
}
+/* Plugin Initialization --------------------------------------------------- */
+
static Bool
wiimoteInit (CompPlugin *p)
{