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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
|
/*
joy2key
This program gets events from the joystick device /dev/js0 and
dispatches X events or console keypresses to a specific window
based on joystick status. It is most useful for adding joystick
support to games that don't nativly support the joystick, using
joy2key to send XKeyPress (or console ioctl()) messages in place of
actually typing movement keys. This code is under the GNU Public
License (see COPYING), you can do whatever you want to it, and if
you do anything really neat, I'd like to see it!
- Peter Amstutz (tetron@interreality.org)
special thanks to Zhivago of EFNet's #c for helping me out
a LOT with the XSendEvent() code...
The latest version of joy2key can be found in
ftp://sunsite.unc.edu/pub/Linux/X11/xutils
or on joy2key's (rather spartan) website,
http://www-unix.oit.umass.edu/~tetron/joy2key
Revision History
----------------
1.0 (12 Aug 1997) - First version.
1.1 (04 Dec 1997) - Thanks to Craig Lamparter
(craiger@hemna.rose.hp.com),
joy2key now has support for four-buttoned
joysticks/gamepads! Also some bugfixes.
1.2 (28 Apr 1998) - Pierre Juhen (pierre.juhen@wanadoo.fr)
fixed a dumb little bug and added the
autorepeat feature!
1.3 (04 Jun 1998) - Frederic Lepied (fred@sugix.frmug.org) added
option to call w/o the window title arg to
select target window interactivly
(gee, why didn't I do that in the first
place?) and detect when the window closed and
exit properly. Also now uses 1.0 joystick
module event-based handling instead of
polling (now _requires_ 1.0+ joystick driver,
which is available for v2.0+ Linux kernels),
and is able to send events to the console
with an ioctl() as well!
Lots of code cleanup and restructuring.
1.4 (17 Aug 1998) - .joy2keyrc implemented, this adds -rcfile and
-config. Bugfix for -dev command. Also
automatic calibration when no -thresh is found,
added -deadzone option.
1.5 (20 Jul 1999) - Patch by Jeremy (jeremy@xxedgexx.com) to look up
windows by their numeric ID when they cannot by
found by name; this makes it possible to use
joy2key with full-screen (DGA) X apps.
Joy2key also now uses autoconf/automake.
1.6 (10 Apr 2001) - Okay, after getting mail about it for
literally years, I've finally fixed the
bug parsing joy2keyrc files... ;-p
1.6.1 (27 Jan 2005) - Just a trivial update to fix the web page
and email addresses.
*/
/* Should be specified in the makefile */
/* #define ENABLE_X */
/* #define ENABLE_CONSOLE */
#include "config.h"
#define JOY2KEY_VERSION VERSION
#define DEFAULT_AUTOREPEAT 5
#define DEFAULT_DEADZONE 50
#define DEFAULT_DEVICE "/dev/js0"
#define DEFAULT_RCFILE ".joy2keyrc" /* located in $(HOME) */
#define PETERS_EMAIL "tetron@interreality.org"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <linux/joystick.h>
#ifdef ENABLE_X
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#endif
int jsfd=-1,
button_actions[256],
axis_actions[256][2],
axis_threshold[256][2], /* 0 = low (down); 1 = hi (up) */
axis_act_counter=0,
button_act_counter=0,
thresh_counter=0,
deadzone=DEFAULT_DEADZONE;
char *device=DEFAULT_DEVICE,
*rcfile=DEFAULT_RCFILE,
button_repeat_flags[256],
common_read=0;
typedef enum {NONE, X, RAWCONSOLE, TERMINAL} target_type;
typedef enum {PRESS, RELEASE} press_or_release_type;
struct itimerval repeat_time;
target_type target=NONE;
#ifndef ENABLE_X
#ifndef ENABLE_CONSOLE
#error No targets defined!
#endif
#endif
#ifdef ENABLE_X
Display *thedisp;
int thewindow=0,
thescreen=0;
static Window RegisterCloseEvent(Display *disp, Window win);
static void CheckIfWindowClosed(Display *disp, Window parentwin, Window win);
#endif
#ifdef ENABLE_CONSOLE
int consolefd;
#endif
void process_args(int argc, char **argv);
int check_config(int argc, char **argv);
int check_device(int argc, char **argv);
void sendkey(int keycode, press_or_release_type PoR);
void cleanup(int s);
void repeat_handler(int s);
void calibrate(int num);
int main(int argc, char **argv)
{
#ifdef ENABLE_X
int parentwin=0;
fd_set close_set;
#endif
FILE *foo;
char string[255],
axis_hold_flags[256],
numaxes,
numbuttons;
struct js_event js;
int i;
struct timeval tv;
puts("joy2key - reads joystick status and dispatches keyboard events");
puts("By Peter Amstutz ("PETERS_EMAIL")");
puts("This is free software under the GNU General Public License (GPL v2)");
puts(" (see COPYING in the joy2key archive)");
puts("You are welcome to use/modify this code, and please e-mail me");
puts("if anything cool comes of it!");
printf("Version: %s Binary built on %s at %s\n\n",
JOY2KEY_VERSION, __DATE__, __TIME__);
memset(axis_threshold, 0, sizeof(axis_threshold));
memset(axis_actions, 0, sizeof(axis_actions));
memset(button_actions, 0, sizeof(button_actions));
memset(button_repeat_flags, 0, sizeof(button_repeat_flags));
memset(axis_hold_flags, 0, sizeof(axis_hold_flags));
repeat_time.it_interval.tv_sec=0;
repeat_time.it_interval.tv_usec=0;
repeat_time.it_value.tv_sec=0;
repeat_time.it_value.tv_usec=0;
argc=check_config(argc, argv);
process_args(argc, argv);
if((jsfd=open(device,O_RDONLY))==-1)
{
printf("Error opening %s!\n", device);
puts("Are you sure you have joystick support in your kernel?");
return 1;
}
if (ioctl(jsfd, JSIOCGAXES, &numaxes)) {
/* acording to the American Heritage Dictionary of the English
Language 'axes' *IS* the correct pluralization of 'axis' */
perror("joy2key: error getting axes");
return 1;
}
if (ioctl(jsfd, JSIOCGBUTTONS, &numbuttons)) {
perror("joy2key: error getting buttons");
return 1;
}
if(numaxes<axis_act_counter) puts("More axes specificed than joystick has!");
if(numbuttons<button_act_counter) puts("More buttons specificed than joystick has!");
switch(target)
{
#ifdef ENABLE_X
case X:
thescreen=DefaultScreen(thedisp);
if (argc == 1 || argv[1][0] == '-') {
{
puts("Please select a window to send events to");
sprintf(string, "xwininfo");
}
} else {
sprintf(string, "xwininfo -name \"%s\"", argv[1]);
}
foo=popen(string, "r");
while(!feof(foo) && fgets(string, 255, foo) &&
(sscanf(string, "xwininfo: Window id: %x", &thewindow) != 1));
pclose(foo);
if(thewindow==0)
{
sprintf(string, "xwininfo -id \"%s\"", argv[1]);
foo=popen(string, "r");
while(!feof(foo) && fgets(string, 255, foo) &&
(sscanf(string, "xwininfo: Window id: %x", &thewindow) != 1));
pclose(foo);
if(thewindow==0)
{
puts("Error! Can't find window to send events to!");
return 1;
}
}
parentwin = RegisterCloseEvent(thedisp, thewindow);
XFlush(thedisp);
break;
#endif
#ifdef ENABLE_CONSOLE
case RAWCONSOLE:
case TERMINAL:
if(argv[1][0]=='/')
{
strcpy(string, argv[1]);
} else {
sprintf(string, "/dev/tty%s", argv[1][0] == '-' ? "" : argv[1]);
}
if((consolefd=open(string, O_RDONLY, 0)) < 0)
{
printf("Can't open %s\n", string);
return 1;
}
break;
#endif
default:
puts("Must specify a target!");
exit(1);
}
for(i=0; i<numaxes; i++)
{
if(!(axis_threshold[i][0] | axis_threshold[i][1]))
calibrate(i);
}
memset(&js, 0, sizeof(struct js_event));
signal(SIGINT, cleanup);
signal(SIGTERM, cleanup);
if(repeat_time.it_interval.tv_usec)
{
signal(SIGALRM, repeat_handler);
repeat_time.it_value.tv_usec=repeat_time.it_interval.tv_usec;
setitimer(ITIMER_REAL, &repeat_time, NULL);
}
puts("Initialization complete, entering main loop, ^C to exit...");
/* Main Loop */
for(;;)
{
memset(&js, 0, sizeof(struct js_event));
#ifdef ENABLE_X
do
{
/*
FD_ZERO(&close_set);
FD_SET(ConnectionNumber(thedisp), &close_set);
FD_SET(jsfd, &close_set);
XFlush(thedisp);
select((ConnectionNumber(thedisp) > jsfd ? ConnectionNumber(thedisp) : jsfd) + 1,
&close_set, NULL, NULL, NULL);
if(FD_ISSET(ConnectionNumber(thedisp), &close_set) && target==X)
if(target==X) CheckIfWindowClosed(thedisp, parentwin, thewindow);
*/
FD_ZERO(&close_set);
FD_SET(jsfd, &close_set);
tv.tv_sec=0;
tv.tv_usec=200000;
select(jsfd + 1, &close_set, NULL, NULL, &tv);
if(target==X) CheckIfWindowClosed(thedisp, parentwin, thewindow);
} while(!FD_ISSET(jsfd, &close_set));
#endif
read(jsfd, &js, sizeof(struct js_event));
switch(js.type)
{
case JS_EVENT_BUTTON: /* buttons */
if(button_actions[js.number])
{
button_repeat_flags[js.number]=js.value;
sendkey(button_actions[js.number],
js.value ? PRESS : RELEASE);
}
break;
case JS_EVENT_AXIS: /* axis */
if(axis_actions[js.number][0] || axis_actions[js.number][1])
{
if(axis_hold_flags[js.number])
{
switch(axis_hold_flags[js.number])
{
case 1:
if(js.value>axis_threshold[js.number][0])
sendkey(axis_actions[js.number][0], RELEASE);
break;
case 2:
if(js.value<axis_threshold[js.number][1])
sendkey(axis_actions[js.number][1], RELEASE);
break;
}
axis_hold_flags[js.number]=0;
}
if(js.value<axis_threshold[js.number][0])
{
if(!axis_actions[js.number][0]) break;
sendkey(axis_actions[js.number][0], PRESS);
axis_hold_flags[js.number]=1;
}
if(js.value>axis_threshold[js.number][1])
{
if(!axis_actions[js.number][1]) break;
sendkey(axis_actions[js.number][1], PRESS);
axis_hold_flags[js.number]=2;
}
}
break;
}
#ifdef ENABLE_X
if(target==X) XFlush(thedisp);
#endif
}
}
int check_config(int argc, char **argv)
{
int i, x, f=0;
FILE *file;
int rcargc;
char *rcargv[255], line[255];
for(i=1; i<argc; i++)
{
if(!strcmp("-rcfile", argv[i]))
{
if(i+2>argc)
{
puts("Not enough arguments to -rcfile");
exit(1);
}
if((file=fopen(argv[i+1], "r"))==NULL)
{
printf("Cannot open rc file \"%s\"\n", argv[i+1]);
exit(1);
}
fscanf(file, " %s ", line);
while(!feof(file))
{
if(!strcmp(line, "COMMON"))
{
fscanf(file, " %s ", line);
for(rcargc=1; strcmp(line, "START") && strcmp(line, "COMMON")
&& !feof(file); rcargc++)
{
if(line[0]=='#')
{
while(fgetc(file)!='\n');
rcargc--;
}
else rcargv[rcargc]=strdup(line);
fscanf(file, " %s ", line);
}
if(feof(file)) rcargv[rcargc]=strdup(line);
process_args(rcargc, rcargv);
for(rcargc--;rcargc-1;rcargc--) free(rcargv[rcargc]);
}
fscanf(file, " %s ", line);
}
fclose(file);
common_read=1;
rcfile=argv[i+1];
argc-=2;
for(x=i; x<argc; x++) argv[x]=argv[x+2];
}
if(!strcmp("-config", argv[i]))
{
if(i+2>argc)
{
puts("Not enough arguments to -config");
exit(1);
}
if(rcfile==DEFAULT_RCFILE)
{
x=strlen(getenv("HOME")) + strlen(rcfile) + 2;
rcfile=(char*)malloc(x);
sprintf(rcfile, "%s/%s", getenv("HOME"), DEFAULT_RCFILE);
}
if((file=fopen(rcfile, "r"))==NULL)
{
printf("Cannot open rc file \"%s\"\n", rcfile);
exit(1);
}
if(!common_read)
{
fscanf(file, " %s ", line);
while(!feof(file))
{
if(!strcmp(line, "COMMON"))
{
fscanf(file, " %s ", line);
for(rcargc=1; strcmp(line, "START") && strcmp(line, "COMMON")
&& !feof(file); rcargc++)
{
if(line[0]=='#')
{
while(fgetc(file)!='\n');
rcargc--;
}
else rcargv[rcargc]=strdup(line);
fscanf(file, " %s ", line);
}
if(feof(file)) rcargv[rcargc]=strdup(line);
/* XXX process_args(rcargc+1, rcargv); */
process_args(rcargc, rcargv);
for(;rcargc-1;rcargc--) free(rcargv[rcargc]);
}
fscanf(file, " %s ", line);
}
rewind(file);
common_read=1;
}
fscanf(file, " %s ", line);
while(!feof(file))
{
if(!strcmp(line, "START"))
{
fscanf(file, " %s ", line);
if(!strcmp(line, argv[i+1]))
{
f=1;
fscanf(file, " %s ", line);
for(rcargc=1; strcmp(line, "START") && strcmp(line, "COMMON")
&& !feof(file); rcargc++)
{
if(line[0]=='#')
{
while(fgetc(file)!='\n');
rcargc--;
}
else rcargv[rcargc]=strdup(line);
fscanf(file, " %s ", line);
}
if(feof(file)) rcargv[rcargc]=strdup(line);
/* XXX process_args(rcargc+1, rcargv); */
process_args(rcargc, rcargv);
for(rcargc--;rcargc-1;rcargc--) free(rcargv[rcargc]);
}
}
fscanf(file, " %s ", line);
}
fclose(file);
if(!f)
{
printf("Can't find config \"%s\"\n", argv[i+1]);
exit(1);
}
argc-=2;
for(x=i; x<argc; x++) argv[x]=argv[x+2];
}
}
return argc;
}
void process_args(int argc, char **argv)
{
int i;
if(!argv[1]) return;
for(i=(argc == 1 || argv[1][0] == '-') ? 1 : 2; i<argc; i++)
{
if(!strcmp(argv[i], "-axis"))
{
if(target==NONE)
{
puts("Must specify a target first!");
exit(1);
}
if(i+1>argc)
{
puts("Not enough arguments to -axis");
exit(1);
}
axis_act_counter=0;
while((i+1)<argc && argv[i+1][0]!='-')
{
if(i+2>argc)
{
puts("Not enough arguments to -axis");
exit(1);
}
i++;
axis_actions[axis_act_counter][0]=argtokey(argv[i]);
axis_actions[axis_act_counter][1]=argtokey(argv[++i]);
axis_act_counter++;
}
continue;
}
if(!strcmp(argv[i], "-buttons"))
{
if(target==NONE)
{
puts("Must specify a target first!");
exit(1);
}
if(i+1>argc)
{
puts("Not enough arguments to -buttons");
exit(1);
}
button_act_counter=0;
while((i+1)<argc && (argv[i+1][0]!='-' ||
(argv[i+1][0]=='-' && !argv[i+1][1])))
{
button_actions[button_act_counter]=argtokey(argv[++i]);
button_act_counter++;
}
continue;
}
if(!strcmp(argv[i], "-thresh"))
{
if(i+1>argc)
{
puts("Not enough arguments to -thresh");
exit(1);
}
while((i+1)<argc &&
(argv[i+1][0]!='-' ||
(argv[i+1][0]=='-' && isdigit(argv[i+1][1]))))
{
i++;
if(i+2>argc)
{
puts("Not enough arguments to -thresh");
exit(1);
}
axis_threshold[thresh_counter][0]=atoi(argv[i]);
axis_threshold[thresh_counter++][1]=atoi(argv[++i]);
}
continue;
}
if(!strcmp(argv[i], "-autorepeat"))
{
if(i+2>argc || (argv[i+1][0]=='-'))
{
repeat_time.it_interval.tv_usec=1000000/DEFAULT_AUTOREPEAT;
continue;
}
if((repeat_time.it_interval.tv_usec=atoi(argv[++i]))==0)
{
puts("Invalid autorepeat frequency, using default");
repeat_time.it_interval.tv_usec=1000000/DEFAULT_AUTOREPEAT;
}
if(repeat_time.it_interval.tv_usec>50)
{
repeat_time.it_interval.tv_usec=1000000/50;
printf("Set to maximum 50 times / second.\n");
}
repeat_time.it_interval.tv_usec=
1000000/repeat_time.it_interval.tv_usec;
continue;
}
if(!strcmp(argv[i], "-deadzone"))
{
if(i+2>argc || (argv[i+1][0]=='-'))
{
puts("Not enough arguments to -deadzone");
exit(1);
continue;
}
if((deadzone=atoi(argv[++i]))==0)
{
puts("Invalid deadzone setting, using default");
deadzone=DEFAULT_DEADZONE;
}
if(deadzone>99)
{
deadzone=99;
puts("Set to maximum 99%");
}
if(deadzone<1)
{
deadzone=1;
puts("Set to minimum 1%");
}
continue;
}
if(!strcmp(argv[i], "-dev"))
{
if(i+2>argc)
{
puts("Not enough arguments to -dev");
exit(1);
}
device=strdup(argv[++i]);
continue;
}
#ifdef ENABLE_CONSOLE
if(!strcmp(argv[i], "-rawconsole"))
{
target=RAWCONSOLE;
continue;
}
if(!strcmp(argv[i], "-terminal"))
{
target=TERMINAL;
continue;
}
#endif
#ifdef ENABLE_X
if(!strcmp(argv[i], "-X"))
{
target=X;
thedisp=XOpenDisplay(NULL);
if(thedisp==NULL)
{
puts("Error opening X Display");
exit(1);
}
continue;
}
#endif
printf("Unknown option %s\n", argv[i]);
puts("Usage: joy2key [\"Window Name\"]");
#ifdef ENABLE_CONSOLE
printf(" [ -rawconsole ]");
printf("\n [ -terminal ]");
#endif
#ifdef ENABLE_X
printf("\n [ -X ]");
#endif
printf("\n [ -axis [(axis0) low hi] [(axis1) low hi] ... ]");
printf("\n [ -thresh [(axis0) low hi] [(axis1) low hi] ... ]");
printf("\n [ -buttons [(button0)] [(button1)] [(button2)] ... ]");
printf("\n [ -dev {%s} ]", DEFAULT_DEVICE);
printf("\n [ -rcfile {%s} ]", DEFAULT_RCFILE);
printf("\n [ -config {no default} ]");
printf("\n [ -autorepeat {(freq) %i} ]", DEFAULT_AUTOREPEAT);
printf("\n [ -deadzone {(percent) %i} ]", DEFAULT_DEADZONE);
puts("\n\nnote: [] denotes `optional' option or argument,");
puts(" () hints at the wanted arguments for options");
puts(" {} denotes default (compiled-in) parameters");
exit(1);
}
}
void calibrate(int num)
{
struct js_event js;
int joymid=0;
printf("\nCalibrating axis %i\n", num);
printf("Please center the axis and press a button.\n?");
fflush(stdout);
do
{
read(jsfd, &js, sizeof(struct js_event));
if(js.type==JS_EVENT_AXIS && js.number==num)
{
joymid=js.value;
printf("\rvalue: %i ", joymid);
fflush(stdout);
}
} while(js.type!=JS_EVENT_BUTTON || (js.type==JS_EVENT_BUTTON && js.value==0));
printf("\nLocked at %i", joymid);
printf("\nPlease move the axis to its lowest position and press a button.\n?");
fflush(stdout);
do
{
read(jsfd, &js, sizeof(struct js_event));
if(js.type==JS_EVENT_AXIS && js.number==num)
{
axis_threshold[num][0]=js.value;
printf("\rvalue: %i ", axis_threshold[num][0]);
fflush(stdout);
}
} while(js.type!=JS_EVENT_BUTTON || (js.type==JS_EVENT_BUTTON && js.value==0));
printf("\nPlease move the axis to its highest position and press a button.\n?");
fflush(stdout);
do
{
read(jsfd, &js, sizeof(struct js_event));
if(js.type==JS_EVENT_AXIS && js.number==num)
{
axis_threshold[num][1]=js.value;
printf("\rvalue: %i ", axis_threshold[num][1]);
fflush(stdout);
}
} while(js.type!=JS_EVENT_BUTTON || (js.type==JS_EVENT_BUTTON && js.value==0));
printf("\nUsing deadzone of %i%%\n", deadzone);
axis_threshold[num][0]=joymid+((axis_threshold[num][0] - joymid) * (deadzone/100.0));
axis_threshold[num][1]=joymid+((axis_threshold[num][1] - joymid) * (deadzone/100.0));
puts("Calibrations set at:");
printf("Axis %i low threshold set at %i\n", num, axis_threshold[num][0]);
printf("Axis %i high threshold set at %i\n", num, axis_threshold[num][1]);
puts("(you can put these in your .joy2keyrc to avoid calibrating in the future)");
}
int argtokey(char *arg)
{
int ret;
switch(target)
{
#ifdef ENABLE_X
case X:
if((ret=XStringToKeysym(arg))==NoSymbol)
printf("Can't find %s, check include/X11/keysymdef.h\n", arg);
ret=XKeysymToKeycode(thedisp, ret);
return ret;
#endif
#ifdef ENABLE_CONSOLE
case RAWCONSOLE:
case TERMINAL:
if(arg[1])
{
sscanf(arg, "%i", &ret);
} else ret=arg[0];
return ret;
#endif
}
}
void repeat_handler(int s)
{
int i;
signal(SIGALRM, SIG_IGN);
for(i=0; i<button_act_counter; i++)
{
if(button_repeat_flags[i])
{
sendkey(button_actions[i], RELEASE);
sendkey(button_actions[i], PRESS);
}
}
#ifdef ENABLE_X
if(target==X) XFlush(thedisp);
#endif
signal(SIGALRM, repeat_handler);
}
void sendkey(int keycode, press_or_release_type PoR)
{
#ifdef ENABLE_X
static XEvent event;
static char needinitxev=1;
#endif
char conkey;
switch(target)
{
#ifdef ENABLE_X
case X:
if(needinitxev)
{
memset(&event, 0, sizeof(event));
event.xkey.type=KeyPress;
event.xkey.root=RootWindow(thedisp, thescreen);
event.xkey.window=thewindow;
event.xkey.subwindow=thewindow;
event.xkey.same_screen=True;
event.xkey.time=CurrentTime;
event.xkey.display=thedisp;
needinitxev=0;
}
event.xkey.type=PoR==PRESS ? KeyPress : KeyRelease;
event.xkey.state=PoR==PRESS ? KeyPressMask : KeyReleaseMask;
event.xkey.keycode=keycode;
XSendEvent(thedisp, thewindow, False, event.xkey.state, &event);
break;
#endif
#ifdef ENABLE_CONSOLE
case RAWCONSOLE:
if(PoR==PRESS)
conkey=keycode;
if(PoR==RELEASE)
conkey=keycode | 0x80;
ioctl(consolefd, TIOCSTI, &conkey);
break;
case TERMINAL:
if(PoR==PRESS)
{
conkey=keycode;
ioctl(consolefd, TIOCSTI, &conkey);
}
#endif
}
}
void cleanup(int s)
{
printf("\n%s caught, cleaning up & quitting.\n",
s==SIGINT ? "SIGINT" :
(s==SIGTERM ? "SIGTERM" : ((s == 0) ? "Window die" : "Unknown")));
/* Because the window has just closed, it will print out an error upon
calling these functions. To suppress this superflous error, don't call
them :) */
/* XFlush(thedisp); */
/* XCloseDisplay(thedisp); */
#ifdef ENABLE_CONSOLE
if(target==RAWCONSOLE || target==TERMINAL) close(consolefd);
#endif
exit(0);
}
#ifdef ENABLE_X
static Window
RegisterCloseEvent(Display *disp,
Window win)
{
Window root_return;
Window parent_return = 0;
Window *children_return;
unsigned int nchildren_return;
XQueryTree(disp, win, &root_return, &parent_return, &children_return,
&nchildren_return);
if (parent_return) {
XSelectInput(disp, parent_return, SubstructureNotifyMask);
}
if (nchildren_return > 0) {
XFree(children_return);
}
return parent_return;
}
static void
CheckIfWindowClosed(Display *disp,
Window parentwin,
Window win)
{
XEvent event;
if (XCheckWindowEvent(disp, parentwin, SubstructureNotifyMask, &event)
== True) {
if (event.type == DestroyNotify && event.xdestroywindow.window
== win) {
cleanup(0);
}
}
}
#endif
|