void gui_mouse_event_end () { const char *mouse_key; gui_mouse_event_pending = 0; /* end mouse event timer */ if (gui_mouse_event_timer) { unhook (gui_mouse_event_timer); gui_mouse_event_timer = NULL; } /* get key from mouse code */ mouse_key = gui_mouse_event_code2key (gui_key_combo_buffer); if (mouse_key && mouse_key[0]) { if (gui_mouse_grab) { gui_mouse_grab_end (mouse_key); } else { /* execute command (if found) */ (void) gui_key_focus (mouse_key, GUI_KEY_CONTEXT_MOUSE); } gui_mouse_event_reset (); } gui_key_combo_buffer[0] = '\0'; }
const char * gui_mouse_event_code2key (const char *code) { int i, x, y, code_utf8, length; double diff_x, diff_y, distance, angle, pi4; static char key[128]; const char *ptr_code; key[0] = '\0'; /* * mouse code must have at least: * one code (for event) + X + Y == 3 bytes or 3 UTF-8 chars */ code_utf8 = utf8_is_valid (code, NULL); length = (code_utf8) ? utf8_strlen (code) : (int)strlen (code); if (length < 3) return NULL; /* get coordinates and button */ if (code_utf8) { /* get coordinates using UTF-8 chars in code */ x = utf8_char_int (code + 1) - 33; ptr_code = utf8_next_char (code + 1); if (!ptr_code) return NULL; y = utf8_char_int (ptr_code) - 33; } else { /* get coordinates using ISO chars in code */ x = ((unsigned char)code[1]) - 33; y = ((unsigned char)code[2]) - 33; } if (x < 0) x = 0; if (y < 0) y = 0; /* ignore code if it's motion/end code received as first event */ if ((gui_mouse_event_index == 0) && (MOUSE_CODE_MOTION(code[0]) || MOUSE_CODE_END(code[0]))) { return NULL; } /* set data in "gui_mouse_event_xxx" */ gui_mouse_event_x[gui_mouse_event_index] = x; gui_mouse_event_y[gui_mouse_event_index] = y; if (gui_mouse_event_index == 0) gui_mouse_event_button = code[0]; if (gui_mouse_event_index == 0) gui_mouse_event_index = 1; /* * browse wheel codes, if one code is found, return event name immediately */ for (i = 0; gui_mouse_wheel_codes[i][0]; i++) { if (code[0] == gui_mouse_wheel_codes[i][0][0]) { strcat (key, gui_mouse_wheel_codes[i][1]); gui_mouse_event_x[1] = gui_mouse_event_x[0]; gui_mouse_event_y[1] = gui_mouse_event_y[0]; return key; } } /* add name of button event */ for (i = 0; gui_mouse_button_codes[i][0]; i++) { if (gui_mouse_event_button == gui_mouse_button_codes[i][0][0]) { strcat (key, gui_mouse_button_codes[i][1]); break; } } /* nothing found, reset now or mouse will be stuck */ if (!key[0]) { gui_mouse_event_reset (); return NULL; } if (!MOUSE_CODE_END(code[0])) { strcat (key, "-event-"); if (MOUSE_CODE_MOTION(code[0])) { strcat (key, "drag"); } else { gui_mouse_event_x[1] = gui_mouse_event_x[0]; gui_mouse_event_y[1] = gui_mouse_event_y[0]; strcat (key, "down"); } return key; } /* * Mouse gesture: if (x,y) on release is different from (x,y) on click, * compute distance and angle between 2 points. * * Distance: sqrt((x2-x1)²+(y2-y1)²) * Angle : atan2(x1-x1, y2-y1) * * Angle: * * 3.14 pi * /\ * -2.35 || 2.35 3/4 * pi * || * -1.57 /----++----\ 1.57 1/2 * pi * \----++----/ * || * -0.78 || 0.78 1/4 * pi * \/ * 0.00 0 * * Possible returned gestures are: * * key name | dist. | angle * ---------------------------+-------+-------------------------- * buttonX-gesture-up | 3..19 | -2.35..-3.14 + 2.35..3.14 * buttonX-gesture-up-long | >= 20 | * buttonX-gesture-down | 3..19 | -0.78..0.78 * buttonX-gesture-down-long | >= 20 | * buttonX-gesture-left | 3..39 | -0.78..-2.35 * buttonX-gesture-left-long | >= 40 | * buttonX-gesture-right | 3..39 | 0.78..2.35 * buttonX-gesture-right-long | >= 40 | */ if (key[0] && ((gui_mouse_event_x[0] != gui_mouse_event_x[1]) || (gui_mouse_event_y[0] != gui_mouse_event_y[1]))) { diff_x = gui_mouse_event_x[1] - gui_mouse_event_x[0]; diff_y = gui_mouse_event_y[1] - gui_mouse_event_y[0]; distance = sqrt ((diff_x * diff_x) + (diff_y * diff_y)); if (distance >= 3) { angle = atan2 ((double)(gui_mouse_event_x[1] - gui_mouse_event_x[0]), (double)(gui_mouse_event_y[1] - gui_mouse_event_y[0])); pi4 = 3.14159265358979 / 4; if ((angle <= pi4 * (-3)) || (angle >= pi4 * 3)) { strcat (key, "-gesture-up"); if (distance >= 20) strcat (key, "-long"); } else if ((angle >= pi4 * (-1)) && (angle <= pi4)) { strcat (key, "-gesture-down"); if (distance >= 20) strcat (key, "-long"); } else if ((angle >= pi4 * (-3)) && (angle <= pi4 * (-1))) { strcat (key, "-gesture-left"); if (distance >= 40) strcat (key, "-long"); } else if ((angle >= pi4) && (angle <= pi4 * 3)) { strcat (key, "-gesture-right"); if (distance >= 40) strcat (key, "-long"); } } } return key; }