2 * Copyright (C) 2009-2013 Team XBMC
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // Xbox360EventClient.cpp : Defines the entry point for the console application.
23 #include "Xbox360Controller.h"
24 #include "../../lib/c++/xbmcclient.h"
25 #pragma comment(lib, "wsock32.lib") // needed for xbmclient.h?
27 // You can have up to 4 xbox360 controllers on a system
28 #define MAX_NUM_CONTROLLERS 4
31 // needed for exit event handler
34 BOOL exitHandler( DWORD ctrlType )
36 // TODO: Send BYE Packet
44 void checkTrigger(Xbox360Controller &cont, CXBMCClient *client, int num, const char* name)
46 if (cont.triggerMoved(num))
48 client->SendButton(name, "XG", 0x20, cont.getTrigger(num) * 128);
52 void checkThumb(Xbox360Controller &cont, CXBMCClient *client, int num,
53 const char* leftname, const char* rightname)
55 if (cont.thumbMoved(num))
57 if (cont.getThumb(num) < 0)
59 client->SendButton(leftname, "XG", 0x20, -cont.getThumb(num));
63 client->SendButton(rightname, "XG", 0x20, cont.getThumb(num));
68 void checkButton(Xbox360Controller &cont, CXBMCClient *client, int num, const char* name)
70 if (cont.buttonPressed(num))
72 client->SendButton(name, "XG", 0x02);
74 else if (cont.buttonReleased(num))
76 client->SendButton(name, "XG", 0x04);
80 void checkAll(Xbox360Controller &cont)
82 if (cont.isConnected())
85 checkButton(cont, client, 0, "a");
86 checkButton(cont, client, 1, "b");
87 checkButton(cont, client, 2, "x");
88 checkButton(cont, client, 3, "y");
89 checkButton(cont, client, 4, "dpadup");
90 checkButton(cont, client, 5, "dpaddown");
91 checkButton(cont, client, 6, "dpadleft");
92 checkButton(cont, client, 7, "dpadright");
93 checkButton(cont, client, 8, "start");
94 checkButton(cont, client, 9, "back");
95 checkButton(cont, client, 10, "leftthumbbutton");
96 checkButton(cont, client, 11, "rightthumbbutton");
97 checkButton(cont, client, 12, "white");
98 checkButton(cont, client, 13, "black");
99 checkTrigger(cont, client, 0, "rightanalogtrigger");
100 checkTrigger(cont, client, 1, "leftanalogtrigger");
101 checkThumb(cont, client, 0, "leftthumbstickleft", "leftthumbstickright");
102 checkThumb(cont, client, 1, "leftthumbstickdown", "leftthumbstickup");
103 checkThumb(cont, client, 2, "rightthumbstickleft", "rightthumbstickright");
104 checkThumb(cont, client, 3, "rightthumbstickdown", "rightthumbstickup");
108 int main(int argc, char* argv[])
110 char *host = "localhost";
112 Xbox360Controller *controllers[MAX_NUM_CONTROLLERS];
115 // Get access to all potential controllers
116 for ( i = 0; i < MAX_NUM_CONTROLLERS; i++)
118 controllers[i] = new Xbox360Controller(i);
121 // Start Winsock stuff
123 WSAStartup(MAKEWORD(2, 2), &wsaData);
127 printf("USAGE: %s [HOST [PORT]]\n\nThe event client connects to the XBMC EventServer at HOST:PORT.\
128 Default value for HOST is localhost, default value for port is 9777.\n", argv[0]);
142 client = new CXBMCClient(host, atoi(port));
144 SetConsoleCtrlHandler( (PHANDLER_ROUTINE) exitHandler, TRUE);
146 client->SendHELO("Xbox 360 Controller", 0);
150 // Check each controller for activity
151 for ( i = 0; i < MAX_NUM_CONTROLLERS; i++ )
153 checkAll(*controllers[i]);