Joystick should now work.

This commit is contained in:
Pere Pujal i Carabantes 2011-05-27 21:14:15 +00:00
parent 21081427e3
commit 7041da207d
6 changed files with 890 additions and 599 deletions

View file

@ -8,7 +8,8 @@ http://www.tuxpaint.org/
$Id$
2011.May.25 (0.9.22)
2011.May.27 (0.9.22)
* New Tools:
----------
* Label - A tool to add text to a drawing, which can be modified or
@ -112,7 +113,23 @@ $Id$
via config.
* ASDW is QWERTY-centric; drop it, or add ways to support other
keyboard layouts.
* Who wrote this, so we can credit!?!?
* Joystick can be used to drive Tuxpaint
by Ankit Choudary <ankit.goaldecided@gmail.com> (GSOC 2010)
with integration and fixes by Pere Pujal i Carabantes
* Uses the first joystick found on the system, so should work out of the box
* Uses any of the buttons found in the joystick, no need for configuration.
* The hat of the joystick moves one pixel at a time, usefull to carefully place the pointer.
* The ball of the joystick should also trigger pointer motion. FIXME: This should work but is not tested.
* The responsivity of the joystick can be configured via command line or config files:
* --joystick-slownes sets a delay at each axis motion event.
Allowed values from 0 to 500, defaults to 15.
* --joystick-threshold sets the minimum value of axis motion to begin move the pointer.
Allowed values from 0 to 32766, defaults to 3200.
* --joystick_maxsteps sets the maximum number of pixels that the pointer will move at a time.
Allowed values from 1 to 7, defaults to 7.
* Magic Tool Improvememnts:
--------------------------
@ -228,6 +245,12 @@ $Id$
Albert Cahalan <albert@users.sourceforge.net>,
Bill Kendrick <bill@newbreedsoftware.com>
* Packaging all the metadata in the PNG file. Before a draw based on a starter
would have need 3 files: the draw, the starter and the .dat file, whith
the addition of the Labels tool this increased to 5 files. Now all this stuff
is packed in customs chunks inside the PNG file.
* New Starters:
-------------
* Elephant

View file

@ -378,6 +378,18 @@ Windows Users
Presents a clickable on-screen keyboard when using the Text
and Label tools.
joystick-slowness=number
Sets a delay at each axis motion, allowing to slow the joystick.
Allowed values are 0 to 500. Default value is 15.
joystick-threshold
Sets the minimum level of axis motion to start moving the pointer.
Allowed values are from 0 to 32766. Default value is 3200.
joystick-maxsteps
Sets the maximum pixels the pointer will move at once.
Allowed values are from 1 to 7. Default value is 7.
stampsize=SIZE
Use this option to force Tux Paint to set the starting size of
@ -760,8 +772,7 @@ Windows Users
--noquit
--noprint
--printdelay=SECONDS
--printcfg
--altprintnever
--printcfg --altprintnever
--altprintalways
--papersize=PAPERSIZE
--nolockfile
@ -780,6 +791,9 @@ Windows Users
--nolabel
--mouse-accessibility
--onscreen-keyboard
--joystick-slowness
--joystick-threshold
--joystick-maxsteps
--sysfonts
--alllocalefonts
--mirrorstamps

View file

@ -155,6 +155,9 @@ windowed, NEGBOOL(fullscreen)
windowsize, MULTI(parsertmp_windowsize)
mouse-accessibility, POSBOOL(mouseaccessibility)
onscreen-keyboard, POSBOOL(onscreen_keyboard)
joystick-slowness, MULTI(joystick_slowness)
joystick-threshold, MULTI(joystick_lowthreshold)
joystick-maxsteps, MULTI(joystick_maxsteps)
%%
void parse_one_option(struct cfginfo *restrict tmpcfg, const char *str, const char *opt, const char *restrict src)

View file

@ -53,6 +53,9 @@ struct cfginfo
const char *wheely;
const char *mouseaccessibility;
const char *onscreen_keyboard;
const char *joystick_slowness;
const char *joystick_lowthreshold;
const char *joystick_maxsteps;
};
#define CFGINFO_MAXOFFSET (sizeof(struct cfginfo))

View file

@ -62,6 +62,9 @@ _tuxpaint()
--nolockfile \
--mouse-accessibility \
--onscreen-keyboard \
--joystick-slowness \
--joystick-threshold \
--joystick-maxsteps \
--colorfile' -- $cur ) )
# We don't accept filenames on the command-line yet -bjk 2009.09.09
# else

