kodi
CWIID_WiiRemote.h
1 /*
2  * Copyright (C) 2007 by Tobias Arrskog
3  * topfs@tobias
4  *
5  * Copyright (C) 2007-2013 Team XBMC
6  * http://kodi.tv
7  *
8  * This Program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This Program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with XBMC; see the file COPYING. If not, see
20  * <http://www.gnu.org/licenses/>.
21  *
22  */
23 
24 #pragma once
25 
26 /* Toggle one bit */
27 #define ToggleBit(bf,b) (bf) = ((bf) & b) ? ((bf) & ~(b)) : ((bf) | (b))
28 
29 //Settings
30 #define WIIREMOTE_SAMPLES 16
31 
32 #define DEADZONE_Y 0.3f
33 #define DEADZONE_X 0.5f
34 
35 #define MOUSE_MAX 65535
36 #define MOUSE_MIN 0
37 
38 //The work area is from 0 - MAX but the one sent to XBMC is MIN - (MIN + MOUSE_MAX)
39 #define WIIREMOTE_X_MIN MOUSE_MAX * DEADZONE_X
40 #define WIIREMOTE_Y_MIN MOUSE_MAX * DEADZONE_Y
41 
42 #define WIIREMOTE_X_MAX MOUSE_MAX * (1.0f + DEADZONE_X + DEADZONE_X)
43 #define WIIREMOTE_Y_MAX MOUSE_MAX * (1.0f + DEADZONE_Y + DEADZONE_Y)
44 
45 #define WIIREMOTE_BUTTON_REPEAT_TIME 30 // How long between buttonpresses in repeat mode
46 #define WIIREMOTE_BUTTON_DELAY_TIME 500
47 //#define CWIID_OLD // Uncomment if the system is running cwiid that is older than 6.0 (The one from ubuntu gutsy repository is < 6.0)
48 
49 //CWIID
50 #include <cwiid.h>
51 //Bluetooth specific
52 #include <sys/socket.h>
53 #include <bluetooth/bluetooth.h>
54 #include <bluetooth/hci.h>
55 #include <bluetooth/hci_lib.h>
56 // UDP Client
57 #ifdef DEB_PACK
58 #include <xbmc/xbmcclient.h>
59 #else
60 #include "../../lib/c++/xbmcclient.h"
61 #endif
62 /*#include <stdio.h>*/
63 #include <stdlib.h>
64 #include <time.h>
65 #include <sys/time.h>
66 #include <string>
67 
69 {
70 public:
71  CWiiRemote(char *btaddr = NULL);
72  ~CWiiRemote();
73 
74  void Initialize(CAddress Addr, int Socket);
75  void Disconnect();
76  bool GetConnected();
77 
78  bool EnableWiiRemote();
79  bool DisableWiiRemote();
80 
81  void Update();
82 
83  // Mouse functions
84  bool HaveIRSources();
85  bool isActive();
86  void EnableMouseEmulation();
87  void DisableMouseEmulation();
88 
89  bool Connect();
90 
91  void SetBluetoothAddress(const char * btaddr);
92  void SetSensitivity(float DeadX, float DeadY, int Samples);
93  void SetJoystickMap(const char *JoyMap);
94 private:
95  int m_NumSamples;
96  int *m_SamplesX;
97  int *m_SamplesY;
98 
99  float m_MaxX;
100  float m_MaxY;
101  float m_MinX;
102  float m_MinY;
103 #ifdef CWIID_OLD
104  bool CheckConnection();
105  int m_LastMsgTime;
106 #endif
107  char *m_JoyMap;
108  int m_lastKeyPressed;
109  int m_LastKey;
110  bool m_buttonRepeat;
111 
112  int m_lastKeyPressedNunchuck;
113  int m_LastKeyNunchuck;
114  bool m_buttonRepeatNunchuck;
115 
116  void SetRptMode();
117  void SetLedState();
118 
119  void SetupWiiRemote();
120 
121  bool m_connected;
122 
123  bool m_DisconnectWhenPossible;
124  bool m_connectThreadRunning;
125 
126  //CWIID Specific
127  cwiid_wiimote_t *m_wiiremoteHandle;
128  unsigned char m_ledState;
129  unsigned char m_rptMode;
130  bdaddr_t m_btaddr;
131 
132  static void MessageCallback(cwiid_wiimote_t *wiiremote, int mesgCount, union cwiid_mesg mesg[], struct timespec *timestamp);
133 #ifdef CWIID_OLD
134  static void MessageCallback(cwiid_wiimote_t *wiiremote, int mesgCount, union cwiid_mesg mesg[]);
135 #endif
136 
137 #ifndef _DEBUG
138 /* This takes the errors generated at pre-connect and silence them as they are mostly not needed */
139  static void ErrorCallback(struct wiimote *wiiremote, const char *str, va_list ap);
140 #endif
141 
142  int m_Socket;
143  CAddress m_MyAddr;
144 
145  // Mouse
146  bool m_haveIRSources;
147  bool m_isActive;
148  bool m_useIRMouse;
149  int m_lastActiveTime;
150 
151 /* The protected functions is for the static callbacks */
152  protected:
153  //Connection
154  void DisconnectNow(bool startConnectThread);
155 
156  //Mouse
157  void CalculateMousePointer(int x1, int y1, int x2, int y2);
158 // void SetIR(bool set);
159 
160  //Button
161  void ProcessKey(int Key);
162 
163  //Nunchuck
164  void ProcessNunchuck(struct cwiid_nunchuk_mesg &Nunchuck);
165 #ifdef CWIID_OLD
166  //Disconnect check
167  void CheckIn();
168 #endif
169 };
170 
171 extern CWiiRemote g_WiiRemote;
Definition: xbmcclient.h:90
Wiimote structure.
Definition: wiiuse.h:531
Definition: CWIID_WiiRemote.h:68