diff --git a/WiiUseJC/.cproject b/WiiUseJC/.cproject index ea50e8b..8c0aa22 100644 --- a/WiiUseJC/.cproject +++ b/WiiUseJC/.cproject @@ -366,6 +366,13 @@ + + diff --git a/WiiUseJC/lib/wiiuse.lib b/WiiUseJC/lib/wiiuse.lib new file mode 100644 index 0000000..13167be Binary files /dev/null and b/WiiUseJC/lib/wiiuse.lib differ diff --git a/WiiUseJC/wiiuse.c b/WiiUseJC/wiiuse.c deleted file mode 100644 index 23db02f..0000000 --- a/WiiUseJC/wiiuse.c +++ /dev/null @@ -1,168 +0,0 @@ -/* - * 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 index 9a466d9..014bf64 100644 --- a/WiiUseJC/wiiuse.h +++ b/WiiUseJC/wiiuse.h @@ -39,37 +39,20 @@ #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 +#ifdef _WIN32 + /* windows */ + #include +#else + /* nix */ + #include +#endif + +#ifdef WIIUSE_INTERNAL_H_INCLUDED #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 @@ -99,6 +82,7 @@ /* nunchul button codes */ #define NUNCHUK_BUTTON_Z 0x01 #define NUNCHUK_BUTTON_C 0x02 +#define NUNCHUK_BUTTON_ALL 0x03 /* classic controller button codes */ #define CLASSIC_CTRL_BUTTON_UP 0x0001 @@ -116,13 +100,35 @@ #define CLASSIC_CTRL_BUTTON_FULL_L 0x2000 #define CLASSIC_CTRL_BUTTON_DOWN 0x4000 #define CLASSIC_CTRL_BUTTON_RIGHT 0x8000 +#define CLASSIC_CTRL_BUTTON_ALL 0xFEFF + +/* guitar hero 3 button codes */ +#define GUITAR_HERO_3_BUTTON_STRUM_UP 0x0001 +#define GUITAR_HERO_3_BUTTON_YELLOW 0x0008 +#define GUITAR_HERO_3_BUTTON_GREEN 0x0010 +#define GUITAR_HERO_3_BUTTON_BLUE 0x0020 +#define GUITAR_HERO_3_BUTTON_RED 0x0040 +#define GUITAR_HERO_3_BUTTON_ORANGE 0x0080 +#define GUITAR_HERO_3_BUTTON_PLUS 0x0400 +#define GUITAR_HERO_3_BUTTON_MINUS 0x1000 +#define GUITAR_HERO_3_BUTTON_STRUM_DOWN 0x4000 +#define GUITAR_HERO_3_BUTTON_ALL 0xFEFF + /* wiimote option flags */ #define WIIUSE_SMOOTHING 0x01 #define WIIUSE_CONTINUOUS 0x02 -#define WIIUSE_ORIENT_THRESH 0x04 +#define WIIUSE_ORIENT_THRESH 0x04 #define WIIUSE_INIT_FLAGS (WIIUSE_SMOOTHING | WIIUSE_ORIENT_THRESH) +#define WIIUSE_ORIENT_PRECISION 100.0f + +/* expansion codes */ +#define EXP_NONE 0 +#define EXP_NUNCHUK 1 +#define EXP_CLASSIC 2 +#define EXP_GUITAR_HERO_3 3 + /* IR correction types */ typedef enum ir_position_t { WIIUSE_IR_ABOVE, @@ -164,17 +170,25 @@ typedef enum ir_position_t { */ #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) +#define WIIUSE_USING_ACC(wm) ((wm->state & 0x020) == 0x020) +#define WIIUSE_USING_EXP(wm) ((wm->state & 0x040) == 0x040) +#define WIIUSE_USING_IR(wm) ((wm->state & 0x080) == 0x080) +#define WIIUSE_USING_SPEAKER(wm) ((wm->state & 0x100) == 0x100) + +#define WIIUSE_IS_LED_SET(wm, num) ((wm->leds & WIIMOTE_LED_##num) == WIIMOTE_LED_##num) /* * Largest known payload is 21 bytes. * Add 2 for the prefix and round up to a power of 2. */ #define MAX_PAYLOAD 32 + +#ifdef WIN32 + #define WIIMOTE_DEFAULT_TIMEOUT 10 + #define WIIMOTE_EXP_TIMEOUT 50 +#endif -typedef unsigned char byte; +typedef unsigned char byte; typedef char sbyte; struct wiimote_t; @@ -182,69 +196,22 @@ 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. + * @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. + * @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() + * @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(). + * 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 @@ -295,9 +262,12 @@ typedef struct vec3f_t { * Yaw, pitch, and roll range from -180 to 180 degrees. */ typedef struct orient_t { - float roll; - float pitch; + float roll; /**< roll, this may be smoothed if enabled */ + float pitch; /**< pitch, this may be smoothed if enabled */ float yaw; + + float a_roll; /**< absolute roll, unsmoothed */ + float a_pitch; /**< absolute pitch, unsmoothed */ } orient_t; @@ -316,7 +286,7 @@ typedef struct gforce_t { */ typedef struct accel_t { struct vec3b_t cal_zero; /**< zero calibration */ - struct vec3b_t cal_g; /**< gravity calibration */ + struct vec3b_t cal_g; /**< 1g difference around 0cal */ float st_roll; /**< last smoothed roll value */ float st_pitch; /**< last smoothed roll pitch */ @@ -442,42 +412,58 @@ typedef struct classic_ctrl_t { } classic_ctrl_t; +/** + * @struct guitar_hero_3_t + * @brief Guitar Hero 3 expansion device. + */ +typedef struct guitar_hero_3_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 whammy_bar; /**< whammy bar (range 0-1) */ + + struct joystick_t js; /**< joystick calibration */ +} guitar_hero_3_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 */ + int type; /**< type of expansion attached */ union { struct nunchuk_t nunchuk; struct classic_ctrl_t classic; + struct guitar_hero_3_t gh3; }; } 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; - +/** + * @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; + + +/** + * @struct wiimote_state_t + * @brief Significant data from the previous event. + */ 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; + unsigned short exp_btns; struct orient_t exp_orient; float exp_r_shoulder; float exp_l_shoulder; @@ -489,36 +475,50 @@ typedef struct wiimote_state_t { struct orient_t orient; unsigned short btns; + + struct vec3b_t accel; } wiimote_state_t; +/** + * @enum WIIUSE_EVENT_TYPE + * @brief Events that wiiuse can generate from a poll. + */ +typedef enum WIIUSE_EVENT_TYPE { + WIIUSE_NONE = 0, + WIIUSE_EVENT, + WIIUSE_STATUS, + WIIUSE_DISCONNECT +} WIIUSE_EVENT_TYPE; + + /** * @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 */ + + #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 */ + #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 */ + WCONST int timeout; /**< read timeout */ + WCONST byte normal_timeout; /**< normal timeout */ + WCONST byte exp_timeout; /**< timeout for expansion handshake */ #endif - - WCONST int state; /**< various state flags */ - WCONST int leds; /**< currently lit leds */ + + WCONST int state; /**< various state flags */ + WCONST byte leds; /**< currently lit leds */ + WCONST float battery_level; /**< battery level */ 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 */ @@ -536,10 +536,12 @@ typedef struct wiimote_t { WCONST unsigned short btns_released; /**< what buttons were just released this */ WCONST float orient_threshold; /**< threshold for orient to generate an event */ + WCONST int accel_threshold; /**< threshold for accel to generate an event */ WCONST struct wiimote_state_t lstate; /**< last saved state */ - WCONST byte event[MAX_PAYLOAD]; /**< event buffer */ + WCONST WIIUSE_EVENT_TYPE event; /**< type of event that occured */ + WCONST byte event_buf[MAX_PAYLOAD]; /**< event buffer */ } wiimote; @@ -549,139 +551,64 @@ typedef struct wiimote_t { * *****************************************/ +#ifdef _WIN32 + #define WIIUSE_EXPORT_DECL __declspec(dllexport) + #define WIIUSE_IMPORT_DECL __declspec(dllimport) +#else + #define WIIUSE_EXPORT_DECL + #define WIIUSE_IMPORT_DECL +#endif + +#ifdef WIIUSE_COMPILE_LIB + #define WIIUSE_EXPORT WIIUSE_EXPORT_DECL +#else + #define WIIUSE_EXPORT WIIUSE_IMPORT_DECL +#endif + #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); +WIIUSE_EXPORT extern const char* wiiuse_version(); + +WIIUSE_EXPORT extern struct wiimote_t** wiiuse_init(int wiimotes); +WIIUSE_EXPORT extern void wiiuse_disconnected(struct wiimote_t* wm); +WIIUSE_EXPORT extern void wiiuse_cleanup(struct wiimote_t** wm, int wiimotes); +WIIUSE_EXPORT extern void wiiuse_rumble(struct wiimote_t* wm, int status); +WIIUSE_EXPORT extern void wiiuse_toggle_rumble(struct wiimote_t* wm); +WIIUSE_EXPORT extern void wiiuse_set_leds(struct wiimote_t* wm, int leds); +WIIUSE_EXPORT extern void wiiuse_motion_sensing(struct wiimote_t* wm, int status); +WIIUSE_EXPORT extern int wiiuse_read_data(struct wiimote_t* wm, wiiuse_read_cb read_cb, byte* buffer, unsigned int offset, unsigned short len); +WIIUSE_EXPORT extern int wiiuse_write_data(struct wiimote_t* wm, unsigned int addr, byte* data, byte len); +WIIUSE_EXPORT extern void wiiuse_status(struct wiimote_t* wm); +WIIUSE_EXPORT extern struct wiimote_t* wiiuse_get_by_id(struct wiimote_t** wm, int wiimotes, int unid); +WIIUSE_EXPORT extern int wiiuse_set_flags(struct wiimote_t* wm, int enable, int disable); +WIIUSE_EXPORT extern float wiiuse_set_smooth_alpha(struct wiimote_t* wm, float alpha); +WIIUSE_EXPORT extern void wiiuse_set_bluetooth_stack(struct wiimote_t** wm, int wiimotes, enum win_bt_stack_t type); +WIIUSE_EXPORT extern void wiiuse_set_orient_threshold(struct wiimote_t* wm, float threshold); +WIIUSE_EXPORT extern void wiiuse_set_accel_threshold(struct wiimote_t* wm, float threshold); +WIIUSE_EXPORT extern void wiiuse_resync(struct wiimote_t* wm); +WIIUSE_EXPORT extern void wiiuse_set_timeout(struct wiimote_t** wm, int wiimotes, byte normal_timeout, byte exp_timeout); /* 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); +WIIUSE_EXPORT extern int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout); +WIIUSE_EXPORT extern int wiiuse_connect(struct wiimote_t** wm, int wiimotes); +WIIUSE_EXPORT extern void wiiuse_disconnect(struct wiimote_t* wm); /* events.c */ -typedef void (*_wiiuse_poll_fptr)(struct wiimote_t** wm, int wiimotes); +WIIUSE_EXPORT extern int wiiuse_poll(struct wiimote_t** wm, int wiimotes); + +/* ir.c */ +WIIUSE_EXPORT extern void wiiuse_set_ir(struct wiimote_t* wm, int status); +WIIUSE_EXPORT extern void wiiuse_set_ir_vres(struct wiimote_t* wm, unsigned int x, unsigned int y); +WIIUSE_EXPORT extern void wiiuse_set_ir_position(struct wiimote_t* wm, enum ir_position_t pos); +WIIUSE_EXPORT extern void wiiuse_set_aspect_ratio(struct wiimote_t* wm, enum aspect_t aspect); #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 index ddcd75e..4776220 100644 --- a/WiiUseJC/wiiusej_WiiUseApi.c +++ b/WiiUseJC/wiiusej_WiiUseApi.c @@ -12,22 +12,11 @@ * * See below in main() for what they are used for. */ -#define WIIMOTE_ID_1 0 -#define WIIMOTE_ID_2 1 #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)) -/****************** 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);/* function with common code for callbacks */ - /********************* VARIABLES DECLARATIONS *****************************/ /* @@ -36,65 +25,33 @@ static void copy_common_status(struct wiimote_t* wm);/* function with common cod * two wiimotes. Each wiimote connected * will get one of these ids. */ -static int ids[] = { WIIMOTE_ID_1, WIIMOTE_ID_2 }; static wiimote** wiimotes; + +static int nbMaxWiiMotes=0; + 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. - * + * @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) { +(JNIEnv *env, jobject obj, jint nbConnects) { /* variables declarations */ - int found, connected; + int found, connected, i; - /* - * 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); + nbMaxWiiMotes = nbConnects; + + /* initialize wiimotes array with the maximum number of wiimotes */ + wiimotes = wiiuse_init(nbMaxWiiMotes); /* * Find wiimote devices @@ -104,7 +61,7 @@ JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_doConnections * 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); + found = wiiuse_find(wiimotes, nbMaxWiiMotes, 5); if (!found) return 0; /* @@ -113,7 +70,7 @@ JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_doConnections * 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); + connected = wiiuse_connect(wiimotes, nbMaxWiiMotes); if (!connected) return 0; //no problems during connection show that wiimotes are connected @@ -122,10 +79,10 @@ JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_doConnections * 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); + for (i=0;iGetObjectClass(globalEnv, globalWim); + jmethodID mid; + 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); - - /* Set all buttons */ - mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setAllButtons", "(SSS)V"); - if (mid == 0) { - return; - } - (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, wm->btns, - wm->btns_released, 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 (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, - wm->ir.dot[i].x, wm->ir.dot[i].y); + 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; } } - //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 and gravity force */ - cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); - mid = (*globalEnv)->GetMethodID(globalEnv, cls, - "setOrientationAndGforce", "(FFFFFF)V"); - if (mid == 0) { - return; - } - (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, - wm->orient.roll, wm->orient.pitch, wm->orient.yaw, - 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 */ - if (led[0]) - leds += 1; - if (led[1]) - leds += 2; - if (led[2]) - leds += 4; - if (led[3]) - leds += 8; - - /* set values for battery, leds, speaker and attachment*/ - mid = (*globalEnv)->GetMethodID(globalEnv, cls, - "setBatteryLedsSpeakerAttachment", "(FSZZ)V"); - if (mid == 0) { - return; - } - (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, - battery_level, leds, speaker, attachment); - -} - -/** - * @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) { - - /* call java method handling disconnection */ - copy_common_status(wm); -} - -/** - * Fills status variables. This method fills some status variables always filled in a WiiMoteEvent object. - * This function is called in every callback function. - */ -static void copy_common_status(struct wiimote_t* wm) { - - /* Variables Declarations */ - jmethodID mid; - jclass cls = (*globalEnv)->GetObjectClass(globalEnv, globalWim); - - /* set statuses */ - mid = (*globalEnv)->GetMethodID(globalEnv, cls, "setPermanentStatus", - "(IZZZZFZZ)V"); - if (mid == 0) { - return; - } - (*globalEnv)->CallVoidMethod(globalEnv, globalWim, mid, - wm->unid, WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED), - WIIUSE_USING_IR(wm), WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE), - WIIUSE_USING_ACC(wm), wm->orient_threshold, - WIIMOTE_IS_FLAG_SET(wm,WIIUSE_CONTINUOUS), - WIIMOTE_IS_FLAG_SET(wm,WIIUSE_SMOOTHING)); - -} diff --git a/WiiUseJC/wiiusej_WiiUseApi.def b/WiiUseJC/wiiusej_WiiUseApi.def index 258bf8b..aefb3ed 100644 --- a/WiiUseJC/wiiusej_WiiUseApi.def +++ b/WiiUseJC/wiiusej_WiiUseApi.def @@ -1,5 +1,4 @@ EXPORTS -Java_wiiusej_WiiUseApi_loadLibrary Java_wiiusej_WiiUseApi_doConnections Java_wiiusej_WiiUseApi_closeConnection Java_wiiusej_WiiUseApi_shutdownApi @@ -8,9 +7,13 @@ 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_deactivateMotionSensing Java_wiiusej_WiiUseApi_setLeds +Java_wiiusej_WiiUseApi_setOrientThreshold +Java_wiiusej_WiiUseApi_setAccelThreshold +Java_wiiusej_WiiUseApi_setSmoothAlpha +Java_wiiusej_WiiUseApi_reSync +Java_wiiusej_WiiUseApi_activateSmoothing Java_wiiusej_WiiUseApi_deactivateSmoothing Java_wiiusej_WiiUseApi_activateContinuous Java_wiiusej_WiiUseApi_deactivateContinuous diff --git a/WiiUseJC/wiiusej_WiiUseApi.h b/WiiUseJC/wiiusej_WiiUseApi.h index 08efaf1..f202e3b 100644 --- a/WiiUseJC/wiiusej_WiiUseApi.h +++ b/WiiUseJC/wiiusej_WiiUseApi.h @@ -7,21 +7,13 @@ #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 + * Signature: (I)I */ JNIEXPORT jint JNICALL Java_wiiusej_WiiUseApi_doConnections - (JNIEnv *, jobject); + (JNIEnv *, jobject, jint); /* * Class: wiiusej_WiiUseApi @@ -103,6 +95,30 @@ JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_setLeds JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_setOrientThreshold (JNIEnv *, jobject, jint, jfloat); +/* + * Class: wiiusej_WiiUseApi + * Method: setAccelThreshold + * Signature: (IF)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_setAccelThreshold + (JNIEnv *, jobject, jint, jfloat); + +/* + * Class: wiiusej_WiiUseApi + * Method: setSmoothAlpha + * Signature: (IF)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_setSmoothAlpha + (JNIEnv *, jobject, jint, jfloat); + +/* + * Class: wiiusej_WiiUseApi + * Method: reSync + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_reSync + (JNIEnv *, jobject, jint); + /* * Class: wiiusej_WiiUseApi * Method: activateSmoothing @@ -146,7 +162,7 @@ JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_getStatus /* * Class: wiiusej_WiiUseApi * Method: specialPoll - * Signature: (Lwiiusej/WiiMoteEvent;)V + * Signature: (Lwiiusej/wiiuseapievents/EventsGatherer;)V */ JNIEXPORT void JNICALL Java_wiiusej_WiiUseApi_specialPoll (JNIEnv *, jobject, jobject);