From 6c7fb6f12f917302bb3b0e0a6c29ec594d46140f Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Mon, 2 May 2011 10:32:26 -0500 Subject: [PATCH] improved platform definitions and definition checks --- src/classic.c | 2 +- src/definitions.h | 2 +- src/dynamics.c | 5 +---- src/events.c | 8 ++++---- src/guitar_hero_3.c | 2 +- src/io.h | 2 +- src/io_nix.c | 6 +++--- src/io_win.c | 4 ++-- src/ir.c | 14 +++++++------- src/nunchuk.c | 2 +- src/os.h | 8 ++------ src/wiiboard.c | 2 +- src/wiiuse.c | 28 +++++++++++++++------------- src/wiiuse.h | 33 +++++++++++++++++++++++++++------ src/wiiuse_internal.h | 17 +++++++++++++++-- 15 files changed, 82 insertions(+), 53 deletions(-) diff --git a/src/classic.c b/src/classic.c index 4cd16e8..c09dd5f 100644 --- a/src/classic.c +++ b/src/classic.c @@ -105,7 +105,7 @@ int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte /* handshake done */ wm->exp.type = EXP_CLASSIC; - #ifdef WIN32 + #ifdef WIIUSE_WIN32 wm->timeout = WIIMOTE_DEFAULT_TIMEOUT; #endif diff --git a/src/definitions.h b/src/definitions.h index a7bb252..461a90a 100644 --- a/src/definitions.h +++ b/src/definitions.h @@ -61,7 +61,7 @@ extern FILE* logtarget[]; #define WIIUSE_INFO(fmt, ...) do { if (OUTF_INFO) fprintf(OUTF_INFO, "[INFO] " fmt "\n", ##__VA_ARGS__); } while(0) #ifdef WITH_WIIUSE_DEBUG - #ifdef WIN32 + #ifdef WIIUSE_WIN32 #define WIIUSE_DEBUG(fmt, ...) do { \ if (OUTF_DEBUG) { \ char* file = __FILE__; \ diff --git a/src/dynamics.c b/src/dynamics.c index 7c108f7..41f1297 100644 --- a/src/dynamics.c +++ b/src/dynamics.c @@ -40,10 +40,7 @@ #include #include - -#ifdef WIN32 - #include -#endif +#include /** diff --git a/src/events.c b/src/events.c index 5927a95..38aef7c 100644 --- a/src/events.c +++ b/src/events.c @@ -44,7 +44,7 @@ #include "guitar_hero_3.h" #include "wiiboard.h" -#ifndef WIN32 +#ifndef WIIUSE_WIN32 #include #include #include @@ -79,7 +79,7 @@ static int state_changed(struct wiimote_t* wm); int wiiuse_poll(struct wiimote_t** wm, int wiimotes) { int evnt = 0; - #ifndef WIN32 + #ifdef WIIUSE_BLUEZ /* * *nix */ @@ -566,7 +566,7 @@ static void event_status(struct wiimote_t* wm, byte* msg) { exp_changed = 1; } - #ifdef WIN32 + #ifdef WIIUSE_WIN32 if (!attachment) { WIIUSE_DEBUG("Setting timeout to normal %i ms.", wm->normal_timeout); wm->timeout = wm->normal_timeout; @@ -640,7 +640,7 @@ void handshake_expansion(struct wiimote_t* wm, byte* data, uint16_t len) { disable_expansion(wm); /* increase the timeout until the handshake completes */ - #ifdef WIN32 + #ifdef WIIUSE_WIN32 WIIUSE_DEBUG("Setting timeout to expansion %i ms.", wm->exp_timeout); wm->timeout = wm->exp_timeout; #endif diff --git a/src/guitar_hero_3.c b/src/guitar_hero_3.c index 3cf5339..af2d906 100644 --- a/src/guitar_hero_3.c +++ b/src/guitar_hero_3.c @@ -103,7 +103,7 @@ int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, b /* handshake done */ wm->exp.type = EXP_GUITAR_HERO_3; - #ifdef WIN32 + #ifdef WIIUSE_WIN32 wm->timeout = WIIMOTE_DEFAULT_TIMEOUT; #endif diff --git a/src/io.h b/src/io.h index 4c7d31d..8918a2f 100644 --- a/src/io.h +++ b/src/io.h @@ -34,7 +34,7 @@ #ifndef CONNECT_H_INCLUDED #define CONNECT_H_INCLUDED -#ifndef WIN32 +#ifdef WIIUSE_BLUEZ #include #endif diff --git a/src/io_nix.c b/src/io_nix.c index 7ff5b38..90cce7a 100644 --- a/src/io_nix.c +++ b/src/io_nix.c @@ -31,10 +31,10 @@ * @brief Handles device I/O for *nix. */ -#ifndef WIN32 - #include "io.h" +#ifdef WIIUSE_BLUEZ + #include #include @@ -265,4 +265,4 @@ int wiiuse_io_write(struct wiimote_t* wm, byte* buf, int len) { -#endif /* ifndef WIN32 */ +#endif /* ifdef WIIUSE_BLUEZ */ diff --git a/src/io_win.c b/src/io_win.c index b084dcc..745526b 100644 --- a/src/io_win.c +++ b/src/io_win.c @@ -31,10 +31,10 @@ * @brief Handles device I/O for Windows. */ -#ifdef WIN32 #include "io.h" +#ifdef WIIUSE_WIN32 #include #include @@ -241,4 +241,4 @@ int wiiuse_io_write(struct wiimote_t* wm, byte* buf, int len) { return 0; } -#endif /* ifdef WIN32 */ +#endif /* ifdef WIIUSE_WIN32 */ diff --git a/src/ir.c b/src/ir.c index 3ee2412..a4ecd31 100644 --- a/src/ir.c +++ b/src/ir.c @@ -35,7 +35,7 @@ #include -#ifndef WIN32 +#ifndef WIIUSE_WIN32 #include #endif @@ -115,10 +115,10 @@ void wiiuse_set_ir(struct wiimote_t* wm, int status) { wiiuse_write_data(wm, WM_REG_IR, &buf, 1); /* wait for the wiimote to catch up */ - #ifndef WIN32 - usleep(50000); - #else + #ifdef WIIUSE_WIN32 Sleep(50); + #else + usleep(50000); #endif /* write sensitivity blocks */ @@ -132,10 +132,10 @@ void wiiuse_set_ir(struct wiimote_t* wm, int status) { buf = WM_IR_TYPE_EXTENDED; wiiuse_write_data(wm, WM_REG_IR_MODENUM, &buf, 1); - #ifndef WIN32 - usleep(50000); - #else + #ifdef WIIUSE_WIN32 Sleep(50); + #else + usleep(50000); #endif /* set the wiimote report type */ diff --git a/src/nunchuk.c b/src/nunchuk.c index a0a78bc..1d92526 100644 --- a/src/nunchuk.c +++ b/src/nunchuk.c @@ -110,7 +110,7 @@ int nunchuk_handshake(struct wiimote_t* wm, struct nunchuk_t* nc, byte* data, un /* handshake done */ wm->exp.type = EXP_NUNCHUK; - #ifdef WIN32 + #ifdef WIIUSE_WIN32 wm->timeout = WIIMOTE_DEFAULT_TIMEOUT; #endif diff --git a/src/os.h b/src/os.h index 79d4ce6..d8e0d3b 100644 --- a/src/os.h +++ b/src/os.h @@ -39,18 +39,14 @@ #ifndef OS_H_INCLUDED #define OS_H_INCLUDED -#ifdef WIN32 - /* windows */ +#ifdef _MSC_VER + /* windows with visual c */ #define isnan(x) _isnan(x) #define isinf(x) !_finite(x) - /* disable warnings I don't care about */ #pragma warning(disable:4244) /* possible loss of data conversion */ #pragma warning(disable:4273) /* inconsistent dll linkage */ #pragma warning(disable:4217) -#else - /* nix */ #endif - #endif // OS_H_INCLUDED diff --git a/src/wiiboard.c b/src/wiiboard.c index 6122947..37e2374 100644 --- a/src/wiiboard.c +++ b/src/wiiboard.c @@ -101,7 +101,7 @@ int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, byte* data wm->event = WIIUSE_WII_BOARD_CTRL_INSERTED; wm->exp.type = EXP_WII_BOARD; - #ifdef WIN32 + #ifdef WIIUSE_WIN32 wm->timeout = WIIMOTE_DEFAULT_TIMEOUT; #endif diff --git a/src/wiiuse.c b/src/wiiuse.c index 56d22d1..4459bac 100644 --- a/src/wiiuse.c +++ b/src/wiiuse.c @@ -41,7 +41,7 @@ #include -#ifndef WIN32 +#ifndef WIIUSE_WIN32 #include #endif @@ -142,11 +142,12 @@ struct wiimote_t** wiiuse_init(int wiimotes) { wm[i]->unid = i+1; - #ifndef WIN32 + #ifdef WIIUSE_BLUEZ wm[i]->bdaddr = *BDADDR_ANY; wm[i]->out_sock = -1; wm[i]->in_sock = -1; - #else + #endif + #ifdef WIIUSE_WIN32 wm[i]->dev_handle = 0; wm[i]->stack = WIIUSE_STACK_UNKNOWN; wm[i]->normal_timeout = WIIMOTE_DEFAULT_TIMEOUT; @@ -188,10 +189,11 @@ void wiiuse_disconnected(struct wiimote_t* wm) { WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_CONNECTED); /* reset a bunch of stuff */ - #ifndef WIN32 + #ifdef WIIUSE_BLUEZ wm->out_sock = -1; wm->in_sock = -1; - #else + #endif + #ifdef WIIUSE_WIN32 wm->dev_handle = 0; #endif @@ -594,11 +596,11 @@ int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len) { byte buf[32]; /* no payload is better than this */ int rumble = 0; - #ifndef WIN32 + #ifdef WIIUSE_WIN32 + buf[0] = report_type; + #else buf[0] = WM_SET_REPORT | WM_BT_OUTPUT; buf[1] = report_type; - #else - buf[0] = report_type; #endif switch (report_type) { @@ -615,7 +617,7 @@ int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len) { break; } - #ifndef WIN32 + #ifndef WIIUSE_WIN32 memcpy(buf+2, msg, len); if (rumble) buf[2] |= 0x01; @@ -629,7 +631,7 @@ int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len) { { int x = 2; printf("[DEBUG] (id %i) SEND: (%x) %.2x ", wm->unid, buf[0], buf[1]); - #ifndef WIN32 + #ifndef WIIUSE_WIN32 for (; x < len+2; ++x) #else for (; x < len+1; ++x) @@ -639,7 +641,7 @@ int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len) { } #endif - #ifndef WIN32 + #ifndef WIIUSE_WIN32 return wiiuse_io_write(wm, buf, len+2); #else return wiiuse_io_write(wm, buf, len+1); @@ -711,7 +713,7 @@ float wiiuse_set_smooth_alpha(struct wiimote_t* wm, float alpha) { * @param type The type of bluetooth stack to use. */ void wiiuse_set_bluetooth_stack(struct wiimote_t** wm, int wiimotes, enum win_bt_stack_t type) { - #ifdef WIN32 + #ifdef WIIUSE_WIN32 int i; if (!wm) return; @@ -774,7 +776,7 @@ void wiiuse_resync(struct wiimote_t* wm) { * @param exp_timeout The timeout in millisecondsd to wait for an expansion handshake. */ void wiiuse_set_timeout(struct wiimote_t** wm, int wiimotes, byte normal_timeout, byte exp_timeout) { - #ifdef WIN32 + #ifdef WIIUSE_WIN32 int i; if (!wm) return; diff --git a/src/wiiuse.h b/src/wiiuse.h index acd32ec..193692c 100644 --- a/src/wiiuse.h +++ b/src/wiiuse.h @@ -69,10 +69,23 @@ #define WIIUSE_MINOR 13 #define WIIUSE_MICRO 1 -#ifdef _WIN32 +#ifndef WIIUSE_PLATFORM + #if defined(_WIN32) + #define WIIUSE_PLATFORM + #define WIIUSE_WIN32 + #elif defined(__linux) + #define WIIUSE_PLATFORM + #define WIIUSE_BLUEZ + #else + #error "Platform not yet supported!" + #endif +#endif + +#ifdef WIIUSE_WIN32 /* windows */ #include -#else +#endif +#ifdef WIIUSE_BLUEZ /* nix */ #include #endif @@ -262,7 +275,7 @@ typedef enum ir_position_t { * This is left over from an old hack, but it may actually * be a useful feature to keep so it wasn't removed. */ -#ifdef WIN32 +#ifdef WIIUSE_WIN32 #define WIIMOTE_DEFAULT_TIMEOUT 10 #define WIIMOTE_EXP_TIMEOUT 10 #endif @@ -605,18 +618,26 @@ typedef enum WIIUSE_EVENT_TYPE { typedef struct wiimote_t { WCONST int unid; /**< user specified id */ - #ifndef WIN32 + #ifdef WIIUSE_BLUEZ + /** @name Linux-specific (BlueZ) members */ + /** @{ */ 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 + /** @} */ + #endif + + #ifdef WIIUSE_WIN32 + /** @name Windows-specific members */ + /** @{ */ 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 */ @@ -666,7 +687,7 @@ typedef enum wiiuse_loglevel { * *****************************************/ -#ifdef _WIN32 +#ifdef WIIUSE_WIN32 #define WIIUSE_EXPORT_DECL __declspec(dllexport) #define WIIUSE_IMPORT_DECL __declspec(dllimport) #else diff --git a/src/wiiuse_internal.h b/src/wiiuse_internal.h index 5df38e9..7f674ee 100644 --- a/src/wiiuse_internal.h +++ b/src/wiiuse_internal.h @@ -44,9 +44,22 @@ #ifndef WIIUSE_INTERNAL_H_INCLUDED #define WIIUSE_INTERNAL_H_INCLUDED -#ifdef WIN32 +#ifndef WIIUSE_PLATFORM + #if defined(_WIN32) + #define WIIUSE_PLATFORM + #define WIIUSE_WIN32 + #elif defined(__linux) + #define WIIUSE_PLATFORM + #define WIIUSE_BLUEZ + #else + #error "Platform not yet supported!" + #endif +#endif + +#ifdef WIIUSE_WIN32 #include -#else +#endif +#ifdef WIIUSE_BLUEZ #include /* htons() */ #include #endif