-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·387 lines (264 loc) · 10.4 KB
/
main.cpp
File metadata and controls
executable file
·387 lines (264 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "wiimote.h"
#include "input.h"
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#define MINX 85
#define MINY 10
#define MAXX 1960
#define MAXY 1370
#define CURSORX 0
#define CURSORY 0
#define CURSORW 1920
#define CURSORH 1080
#ifdef _WIN32
#define CLOCKS CLOCKS_PER_SEC
#else
// this probably isn't a good idea but CLOCKS_PER_SEC isn't accurate anyway so whatever
#define CLOCKS 5000
#endif
#define ANSI_ESC "\033"
#define ANSI_SAVECURSOR ANSI_ESC "7"
#define ANSI_RESTORECURSOR ANSI_ESC "8"
#define ANSI_ERASELINE ANSI_ESC "[K"
#define ANSI_SETPOS( x, y ) ANSI_ESC "[" #y ";" #x "H"
#define ANSI_RESET ANSI_ESC "[0m"
#define ANSI_BGCOLOR "40"
//#define ANSI_BG ANSI_ESC "[48;5;236m"
//#define ANSI_BG ANSI_ESC "[;" ANSI_BGCOLOR "m"
#define ANSI_BG ""
#define ANSI_BLACK ANSI_ESC "[30m"
#define ANSI_RED ANSI_ESC "[31m"
#define ANSI_GREEN ANSI_ESC "[32m"
#define ANSI_YELLOW ANSI_ESC "[33m"
#define ANSI_BLUE ANSI_ESC "[34m"
#define ANSI_MAGENTA ANSI_ESC "[35m"
#define ANSI_CYAN ANSI_ESC "[36m"
#define ANSI_WHITE ANSI_ESC "[37m"
#define ANSI_BORDER ANSI_BLUE
#define ANSI_NORMAL ANSI_ESC "[22m"
#define ANSI_BOLD ANSI_ESC "[1m"
#define ANSI_FAINT ANSI_ESC "[2m"
#define WRITEKV( color, key ) color ANSI_NORMAL key ":" ANSI_BOLD "%i "
#define WRITEKVSTR( color, key ) color ANSI_NORMAL key ":" ANSI_BOLD "%s "
#define WRITEKVPOS( x, y, color, key ) color ANSI_SETPOS( x, y ) ANSI_NORMAL key ":" ANSI_BOLD "%i"
#define WRITEKVPOSSTR( x, y, color, key ) color ANSI_SETPOS( x, y ) ANSI_NORMAL key ":" ANSI_BOLD "%s"
int hasarg( const char * arg, char ** args, int argc ) {
for ( int i = 1; i < argc; i++ ) {
if ( strcmp( args[ i ], arg ) == 0 ) { return i; }
}
return 0;
}
int getargindex( int arg, int def, char ** args, int argc ) {
if ( arg == 0 || arg >= argc - 1 ) { return def; }
return atoi( args[ arg + 1 ] );
}
int getarg( const char * arg, int def, char ** args, int argc ) {
return getargindex( hasarg( arg, args, argc ), def, args, argc );
}
void printhelp( const char * name ) {
printf( "%s: Link a Wiimote with a uDraw tablet extension to use as a mouse or pen\n", name );
printf( "\nargs:\n\n" );
printf( "\t-help || --help || /? \t: Print this message\n" );
printf( "\t-mouse \t: Enable mouse input instead of pen input by default\n" );
printf( "\t-rumble \t: Enable rumble by default\n" );
printf( "\t-noinfo \t: Hide tablet info\n" );
printf( "\t-minx (int) \t: Set the position of the tablet's left side (default %i)\n", MINX );
printf( "\t-miny (int) \t: Set the position of the tablet's top side (default %i)\n", MINY );
printf( "\t-maxx (int) \t: Set the position of the tablet's right side (default %i)\n", MAXX );
printf( "\t-maxy (int) \t: Set the position of the tablet's bottom side (default %i)\n", MAXY );
printf( "\t-cursorx (int) \t: Set the x offset of the cursor's bounds (default %i)\n", CURSORX );
printf( "\t-cursory (int) \t: Set the y offset of the cursor's bounds (default %i)\n", CURSORY );
printf( "\t-cursorw (int) \t: Set the width of the cursor's bounds (default %i)\n", CURSORW );
printf( "\t-cursorh (int) \t: Set the height of the cursor's bounds (default %i)\n", CURSORH );
}
void wait( unsigned int sec ) {
#ifdef _WIN32
Sleep( 1000 * sec );
#else
sleep( sec );
#endif
}
int main( int argc, char ** args ) {
if ( hasarg( "-help", args, argc ) != 0 || hasarg( "--help", args, argc ) != 0 || hasarg( "/?", args, argc ) != 0 ) {
printhelp( argc > 0 ? args[ 0 ] : "udraw" );
return 0;
}
bool found = false;
void * handle;
for ( int i = 0; i < 5; i++ ) {
handle = Wiimote::GetWiimoteHandle( & found );
if ( found == true ) { break; }
wait( 1 );
}
if ( found != true ) { printf( "Couldn't find wiimote handle\n" ); return 0; }
Wiimote * wiimote = new Wiimote( handle );
bool initudraw = false;
for ( int i = 0; i < 5; i++ ) {
initudraw = wiimote->InitUDraw();
if ( initudraw == true ) { break; }
wait( 1 );
}
if ( initudraw == true ) {
unsigned int lastbuttons = 0;
bool usepen = hasarg( "-mouse", args, argc ) == 0;
bool userumble = hasarg( "-rumble", args, argc ) != 0;
bool calibrate = false;
bool showinfo = hasarg( "-noinfo", args, argc ) == 0;
int minx = getarg( "-minx", MINX, args, argc );
int miny = getarg( "-miny", MINY, args, argc );
int maxx = getarg( "-maxx", MAXX, args, argc );
int maxy = getarg( "-maxy", MAXY, args, argc );
int cursorx = getarg( "-cursorx", CURSORX, args, argc );
int cursory = getarg( "-cursory", CURSORY, args, argc );
int cursorw = getarg( "-cursorw", CURSORW, args, argc );
if ( cursorw <= 0 ) { cursorw = CURSORW; }
int cursorh = getarg( "-cursorh", CURSORH, args, argc );
if ( cursorh <= 0 ) { cursorh = CURSORH; }
Mouse * mouse = nullptr;
Pen * pen = nullptr;
if ( usepen == true ) { wiimote->SetLED( WIIMOTE_LED_ONE | WIIMOTE_LED_THREE ); }
else { wiimote->SetLED( WIIMOTE_LED_ONE | WIIMOTE_LED_FOUR ); }
#ifdef _WIN32
printf( ANSI_SETPOS( 1, 6 ) "uDraw initialized\n" );
#else
printf( "uDraw initialized\n" );
#endif
printf( "Press up on the dpad to switch between mouse mode and pen mode\n" );
printf( "Press down on the dpad to toggle rumble\n" );
printf( "Press left on the dpad to toggle pen bounds calibration mode (move the pen along the edges to set bounds)\n" );
printf( "Press the home button to exit\n" );
int rate = 0;
clock_t lastclock = clock();
int lastrate = 0;
int lastx = -1;
int lasty = -1;
UDrawData data;
memset( & data, 0, sizeof( UDrawData ) );
while ( true ) {
wiimote->PollUDraw( & data );
int curx = data.x;
int cury = data.y;
if ( calibrate == true ) {
if ( curx != WIIMOTE_UDRAW_INVALIDX ) {
if ( curx < minx ) { minx = curx; }
if ( curx > maxx ) { maxx = curx; }
}
if ( cury != WIIMOTE_UDRAW_INVALIDY ) {
if ( cury < miny ) { miny = cury; }
if ( cury > maxy ) { maxy = cury; }
}
}
if ( curx != WIIMOTE_UDRAW_INVALIDX ) {
if ( maxx != minx ) {
data.x = ( int ) ( cursorw * ( ( float ) ( data.x - minx ) / ( maxx - minx ) ) );
} else { data.x -= minx; }
data.x += cursorx;
}
if ( cury != WIIMOTE_UDRAW_INVALIDY ) {
if ( maxy != miny ) {
data.y = ( int ) ( cursorh * ( ( float ) ( data.y - miny ) / ( maxy - miny ) ) );
} else { data.y -= miny; }
data.y += cursory;
}
if ( usepen != true ) {
if ( mouse == nullptr ) { mouse = new Mouse; }
mouse->Send( data );
} else {
if ( pen == nullptr ) { pen = new Pen; }
pen->Send( data );
}
if ( showinfo == true ) {
#ifdef _WIN32
printf(
ANSI_SAVECURSOR
ANSI_BG
ANSI_SETPOS( 1, 1 ) ANSI_ERASELINE "\n" ANSI_ERASELINE "\n" ANSI_ERASELINE "\n" ANSI_ERASELINE
);
if ( curx == WIIMOTE_UDRAW_INVALIDX ) { printf( WRITEKVPOSSTR( 1, 1, ANSI_RED, "x" ), "none" ); }
else { printf( WRITEKVPOS( 1, 1, ANSI_RED, "x" ), data.x ); }
if ( cury == WIIMOTE_UDRAW_INVALIDY ) { printf( WRITEKVPOSSTR( 8, 1, ANSI_GREEN, "y" ), "none" ); }
else { printf( WRITEKVPOS( 8, 1, ANSI_GREEN, "y" ), data.y ); }
printf(
WRITEKVPOS( 15, 1, ANSI_CYAN, "press" )
WRITEKVPOS( 25, 1, ANSI_YELLOW, "rate" )
WRITEKVPOS( 1, 2, ANSI_YELLOW, "click" )
WRITEKVPOS( 9, 2, ANSI_YELLOW, "side1" )
WRITEKVPOS( 17, 2, ANSI_YELLOW, "side2" )
WRITEKVPOS( 25, 2, ANSI_YELLOW, "input" )
WRITEKVPOS( 1, 3, ANSI_CYAN, "pen" )
WRITEKVPOS( 7, 3, ANSI_CYAN, "rumble" )
ANSI_SETPOS( 1, 4 ) ANSI_NORMAL ANSI_BORDER "----------------------------------"
ANSI_RESET ANSI_RESTORECURSOR
, data.pressure, lastrate, data.click, data.sideclick1, data.sideclick2, data.buttons, usepen, userumble );
#else
printf( ANSI_BG ANSI_ERASELINE "\t" );
if ( curx == WIIMOTE_UDRAW_INVALIDX ) { printf( WRITEKVSTR( ANSI_RED, "x" ), "none" ); }
else { printf( WRITEKV( ANSI_RED, "x" ), data.x ); }
if ( cury == WIIMOTE_UDRAW_INVALIDY ) { printf( WRITEKVSTR( ANSI_GREEN, "y" ), "none" ); }
else { printf( WRITEKV( ANSI_GREEN, "y" ), data.y ); }
printf(
WRITEKV( ANSI_CYAN, "press" )
WRITEKV( ANSI_YELLOW, "rate" )
WRITEKV( ANSI_YELLOW, "click" )
WRITEKV( ANSI_YELLOW, "side1" )
WRITEKV( ANSI_YELLOW, "side2" )
WRITEKV( ANSI_YELLOW, "input" )
WRITEKV( ANSI_CYAN, "pen" )
WRITEKV( ANSI_CYAN, "rumble" )
ANSI_RESET
"\r"
, data.pressure, lastrate, data.click, data.sideclick1, data.sideclick2, data.buttons, usepen, userumble );
#endif
fflush( stdout );
}
if ( data.buttons & WIIMOTE_BUT_UP && !( lastbuttons & WIIMOTE_BUT_UP ) ) {
usepen = !usepen;
if ( usepen == true ) { wiimote->SetLED( wiimote->GetLED() | WIIMOTE_LED_THREE ); }
else { wiimote->SetLED( wiimote->GetLED() & ~WIIMOTE_LED_THREE ); }
}
if ( data.buttons & WIIMOTE_BUT_DOWN && !( lastbuttons & WIIMOTE_BUT_DOWN ) ) {
userumble = !userumble;
if ( userumble == true ) { wiimote->SetLED( wiimote->GetLED() | WIIMOTE_LED_FOUR ); }
else { wiimote->SetLED( wiimote->GetLED() & ~WIIMOTE_LED_FOUR ); }
}
if ( data.buttons & WIIMOTE_BUT_LEFT && !( lastbuttons & WIIMOTE_BUT_LEFT ) ) {
calibrate = !calibrate;
if ( calibrate == true ) {
minx = 9999;
miny = 9999;
maxx = -9999;
maxy = -9999;
wiimote->SetLED( wiimote->GetLED() | WIIMOTE_LED_TWO );
}
else {
printf( ANSI_ERASELINE "-minx %i -miny %i -maxx %i -maxy %i\n", minx, miny, maxx, maxy );
wiimote->SetLED( wiimote->GetLED() & ~WIIMOTE_LED_TWO );
}
}
if ( data.buttons & WIIMOTE_BUT_HOME ) { printf( "\nHome pressed, exiting\n" ); break; }
wiimote->SetRumble( userumble == true && data.pressure > 16 && ( data.pressure / 18 ) + abs( curx - lastx ) + abs( cury - lasty ) > 32 );
lastbuttons = data.buttons;
lastx = curx;
lasty = cury;
rate++;
clock_t curclock = clock();
if ( curclock > lastclock + CLOCKS ) {
lastclock = curclock;
lastrate = rate;
rate = 0;
}
}
if ( mouse != nullptr ) { delete mouse; }
if ( pen != nullptr ) { delete pen; }
} else { printf( "Couldn't initialize uDraw\n" ); }
delete wiimote;
Wiimote::CloseWiimoteHandle( handle );
return 0;
}