2015-08-20 13:22:15 +10:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <ctype.h>
|
2015-08-24 05:28:49 +10:00
|
|
|
#include "log.h"
|
2015-08-20 13:22:15 +10:00
|
|
|
|
|
|
|
#include "input_state.h"
|
|
|
|
|
2015-08-20 21:24:39 +10:00
|
|
|
#define KEY_STATE_MAX_LENGTH 64
|
2015-08-20 13:22:15 +10:00
|
|
|
|
2015-08-24 05:28:49 +10:00
|
|
|
struct key_state {
|
|
|
|
/*
|
|
|
|
* Aims to store state regardless of modifiers.
|
|
|
|
* If you press a key, then hold shift, then release the key, we'll
|
|
|
|
* get two different key syms, but the same key code. This handles
|
|
|
|
* that scenario and makes sure we can use the right bindings.
|
|
|
|
*/
|
|
|
|
uint32_t key_sym;
|
|
|
|
uint32_t alt_sym;
|
|
|
|
uint32_t key_code;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct key_state key_state_array[KEY_STATE_MAX_LENGTH];
|
2015-08-20 13:22:15 +10:00
|
|
|
|
2015-08-21 07:14:26 +10:00
|
|
|
void input_init(void) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) {
|
2015-08-24 05:28:49 +10:00
|
|
|
struct key_state none = { 0, 0, 0 };
|
|
|
|
key_state_array[i] = none;
|
2015-08-21 07:14:26 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 05:28:49 +10:00
|
|
|
static uint8_t find_key(uint32_t key_sym, uint32_t key_code, bool update) {
|
2015-08-20 13:22:15 +10:00
|
|
|
int i;
|
2015-08-20 21:24:39 +10:00
|
|
|
for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) {
|
2015-08-24 05:28:49 +10:00
|
|
|
if (0 == key_sym && 0 == key_code && key_state_array[i].key_sym == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (key_state_array[i].key_sym == key_sym
|
|
|
|
|| key_state_array[i].alt_sym == key_sym) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (update && key_state_array[i].key_code == key_code) {
|
|
|
|
key_state_array[i].alt_sym = key_sym;
|
2015-08-20 13:22:15 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2015-08-24 05:28:49 +10:00
|
|
|
bool check_key(uint32_t key_sym, uint32_t key_code) {
|
|
|
|
return find_key(key_sym, key_code, false) < KEY_STATE_MAX_LENGTH;
|
2015-08-20 13:22:15 +10:00
|
|
|
}
|
|
|
|
|
2015-08-24 05:28:49 +10:00
|
|
|
void press_key(uint32_t key_sym, uint32_t key_code) {
|
|
|
|
if (key_code == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-20 13:22:15 +10:00
|
|
|
// Check if key exists
|
2015-08-24 05:28:49 +10:00
|
|
|
if (!check_key(key_sym, key_code)) {
|
2015-08-20 13:22:15 +10:00
|
|
|
// Check that we dont exceed buffer length
|
2015-08-24 05:28:49 +10:00
|
|
|
int insert = find_key(0, 0, true);
|
2015-08-20 21:24:39 +10:00
|
|
|
if (insert < KEY_STATE_MAX_LENGTH) {
|
2015-08-24 05:28:49 +10:00
|
|
|
key_state_array[insert].key_sym = key_sym;
|
|
|
|
key_state_array[insert].key_code = key_code;
|
2015-08-20 13:22:15 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 05:28:49 +10:00
|
|
|
void release_key(uint32_t key_sym, uint32_t key_code) {
|
|
|
|
uint8_t index = find_key(key_sym, key_code, true);
|
2015-08-20 21:24:39 +10:00
|
|
|
if (index < KEY_STATE_MAX_LENGTH) {
|
2015-08-24 05:28:49 +10:00
|
|
|
struct key_state none = { 0, 0, 0 };
|
|
|
|
key_state_array[index] = none;
|
2015-08-20 13:22:15 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 14:03:45 +10:00
|
|
|
// Pointer state and mode
|
|
|
|
|
2015-08-22 02:31:05 +10:00
|
|
|
struct pointer_state pointer_state;
|
2015-08-20 13:22:15 +10:00
|
|
|
|
2015-08-23 11:01:38 +10:00
|
|
|
static struct mode_state {
|
2015-08-23 14:03:45 +10:00
|
|
|
// initial view state
|
|
|
|
double x, y, w, h;
|
|
|
|
swayc_t *ptr;
|
2015-08-24 00:59:18 +10:00
|
|
|
// Containers used for resizing horizontally
|
2015-08-23 11:01:38 +10:00
|
|
|
struct {
|
2015-08-24 00:59:18 +10:00
|
|
|
double w;
|
2015-08-23 11:01:38 +10:00
|
|
|
swayc_t *ptr;
|
2015-08-23 15:32:41 +10:00
|
|
|
struct {
|
2015-08-24 00:59:18 +10:00
|
|
|
double w;
|
2015-08-23 15:32:41 +10:00
|
|
|
swayc_t *ptr;
|
2015-08-24 00:59:18 +10:00
|
|
|
} parent;
|
|
|
|
} horiz;
|
|
|
|
// Containers used for resizing vertically
|
2015-08-23 11:01:38 +10:00
|
|
|
struct {
|
2015-08-24 00:59:18 +10:00
|
|
|
double h;
|
2015-08-23 14:03:45 +10:00
|
|
|
swayc_t *ptr;
|
2015-08-23 15:32:41 +10:00
|
|
|
struct {
|
2015-08-24 00:59:18 +10:00
|
|
|
double h;
|
2015-08-23 15:32:41 +10:00
|
|
|
swayc_t *ptr;
|
2015-08-24 00:59:18 +10:00
|
|
|
} parent;
|
|
|
|
} vert;
|
2015-08-23 11:01:38 +10:00
|
|
|
} initial;
|
|
|
|
|
|
|
|
static struct {
|
2015-08-23 14:03:45 +10:00
|
|
|
bool left;
|
|
|
|
bool top;
|
2015-08-23 11:01:38 +10:00
|
|
|
} lock;
|
|
|
|
|
2015-08-23 15:14:59 +10:00
|
|
|
// initial set/unset
|
2015-08-23 11:01:38 +10:00
|
|
|
|
2015-08-23 14:03:45 +10:00
|
|
|
static void set_initial_view(swayc_t *view) {
|
|
|
|
initial.ptr = view;
|
|
|
|
initial.x = view->x;
|
|
|
|
initial.y = view->y;
|
|
|
|
initial.w = view->width;
|
|
|
|
initial.h = view->height;
|
2015-08-23 11:01:38 +10:00
|
|
|
}
|
|
|
|
|
2015-08-23 15:14:59 +10:00
|
|
|
static void set_initial_sibling(void) {
|
|
|
|
bool reset = true;
|
2015-08-24 00:59:18 +10:00
|
|
|
if ((initial.horiz.ptr = get_swayc_in_direction(initial.ptr, lock.left ? MOVE_RIGHT: MOVE_LEFT))) {
|
|
|
|
initial.horiz.w = initial.horiz.ptr->width;
|
|
|
|
initial.horiz.parent.ptr = get_swayc_in_direction(initial.horiz.ptr, lock.left ? MOVE_LEFT : MOVE_RIGHT);
|
|
|
|
initial.horiz.parent.w = initial.horiz.parent.ptr->width;
|
2015-08-23 15:14:59 +10:00
|
|
|
reset = false;
|
|
|
|
}
|
2015-08-24 00:59:18 +10:00
|
|
|
if ((initial.vert.ptr = get_swayc_in_direction(initial.ptr, lock.top ? MOVE_DOWN: MOVE_UP))) {
|
|
|
|
initial.vert.h = initial.vert.ptr->height;
|
|
|
|
initial.vert.parent.ptr = get_swayc_in_direction(initial.vert.ptr, lock.top ? MOVE_UP : MOVE_DOWN);
|
|
|
|
initial.vert.parent.h = initial.vert.parent.ptr->height;
|
2015-08-23 15:14:59 +10:00
|
|
|
reset = false;
|
|
|
|
}
|
2015-08-24 00:59:18 +10:00
|
|
|
// If nothing will change just undo the mode
|
2015-08-23 15:14:59 +10:00
|
|
|
if (reset) {
|
|
|
|
pointer_state.mode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 14:03:45 +10:00
|
|
|
static void reset_initial_view(void) {
|
|
|
|
initial.ptr->x = initial.x;
|
|
|
|
initial.ptr->y = initial.y;
|
|
|
|
initial.ptr->width = initial.w;
|
|
|
|
initial.ptr->height = initial.h;
|
|
|
|
arrange_windows(initial.ptr, -1, -1);
|
2015-08-23 11:01:38 +10:00
|
|
|
pointer_state.mode = 0;
|
|
|
|
}
|
|
|
|
|
2015-08-24 00:59:18 +10:00
|
|
|
static void reset_initial_sibling(void) {
|
|
|
|
initial.horiz.ptr->width = initial.horiz.w;
|
|
|
|
initial.horiz.parent.ptr->width = initial.horiz.parent.w;
|
|
|
|
initial.vert.ptr->height = initial.vert.h;
|
|
|
|
initial.vert.parent.ptr->height = initial.vert.parent.h;
|
|
|
|
arrange_windows(initial.horiz.ptr->parent, -1, -1);
|
|
|
|
arrange_windows(initial.vert.ptr->parent, -1, -1);
|
|
|
|
pointer_state.mode = 0;
|
|
|
|
}
|
|
|
|
|
2015-08-23 11:01:38 +10:00
|
|
|
// Mode set left/right click
|
|
|
|
|
|
|
|
static void pointer_mode_set_left(void) {
|
2015-08-23 14:03:45 +10:00
|
|
|
set_initial_view(pointer_state.left.view);
|
|
|
|
if (initial.ptr->is_floating) {
|
2015-08-23 11:01:38 +10:00
|
|
|
pointer_state.mode = M_DRAGGING | M_FLOATING;
|
|
|
|
} else {
|
|
|
|
pointer_state.mode = M_DRAGGING | M_TILING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pointer_mode_set_right(void) {
|
2015-08-23 14:03:45 +10:00
|
|
|
set_initial_view(pointer_state.right.view);
|
2015-08-23 11:01:38 +10:00
|
|
|
// Setup locking information
|
2015-08-23 14:03:45 +10:00
|
|
|
int midway_x = initial.ptr->x + initial.ptr->width/2;
|
|
|
|
int midway_y = initial.ptr->y + initial.ptr->height/2;
|
2015-08-23 11:01:38 +10:00
|
|
|
|
2015-08-23 14:03:45 +10:00
|
|
|
lock.left = pointer_state.origin.x > midway_x;
|
|
|
|
lock.top = pointer_state.origin.y > midway_y;
|
2015-08-23 11:01:38 +10:00
|
|
|
|
2015-08-23 14:03:45 +10:00
|
|
|
if (initial.ptr->is_floating) {
|
2015-08-23 11:01:38 +10:00
|
|
|
pointer_state.mode = M_RESIZING | M_FLOATING;
|
|
|
|
} else {
|
|
|
|
pointer_state.mode = M_RESIZING | M_TILING;
|
2015-08-23 15:14:59 +10:00
|
|
|
set_initial_sibling();
|
2015-08-23 11:01:38 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mode set/update/reset
|
|
|
|
|
|
|
|
void pointer_mode_set(uint32_t button, bool condition) {
|
|
|
|
// switch on drag/resize mode
|
|
|
|
switch (pointer_state.mode & (M_DRAGGING | M_RESIZING)) {
|
|
|
|
case M_DRAGGING:
|
|
|
|
// end drag mode when left click is unpressed
|
2015-08-23 14:03:45 +10:00
|
|
|
if (!pointer_state.left.held) {
|
2015-08-23 11:01:38 +10:00
|
|
|
pointer_state.mode = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case M_RESIZING:
|
|
|
|
// end resize mode when right click is unpressed
|
2015-08-23 14:03:45 +10:00
|
|
|
if (!pointer_state.right.held) {
|
2015-08-23 11:01:38 +10:00
|
|
|
pointer_state.mode = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// No mode case
|
|
|
|
default:
|
|
|
|
// return if failed condition, or no view
|
|
|
|
if (!condition || !pointer_state.view) {
|
|
|
|
break;
|
|
|
|
}
|
2015-08-23 14:03:45 +10:00
|
|
|
|
2015-08-23 11:01:38 +10:00
|
|
|
// Set mode depending on current button press
|
|
|
|
switch (button) {
|
|
|
|
// Start dragging mode
|
|
|
|
case M_LEFT_CLICK:
|
|
|
|
// if button release dont do anything
|
2015-08-23 14:03:45 +10:00
|
|
|
if (pointer_state.left.held) {
|
2015-08-23 11:01:38 +10:00
|
|
|
pointer_mode_set_left();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Start resize mode
|
|
|
|
case M_RIGHT_CLICK:
|
|
|
|
// if button release dont do anyhting
|
2015-08-23 14:03:45 +10:00
|
|
|
if (pointer_state.right.held) {
|
2015-08-23 11:01:38 +10:00
|
|
|
pointer_mode_set_right();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pointer_mode_update(void) {
|
2015-08-23 14:03:45 +10:00
|
|
|
if (initial.ptr->type != C_VIEW) {
|
2015-08-23 11:01:38 +10:00
|
|
|
pointer_state.mode = 0;
|
|
|
|
return;
|
|
|
|
}
|
2015-08-23 14:03:45 +10:00
|
|
|
int dx = pointer_state.origin.x;
|
|
|
|
int dy = pointer_state.origin.y;
|
2015-08-23 11:01:38 +10:00
|
|
|
|
|
|
|
switch (pointer_state.mode) {
|
|
|
|
case M_FLOATING | M_DRAGGING:
|
|
|
|
// Update position
|
2015-08-23 14:03:45 +10:00
|
|
|
dx -= pointer_state.left.x;
|
|
|
|
dy -= pointer_state.left.y;
|
|
|
|
if (initial.x + dx != initial.ptr->x) {
|
|
|
|
initial.ptr->x = initial.x + dx;
|
2015-08-23 11:01:38 +10:00
|
|
|
}
|
2015-08-23 14:03:45 +10:00
|
|
|
if (initial.y + dy != initial.ptr->y) {
|
|
|
|
initial.ptr->y = initial.y + dy;
|
2015-08-23 11:01:38 +10:00
|
|
|
}
|
2015-08-23 14:03:45 +10:00
|
|
|
update_geometry(initial.ptr);
|
2015-08-23 11:01:38 +10:00
|
|
|
break;
|
|
|
|
|
|
|
|
case M_FLOATING | M_RESIZING:
|
2015-08-23 14:03:45 +10:00
|
|
|
dx -= pointer_state.right.x;
|
|
|
|
dy -= pointer_state.right.y;
|
|
|
|
initial.ptr = pointer_state.right.view;
|
|
|
|
if (lock.left) {
|
|
|
|
if (initial.w + dx > min_sane_w) {
|
|
|
|
initial.ptr->width = initial.w + dx;
|
2015-08-23 11:01:38 +10:00
|
|
|
}
|
2015-08-24 00:59:18 +10:00
|
|
|
} else { // lock.right
|
2015-08-23 14:03:45 +10:00
|
|
|
if (initial.w - dx > min_sane_w) {
|
|
|
|
initial.ptr->width = initial.w - dx;
|
|
|
|
initial.ptr->x = initial.x + dx;
|
2015-08-23 11:01:38 +10:00
|
|
|
}
|
|
|
|
}
|
2015-08-23 14:03:45 +10:00
|
|
|
if (lock.top) {
|
|
|
|
if (initial.h + dy > min_sane_h) {
|
|
|
|
initial.ptr->height = initial.h + dy;
|
2015-08-23 11:01:38 +10:00
|
|
|
}
|
2015-08-24 00:59:18 +10:00
|
|
|
} else { // lock.bottom
|
2015-08-23 14:03:45 +10:00
|
|
|
if (initial.h - dy > min_sane_h) {
|
|
|
|
initial.ptr->height = initial.h - dy;
|
|
|
|
initial.ptr->y = initial.y + dy;
|
2015-08-23 11:01:38 +10:00
|
|
|
}
|
|
|
|
}
|
2015-08-23 14:03:45 +10:00
|
|
|
update_geometry(initial.ptr);
|
2015-08-23 11:01:38 +10:00
|
|
|
break;
|
|
|
|
|
|
|
|
case M_TILING | M_DRAGGING:
|
|
|
|
// swap current view under pointer with dragged view
|
2015-08-23 14:54:39 +10:00
|
|
|
if (pointer_state.view && pointer_state.view->type == C_VIEW
|
|
|
|
&& pointer_state.view != initial.ptr) {
|
2015-08-23 11:01:38 +10:00
|
|
|
// Swap them around
|
2015-08-23 14:03:45 +10:00
|
|
|
swap_container(pointer_state.view, initial.ptr);
|
2015-08-23 11:01:38 +10:00
|
|
|
update_geometry(pointer_state.view);
|
2015-08-23 14:03:45 +10:00
|
|
|
update_geometry(initial.ptr);
|
2015-08-23 11:01:38 +10:00
|
|
|
// Set focus back to initial view
|
2015-08-23 14:03:45 +10:00
|
|
|
set_focused_container(initial.ptr);
|
2015-08-23 11:01:38 +10:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case M_TILING | M_RESIZING:
|
2015-08-23 14:03:45 +10:00
|
|
|
dx -= pointer_state.right.x;
|
|
|
|
dy -= pointer_state.right.y;
|
|
|
|
// resize if we can
|
2015-08-24 00:59:18 +10:00
|
|
|
if (initial.horiz.ptr) {
|
2015-08-23 14:03:45 +10:00
|
|
|
if (lock.left) {
|
|
|
|
// Check whether its fine to resize
|
2015-08-24 00:59:18 +10:00
|
|
|
if (initial.w + dx > min_sane_w && initial.horiz.w - dx > min_sane_w) {
|
|
|
|
initial.horiz.ptr->width = initial.horiz.w - dx;
|
|
|
|
initial.horiz.parent.ptr->width = initial.horiz.parent.w + dx;
|
2015-08-23 14:03:45 +10:00
|
|
|
}
|
2015-08-24 00:59:18 +10:00
|
|
|
} else { // lock.right
|
|
|
|
if (initial.w - dx > min_sane_w && initial.horiz.w + dx > min_sane_w) {
|
|
|
|
initial.horiz.ptr->width = initial.horiz.w + dx;
|
|
|
|
initial.horiz.parent.ptr->width = initial.horiz.parent.w - dx;
|
2015-08-23 14:03:45 +10:00
|
|
|
}
|
|
|
|
}
|
2015-08-24 00:59:18 +10:00
|
|
|
arrange_windows(initial.horiz.ptr->parent, -1, -1);
|
2015-08-23 14:03:45 +10:00
|
|
|
}
|
2015-08-24 00:59:18 +10:00
|
|
|
if (initial.vert.ptr) {
|
2015-08-23 14:03:45 +10:00
|
|
|
if (lock.top) {
|
2015-08-24 00:59:18 +10:00
|
|
|
if (initial.h + dy > min_sane_h && initial.vert.h - dy > min_sane_h) {
|
|
|
|
initial.vert.ptr->height = initial.vert.h - dy;
|
|
|
|
initial.vert.parent.ptr->height = initial.vert.parent.h + dy;
|
2015-08-23 14:03:45 +10:00
|
|
|
}
|
2015-08-24 00:59:18 +10:00
|
|
|
} else { // lock.bottom
|
|
|
|
if (initial.h - dy > min_sane_h && initial.vert.h + dy > min_sane_h) {
|
|
|
|
initial.vert.ptr->height = initial.vert.h + dy;
|
|
|
|
initial.vert.parent.ptr->height = initial.vert.parent.h - dy;
|
2015-08-23 14:03:45 +10:00
|
|
|
}
|
|
|
|
}
|
2015-08-24 00:59:18 +10:00
|
|
|
arrange_windows(initial.vert.ptr->parent, -1, -1);
|
2015-08-23 14:03:45 +10:00
|
|
|
}
|
2015-08-23 11:01:38 +10:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pointer_mode_reset(void) {
|
|
|
|
switch (pointer_state.mode) {
|
|
|
|
case M_FLOATING | M_RESIZING:
|
2015-08-24 00:59:18 +10:00
|
|
|
case M_FLOATING | M_DRAGGING:
|
2015-08-23 14:03:45 +10:00
|
|
|
reset_initial_view();
|
2015-08-23 11:01:38 +10:00
|
|
|
break;
|
|
|
|
|
|
|
|
case M_TILING | M_RESIZING:
|
2015-08-24 00:59:18 +10:00
|
|
|
(void) reset_initial_sibling;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case M_TILING | M_DRAGGING:
|
2015-08-23 11:01:38 +10:00
|
|
|
default:
|
2015-08-24 00:59:18 +10:00
|
|
|
break;
|
2015-08-23 11:01:38 +10:00
|
|
|
}
|
|
|
|
}
|