summaryrefslogtreecommitdiff
path: root/action.c
diff options
context:
space:
mode:
authorSam Spillaz <smspillaz@gmail.com>2008-04-26 21:26:42 +0800
committerSam Spillaz <smspillaz@gmail.com>2008-04-26 21:26:42 +0800
commite1737e4024c66e066531fd3552abaf5428c1c382 (patch)
tree9b8fca80b1e6e0db9c2ff4f81f7341d8aab1ad64 /action.c
parentca73db7c91c0e28c3170f9946c168cf4576befa8 (diff)
downloadwiimote-e1737e4024c66e066531fd3552abaf5428c1c382.tar.gz
wiimote-e1737e4024c66e066531fd3552abaf5428c1c382.tar.bz2
* Added the new files that I split into (I think I went overboard. Meh)
Diffstat (limited to 'action.c')
-rw-r--r--action.c198
1 files changed, 198 insertions, 0 deletions
diff --git a/action.c b/action.c
new file mode 100644
index 0000000..6e83042
--- /dev/null
+++ b/action.c
@@ -0,0 +1,198 @@
+/**
+ *
+ * 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"
+
+/* Callable Actions --------------------------------------------------- */
+
+Bool
+wiimoteToggle (CompDisplay *d,
+ CompAction *action,
+ CompActionState cstate,
+ CompOption *option,
+ int nOption)
+{
+ int rc;
+
+ WIIMOTE_DISPLAY (d);
+
+ /* Create second thread attributes */
+
+ pthread_attr_t secondThreadAttr;
+ pthread_attr_init(&secondThreadAttr);
+ pthread_attr_setdetachstate(&secondThreadAttr, PTHREAD_CREATE_JOINABLE);
+
+ CompWindow *w;
+ w = findWindowAtDisplay (d, getIntOptionNamed (option, nOption,
+ "window", 0));
+ if (!w)
+ return TRUE;
+
+ CompScreen *s;
+ s = findScreenAtDisplay (d, getIntOptionNamed (option, nOption,
+ "root", 0));
+ if (s)
+ {
+ ad->firstRoot = s->root;
+
+ /* Create Message */
+ 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 = "Put your Wii Remote in a neutral position and \n"\
+ "hold down the 1 and 2 Buttons now";
+ nArg++;
+
+ arg[nArg].name = "timeout";
+ arg[nArg].type = CompOptionTypeInt;
+ arg[nArg].value.i = 2000;
+
+ sendInfoToPlugin (d, arg, nArg,
+ "prompt",
+ "display_text");
+ compLogMessage (d, "wiimote", CompLogLevelInfo,
+ "Hold down the 1 and 2 Buttons on your Nintendo Wii Remote now");
+ /* Create the second thread. */
+ rc = pthread_create(&ad->cWiimote[ad->nWiimote].connectWiimote, &secondThreadAttr, connectWiimote, (void *)d);
+
+ /* Clean up second thread's attribute. */
+ pthread_attr_destroy(&secondThreadAttr);
+ if (!rc)
+ {
+ /* So the thread succeeded!
+ * Start Checking! */
+ ad->checkingTimeoutHandle = compAddTimeout (10, checkConnected, d);
+ }
+ }
+ return TRUE;
+}
+
+Bool
+wiimoteDisable (CompDisplay *d,
+ CompAction *action,
+ CompActionState cstate,
+ CompOption *option,
+ int nOption)
+{
+
+ int rc;
+
+ WIIMOTE_DISPLAY (d);
+
+ /* nWiimote -1 because we increment nWiimote
+ * so we need to find the most recent (Which is -1)
+ */
+
+
+ if (ad->nWiimote < 1)
+ return FALSE;
+
+ 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);
+ if (rc)
+ compLogMessage (d, "wiimote", CompLogLevelError,
+ "Threading error occurred.\n");
+
+ /* Remove timeout handles */
+
+ if (ad->infoTimeoutHandle)
+ compRemoveTimeout (ad->infoTimeoutHandle);
+
+ if (ad->gestureTimeoutHandle)
+ compRemoveTimeout (ad->gestureTimeoutHandle);
+
+ /* Remove the lights timeout handle */
+
+ 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\n");
+ return -1;
+ }
+
+ /* Decrement Wiimote Iter */
+
+ ad->nWiimote--;
+
+ return TRUE;
+}
+
+
+
+Bool
+wiimoteSendInfo (CompDisplay *d,
+ CompAction *action,
+ CompActionState cstate,
+ CompOption *option,
+ int nOption)
+{
+ WIIMOTE_DISPLAY (d);
+
+ Window root;
+ CompScreen *s;
+
+ ad->report = !ad->report;
+
+ if (!ad->report)
+ {
+ if (ad->infoTimeoutHandle)
+ compRemoveTimeout (ad->infoTimeoutHandle);
+ return FALSE;
+ }
+
+ root = getIntOptionNamed (option, nOption, "root", 0);
+ s = findScreenAtDisplay (d, root);
+
+ if (s)
+ {
+ /* Add a timeout to send info to another
+ * plugin. The preferable way would be for
+ * other plugins to get the values when they
+ * need them, but this is just a workaround
+ * for plugins that support the action system
+ */
+
+ ad->infoTimeoutHandle = compAddTimeout(wiimoteGetPollInterval (s->display) , sendReports, s);
+ ad->gestureTimeoutHandle = compAddTimeout(wiimoteGetGestureTimeout (s->display) , wiimoteCheckForGestures, s);
+ }
+ return TRUE;
+}