From d3499d4997b2620baf616398fef4a990d7eb3b22 Mon Sep 17 00:00:00 2001 From: sorgelig Date: Mon, 17 Jul 2017 04:51:35 +0800 Subject: [PATCH] Move mouse emu by keyboard to input module. --- input.c | 178 +++++++++++++++++++-------------- input.h | 1 + menu.c | 15 ++- mist_cfg.c | 1 - user_io.c | 286 +++++++++-------------------------------------------- user_io.h | 1 - 6 files changed, 167 insertions(+), 315 deletions(-) diff --git a/input.c b/input.c index 4e9fcf3..56cba5e 100644 --- a/input.c +++ b/input.c @@ -530,6 +530,8 @@ static uint32_t af_delay[2] = { 50, 50 }; static unsigned char mouse_btn = 0; static int mouse_emu = 0; +static int kbd_mouse_emu = 0; +static int mouse_sniper = 0; static int mouse_emu_x = 0; static int mouse_emu_y = 0; @@ -576,6 +578,13 @@ static void joy_digital(int num, uint16_t mask, char press, int bnum) { if (press) { + mouse_sniper = 0; + mouse_timer = 0; + mouse_btn = 0; + mouse_emu_x = 0; + mouse_emu_y = 0; + user_io_mouse(0, 0, 0); + mouse_emu ^= 2; InfoMessage((mouse_emu & 2) ? "\n\n Mouse mode lock\n ON" : "\n\n Mouse mode lock\n OFF"); @@ -800,20 +809,19 @@ static void input_cb(struct input_event *ev, int dev) { if (ev->code == input[dev].mmap[i]) { - int val = (mouse_emu == 3) ? 2 : 10; switch (i) { case 8: - mouse_emu_x = ev->value ? val : 0; + mouse_emu_x = ev->value ? 10 : 0; break; case 9: - mouse_emu_x = ev->value ? -val : 0; + mouse_emu_x = ev->value ? -10 : 0; break; case 10: - mouse_emu_y = ev->value ? val : 0; + mouse_emu_y = ev->value ? 10 : 0; break; case 11: - mouse_emu_y = ev->value ? -val : 0; + mouse_emu_y = ev->value ? -10 : 0; break; default: @@ -861,17 +869,22 @@ static void input_cb(struct input_event *ev, int dev) return; } - if (ev->code == input[dev].mmap[15]) { mouse_emu = ev->value ? mouse_emu | 1 : mouse_emu & ~1; printf("mouse_emu = %d\n", mouse_emu); - - mouse_timer = 0; - mouse_btn = 0; - mouse_emu_x = 0; - mouse_emu_y = 0; - user_io_mouse(0, 0, 0); + if (mouse_emu & 2) + { + mouse_sniper = ev->value; + } + else + { + mouse_timer = 0; + mouse_btn = 0; + mouse_emu_x = 0; + mouse_emu_y = 0; + user_io_mouse(0, 0, 0); + } } return; } @@ -903,6 +916,61 @@ static void input_cb(struct input_event *ev, int dev) kbd_toggle = 0; } + if (!user_io_osd_is_visible() && (user_io_get_kbdemu() == EMU_MOUSE)) + { + if (kbd_mouse_emu) + { + for (int i = 8; i <= 14; i++) + { + if (ev->code == input[dev].mmap[i]) + { + switch (i) + { + case 8: + mouse_emu_x = ev->value ? 10 : 0; + break; + case 9: + mouse_emu_x = ev->value ? -10 : 0; + break; + case 10: + mouse_emu_y = ev->value ? 10 : 0; + break; + case 11: + mouse_emu_y = ev->value ? -10 : 0; + break; + + default: + mouse_btn = ev->value ? mouse_btn | 1 << (i - 12) : mouse_btn & ~(1 << (i - 12)); + user_io_mouse(mouse_btn, 0, 0); + break; + } + return; + } + } + + if (ev->code == input[dev].mmap[15]) + { + mouse_sniper = ev->value; + return; + } + } + + if (ev->code == input[dev].map[16]) + { + if (ev->value) + { + kbd_mouse_emu = !kbd_mouse_emu; + printf("kbd_mouse_emu = %d\n", kbd_mouse_emu); + + mouse_timer = 0; + mouse_btn = 0; + mouse_emu_x = 0; + mouse_emu_y = 0; + user_io_mouse(0, 0, 0); + } + return; + } + } if (!input[dev].has_kbdmap) { @@ -967,13 +1035,6 @@ static void input_cb(struct input_event *ev, int dev) mouse_emu_x = 0; if (ev->value < 127 || ev->value>129) mouse_emu_x = ev->value - 128; mouse_emu_x /= 12; - if (mouse_emu == 3 && mouse_emu_x > 0) mouse_emu_x = 2; - if (mouse_emu == 3 && mouse_emu_x < 0) mouse_emu_x = -2; - /* - mouse_emu_x = 0; - if (ev->value < 127) mouse_emu_x = -1; - else if(ev->value > 129) mouse_emu_x = 1; - */ return; } @@ -982,13 +1043,6 @@ static void input_cb(struct input_event *ev, int dev) mouse_emu_y = 0; if (ev->value < 127 || ev->value>129) mouse_emu_y = ev->value - 128; mouse_emu_y /= 12; - if (mouse_emu == 3 && mouse_emu_y > 0) mouse_emu_y = 2; - if (mouse_emu == 3 && mouse_emu_y < 0) mouse_emu_y = -2; - /* - mouse_emu_y = 0; - if (ev->value < 127) mouse_emu_y = -1; - else if (ev->value > 129) mouse_emu_y = 1; - */ return; } } @@ -1264,67 +1318,31 @@ int input_poll(int getchar) static int prev_dx = 0; static int prev_dy = 0; - /* - static int cx = 0; - static int cy = 0; - */ - if (mouse_emu) + if (mouse_emu || ((user_io_get_kbdemu() == EMU_MOUSE) && kbd_mouse_emu)) { - /* - if (!mouse_emu_x || (prev_dx > 0 && mouse_emu_x < 0) || (prev_dx < 0 && mouse_emu_x>0)) cx = 0; - if (!mouse_emu_y || (prev_dx > 0 && mouse_emu_y < 0) || (prev_dx < 0 && mouse_emu_y>0)) cy = 0; - */ - if((prev_dx || mouse_emu_x || prev_dy || mouse_emu_y) && (!mouse_timer || CheckTimer(mouse_timer))) { mouse_timer = GetTimer(20); - user_io_mouse(mouse_btn, mouse_emu_x, mouse_emu_y); - /* - if (mouse_emu_x) - { - cx++; - if (cx && !(cx % 10)) - { - if (mouse_emu_x < 0) - { - mouse_emu_x -= 2; - if (mouse_emu_x < -32) mouse_emu_x = -32; - } - if (mouse_emu_x > 0) - { - mouse_emu_x += 2; - if (mouse_emu_x > 32) mouse_emu_x = 32; - } - } + int dx = mouse_emu_x; + int dy = mouse_emu_y; + if (mouse_sniper) + { + if (dx > 2) dx = 2; + if (dx < -2) dx = -2; + if (dy > 2) dy = 2; + if (dy < -2) dy = -2; } - if (mouse_emu_y) - { - cy++; - if (cy && !(cy % 10)) - { - if (mouse_emu_y < 0) - { - mouse_emu_y -= 2; - if (mouse_emu_y < -32) mouse_emu_y = -32; - } - - if (mouse_emu_y > 0) - { - mouse_emu_y += 2; - if (mouse_emu_y > 32) mouse_emu_y = 32; - } - } - } - */ - //printf("mouse_emu %d, %d, %d\n", mouse_btn, mouse_emu_x, mouse_emu_y); + user_io_mouse(mouse_btn, dx, dy); prev_dx = mouse_emu_x; prev_dy = mouse_emu_y; } } + if (!mouse_emu_x && !mouse_emu_y) mouse_timer = 0; + for (int i = 0; i < 2; i++) { if (!time[i]) time[i] = GetTimer(af_delay[i]); @@ -1383,3 +1401,15 @@ int is_key_pressed(int key) return 0; } + +void input_notify_mode() +{ + //reset mouse parameters on any mode switch + kbd_mouse_emu = 1; + mouse_sniper = 0; + mouse_timer = 0; + mouse_btn = 0; + mouse_emu_x = 0; + mouse_emu_y = 0; + user_io_mouse(0, 0, 0); +} diff --git a/input.h b/input.h index 88b7369..c73bdaa 100644 --- a/input.h +++ b/input.h @@ -11,6 +11,7 @@ void set_kbdled(int mask, int state); int get_kbdled(int mask); int toggle_kbdled(int mask); +void input_notify_mode(); int input_poll(int getchar); int is_key_pressed(int key); diff --git a/menu.c b/menu.c index b2cceff..e2a5597 100644 --- a/menu.c +++ b/menu.c @@ -1109,7 +1109,18 @@ void HandleUI(void) p = (get_map_button() < 8) ? joy_button_map[get_map_button()] : joy_button_map[8 + get_map_type()]; } - sprintf(s, " Press: %s", p); + + { + s[0] = 0; + int len = (30-(strlen(p)+7))/2; + while (len > 0) + { + strcat(s, " "); + len--; + } + } + strcat(s, "Press: "); + strcat(s, p); OsdWrite(3, s, 0, 0); if (get_map_button()) { @@ -2885,7 +2896,7 @@ void HandleUI(void) strcpy(joy_bnames[8], "L.MOUSE"); strcpy(joy_bnames[9], "R.MOUSE"); strcpy(joy_bnames[10], "M.MOUSE"); - strcpy(joy_bnames[11], "Mouse Emu"); + strcpy(joy_bnames[11], "Mouse Emu/Sniper"); start_map_setting(17); menustate = MENU_JOYDIGMAP; menusub = 0; diff --git a/mist_cfg.c b/mist_cfg.c index 0deb045..5ddc121 100644 --- a/mist_cfg.c +++ b/mist_cfg.c @@ -30,7 +30,6 @@ const ini_var_t mist_ini_vars[] = { { "KEYRAH_MODE", (void*)(&(mist_cfg.keyrah_mode)), UINT32, 0, 0xFFFFFFFF, 1 }, { "RESET_COMBO", (void*)(&(mist_cfg.reset_combo)), UINT8, 0, 2, 1 }, { "KEY_MENU_AS_RGUI", (void*)(&(mist_cfg.key_menu_as_rgui)), UINT8, 0, 1, 1 }, - { "KEY_REMAP", (void*)user_io_key_remap, CUSTOM_HANDLER, 0, 0, 1 }, { "VIDEO_MODE", (void*)(&(mist_cfg.video_mode)), UINT8, 0, 9, 1 }, { "HDMI_AUDIO_96K", (void*)(&(mist_cfg.hdmi_audio_96k)), UINT8, 0, 1, 1 }, }; diff --git a/user_io.c b/user_io.c index 9c29cdc..e4921c3 100644 --- a/user_io.c +++ b/user_io.c @@ -22,19 +22,12 @@ #include "file_io.h" #include "config.h" -// up to 16 key can be remapped -#define MAX_REMAP 16 -unsigned char key_remap_table[MAX_REMAP][2]; - #define BREAK 0x8000 fileTYPE sd_image[4] = { 0 }; // mouse and keyboard emulation state static emu_mode_t emu_mode = EMU_NONE; -static unsigned char emu_state = 0; -static unsigned long emu_timer = 0; -#define EMU_MOUSE_FREQ 5 // keep state over core type and its capabilities static unsigned char core_type = CORE_TYPE_UNKNOWN; @@ -72,13 +65,7 @@ char user_io_osd_is_visible() void user_io_init() { - // no sd card image selected, SD card accesses will go directly - // to the card memset(sd_image, 0, sizeof(sd_image)); - - // mark remap table as unused - memset(key_remap_table, 0, sizeof(key_remap_table)); - ikbd_init(); } @@ -199,13 +186,13 @@ static void parse_config() char *p; do { p = user_io_8bit_get_string(i); - printf("*** %d: %s\n", i, p); if (i && p && p[0]) { if (p[0] == 'J' && p[1] == '1') { joy_force = 1; emu_mode = EMU_JOY0; + input_notify_mode(); set_kbd_led(HID_LED_NUM_LOCK, true); } } @@ -794,30 +781,6 @@ void user_io_poll() user_io_send_buttons(0); - // mouse movement emulation is continous - if (emu_mode == EMU_MOUSE) - { - if (CheckTimer(emu_timer)) - { - emu_timer = GetTimer(EMU_MOUSE_FREQ); - - if (emu_state & JOY_MOVE) - { - unsigned char b = 0; - int16_t x = 0, y = 0; - if ((emu_state & (JOY_LEFT | JOY_RIGHT)) == JOY_LEFT) x = -1; - if ((emu_state & (JOY_LEFT | JOY_RIGHT)) == JOY_RIGHT) x = +1; - if ((emu_state & (JOY_UP | JOY_DOWN)) == JOY_UP) y = -1; - if ((emu_state & (JOY_UP | JOY_DOWN)) == JOY_DOWN) y = +1; - - if (emu_state & JOY_BTN1) b |= 1; - if (emu_state & JOY_BTN2) b |= 2; - - user_io_mouse(b, x, y); - } - } - } - if (core_type == CORE_TYPE_MINIMIG2) { kbd_fifo_poll(); @@ -1311,36 +1274,6 @@ void user_io_mouse(unsigned char b, int16_t x, int16_t y) if (core_type == CORE_TYPE_ARCHIE) archie_mouse(b, x, y); } -// check if this is a key that's supposed to be suppressed -// when emulation is active -static unsigned char is_emu_key(unsigned char c, unsigned alt) { - static const unsigned char m[] = { JOY_RIGHT, JOY_LEFT, JOY_DOWN, JOY_UP }; - static const unsigned char m2[] = - { - 0x5A, JOY_DOWN, - 0x5C, JOY_LEFT, - 0x5D, JOY_DOWN, - 0x5E, JOY_RIGHT, - 0x60, JOY_UP, - 0x5F, JOY_BTN1, - 0x61, JOY_BTN2 - }; - - if (emu_mode != EMU_MOUSE) return 0; - - if (alt) - { - for (int i = 0; i<(sizeof(m2) / sizeof(m2[0])); i += 2) if (c == m2[i]) return m2[i + 1]; - } - else - { - // direction keys R/L/D/U - if (c >= 0x4f && c <= 0x52) return m[c - 0x4f]; - } - - return 0; -} - /* usb modifer bits: 0 1 2 3 4 5 6 7 LCTRL LSHIFT LALT LGUI RCTRL RSHIFT RALT RGUI @@ -1563,13 +1496,11 @@ static void keyrah_trans(unsigned char *m, unsigned char *k) void user_io_kbd(unsigned char m, unsigned char *k, unsigned short vid, unsigned short pid) { char keyrah = KEYRAH_ID ? 1 : 0; - if (emu_mode == EMU_MOUSE) keyrah <<= 1; - if (keyrah) keyrah_trans(&m, k); unsigned short reset_m = m; for (char i = 0; i<6; i++) if (k[i] == 0x4c) reset_m |= 0x100; - check_reset(reset_m, KEYRAH_ID ? 1 : mist_cfg.reset_combo); + check_reset(reset_m, keyrah ? 1 : mist_cfg.reset_combo); if ((core_type == CORE_TYPE_MINIMIG2) || (core_type == CORE_TYPE_MIST) || @@ -1584,63 +1515,6 @@ void user_io_kbd(unsigned char m, unsigned char *k, unsigned short vid, unsigned uint16_t keycodes_ps2[6] = { 0,0,0,0,0,0 }; char i, j; - // remap keycodes if requested - for (i = 0; (i<6) && k[i]; i++) - { - for (j = 0; j