Skip to content

Commit

Permalink
vita: Little refactor mouse control
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m3vilurr committed Aug 22, 2016
1 parent 90e9992 commit b218897
Showing 1 changed file with 88 additions and 79 deletions.
167 changes: 88 additions & 79 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,61 +96,69 @@ static short a2m(unsigned char in) {
#define lerp(value, from_max, to_max) ((((value*10) * (to_max*10))/(from_max*10))/10)

static bool TOUCH(SceTouchData scr, int lx, int ly, int rx, int ry) {
for (int i = 0; i < scr.reportNum; i++) {
int x = lerp(scr.report[i].x, 1919, 960);
int y = lerp(scr.report[i].y, 1087, 544);
if (x < lx || x > rx || y < ly || y > ry) continue;
return true;
}
return false;
for (int i = 0; i < scr.reportNum; i++) {
int x = lerp(scr.report[i].x, 1919, 960);
int y = lerp(scr.report[i].y, 1087, 544);
if (x < lx || x > rx || y < ly || y > ry) continue;
return true;
}
return false;
}

#define TOUCH_BTN(scr, lx, ly, rx, ry, y) \
if (TOUCH((scr), (lx), (ly), (rx), (ry))) \
btn |= y

#define TOUCH_DELAY 100000 // 100ms
#define MOUSE_ACTION_DELAY 100000 // 100ms

static bool mouse_click(short finger_count, bool press) {
int mode;
int mode;

if (press) {
mode = BUTTON_ACTION_PRESS;
} else {
mode = BUTTON_ACTION_RELEASE;
}
if (press) {
mode = BUTTON_ACTION_PRESS;
} else {
mode = BUTTON_ACTION_RELEASE;
}

switch (finger_count) {
case 1:
LiSendMouseButtonEvent(mode, BUTTON_LEFT);
return true;
case 2:
LiSendMouseButtonEvent(mode, BUTTON_RIGHT);
return true;
}
return false;
switch (finger_count) {
case 1:
LiSendMouseButtonEvent(mode, BUTTON_LEFT);
return true;
case 2:
LiSendMouseButtonEvent(mode, BUTTON_RIGHT);
return true;
}
return false;
}

static void move_mouse(SceTouchData old, SceTouchData cur) {
int delta_x = (cur.report[0].x - old.report[0].x) / 2;
int delta_y = (cur.report[0].y - old.report[0].y) / 2;
int delta_x = (cur.report[0].x - old.report[0].x) / 2;
int delta_y = (cur.report[0].y - old.report[0].y) / 2;

if (!delta_x && !delta_y) {
return;
}
LiSendMouseMoveEvent(delta_x, delta_y);
if (!delta_x && !delta_y) {
return;
}
LiSendMouseMoveEvent(delta_x, delta_y);
}

static void move_wheel(SceTouchData old, SceTouchData cur) {
int old_y = (old.report[0].y + old.report[1].y) / 2;
int cur_y = (cur.report[0].y + cur.report[1].y) / 2;
int delta_y = (cur_y - old_y) / 2;
if (!delta_y) {
return;
}
LiSendScrollEvent(delta_y);
int old_y = (old.report[0].y + old.report[1].y) / 2;
int cur_y = (cur.report[0].y + cur.report[1].y) / 2;
int delta_y = (cur_y - old_y) / 2;
if (!delta_y) {
return;
}
LiSendScrollEvent(delta_y);
}

enum {
NO_TOUCH_ACTION = 0,
ON_SCREEN_TOUCH,
SCREEN_TAP,
SWIPE_START,
ON_SCREEN_SWIPE
} TouchScreenState;

static void vita_process_input(void) {
sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG_WIDE);
sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, SCE_TOUCH_SAMPLING_STATE_START);
Expand All @@ -161,11 +169,8 @@ static void vita_process_input(void) {
SceTouchData front_old;
SceTouchData back;

bool on_move = false;
bool on_press = false;
bool need_mouse_button_release = false;
int front_state = NO_TOUCH_ACTION;
short finger_count = 0;

SceRtcTick current, until;

while (1) {
Expand All @@ -176,52 +181,56 @@ static void vita_process_input(void) {
sceTouchPeek(SCE_TOUCH_PORT_BACK, &back, 1);
sceRtcGetCurrentTick(&current);

if (need_mouse_button_release) {
if (sceRtcCompareTick(&current, &until) >= 0) {
mouse_click(finger_count, false);
need_mouse_button_release = false;
}
} else if (!on_press) {
if (front.reportNum > 0) {
on_press = true;
finger_count = front.reportNum;
sceRtcTickAddMicroseconds(&until, &current, TOUCH_DELAY);
}
} else if (on_press) {
if (sceRtcCompareTick(&current, &until) < 0) {
if (front.reportNum < finger_count) {
// TAP
if (mouse_click(finger_count, true)) {
need_mouse_button_release = true;
sceRtcTickAddMicroseconds(&until, &current, 100000);
}
on_press = false;
on_move = false;
} else if (front.reportNum > finger_count) {
// finger count changed
switch (front_state) {
case NO_TOUCH_ACTION:
if (front.reportNum > 0) {
front_state = ON_SCREEN_TOUCH;
finger_count = front.reportNum;
sceRtcTickAddMicroseconds(&until, &current, MOUSE_ACTION_DELAY);
}
} else {
if (front.reportNum > 0) {
// MOVE
if (on_move) {
switch (front.reportNum) {
case 1:
move_mouse(front_old, front);
break;
case 2:
move_wheel(front_old, front);
break;
case ON_SCREEN_TOUCH:
if (sceRtcCompareTick(&current, &until) < 0) {
if (front.reportNum < finger_count) {
// TAP
if (mouse_click(finger_count, true)) {
front_state = SCREEN_TAP;
sceRtcTickAddMicroseconds(&until, &current, MOUSE_ACTION_DELAY);
} else {
front_state = NO_TOUCH_ACTION;
}
} else {
on_move = true;
} else if (front.reportNum > finger_count) {
// finger count changed
finger_count = front.reportNum;
}
} else {
front_state = SWIPE_START;
}
break;
case SCREEN_TAP:
if (sceRtcCompareTick(&current, &until) >= 0) {
mouse_click(finger_count, false);
front_state = NO_TOUCH_ACTION;
}
break;
case SWIPE_START:
memcpy(&front_old, &front, sizeof(front_old));
front_state = ON_SCREEN_SWIPE;
break;
case ON_SCREEN_SWIPE:
if (front.reportNum > 0) {
switch (front.reportNum) {
case 1:
move_mouse(front_old, front);
break;
case 2:
move_wheel(front_old, front);
}
memcpy(&front_old, &front, sizeof(front_old));
} else {
// MOVE END
on_press = false;
on_move = false;
front_state = NO_TOUCH_ACTION;
}
}
break;
}

short btn = 0;
Expand Down

0 comments on commit b218897

Please sign in to comment.