View file

@ -973,6 +973,11 @@ static void update_canvas(int x1, int y1, int x2, int y2)
static int emulate_button_pressed = 0;
static int mouseaccessibility = 0;
static int onscreen_keyboard = 0;
static int joystick_low_threshold = 3200;
static int joystick_slowness = 15;
static int joystick_maxsteps = 7;
static int oldpos_x;
static int oldpos_y;
static int disable_screensaver;
#ifdef NOKIA_770
static int fullscreen = 1;
@ -1105,6 +1110,10 @@ static void set_label_fonts(void);
static void tmp_apply_uncommited_text(void);
static void undo_tmp_applied_text(void);
static void handle_joyaxismotion(SDL_Event event, int *motioner, int *val_x, int *val_y);
static void handle_joyhatmotion(SDL_Event event, int oldpos_x, int oldpos_y);
static void handle_joyballmotion(SDL_Event event, int oldpos_x, int oldpos_y);
static void handle_joybuttonupdown(SDL_Event event, int oldpos_x, int oldpos_y);
/* Magic tools API and tool handles: */
@ -3127,94 +3136,18 @@ static void mainloop(void)
}
}
}
else if (event.type == SDL_JOYAXISMOTION)
{
motioner = event.jaxis.value;
if ( ( event.jaxis.value < -3200 ) || (event.jaxis.value > 3200 ))
{
val_x = (int)((SDL_JoystickGetAxis(joystick, 0) / 32768.0f) * 3);
val_y = (int)((SDL_JoystickGetAxis(joystick, 1) / 32768.0f) * 3);
// printf ("\n value : %d , %d, %d, %d\n",event.jaxis.value, event.jaxis.axis, val_x, val_y);
old_x += val_x;
old_y += val_y;
new_x = old_x + button_w * 2;
new_y = old_y;
SDL_WarpMouse(old_x + button_w * 2, old_y);
update_canvas(0, 0, WINDOW_WIDTH - 96, (48 * 7) + 40 + HEIGHTOFFSET);
}
}
handle_joyaxismotion(event, &motioner, &val_x, &val_y);
else if (motioner == -32768)
{
// printf ("\n values are : %d , %d\n",val_x, val_y);
old_x += val_x;
old_y += val_y;
new_x = old_x + button_w * 2;
new_y = old_y;
SDL_WarpMouse(old_x + button_w * 2, old_y);
update_canvas(0, 0, WINDOW_WIDTH - 96, (48 * 7) + 40 + HEIGHTOFFSET);
}
else if (motioner == 32767)
{
old_x += val_x;
old_y += val_y;
new_x = old_x + button_w * 2;
new_y = old_y;
SDL_WarpMouse(old_x + button_w * 2, old_y);
update_canvas(0, 0, WINDOW_WIDTH - 96, (48 * 7) + 40 + HEIGHTOFFSET);
}
else if (event.type == SDL_JOYHATMOTION)
handle_joyhatmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBALLMOTION)
{
if ( event.jball.ball == 0 )
{
printf("\n ball movement \n");
val_x = event.jball.xrel;
val_y = event.jball.yrel;
old_x += val_x;
old_y += val_y;
new_x = old_x + button_w * 2;
new_y = old_y;
SDL_WarpMouse(old_x + button_w * 2, old_y);
update_canvas(0, 0, WINDOW_WIDTH - 96, (48 * 7) + 40 + HEIGHTOFFSET);
}
}
handle_joyballmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBUTTONDOWN)
{
// printf("\n button num : %d \n", event.jbutton.button);
if (event.jbutton.button == 0)
{
// printf("\n button pressed \n");
ev.type = SDL_MOUSEBUTTONDOWN;
ev.button.which = 0;
ev.button.state = SDL_PRESSED;
ev.button.x = old_x + button_w * 2;
ev.button.y = old_y;
ev.button.button = SDL_BUTTON_LEFT;
SDL_PushEvent(&ev);
playsound(screen, 1, SND_CLICK, 0, SNDPOS_LEFT, SNDDIST_NEAR);
update_screen(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
}
}
else if (event.type == SDL_JOYBUTTONUP)
{
// printf("\n button num : %d \n", event.jbutton.button);
if (event.jbutton.button == 0)
{
// printf("\n button released \n");
ev.type = SDL_MOUSEBUTTONUP;
ev.button.which = 0;
ev.button.state = SDL_RELEASED;
ev.button.button = SDL_BUTTON_LEFT;
SDL_PushEvent(&ev);
update_screen(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
}
}
else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP)
handle_joybuttonupdown(event, oldpos_x, oldpos_y);
else if (event.type == SDL_MOUSEBUTTONDOWN &&
event.button.button >= 2 &&
@ -5086,7 +5019,6 @@ static void mainloop(void)
}
}
}
button_down = 0;
}
else if (event.type == SDL_MOUSEMOTION && !ignoring_motion)
@ -5094,6 +5026,9 @@ static void mainloop(void)
new_x = event.button.x - r_canvas.x;
new_y = event.button.y - r_canvas.y;
oldpos_x = event.motion.x;
oldpos_y = event.motion.y;
if (keybd_flag == 1)
{
// uistate.mousedown = 1;
@ -5506,6 +5441,8 @@ static void mainloop(void)
old_x = new_x;
old_y = new_y;
oldpos_x = event.button.x;
oldpos_y = event.button.y;
}
}
@ -5523,6 +5460,13 @@ static void mainloop(void)
}
}
if (motioner)
{
SDL_WarpMouse(oldpos_x + val_x, oldpos_y + val_y);
if (joystick_slowness)
SDL_Delay(joystick_slowness);
}
SDL_Delay(1);
}
while (!done);
@ -6492,6 +6436,11 @@ void show_usage(int exitcode)
" %s [--nosysconfig]\n"
" %s [--nolockfile]\n"
" %s [--colorfile FILE]\n"
" %s [--mouse-accessibility]\n"
" %s [--onscreen-keyboard]\n"
" %s [--joystick-slowness] (0-500). Default value is 15\n"
" %s [--joystick-threshold] (0-32766). Default value is 3200\n"
" %s [--joystick-maxsteps] (1-7). Default value is 7\n"
"\n",
progname, progname,
blank, blank, blank, blank,
@ -6507,7 +6456,7 @@ void show_usage(int exitcode)
#if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__) && !defined(__HAIKU__)
blank,
#endif
blank, blank, blank, blank, blank);
blank, blank, blank, blank, blank, blank, blank, blank, blank, blank);
free(blank);
}
@ -11683,7 +11632,9 @@ static int do_prompt_image_flash_snd(const char *const text,
SDL_Surface *img1b;
int free_img1b;
int txt_left, txt_right, img_left, btn_left, txt_btn_left, txt_btn_right;
int val_x, val_y, motioner;
val_x = val_y = motioner = 0;
emulate_button_pressed = 0;
hide_blinking_cursor();
@ -12025,10 +11976,31 @@ static int do_prompt_image_flash_snd(const char *const text,
{
do_setcursor(cursor_arrow);
}
}
oldpos_x = event.button.x;
oldpos_y = event.button.y;
}
SDL_Delay(100);
else if (event.type == SDL_JOYAXISMOTION)
handle_joyaxismotion(event, &motioner, &val_x, &val_y);
else if (event.type == SDL_JOYHATMOTION)
handle_joyhatmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBALLMOTION)
handle_joyballmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBUTTONDOWN)
handle_joybuttonupdown(event, oldpos_x, oldpos_y);
}
if (motioner)
{
if (joystick_slowness)
SDL_Delay(joystick_slowness);
SDL_WarpMouse(oldpos_x + val_x, oldpos_y + val_y);
}
SDL_Delay(10);
if (animate)
{
@ -13552,6 +13524,7 @@ static int do_quit(int tool)
draw_tux_text(TUX_BORED, "", 0);
cur_tool = tmp_tool;
}
if (done)
SDL_JoystickClose(joystick);
return (done);
}
@ -13596,6 +13569,9 @@ static int do_open(void)
int last_click_which, last_click_button;
int places_to_look;
int opened_something;
int val_x, val_y, motioner;
val_x = val_y = motioner = 0;
opened_something = 0;
@ -14080,9 +14056,8 @@ static int do_open(void)
update_list = 0;
}
SDL_WaitEvent(&event);
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
done = 1;
@ -14388,8 +14363,30 @@ static int do_open(void)
do_setcursor(cursor_arrow);
}
oldpos_x = event.button.x;
oldpos_y = event.button.y;
}
else if (event.type == SDL_JOYAXISMOTION)
handle_joyaxismotion(event, &motioner, &val_x, &val_y);
else if (event.type == SDL_JOYHATMOTION)
handle_joyhatmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBALLMOTION)
handle_joyballmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP)
handle_joybuttonupdown(event, oldpos_x, oldpos_y);
}
if (motioner)
{
SDL_WarpMouse(oldpos_x + val_x, oldpos_y + val_y);
if (joystick_slowness)
SDL_Delay(joystick_slowness);
}
SDL_Delay(10);
if (want_erase)
{
@ -14503,7 +14500,6 @@ static int do_open(void)
update_list = 1;
}
}
}
while (!done);
@ -14684,6 +14680,9 @@ static int do_slideshow(void)
float x_per, y_per;
int xx, yy;
SDL_Surface *btn, *blnk;
int val_x, val_y, motioner;
val_x = val_y = motioner = 0;
do_setcursor(cursor_watch);
@ -15099,9 +15098,10 @@ static int do_slideshow(void)
update_list = 0;
}
SDL_WaitEvent(&event);
/* Was a call to SDL_WaitEvent(&event); before,
changed to this while loop in order to get joystick working */
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
done = 1;
@ -15252,8 +15252,7 @@ static int do_slideshow(void)
draw_none();
/* Instructions for Slideshow file dialog (FIXME: Make a #define) */
freeme = textdir(gettext_noop("Choose the pictures you want, "
"then click “Play”."));
freeme = textdir(gettext_noop("Choose the pictures you want, " "then click “Play”."));
draw_tux_text(TUX_BORED, freeme, 1);
free(freeme);
@ -15283,8 +15282,7 @@ static int do_slideshow(void)
if (control_sound != -1)
{
playsound(screen, 0, control_sound, 0, SNDPOS_CENTER,
SNDDIST_NEAR);
playsound(screen, 0, control_sound, 0, SNDPOS_CENTER, SNDDIST_NEAR);
update_list = 1;
}
@ -15381,7 +15379,29 @@ static int do_slideshow(void)
do_setcursor(cursor_arrow);
}
oldpos_x = event.button.x;
oldpos_y = event.button.y;
}
else if (event.type == SDL_JOYAXISMOTION)
handle_joyaxismotion(event, &motioner, &val_x, &val_y);
else if (event.type == SDL_JOYHATMOTION)
handle_joyhatmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBALLMOTION)
handle_joyballmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP)
handle_joybuttonupdown(event, oldpos_x, oldpos_y);
}
if (motioner)
{
if (joystick_slowness)
SDL_Delay(joystick_slowness);
SDL_WarpMouse(oldpos_x + val_x, oldpos_y + val_y);
}
SDL_Delay(10);
}
while (!done);
@ -15413,6 +15433,7 @@ static void play_slideshow(int * selected, int num_selected, char * dirname,
char **d_names, char **d_exts, int speed)
{
int i, which, next, done;
int val_x, val_y, motioner;
SDL_Surface * img;
char * tmp_starter_id, * tmp_template_id, * tmp_file_id;
int tmp_starter_mirrored, tmp_starter_flipped, tmp_starter_personal;
@ -15422,6 +15443,7 @@ static void play_slideshow(int * selected, int num_selected, char * dirname,
SDL_Rect dest;
Uint32 last_ticks;
val_x = val_y = motioner = 0;
/* Back up the current image's IDs, because they will get
clobbered below! */
@ -15597,10 +15619,32 @@ static void play_slideshow(int * selected, int num_selected, char * dirname,
do_setcursor(cursor_tiny);
}
}
oldpos_x = event.button.x;
oldpos_y = event.button.y;
}
SDL_Delay(100);
else if (event.type == SDL_JOYAXISMOTION)
handle_joyaxismotion(event, &motioner, &val_x, &val_y);
else if (event.type == SDL_JOYHATMOTION)
handle_joyhatmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBALLMOTION)
handle_joyballmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP)
handle_joybuttonupdown(event, oldpos_x, oldpos_y);
}
if (motioner)
{
SDL_WarpMouse(oldpos_x + val_x, oldpos_y + val_y);
if (joystick_slowness)
SDL_Delay(joystick_slowness);
}
SDL_Delay(10);
/* Automatically skip to the next one after time expires: */
@ -17966,7 +18010,9 @@ static int do_new_dialog(void)
int added;
Uint8 r, g, b;
int white_in_palette;
int val_x, val_y, motioner;
val_x = val_y = motioner = 0;
do_setcursor(cursor_watch);
@ -18560,9 +18606,10 @@ static int do_new_dialog(void)
update_list = 0;
}
SDL_WaitEvent(&event);
/* Was a call to SDL_WaitEvent(&event); before,
changed to this while loop in order to get joystick working */
while(SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
done = 1;
@ -18833,7 +18880,30 @@ static int do_new_dialog(void)
do_setcursor(cursor_arrow);
}
oldpos_x = event.button.x;
oldpos_y = event.button.y;
}
else if (event.type == SDL_JOYAXISMOTION)
handle_joyaxismotion(event, &motioner, &val_x, &val_y);
else if (event.type == SDL_JOYHATMOTION)
handle_joyhatmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBALLMOTION)
handle_joyballmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP)
handle_joybuttonupdown(event, oldpos_x, oldpos_y);
}
if (motioner)
{
SDL_WarpMouse(oldpos_x + val_x, oldpos_y + val_y);
if (joystick_slowness)
SDL_Delay(joystick_slowness);
}
SDL_Delay(10);
}
while (!done);
@ -19114,6 +19184,7 @@ static int do_color_picker(void)
SDL_Rect dest;
int x, y, w;
int ox, oy, oox, ooy, nx, ny;
int val_x, val_y, motioner;
SDL_Surface * tmp_btn_up, * tmp_btn_down;
Uint32(*getpixel_tmp_btn_up) (SDL_Surface *, int, int);
Uint32(*getpixel_tmp_btn_down) (SDL_Surface *, int, int);
@ -19129,6 +19200,7 @@ static int do_color_picker(void)
SDL_Rect color_example_dest;
SDL_Surface * backup;
val_x = val_y = motioner = 0;
hide_blinking_cursor();
@ -19315,6 +19387,7 @@ static int do_color_picker(void)
done = 0;
chose = 0;
x = y = 0;
SDL_WarpMouse(back_left + button_w / 2, back_top - button_w / 2);
do
{
@ -19434,7 +19507,28 @@ static int do_color_picker(void)
else
do_setcursor(cursor_arrow);
}
oldpos_x = event.motion.x;
oldpos_y = event.motion.y;
}
else if (event.type == SDL_JOYAXISMOTION)
handle_joyaxismotion(event, &motioner, &val_x, &val_y);
else if (event.type == SDL_JOYHATMOTION)
handle_joyhatmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBALLMOTION)
handle_joyballmotion(event, oldpos_x, oldpos_y);
else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP)
handle_joybuttonupdown(event, oldpos_x, oldpos_y);
}
if (motioner)
{
if (joystick_slowness)
SDL_Delay(joystick_slowness);
SDL_WarpMouse(oldpos_x + val_x, oldpos_y + val_y);
}
SDL_Delay(10);
@ -21349,6 +21443,35 @@ static void setup_config(char *argv[])
if(tmpcfg.papersize)
papersize = tmpcfg.papersize;
#endif
if(tmpcfg.joystick_slowness)
{
if(strtof(tmpcfg.joystick_slowness, NULL) < 0 || strtof(tmpcfg.joystick_slowness, NULL) > 500)
{
printf("Joystick slowness (now %s) must be between 0 and 500.\n", tmpcfg.joystick_slowness);
exit(1);
}
joystick_slowness = strtof(tmpcfg.joystick_slowness, NULL);
}
if(tmpcfg.joystick_lowthreshold)
{
if (strtof(tmpcfg.joystick_lowthreshold, NULL) < 0 || strtof(tmpcfg.joystick_lowthreshold, NULL) > 32766)
{
/* FIXME: Find better exit code */
printf("Joystick lower threshold (now %s) must be between 0 and 32766", tmpcfg.joystick_lowthreshold);
exit(1);
}
joystick_low_threshold = strtof(tmpcfg.joystick_lowthreshold, NULL);
}
if(tmpcfg.joystick_maxsteps)
{
if (strtof(tmpcfg.joystick_maxsteps, NULL) < 1 || strtof(tmpcfg.joystick_maxsteps, NULL) > 7)
{
/* FIXME: Find better exit code */
printf("Joystick lower threshold (now %s) must be between 1 and 7", tmpcfg.joystick_maxsteps);
exit(1);
}
joystick_maxsteps = strtof(tmpcfg.joystick_maxsteps, NULL);
}
}
@ -21630,6 +21753,7 @@ int TP_EventFilter(const SDL_Event * event)
event->type == SDL_ACTIVEEVENT ||
event->type == SDL_JOYAXISMOTION ||
event->type == SDL_JOYBALLMOTION ||
event->type == SDL_JOYHATMOTION ||
event->type == SDL_JOYBUTTONDOWN ||
event->type == SDL_JOYBUTTONUP ||
event->type == SDL_KEYDOWN ||
@ -22659,6 +22783,10 @@ static void claim_to_be_ready(void)
mouse_x = WINDOW_WIDTH / 2;
mouse_y = WINDOW_HEIGHT / 2;
oldpos_x = mouse_x;
oldpos_y = mouse_y;
SDL_WarpMouse(mouse_x, mouse_y);
mousekey_up = SDL_KEYUP;
@ -22761,8 +22889,9 @@ int main(int argc, char *argv[])
printf(" %s\n", SDL_JoystickName(i));
}
SDL_JoystickEventState(SDL_ENABLE);
joystick = SDL_JoystickOpen(0);
SDL_JoystickEventState(SDL_ENABLE);
printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joystick));
printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joystick));
printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joystick));
@ -23968,3 +24097,119 @@ int file_exists(char * path) {
return(res == 0);
}
/* Don't move the mouse here as this is only called when an event triggers it
and the joystick can be holded withouth sending any event. */
static void handle_joyaxismotion(SDL_Event event, int *motioner, int *val_x, int *val_y) {
int i, j, step;
if (event.jaxis.which != 0)
return;
i = SDL_JoystickGetAxis(joystick, 0);
j = SDL_JoystickGetAxis(joystick, 1);
step = 5000;
if (abs(i) < joystick_low_threshold && abs(j) < joystick_low_threshold)
*motioner = FALSE;
else
{
if (i > joystick_low_threshold)
*val_x = min((i - joystick_low_threshold) / step + 1, joystick_maxsteps);
else if (i < -joystick_low_threshold)
*val_x = max((i + joystick_low_threshold) / step - 1, -joystick_maxsteps);
else
*val_x = 0;
if (j > joystick_low_threshold)
*val_y = min((j - joystick_low_threshold) / step + 1, joystick_maxsteps);
else if (j < -joystick_low_threshold)
*val_y = max((j + joystick_low_threshold) / step - 1, -joystick_maxsteps);
else
*val_y = 0;
// printf("i %d valx %d j %d val_y %d\n", i, val_x, j, val_y);
if (*val_x || *val_y)
{
*motioner = TRUE;
}
else
*motioner = FALSE;
}
}
static void handle_joyhatmotion(SDL_Event event, int oldpos_x, int oldpos_y) {
int val_x, val_y;
val_x = val_y = 0;
switch (event.jhat.value) {
case SDL_HAT_CENTERED:
val_x = 0;
val_y = 0;
break;
case SDL_HAT_UP:
val_x = 0;
val_y = -1;
break;
case SDL_HAT_RIGHTUP:
val_x = 1;
val_y = -1;
break;
case SDL_HAT_RIGHT:
val_x = 1;
val_y = 0;
break;
case SDL_HAT_RIGHTDOWN:
val_x = 1;
val_y = 1;
break;
case SDL_HAT_DOWN:
val_x = 0;
val_y = 1;
break;
case SDL_HAT_LEFTDOWN:
val_x = -1;
val_y = 1;
break;
case SDL_HAT_LEFT:
val_x = -1;
val_y = 0;
break;
case SDL_HAT_LEFTUP:
val_x = -1;
val_y = -1;
break;
}
if(val_x || val_y)
SDL_WarpMouse(oldpos_x + val_x, oldpos_y + val_y);
}
static void handle_joyballmotion(SDL_Event event, int oldpos_x, int oldpos_y) {
int val_x, val_y;
/* FIXME: NOT TESTED Should this act like handle_joyaxismotion?
in the sense of setting the values for the moving but don't move the mouse here? */
/* printf("\n ball movement \n"); */
val_x = event.jball.xrel;
val_y = event.jball.yrel;
SDL_WarpMouse(oldpos_x + val_x, oldpos_y + val_y);
}
static void handle_joybuttonupdown(SDL_Event event, int oldpos_x, int oldpos_y) {
SDL_Event ev;
ev.button.x = oldpos_x;
ev.button.y = oldpos_y;
ev.button.button = SDL_BUTTON_LEFT;
if (event.type == SDL_JOYBUTTONDOWN)
{
ev.button.type = SDL_MOUSEBUTTONDOWN;
ev.button.state = SDL_PRESSED;
}
else
{
ev.button.type = SDL_MOUSEBUTTONUP;
ev.button.state = SDL_RELEASED;
}
SDL_PushEvent(&ev);
}