diff --git a/WiiUseJC/wiiuse.c b/WiiUseJC/wiiuse.c new file mode 100644 index 0000000..23db02f --- /dev/null +++ b/WiiUseJC/wiiuse.c @@ -0,0 +1,168 @@ +/* + * wiiuse + * + * Written By: + * Michael Laforest < para > + * Email: < thepara (--AT--) g m a i l [--DOT--] com > + * + * Copyright 2006-2007 + * + * This file is part of wiiuse. + * + * 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 3 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * $Header$ + * + */ + +/** + * @file + * @brief API source file + * + * The file must be linked to any third party + * program that is utilizing wiiuse as an + * external library. + */ + +#define WIIUSE_API_SRC + +#include +#include "wiiuse.h" + +#ifdef WIN32 + #define WIN32_LEAN_AND_MEAN + #include +#else + #include +#endif + +typedef int (*entry_func_t)(struct wiiuse_api_t**); + +struct wiiuse_api_t* wiiuse_api = NULL; +void* wiiuse_mod = NULL; + +/** + * @brief Load the wiiuse library and initialize the function pointers. + * + * @param wiiuse_file The relative or absolute path to the wiiuse library file. + * + * @return The version of the wiiuse library loaded. + * + * @see wiiuse_shutdown() + * + * If the version of wiiuse being used has a different API + * version as expected, this function will fail and return 0. + */ +const char* wiiuse_startup(char* wiiuse_file) { + entry_func_t entry_func = NULL; + + if (wiiuse_api) + /* already loaded */ + return wiiuse_api->version; + + if (!wiiuse_file) + return NULL; + + /* load the module */ + wiiuse_mod = dlopen(wiiuse_file, RTLD_NOW); + + if (!wiiuse_mod) + /* can not load module */ + return NULL; + + /* get the entry point */ + entry_func = (entry_func_t)dlsym(wiiuse_mod, "wiiuse_main"); + + if (!entry_func) { + wiiuse_shutdown(); + return NULL; + } + + /* call the entry function */ + entry_func(&wiiuse_api); + + /* make sure the API versions are the same */ + if (wiiuse_api->api_version != WIIUSE_API_VERSION) { + wiiuse_shutdown(); + return NULL; + } + + /* set all the function pointers */ + wiiuse_init = wiiuse_api->_wiiuse_init; + wiiuse_disconnected = wiiuse_api->_wiiuse_disconnected; + wiiuse_rumble = wiiuse_api->_wiiuse_rumble; + wiiuse_toggle_rumble = wiiuse_api->_wiiuse_toggle_rumble; + wiiuse_set_leds = wiiuse_api->_wiiuse_set_leds; + wiiuse_motion_sensing = wiiuse_api->_wiiuse_motion_sensing; + wiiuse_read_data = wiiuse_api->_wiiuse_read_data; + wiiuse_write_data = wiiuse_api->_wiiuse_write_data; + wiiuse_status = wiiuse_api->_wiiuse_status; + wiiuse_get_by_id = wiiuse_api->_wiiuse_get_by_id; + wiiuse_set_flags = wiiuse_api->_wiiuse_set_flags; + wiiuse_set_smooth_alpha = wiiuse_api->_wiiuse_set_smooth_alpha; + wiiuse_set_ir = wiiuse_api->_wiiuse_set_ir; + wiiuse_set_ir_vres = wiiuse_api->_wiiuse_set_ir_vres; + wiiuse_set_ir_position = wiiuse_api->_wiiuse_set_ir_position; + wiiuse_set_aspect_ratio = wiiuse_api->_wiiuse_set_aspect_ratio; + wiiuse_set_bluetooth_stack = wiiuse_api->_wiiuse_set_bluetooth_stack; + wiiuse_set_orient_threshold = wiiuse_api->_wiiuse_set_orient_threshold; + wiiuse_find = wiiuse_api->_wiiuse_find; + wiiuse_connect = wiiuse_api->_wiiuse_connect; + wiiuse_disconnect = wiiuse_api->_wiiuse_disconnect; + wiiuse_poll = wiiuse_api->_wiiuse_poll; + + printf("wiiuse v%s loaded ( http://wiiuse.net http://wiiuse.sf.net/ ).\n", wiiuse_api->version); + + return wiiuse_api->version; +} + + +/** + * @brief Unload the library. + * + * @see wiiuse_startup() + */ +void wiiuse_shutdown() { + if (!wiiuse_mod) + return; + + /* unload the module */ + dlclose(wiiuse_mod); + + wiiuse_api = NULL; + + wiiuse_init = NULL; + wiiuse_disconnected = NULL; + wiiuse_rumble = NULL; + wiiuse_toggle_rumble = NULL; + wiiuse_set_leds = NULL; + wiiuse_motion_sensing = NULL; + wiiuse_read_data = NULL; + wiiuse_write_data = NULL; + wiiuse_status = NULL; + wiiuse_get_by_id = NULL; + wiiuse_set_flags = NULL; + wiiuse_set_smooth_alpha = NULL; + wiiuse_set_ir = NULL; + wiiuse_set_ir_vres = NULL; + wiiuse_set_ir_position = NULL; + wiiuse_set_aspect_ratio = NULL; + wiiuse_set_bluetooth_stack = NULL; + wiiuse_set_orient_threshold = NULL; + wiiuse_find = NULL; + wiiuse_connect = NULL; + wiiuse_disconnect = NULL; + wiiuse_poll = NULL; + +} diff --git a/WiiUseJC/wiiuse.h b/WiiUseJC/wiiuse.h new file mode 100644 index 0000000..9a466d9 --- /dev/null +++ b/WiiUseJC/wiiuse.h @@ -0,0 +1,687 @@ +/* + * wiiuse + * + * Written By: + * Michael Laforest < para > + * Email: < thepara (--AT--) g m a i l [--DOT--] com > + * + * Copyright 2006-2007 + * + * This file is part of wiiuse. + * + * 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 3 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * $Header$ + * + */ + +/** + * @file + * + * @brief API header file. + * + * If this file is included from inside the wiiuse source + * and not from a third party program, then wiimote_internal.h + * is also included which extends this file. + */ + +#ifndef WIIUSE_H_INCLUDED +#define WIIUSE_H_INCLUDED + +/* ignore this, this is used internally for wiiuse */ +#ifdef __WIIUSE__ + #ifndef WIIUSE_INTERNAL_H_INCLUDED + #error wiiuse.h included directly. Must include wiiuse_internal.h instead. + #endif + #define WCONST +#else + #define WCONST const +#endif + +#if defined(WIN32) || defined(__WIN32__) + /* windows */ + #include + #include + #include + #include + + #define dlopen(file, x) (void*)LoadLibrary(file) + #define dlsym(dll, func) (void*)GetProcAddress((HMODULE)(dll), (func)) + #define dlclose(dll) FreeLibrary((HMODULE)(dll)) + #define Dl_info MEMORY_BASIC_INFORMATION + #define dladdr(func, inf) VirtualQuery(func, inf, sizeof(*inf)) +#else + /* nix */ + #include +#endif + +/* wiiuse version and API version */ +#define WIIUSE_VERSION "0.9" +#define WIIUSE_API_VERSION 8 + +/* led bit masks */ +#define WIIMOTE_LED_NONE 0x00 +#define WIIMOTE_LED_1 0x10 +#define WIIMOTE_LED_2 0x20 +#define WIIMOTE_LED_3 0x40 +#define WIIMOTE_LED_4 0x80 + +/* button codes */ +#define WIIMOTE_BUTTON_TWO 0x0001 +#define WIIMOTE_BUTTON_ONE 0x0002 +#define WIIMOTE_BUTTON_B 0x0004 +#define WIIMOTE_BUTTON_A 0x0008 +#define WIIMOTE_BUTTON_MINUS 0x0010 +#define WIIMOTE_BUTTON_ZACCEL_BIT6 0x0020 +#define WIIMOTE_BUTTON_ZACCEL_BIT7 0x0040 +#define WIIMOTE_BUTTON_HOME 0x0080 +#define WIIMOTE_BUTTON_LEFT 0x0100 +#define WIIMOTE_BUTTON_RIGHT 0x0200 +#define WIIMOTE_BUTTON_DOWN 0x0400 +#define WIIMOTE_BUTTON_UP 0x0800 +#define WIIMOTE_BUTTON_PLUS 0x1000 +#define WIIMOTE_BUTTON_ZACCEL_BIT4 0x2000 +#define WIIMOTE_BUTTON_ZACCEL_BIT5 0x4000 +#define WIIMOTE_BUTTON_UNKNOWN 0x8000 +#define WIIMOTE_BUTTON_ALL 0x1F9F + +/* nunchul button codes */ +#define NUNCHUK_BUTTON_Z 0x01 +#define NUNCHUK_BUTTON_C 0x02 + +/* classic controller button codes */ +#define CLASSIC_CTRL_BUTTON_UP 0x0001 +#define CLASSIC_CTRL_BUTTON_LEFT 0x0002 +#define CLASSIC_CTRL_BUTTON_ZR 0x0004 +#define CLASSIC_CTRL_BUTTON_X 0x0008 +#define CLASSIC_CTRL_BUTTON_A 0x0010 +#define CLASSIC_CTRL_BUTTON_Y 0x0020 +#define CLASSIC_CTRL_BUTTON_B 0x0040 +#define CLASSIC_CTRL_BUTTON_ZL 0x0080 +#define CLASSIC_CTRL_BUTTON_FULL_R 0x0200 +#define CLASSIC_CTRL_BUTTON_PLUS 0x0400 +#define CLASSIC_CTRL_BUTTON_HOME 0x0800 +#define CLASSIC_CTRL_BUTTON_MINUS 0x1000 +#define CLASSIC_CTRL_BUTTON_FULL_L 0x2000 +#define CLASSIC_CTRL_BUTTON_DOWN 0x4000 +#define CLASSIC_CTRL_BUTTON_RIGHT 0x8000 + +/* wiimote option flags */ +#define WIIUSE_SMOOTHING 0x01 +#define WIIUSE_CONTINUOUS 0x02 +#define WIIUSE_ORIENT_THRESH 0x04 +#define WIIUSE_INIT_FLAGS (WIIUSE_SMOOTHING | WIIUSE_ORIENT_THRESH) + +/* IR correction types */ +typedef enum ir_position_t { + WIIUSE_IR_ABOVE, + WIIUSE_IR_BELOW +} ir_position_t; + +/** + * @brief Check if a button is pressed. + * @param dev Pointer to a wiimote_t or expansion structure. + * @param button The button you are interested in. + * @return 1 if the button is pressed, 0 if not. + */ +#define IS_PRESSED(dev, button) ((dev->btns & button) == button) + +/** + * @brief Check if a button is being held. + * @param dev Pointer to a wiimote_t or expansion structure. + * @param button The button you are interested in. + * @return 1 if the button is held, 0 if not. + */ +#define IS_HELD(dev, button) ((dev->btns_held & button) == button) + +/** + * @brief Check if a button is released on this event. \n\n + * This does not mean the button is not pressed, it means \n + * this button was just now released. + * @param dev Pointer to a wiimote_t or expansion structure. + * @param button The button you are interested in. + * @return 1 if the button is released, 0 if not. + * + */ +#define IS_RELEASED(dev, button) ((dev->btns_released & button) == button) + +/** + * @brief Check if a button has just been pressed this event. + * @param dev Pointer to a wiimote_t or expansion structure. + * @param button The button you are interested in. + * @return 1 if the button is pressed, 0 if not. + */ +#define IS_JUST_PRESSED(dev, button) (IS_PRESSED(dev, button) && !IS_HELD(dev, button)) + +#define WIIUSE_USING_ACC(wm) ((wm->state & 0x10) == 0x10) +#define WIIUSE_USING_EXP(wm) ((wm->state & 0x20) == 0x20) +#define WIIUSE_USING_IR(wm) ((wm->state & 0x40) == 0x40) + +/* + * Largest known payload is 21 bytes. + * Add 2 for the prefix and round up to a power of 2. + */ +#define MAX_PAYLOAD 32 + +typedef unsigned char byte; +typedef char sbyte; + +struct wiimote_t; +struct vec3b_t; +struct orient_t; +struct gforce_t; + +/** + * @brief Event callback. + * + * @param wm Pointer to a wiimote_t structure. + * @param btns What buttons are currently pressed. They are OR'ed together. + * @param accel Acceleration of the device along each axis. + * This is the raw data reported by the wiimote. + * @param orient Orientation (roll, pitch, yaw) of the device. + * @param gforce Pull of gravity on each axis of the device (measured in gravity units). + * + * @see wiiuse_init() + * + * A registered function of this type is called automatically by the wiiuse + * library when an event occurs on the specified wiimote. + */ +typedef void (*wiiuse_event_cb)(struct wiimote_t* wm); + +/** + * @brief Callback that handles a read event. + * + * @param wm Pointer to a wiimote_t structure. + * @param data Pointer to the filled data block. + * @param len Length in bytes of the data block. + * + * @see wiiuse_init() + * + * A registered function of this type is called automatically by the wiiuse + * library when the wiimote has returned the full data requested by a previous + * call to wiiuse_read_data(). + */ +typedef void (*wiiuse_read_cb)(struct wiimote_t* wm, byte* data, unsigned short len); + +/** + * @brief Callback that handles a controller status event. + * + * @param wm Pointer to a wiimote_t structure. + * @param attachment Is there an attachment? (1 for yes, 0 for no) + * @param speaker Is the speaker enabled? (1 for yes, 0 for no) + * @param ir Is the IR support enabled? (1 for yes, 0 for no) + * @param led What LEDs are lit. + * @param battery_level Battery level, between 0.0 (0%) and 1.0 (100%). + * + * @see wiiuse_init() + * + * A registered function of this type is called automatically by the wiiuse + * library when either the controller status changed or the controller + * status was requested explicitly by wiiuse_status(). + */ +typedef void (*wiiuse_ctrl_status_cb)(struct wiimote_t* wm, int attachment, int speaker, int ir, int led[4], float battery_level); + +/** + * @brief Callback that handles a disconnection event. + * + * @param wm Pointer to a wiimote_t structure. + * + * @see wiiuse_init() + * + * A registered function of this type is called automatically by the wiiuse + * library when a disconnection occurs. This can happen if the POWER button + * is pressed or if the connection is interrupted. + */ +typedef void (*wiiuse_dis_cb)(struct wiimote_t* wm); + + +/** + * @struct read_req_t + * @brief Data read request structure. + */ +struct read_req_t { + wiiuse_read_cb cb; /**< read data callback */ + byte* buf; /**< buffer where read data is written */ + unsigned int addr; /**< the offset that the read started at */ + unsigned short size; /**< the length of the data read */ + unsigned short wait; /**< num bytes still needed to finish read */ + + struct read_req_t* next; /**< next read request in the queue */ +}; + + +/** + * @struct vec2b_t + * @brief Unsigned x,y byte vector. + */ +typedef struct vec2b_t { + byte x, y; +} vec2b_t; + + +/** + * @struct vec3b_t + * @brief Unsigned x,y,z byte vector. + */ +typedef struct vec3b_t { + byte x, y, z; +} vec3b_t; + + +/** + * @struct vec3f_t + * @brief Signed x,y,z float struct. + */ +typedef struct vec3f_t { + float x, y, z; +} vec3f_t; + + +/** + * @struct orient_t + * @brief Orientation struct. + * + * Yaw, pitch, and roll range from -180 to 180 degrees. + */ +typedef struct orient_t { + float roll; + float pitch; + float yaw; +} orient_t; + + +/** + * @struct gforce_t + * @brief Gravity force struct. + */ +typedef struct gforce_t { + float x, y, z; +} gforce_t; + + +/** + * @struct accel_t + * @brief Accelerometer struct. For any device with an accelerometer. + */ +typedef struct accel_t { + struct vec3b_t cal_zero; /**< zero calibration */ + struct vec3b_t cal_g; /**< gravity calibration */ + + float st_roll; /**< last smoothed roll value */ + float st_pitch; /**< last smoothed roll pitch */ + float st_alpha; /**< alpha value for smoothing [0-1] */ +} accel_t; + + +/** + * @struct ir_dot_t + * @brief A single IR source. + */ +typedef struct ir_dot_t { + byte visible; /**< if the IR source is visible */ + + unsigned int x; /**< interpolated X coordinate */ + unsigned int y; /**< interpolated Y coordinate */ + + short rx; /**< raw X coordinate (0-1023) */ + short ry; /**< raw Y coordinate (0-767) */ + + byte order; /**< increasing order by x-axis value */ + + byte size; /**< size of the IR dot (0-15) */ +} ir_dot_t; + + +/** + * @enum aspect_t + * @brief Screen aspect ratio. + */ +typedef enum aspect_t { + WIIUSE_ASPECT_4_3, + WIIUSE_ASPECT_16_9 +} aspect_t; + + +/** + * @struct ir_t + * @brief IR struct. Hold all data related to the IR tracking. + */ +typedef struct ir_t { + struct ir_dot_t dot[4]; /**< IR dots */ + byte num_dots; /**< number of dots at this time */ + + enum aspect_t aspect; /**< aspect ratio of the screen */ + + enum ir_position_t pos; /**< IR sensor bar position */ + + unsigned int vres[2]; /**< IR virtual screen resolution */ + int offset[2]; /**< IR XY correction offset */ + int state; /**< keeps track of the IR state */ + + int ax; /**< absolute X coordinate */ + int ay; /**< absolute Y coordinate */ + + int x; /**< calculated X coordinate */ + int y; /**< calculated Y coordinate */ + + float distance; /**< pixel distance between first 2 dots*/ + float z; /**< calculated distance */ +} ir_t; + + +/** + * @struct joystick_t + * @brief Joystick calibration structure. + * + * The angle \a ang is relative to the positive y-axis into quadrant I + * and ranges from 0 to 360 degrees. So if the joystick is held straight + * upwards then angle is 0 degrees. If it is held to the right it is 90, + * down is 180, and left is 270. + * + * The magnitude \a mag is the distance from the center to where the + * joystick is being held. The magnitude ranges from 0 to 1. + * If the joystick is only slightly tilted from the center the magnitude + * will be low, but if it is closer to the outter edge the value will + * be higher. + */ +typedef struct joystick_t { + struct vec2b_t max; /**< maximum joystick values */ + struct vec2b_t min; /**< minimum joystick values */ + struct vec2b_t center; /**< center joystick values */ + + float ang; /**< angle the joystick is being held */ + float mag; /**< magnitude of the joystick (range 0-1) */ +} joystick_t; + + +/** + * @struct nunchuk_t + * @brief Nunchuk expansion device. + */ +typedef struct nunchuk_t { + struct accel_t accel_calib; /**< nunchuk accelerometer calibration */ + struct joystick_t js; /**< joystick calibration */ + + int* flags; /**< options flag (points to wiimote_t.flags) */ + + byte btns; /**< what buttons have just been pressed */ + byte btns_held; /**< what buttons are being held down */ + byte btns_released; /**< what buttons were just released this */ + + struct vec3b_t accel; /**< current raw acceleration data */ + struct orient_t orient; /**< current orientation on each axis */ + struct gforce_t gforce; /**< current gravity forces on each axis */ +} nunchuk_t; + + +/** + * @struct classic_ctrl_t + * @brief Classic controller expansion device. + */ +typedef struct classic_ctrl_t { + short btns; /**< what buttons have just been pressed */ + short btns_held; /**< what buttons are being held down */ + short btns_released; /**< what buttons were just released this */ + + float r_shoulder; /**< right shoulder button (range 0-1) */ + float l_shoulder; /**< left shoulder button (range 0-1) */ + + struct joystick_t ljs; /**< left joystick calibration */ + struct joystick_t rjs; /**< right joystick calibration */ +} classic_ctrl_t; + + +/** + * @struct expansion_t + * @brief Generic expansion device plugged into wiimote. + */ +typedef struct expansion_t { + enum { + EXP_NONE, + EXP_NUNCHUK, + EXP_CLASSIC + } type; /**< type of expansion attached */ + + union { + struct nunchuk_t nunchuk; + struct classic_ctrl_t classic; + }; +} expansion_t; + + +/** + * @enum win32_bt_stack_t + * @brief Available bluetooth stacks for Windows. + */ +typedef enum win_bt_stack_t { + WIIUSE_STACK_UNKNOWN, + WIIUSE_STACK_MS, + WIIUSE_STACK_BLUESOLEIL +} win_bt_stack_t; + + +typedef struct wiimote_state_t { + /* expansion_t */ + float exp_ljs_ang; + float exp_rjs_ang; + float exp_ljs_mag; + float exp_rjs_mag; + byte exp_btns; + struct orient_t exp_orient; + float exp_r_shoulder; + float exp_l_shoulder; + + /* ir_t */ + int ir_ax; + int ir_ay; + float ir_distance; + + struct orient_t orient; + unsigned short btns; +} wiimote_state_t; + + +/** + * @struct wiimote_t + * @brief Wiimote structure. + */ +typedef struct wiimote_t { + WCONST int unid; /**< user specified id */ + + #ifndef WIN32 + WCONST bdaddr_t bdaddr; /**< bt address */ + WCONST char bdaddr_str[18]; /**< readable bt address */ + WCONST int out_sock; /**< output socket */ + WCONST int in_sock; /**< input socket */ + #else + WCONST HANDLE dev_handle; /**< HID handle */ + WCONST OVERLAPPED hid_overlap; /**< overlap handle */ + WCONST enum win_bt_stack_t stack; /**< type of bluetooth stack to use */ + #endif + + WCONST int state; /**< various state flags */ + WCONST int leds; /**< currently lit leds */ + + WCONST int flags; /**< options flag */ + + WCONST wiiuse_event_cb event_cb; /**< event callback */ + WCONST wiiuse_dis_cb dis_cb; /**< disconnect callback */ + WCONST wiiuse_ctrl_status_cb stat_cb; /**< controller status callback */ + + WCONST byte handshake_state; /**< the state of the connection handshake */ + + WCONST struct read_req_t* read_req; /**< list of data read requests */ + WCONST struct accel_t accel_calib; /**< wiimote accelerometer calibration */ + WCONST struct expansion_t exp; /**< wiimote expansion device */ + + WCONST struct vec3b_t accel; /**< current raw acceleration data */ + WCONST struct orient_t orient; /**< current orientation on each axis */ + WCONST struct gforce_t gforce; /**< current gravity forces on each axis */ + + WCONST struct ir_t ir; /**< IR data */ + + WCONST unsigned short btns; /**< what buttons have just been pressed */ + WCONST unsigned short btns_held; /**< what buttons are being held down */ + WCONST unsigned short btns_released; /**< what buttons were just released this */ + + WCONST float orient_threshold; /**< threshold for orient to generate an event */ + + WCONST struct wiimote_state_t lstate; /**< last saved state */ + + WCONST byte event[MAX_PAYLOAD]; /**< event buffer */ +} wiimote; + + +/***************************************** + * + * Include API specific stuff + * + *****************************************/ + +#ifdef __cplusplus +extern "C" { +#endif + +/* wiiuse.c */ +typedef struct wiimote_t** (*_wiiuse_init_fptr)(int wiimotes, int* unids, wiiuse_event_cb event_cb, wiiuse_ctrl_status_cb stat_cb, wiiuse_dis_cb dis_cb); +typedef void (*_wiiuse_disconnected_fptr)(struct wiimote_t* wm); +typedef void (*_wiiuse_rumble_fptr)(struct wiimote_t* wm, int status); +typedef void (*_wiiuse_toggle_rumble_fptr)(struct wiimote_t* wm); +typedef void (*_wiiuse_set_leds_fptr)(struct wiimote_t* wm, int leds); +typedef void (*_wiiuse_motion_sensing_fptr)(struct wiimote_t* wm, int status); +typedef int (*_wiiuse_read_data_fptr)(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buffer, unsigned int offset, unsigned short len); +typedef int (*_wiiuse_write_data_fptr)(struct wiimote_t* wm, unsigned int addr, byte* data, byte len); +typedef void (*_wiiuse_status_fptr)(struct wiimote_t* wm); +typedef struct wiimote_t* (*_wiiuse_get_by_id_fptr)(struct wiimote_t** wm, int wiimotes, int unid); +typedef int (*_wiiuse_set_flags_fptr)(struct wiimote_t* wm, int enable, int disable); +typedef float (*_wiiuse_set_smooth_alpha_fptr)(struct wiimote_t* wm, float alpha); +typedef void (*_wiiuse_set_ir_fptr)(struct wiimote_t* wm, int status); +typedef void (*_wiiuse_set_ir_vres_fptr)(struct wiimote_t* wm, unsigned int x, unsigned int y); +typedef void (*_wiiuse_set_ir_position_fptr)(struct wiimote_t* wm, enum ir_position_t pos); +typedef void (*_wiiuse_set_aspect_ratio_fptr)(struct wiimote_t* wm, enum aspect_t aspect); +typedef void (*_wiiuse_set_bluetooth_stack_fptr)(struct wiimote_t** wm, int wiimotes, enum win_bt_stack_t type); +typedef void (*_wiiuse_set_orient_threshold_fptr)(struct wiimote_t* wm, float threshold); + +/* connect.c */ +typedef int (*_wiiuse_find_fptr)(struct wiimote_t** wm, int max_wiimotes, int timeout); +typedef int (*_wiiuse_connect_fptr)(struct wiimote_t** wm, int wiimotes); +typedef void (*_wiiuse_disconnect_fptr)(struct wiimote_t* wm); + +/* events.c */ +typedef void (*_wiiuse_poll_fptr)(struct wiimote_t** wm, int wiimotes); + +#ifdef __cplusplus +} +#endif + +/** + * @struct wiiuse_api_t + * @brief API structure that is filled by the library + * when the entry point is invoked. + * + * Unless you are modifying wiiuse, you probably + * will not be interested in any members + * that begin with an underscore (_). + */ +struct wiiuse_api_t { + const char* version; /**< wiiuse version */ + int api_version; /**< wiiuse API version */ + + _wiiuse_init_fptr _wiiuse_init; + _wiiuse_disconnected_fptr _wiiuse_disconnected; + _wiiuse_rumble_fptr _wiiuse_rumble; + _wiiuse_toggle_rumble_fptr _wiiuse_toggle_rumble; + _wiiuse_set_leds_fptr _wiiuse_set_leds; + _wiiuse_motion_sensing_fptr _wiiuse_motion_sensing; + _wiiuse_read_data_fptr _wiiuse_read_data; + _wiiuse_write_data_fptr _wiiuse_write_data; + _wiiuse_status_fptr _wiiuse_status; + _wiiuse_get_by_id_fptr _wiiuse_get_by_id; + _wiiuse_set_flags_fptr _wiiuse_set_flags; + _wiiuse_set_smooth_alpha_fptr _wiiuse_set_smooth_alpha; + _wiiuse_set_ir_fptr _wiiuse_set_ir; + _wiiuse_set_ir_vres_fptr _wiiuse_set_ir_vres; + _wiiuse_set_ir_position_fptr _wiiuse_set_ir_position; + _wiiuse_set_aspect_ratio_fptr _wiiuse_set_aspect_ratio; + _wiiuse_set_bluetooth_stack_fptr _wiiuse_set_bluetooth_stack; + _wiiuse_set_orient_threshold_fptr _wiiuse_set_orient_threshold; + + _wiiuse_find_fptr _wiiuse_find; + _wiiuse_connect_fptr _wiiuse_connect; + _wiiuse_disconnect_fptr _wiiuse_disconnect; + + _wiiuse_poll_fptr _wiiuse_poll; +}; + +#ifndef __WIIUSE__ + +/* + * Operating system dependent macros. + */ +#ifdef __WIN32__ + #define dlopen(file, x) (void*)LoadLibrary(file) + #define dlsym(dll, func) (void*)GetProcAddress((HMODULE)(dll), (func)) + #define dlclose(dll) FreeLibrary((HMODULE)(dll)) + + char* _dlerror(); + #define dlerror() _dlerror() +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* api/wiiuse.c */ +const char* wiiuse_startup(char* wiiuse_file); +void wiiuse_shutdown(); + +#ifdef WIIUSE_API_SRC + #define WEXTERN +#else + #define WEXTERN extern +#endif + +WEXTERN _wiiuse_init_fptr wiiuse_init; +WEXTERN _wiiuse_disconnected_fptr wiiuse_disconnected; +WEXTERN _wiiuse_rumble_fptr wiiuse_rumble; +WEXTERN _wiiuse_toggle_rumble_fptr wiiuse_toggle_rumble; +WEXTERN _wiiuse_set_leds_fptr wiiuse_set_leds; +WEXTERN _wiiuse_motion_sensing_fptr wiiuse_motion_sensing; +WEXTERN _wiiuse_read_data_fptr wiiuse_read_data; +WEXTERN _wiiuse_write_data_fptr wiiuse_write_data; +WEXTERN _wiiuse_status_fptr wiiuse_status; +WEXTERN _wiiuse_get_by_id_fptr wiiuse_get_by_id; +WEXTERN _wiiuse_set_flags_fptr wiiuse_set_flags; +WEXTERN _wiiuse_set_smooth_alpha_fptr wiiuse_set_smooth_alpha; +WEXTERN _wiiuse_set_ir_fptr wiiuse_set_ir; +WEXTERN _wiiuse_set_ir_vres_fptr wiiuse_set_ir_vres; +WEXTERN _wiiuse_set_ir_position_fptr wiiuse_set_ir_position; +WEXTERN _wiiuse_set_aspect_ratio_fptr wiiuse_set_aspect_ratio; +WEXTERN _wiiuse_set_bluetooth_stack_fptr wiiuse_set_bluetooth_stack; +WEXTERN _wiiuse_set_orient_threshold_fptr wiiuse_set_orient_threshold; + +WEXTERN _wiiuse_find_fptr wiiuse_find; +WEXTERN _wiiuse_connect_fptr wiiuse_connect; +WEXTERN _wiiuse_disconnect_fptr wiiuse_disconnect; + +WEXTERN _wiiuse_poll_fptr wiiuse_poll; + +#ifdef __cplusplus +} +#endif + +#endif /* __WIIUSE__ */ + +#endif /* WIIUSE_H_INCLUDED */ + diff --git a/WiiUseJC/wiiusej_WiiUseApi.c b/WiiUseJC/wiiusej_WiiUseApi.c new file mode 100644 index 0000000..063d347 --- /dev/null +++ b/WiiUseJC/wiiusej_WiiUseApi.c @@ -0,0 +1,659 @@ +#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_ID_1 1 +#define WIIMOTE_ID_2 2 +#define WIIMOTE_STATE_RUMBLE 0x08 +#define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s)) +#define WIIMOTE_IS_FLAG_SET(wm, s) ((wm->flags & (s)) == (s)) + +/****************** CALLBACKS DECLARATIONS *************************/ + +static void handle_event(struct wiimote_t* wm); +static void handle_ctrl_status(struct wiimote_t* wm, int attachment, + int speaker, int ir, int led[4], float battery_level); +static void handle_disconnect(wiimote* wm); +static void copy_common_status(struct wiimote_t* wm); + +/********************* 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 int ids[] = { WIIMOTE_ID_1, WIIMOTE_ID_2 }; +static wiimote** wiimotes; +static JNIEnv *globalEnv; +static jobject globalObj; +static jobject globalWim; + +/****************** GENERAL FUNCTIONS DEFINITIONS *************************/ + +/* + * Load the wiiuse library + * + * This needs to be done before anything else can happen + * wiiuse_startup() will return the version of the library loaded. + * + * @return 0 if there is an error, 1 if everything is ok. + */ +JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_loadLibrary +(JNIEnv *env, jobject obj) { + const char* version; + version = wiiuse_startup(WIIUSE_PATH); + //printf("Wiiuse Version = %s\n", version); + if (!version) { + return 0; + } + + /* no problems loading library */ + return 1; +} + +/** + * Try to connect to 2 wiimotes. + * Make them rumble to show they are connected. + * + * @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) { + + /* variables declarations */ + int found, connected; + + /* + * Initialize an array of wiimote objects. + * + * The first parameter is the number of wiimotes + * I want to create. I only have two wiimotes + * so I'm limiting the test to just 2. + * + * Then I get it the array of ids and a couple + * callback functions to invoke when something + * happens on one of the wiimotes. + * + * handle_event gets called when a generic event occurs (button press, motion sensing, etc) + * handle_ctrl_status gets called when a response to a status request arrives (battery power, etc) + * handle_disconnect gets called when the wiimote disconnect (holding power button) + */ + wiimotes = wiiuse_init(2, ids, handle_event, handle_ctrl_status, + handle_disconnect); + + /* + * 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, 2, 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, 2); + 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). + */ + wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1); + wiiuse_set_leds(wiimotes[1], WIIMOTE_LED_2); + wiiuse_rumble(wiimotes[0], 1); + wiiuse_rumble(wiimotes[1], 1); + +#ifndef WIN32 + usleep(200000); +#else + Sleep(200); +#endif + + wiiuse_rumble(wiimotes[0], 0); + wiiuse_rumble(wiimotes[1], 0); + + //no pb connecting leave + return connected; +} + +/** + * Close connection to the wiimote with the given id. + * + * @param id the id of the wiimote to disconnect.Must be 1 or 2. + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_closeConnection +(JNIEnv *env, jobject obj, jint id) { + wiiuse_disconnect(wiimotes[id-1]); +} + +/** + * Shutdown api. + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_shutdownApi +(JNIEnv *env, jobject obj) { + wiiuse_shutdown(); +} + +/** + * Activate rumble for the wiimote with the given id. + * @param id the id of the wiimote.Must be 1 or 2. + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_activateRumble +(JNIEnv *env, jobject obj, jint id) { + wiiuse_rumble(wiimotes[id-1], 1); +} + +/** + * Deactivate rumble for the wiimote with the given id. + * @param id the id of the wiimote.Must be 1 or 2. + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateRumble +(JNIEnv *env, jobject obj, jint id) { + wiiuse_rumble(wiimotes[id-1], 0); +} + +/** + * Activate IR TRacking for the wiimote with the given id. + * @param id the id of the wiimote. + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_activateIRTracking +(JNIEnv *env, jobject obj, jint id) { + wiiuse_set_ir(wiimotes[id-1], 1); +} + +/** + * Deactivate IR TRacking for the wiimote with the given id. + * @param id the id of the wiimote.Must be 1 or 2. + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateIRTracking +(JNIEnv *env, jobject obj, jint id) { + wiiuse_set_ir(wiimotes[id-1], 0); +} + +/** + * Activate Motion Sensing for the wiimote with the given id. + * @param id the id of the wiimote.Must be 1 or 2. + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_activateMotionSensing +(JNIEnv *env, jobject obj, jint id) { + wiiuse_motion_sensing(wiimotes[id-1], 1); +} + +/** + * Deactivate Motion Sensing for the wiimote with the given id. + * @param id the id of the wiimote.Must be 1 or 2. + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateMotionSensing +(JNIEnv *env, jobject obj, jint id) { + wiiuse_motion_sensing(wiimotes[id-1], 0); +} + +/** + * Set wiimote leds status. + * @param id the id of the wiimote concerned + * @param led1 status of led1: True=ON, False=OFF + * @param led2 status of led2: True=ON, False=OFF + * @param led3 status of led3: True=ON, False=OFF + * @param led4 status of led4: True=ON, False=OFF + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_setLeds +(JNIEnv *env, jobject obj, jint id, jboolean led1, jboolean led2, jboolean led3, jboolean led4) { + int leds = 0; + + if (led1) leds |= WIIMOTE_LED_1; + if (led2) leds |= WIIMOTE_LED_2; + if (led3) leds |= WIIMOTE_LED_3; + if (led4) leds |= WIIMOTE_LED_4; + + wiiuse_set_leds(wiimotes[id-1], leds); +} + +/** + * Set how many degrees an angle must change to generate an event. + * @param id id of the wiimote concerned + * @param angle minimum angle detected by an event + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_setOrientThreshold +(JNIEnv *env, jobject obj, jint id, jfloat thresh) { + wiiuse_set_orient_threshold(wiimotes[id-1], thresh); +} + +/** + * Make the the accelerometers give smoother results. + * This is set by default. + * @param id the id of the wiimote concerned + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_activateSmoothing +(JNIEnv *env, jobject obj, jint id) { + wiiuse_set_flags(wiimotes[id-1], WIIUSE_SMOOTHING, 0); +} + +/** + * Make the the accelerometers give raw results. + * @param id the id of the wiimote concerned + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateSmoothing +(JNIEnv *env, jobject obj, jint id) { + wiiuse_set_flags(wiimotes[id-1], 0, WIIUSE_SMOOTHING); +} + +/** + * Make the wiimote generate an event each time we poll. + * Not set by default. + * @param id the id of the wiimote concerned + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_activateContinuous +(JNIEnv *env, jobject obj, jint id) { + wiiuse_set_flags(wiimotes[id-1], WIIUSE_CONTINUOUS, 0); +} + +/** + * Make the wiimote generate an event only when there is one. + * (default behavior) + * @param id the id of the wiimote concerned + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateContinuous +(JNIEnv *env, jobject obj, jint id) { + wiiuse_set_flags(wiimotes[id-1], 0, WIIUSE_CONTINUOUS); +} + +/** + * Get status from the wiimotes and send it through call backs. + * + * @param id the id of the wiimote. Must be 1 or 2. + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_getStatus +(JNIEnv *env, jobject obj, jint id) { + wiiuse_status(wiimotes[id-1]); +} + +/** + * Get status and values from the wiimotes and send it through callbacks. + * @param wim the wiimote object to fill with the datas. + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_specialPoll +(JNIEnv *env, jobject obj, jobject wim) { + + globalEnv = env; + globalObj = obj; + globalWim = wim; + + wiiuse_poll(wiimotes, 2); +} + +/****************** CALLBACKS DEFINITIONS *************************/ + +/** + * @brief Callback that handles an event. + * + * @param wm Pointer to a wiimote_t structure. + * + * This function is called automatically by the wiiuse library when an + * event occurs on the specified wiimote. + */ +static void handle_event(struct wiimote_t* wm) { + + /* Variables Declarations */ + jclass cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); + jmethodID mid; + + /* fill java class */ + copy_common_status(wm); + + /* Take care of buttons pressed */ + if (wm->btns) { + /* if a button is just pressed, report it */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, + "setButtonsJustPressed", "(S)V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wm->btns); + } + if (wm->btns_released) { + /* if a button is just released, report it */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, + "setButtonsJustReleased", "(S)V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, + wm->btns_released); + } + if (wm->btns_held) { + /* if a button is held, report it */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setButtonsHeld", + "(S)V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, + wm->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(wm)) { + int i = 0; + /* go through each of the 4 possible IR sources */ + for (; i < 4; ++i) { + /* check if the source is visible */ + if (wm->ir.dot[i].visible) { + cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "addIRpoint", + "(II)V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, + wm->ir.dot[i].x, wm->ir.dot[i].y); + } + } + //printf("IR cursor: (%u, %u)\n", wm->ir.x, wm->ir.y); + //printf("IR z distance: %f\n", wm->ir.z); + } + + /* Motion Sensing */ + if (WIIUSE_USING_ACC(wm)) { + /* set orientation */ + cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setOrientation", + "(FFF)V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, + wm->orient.roll, wm->orient.pitch, wm->orient.yaw); + /* set gravity force*/ + cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setGforce", "(FFF)V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, + wm->gforce.x, wm->gforce.y, wm->gforce.z); + } +} + +/** + * @brief Callback that handles a controller status event. + * + * @param wm Pointer to a wiimote_t structure. + * @param attachment Is there an attachment? (1 for yes, 0 for no) + * @param speaker Is the speaker enabled? (1 for yes, 0 for no) + * @param ir Is the IR support enabled? (1 for yes, 0 for no) + * @param led What LEDs are lit. + * @param battery_level Battery level, between 0.0 (0%) and 1.0 (100%). + * + * This occurs when either the controller status changed + * or the controller status was requested explicitly by + * wiiuse_status(). + * + * One reason the status can change is if the nunchuk was + * inserted or removed from the expansion port. + */ +static void handle_ctrl_status(struct wiimote_t* wm, int attachment, + int speaker, int ir, int led[4], float battery_level) { + + /* Variables Declarations */ + jclass cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); + jmethodID mid; + short leds = 0; + + /* fill java class */ + copy_common_status(wm); + + /* LEDS */ + cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setLeds", "(S)V"); + if (mid == 0) { + return; + } + if (led[0]) + leds += 1; + if (led[1]) + leds += 2; + if (led[2]) + leds += 4; + if (led[3]) + leds += 8; + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, leds); + + /* Battery */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setBatteryLevel", "(F)V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, + battery_level); + + /* Speaker */ + if (speaker) { + /* set Speaker Enabled */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setSpeakerEnabled", + "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } else { + /* set Speaker Disabled */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setSpeakerDisabled", + "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } + + /* Attachment */ + if (attachment) { + /* set there is an attachment */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, + "setThereIsAnAttachment", "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } else { + /* set there is no attachment */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, + "setThereIsNoAttachment", "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } + +} + +/** + * @brief Callback that handles a disconnection event. + * + * @param wm Pointer to a wiimote_t structure. + * + * This can happen if the POWER button is pressed, or + * if the connection is interrupted. + */ +static void handle_disconnect(wiimote* wm) { + //printf("\n\n--- DISCONNECTED [wiimote id %i] ---\n", wm->unid); + + /* Variables Declarations */ + jclass cls; + jmethodID mid; + + /* call java method handling disconnection */ + + /* Set wiimote id */ + cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setWiimoteId", "(I)V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wm->unid); + + /* set the wiimote disconnected */ + cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setDisconnected", "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + +} + + +/** + * Fills status variables. + * This function is used in handle_event and handle_ctrl_status. + */ +static void copy_common_status(struct wiimote_t* wm) { + + /* Variables Declarations */ + jmethodID mid; + jclass cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); + + /* Set wiimote id */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setWiimoteId", "(I)V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wm->unid); + + /* set the wiimote connected */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setConnected", "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + + /* IR state */ + if (WIIUSE_USING_IR(wm)) { + /* set IRActive */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setIrActive", "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } else { + /* set IRInactive */ + mid + = (*globalEnv)->GetMethodID(globalEnv, cls, + "setIrInactive", "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } + + /* Rumble status */ + if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)) { + /* set rumble active */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setRumbleActive", + "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } else { + /* set rumble inactive */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setRumbleInactive", + "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } + + /* Motion sensing status */ + if (WIIUSE_USING_ACC(wm)) { + /* Set motion sensing active*/ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, + "setMotionSensingActive", "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } else {/* motion sensing not activated */ + /* Set motion sensing inactive*/ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, + "setMotionSensingInactive", "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } + + /* orientation threshold value */ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setOrientationThreshold", + "(F)V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, + wm->orient_threshold); + + /* continuous status */ + if (WIIMOTE_IS_FLAG_SET(wm,WIIUSE_CONTINUOUS)) { + /* Set WIIUSE_CONTINUOUS active*/ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setContinuousActive", + "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } else { + /* Set WIIUSE_CONTINUOUS inactive*/ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, + "setContinuousInactive", "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } + + /* smoothing status */ + if (WIIMOTE_IS_FLAG_SET(wm,WIIUSE_SMOOTHING)) { + /* Set WIIUSE_SMOOTHING active*/ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setSmoothingActive", + "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } else { + /* Set WIIUSE_SMOOTHING inactive*/ + mid = (*globalEnv)->GetMethodID(globalEnv, cls, + "setSmoothingInactive", "()V"); + if (mid == 0) { + return; + } + (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid); + } +} diff --git a/WiiUseJC/wiiusej_WiiUseApi.def b/WiiUseJC/wiiusej_WiiUseApi.def new file mode 100644 index 0000000..258bf8b --- /dev/null +++ b/WiiUseJC/wiiusej_WiiUseApi.def @@ -0,0 +1,19 @@ +EXPORTS +Java_wiiusej_WiiUseApi_loadLibrary +Java_wiiusej_WiiUseApi_doConnections +Java_wiiusej_WiiUseApi_closeConnection +Java_wiiusej_WiiUseApi_shutdownApi +Java_wiiusej_WiiUseApi_activateRumble +Java_wiiusej_WiiUseApi_deactivateRumble +Java_wiiusej_WiiUseApi_activateIRTracking +Java_wiiusej_WiiUseApi_deactivateIRTracking +Java_wiiusej_WiiUseApi_activateMotionSensing +Java_wiiusej_WiiUseApi_setOrientThreshold +Java_wiiusej_WiiUseApi_activateSmoothing +Java_wiiusej_WiiUseApi_setLeds +Java_wiiusej_WiiUseApi_deactivateSmoothing +Java_wiiusej_WiiUseApi_activateContinuous +Java_wiiusej_WiiUseApi_deactivateContinuous +Java_wiiusej_WiiUseApi_deactivateMotionSensing +Java_wiiusej_WiiUseApi_getStatus +Java_wiiusej_WiiUseApi_specialPoll diff --git a/WiiUseJC/wiiusej_WiiUseApi.h b/WiiUseJC/wiiusej_WiiUseApi.h new file mode 100644 index 0000000..08efaf1 --- /dev/null +++ b/WiiUseJC/wiiusej_WiiUseApi.h @@ -0,0 +1,157 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class wiiusej_WiiUseApi */ + +#ifndef _Included_wiiusej_WiiUseApi +#define _Included_wiiusej_WiiUseApi +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: wiiusej_WiiUseApi + * Method: loadLibrary + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_loadLibrary + (JNIEnv *, jobject); + +/* + * Class: wiiusej_WiiUseApi + * Method: doConnections + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_doConnections + (JNIEnv *, jobject); + +/* + * Class: wiiusej_WiiUseApi + * Method: closeConnection + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_closeConnection + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: shutdownApi + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_shutdownApi + (JNIEnv *, jobject); + +/* + * Class: wiiusej_WiiUseApi + * Method: activateRumble + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_activateRumble + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: deactivateRumble + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateRumble + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: activateIRTracking + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_activateIRTracking + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: deactivateIRTracking + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateIRTracking + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: activateMotionSensing + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_activateMotionSensing + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: deactivateMotionSensing + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateMotionSensing + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: setLeds + * Signature: (IZZZZ)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_setLeds + (JNIEnv *, jobject, jint, jboolean, jboolean, jboolean, jboolean); + +/* + * Class: wiiusej_WiiUseApi + * Method: setOrientThreshold + * Signature: (IF)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_setOrientThreshold + (JNIEnv *, jobject, jint, jfloat); + +/* + * Class: wiiusej_WiiUseApi + * Method: activateSmoothing + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_activateSmoothing + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: deactivateSmoothing + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateSmoothing + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: activateContinuous + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_activateContinuous + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: deactivateContinuous + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_deactivateContinuous + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: getStatus + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_getStatus + (JNIEnv *, jobject, jint); + +/* + * Class: wiiusej_WiiUseApi + * Method: specialPoll + * Signature: (Lwiiusej/WiiMoteEvent;)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_specialPoll + (JNIEnv *, jobject, jobject); + +#ifdef __cplusplus +} +#endif +#endif