156 lines
4.9 KiB
C
156 lines
4.9 KiB
C
/*
|
|
* Released into the public domain
|
|
* by Aki Goto <tyatsumi@gmail.com>
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include "gamepad.h"
|
|
|
|
void Button_initialize(struct Button *this, int id)
|
|
{
|
|
this->id = id;
|
|
this->type = ButtonType_BUTTON;
|
|
this->number = -1;
|
|
this->shift = NULL;
|
|
this->control = NULL;
|
|
this->alt = NULL;
|
|
this->prev = 0;
|
|
this->current = 0;
|
|
}
|
|
|
|
void Button_setDevice(struct Button *this, int type, int number)
|
|
{
|
|
this->type = type;
|
|
this->number = number;
|
|
}
|
|
|
|
static void update(struct Button *this, int state)
|
|
{
|
|
this->prev = this->current;
|
|
this->current = state;
|
|
}
|
|
|
|
int Button_update(struct Button *this, struct js_event *event)
|
|
{
|
|
if (event->type == JS_EVENT_BUTTON) {
|
|
if (this->type == ButtonType_BUTTON) {
|
|
if (event->number != this->number) {
|
|
return 0;
|
|
}
|
|
if (event->value) {
|
|
update(this, 1);
|
|
} else {
|
|
update(this, 0);
|
|
}
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
} else if (event->type == JS_EVENT_AXIS) {
|
|
if (this->type == ButtonType_POSITIVE_AXIS) {
|
|
if (event->number != this->number) {
|
|
return 0;
|
|
}
|
|
if (event->value >= 16384) {
|
|
update(this, 1);
|
|
} else {
|
|
update(this, 0);
|
|
}
|
|
return 1;
|
|
} else if (this->type == ButtonType_NEGATIVE_AXIS) {
|
|
if (event->number != this->number) {
|
|
return 0;
|
|
}
|
|
if (event->value <= -16384) {
|
|
update(this, 1);
|
|
} else {
|
|
update(this, 0);
|
|
}
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
int Button_isDown(struct Button *this)
|
|
{
|
|
if (!this->prev && this->current) {
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
int Button_isUp(struct Button *this)
|
|
{
|
|
if (this->prev && !this->current) {
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
int Button_isHolded(struct Button *this)
|
|
{
|
|
return this->current;
|
|
}
|
|
|
|
void Gamepad_initialize(struct Gamepad *this)
|
|
{
|
|
Button_initialize(&this->buttons[ButtonId_UP], ButtonId_UP);
|
|
Button_initialize(&this->buttons[ButtonId_DOWN], ButtonId_DOWN);
|
|
Button_initialize(&this->buttons[ButtonId_LEFT], ButtonId_LEFT);
|
|
Button_initialize(&this->buttons[ButtonId_RIGHT], ButtonId_RIGHT);
|
|
Button_initialize(&this->buttons[ButtonId_A], ButtonId_A);
|
|
Button_initialize(&this->buttons[ButtonId_B], ButtonId_B);
|
|
Button_initialize(&this->buttons[ButtonId_X], ButtonId_X);
|
|
Button_initialize(&this->buttons[ButtonId_Y], ButtonId_Y);
|
|
Button_initialize(&this->buttons[ButtonId_L1], ButtonId_L1);
|
|
Button_initialize(&this->buttons[ButtonId_R1], ButtonId_R1);
|
|
Button_initialize(&this->buttons[ButtonId_L2], ButtonId_L2);
|
|
Button_initialize(&this->buttons[ButtonId_R2], ButtonId_R2);
|
|
|
|
this->buttons[ButtonId_UP].shift = &this->buttons[ButtonId_X];
|
|
this->buttons[ButtonId_DOWN].shift = &this->buttons[ButtonId_X];
|
|
this->buttons[ButtonId_LEFT].shift = &this->buttons[ButtonId_X];
|
|
this->buttons[ButtonId_RIGHT].shift = &this->buttons[ButtonId_X];
|
|
this->buttons[ButtonId_A].shift = &this->buttons[ButtonId_DOWN];
|
|
this->buttons[ButtonId_B].shift = &this->buttons[ButtonId_DOWN];
|
|
this->buttons[ButtonId_X].shift = &this->buttons[ButtonId_DOWN];
|
|
this->buttons[ButtonId_Y].shift = &this->buttons[ButtonId_DOWN];
|
|
|
|
this->buttons[ButtonId_UP].control = &this->buttons[ButtonId_Y];
|
|
this->buttons[ButtonId_DOWN].control = &this->buttons[ButtonId_Y];
|
|
this->buttons[ButtonId_LEFT].control = &this->buttons[ButtonId_Y];
|
|
this->buttons[ButtonId_RIGHT].control = &this->buttons[ButtonId_Y];
|
|
this->buttons[ButtonId_A].control = &this->buttons[ButtonId_LEFT];
|
|
this->buttons[ButtonId_B].control = &this->buttons[ButtonId_LEFT];
|
|
this->buttons[ButtonId_X].control = &this->buttons[ButtonId_LEFT];
|
|
this->buttons[ButtonId_Y].control = &this->buttons[ButtonId_LEFT];
|
|
|
|
this->buttons[ButtonId_UP].alt = &this->buttons[ButtonId_A];
|
|
this->buttons[ButtonId_DOWN].alt = &this->buttons[ButtonId_A];
|
|
this->buttons[ButtonId_LEFT].alt = &this->buttons[ButtonId_A];
|
|
this->buttons[ButtonId_RIGHT].alt = &this->buttons[ButtonId_A];
|
|
this->buttons[ButtonId_A].alt = &this->buttons[ButtonId_RIGHT];
|
|
this->buttons[ButtonId_B].alt = &this->buttons[ButtonId_RIGHT];
|
|
this->buttons[ButtonId_X].alt = &this->buttons[ButtonId_RIGHT];
|
|
this->buttons[ButtonId_Y].alt = &this->buttons[ButtonId_RIGHT];
|
|
|
|
Button_setDevice(&this->buttons[ButtonId_UP], ButtonType_NEGATIVE_AXIS, 1);
|
|
Button_setDevice(&this->buttons[ButtonId_DOWN], ButtonType_POSITIVE_AXIS, 1);
|
|
Button_setDevice(&this->buttons[ButtonId_LEFT], ButtonType_NEGATIVE_AXIS, 0);
|
|
Button_setDevice(&this->buttons[ButtonId_RIGHT], ButtonType_POSITIVE_AXIS, 0);
|
|
Button_setDevice(&this->buttons[ButtonId_A], ButtonType_BUTTON, 0);
|
|
Button_setDevice(&this->buttons[ButtonId_B], ButtonType_BUTTON, 1);
|
|
Button_setDevice(&this->buttons[ButtonId_X], ButtonType_BUTTON, 2);
|
|
Button_setDevice(&this->buttons[ButtonId_Y], ButtonType_BUTTON, 3);
|
|
Button_setDevice(&this->buttons[ButtonId_L1], ButtonType_BUTTON, 4);
|
|
Button_setDevice(&this->buttons[ButtonId_R1], ButtonType_BUTTON, 5);
|
|
Button_setDevice(&this->buttons[ButtonId_L2], ButtonType_BUTTON, 6);
|
|
Button_setDevice(&this->buttons[ButtonId_R2], ButtonType_BUTTON, 7);
|
|
}
|