#ifndef WIN32 #include #define WIIUSE_PATH "./wiiuse.so" #else #define WIIUSE_PATH "wiiuse.dll" #endif #include "wiiusej_WiiUseApi.h" #include "wiiuse.h" /* * These are some identifiers for wiimotes * * See below in main() for what they are used for. */ #define WIIMOTE_STATE_RUMBLE 0x08 #define WIIMOTE_STATE_CONNECTED 0x04 #define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s)) #define WIIMOTE_IS_FLAG_SET(wm, s) ((wm->flags & (s)) == (s)) /********************* VARIABLES DECLARATIONS *****************************/ /* * Make a temp array of wiimote ids. * Here I only anticipate connecting up to * two wiimotes. Each wiimote connected * will get one of these ids. */ static wiimote** wiimotes; static int nbMaxWiiMotes=0; static JNIEnv *globalEnv; static jobject globalObj; static jobject globalWim; /****************** GENERAL FUNCTIONS DEFINITIONS *************************/ /** * Try to connect to 2 wiimotes. * Make them rumble to show they are connected. * @param nbConnects number of connections maximum. * @return 0 if there is an error otherwise it returns * the number of wiimotes connected.. */ JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_doConnections (JNIEnv *env, jobject obj, jint nbConnects) { /* variables declarations */ int found, connected, i; nbMaxWiiMotes = nbConnects; /* initialize wiimotes array with the maximum number of wiimotes */ wiimotes = wiiuse_init(nbMaxWiiMotes); /* * Find wiimote devices * Now we need to find some wiimotes. * Give the function the wiimote array we created, and tell it there * are 2 wiimotes we are interested in. * Set the timeout to be 5 seconds. * This will return the number of actual wiimotes that are in discovery mode. */ found = wiiuse_find(wiimotes, nbMaxWiiMotes, 5); if (!found) return 0; /* * Connect to the wiimotes * Now that we found some wiimotes, connect to them. * Give the function the wiimote array and the number of wiimote devices we found. * This will return the number of established connections to the found wiimotes. */ connected = wiiuse_connect(wiimotes, nbMaxWiiMotes); if (!connected) return 0; //no problems during connection show that wiimotes are connected /* * Now set the LEDs and rumble for a second so it's easy * to tell which wiimotes are connected (just like the wii does). */ for (i=0;iGetObjectClass(globalEnv, globalWim); jmethodID mid; globalEnv = env; globalObj = obj; globalWim = wim; if (wiiuse_poll(wiimotes, nbMaxWiiMotes)) { /* * This happens if something happened on any wiimote. * So go through each one and check if anything happened. */ for (i=0; i < nbMaxWiiMotes; ++i) { switch (wiimotes[i]->event) { case WIIUSE_EVENT: /* a generic event occured */ mid = (*globalEnv)->GetMethodID(globalEnv, cls, "prepareWiiMoteEvent", "(ISSS)V"); if (mid == 0) { return; } (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wiimotes[i]->unid, wiimotes[i]->btns, wiimotes[i]->btns_released, wiimotes[i]->btns_held); /* * If IR tracking is enabled then print the coordinates * on the virtual screen that the wiimote is pointing to. * * Also make sure that we see at least 1 dot. */ if (WIIUSE_USING_IR(wiimotes[i])) { int i = 0; /* go through each of the 4 possible IR sources */ for (; i < 4; ++i) { /* check if the source is visible */ if (wiimotes[i]->ir.dot[i].visible) { mid = (*globalEnv)->GetMethodID(globalEnv, cls, "addIRPointToPreparedWiiMoteEvent", "(II)V"); if (mid == 0) { return; } (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wiimotes[i]->ir.dot[i].x, wiimotes[i]->ir.dot[i].y); } } } /* Motion Sensing */ if (WIIUSE_USING_ACC(wiimotes[i])) { /* set orientation and gravity force */ mid = (*globalEnv)->GetMethodID(globalEnv, cls, "addMotionSensingValues", "(FFFFFF)V"); if (mid == 0) { return; } (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wiimotes[i]->orient.roll, wiimotes[i]->orient.pitch, wiimotes[i]->orient.yaw, wiimotes[i]->gforce.x, wiimotes[i]->gforce.y, wiimotes[i]->gforce.z); } mid = (*globalEnv)->GetMethodID(globalEnv, cls, "addWiimoteEvent", "()V"); if (mid == 0) { return; } (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); break; case WIIUSE_STATUS: /* a status event occured */ mid = (*globalEnv)->GetMethodID(globalEnv, cls, "addDisconnectionEvent", "(IZFSZIZFFFZZZZ)V"); if (mid == 0) { return; } /* LEDS */ if (WIIUSE_IS_LED_SET(wiimotes[i], 1)) leds += 1; if (WIIUSE_IS_LED_SET(wiimotes[i], 2)) leds += 2; if (WIIUSE_IS_LED_SET(wiimotes[i], 3)) leds += 4; if (WIIUSE_IS_LED_SET(wiimotes[i], 4)) leds += 8; (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wiimotes[i]->unid, WIIMOTE_IS_SET(wiimotes[i], WIIMOTE_STATE_CONNECTED), wiimotes[i]->battery_level, leds, WIIUSE_USING_SPEAKER(wiimotes[i]), wiimotes[i]->exp.type,WIIMOTE_IS_SET(wiimotes[i], WIIMOTE_STATE_RUMBLE), wiimotes[i]->orient_threshold, wiimotes[i]->accel_threshold, wiimotes[i]->accel_calib.st_alpha, WIIMOTE_IS_FLAG_SET(wiimotes[i],WIIUSE_CONTINUOUS), WIIMOTE_IS_FLAG_SET(wiimotes[i],WIIUSE_SMOOTHING), WIIUSE_USING_IR(wiimotes[i]), WIIUSE_USING_ACC(wiimotes[i])); break; case WIIUSE_DISCONNECT: /* the wiimote disconnected */ mid = (*globalEnv)->GetMethodID(globalEnv, cls, "addDisconnectionEvent", "(I)V"); if (mid == 0) { return; } (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wiimotes[i]->unid); break; default: break; } } } }