From b2188979e145756b28fd77f8c555b6714b0a5e7c Mon Sep 17 00:00:00 2001 From: Sunguk Lee Date: Tue, 23 Aug 2016 03:31:00 +0900 Subject: [PATCH] vita: Little refactor mouse control --- src/main.c | 167 ++++++++++++++++++++++++++++------------------------- 1 file changed, 88 insertions(+), 79 deletions(-) diff --git a/src/main.c b/src/main.c index bb0bad23..595fa118 100644 --- a/src/main.c +++ b/src/main.c @@ -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); @@ -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) { @@ -176,52 +181,56 @@ static void vita_process_input(void) { sceTouchPeek(SCE_TOUCH_PORT_BACK, &back, 1); sceRtcGetCurrentTick(¤t); - if (need_mouse_button_release) { - if (sceRtcCompareTick(¤t, &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, ¤t, TOUCH_DELAY); - } - } else if (on_press) { - if (sceRtcCompareTick(¤t, &until) < 0) { - if (front.reportNum < finger_count) { - // TAP - if (mouse_click(finger_count, true)) { - need_mouse_button_release = true; - sceRtcTickAddMicroseconds(&until, ¤t, 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, ¤t, 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(¤t, &until) < 0) { + if (front.reportNum < finger_count) { + // TAP + if (mouse_click(finger_count, true)) { + front_state = SCREEN_TAP; + sceRtcTickAddMicroseconds(&until, ¤t, 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(¤t, &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;