You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
491 lines
12 KiB
491 lines
12 KiB
9 years ago
|
#include <string.h>
|
||
|
#include <stdbool.h>
|
||
|
#include <ctype.h>
|
||
8 years ago
|
#include "sway/input_state.h"
|
||
|
#include "sway/config.h"
|
||
9 years ago
|
#include "log.h"
|
||
9 years ago
|
|
||
9 years ago
|
#define KEY_STATE_MAX_LENGTH 64
|
||
9 years ago
|
|
||
9 years ago
|
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];
|
||
9 years ago
|
|
||
9 years ago
|
static struct key_state last_released;
|
||
|
|
||
9 years ago
|
static uint32_t modifiers_state;
|
||
|
|
||
9 years ago
|
void input_init(void) {
|
||
|
int i;
|
||
|
for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) {
|
||
9 years ago
|
struct key_state none = { 0, 0, 0 };
|
||
|
key_state_array[i] = none;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
struct key_state none = { 0, 0, 0 };
|
||
|
last_released = none;
|
||
|
|
||
9 years ago
|
modifiers_state = 0;
|
||
|
}
|
||
|
|
||
|
uint32_t modifier_state_changed(uint32_t new_state, uint32_t mod) {
|
||
|
if ((new_state & mod) != 0) { // pressed
|
||
|
if ((modifiers_state & mod) != 0) { // already pressed
|
||
|
return MOD_STATE_UNCHANGED;
|
||
|
} else { // pressed
|
||
|
return MOD_STATE_PRESSED;
|
||
|
}
|
||
|
} else { // not pressed
|
||
|
if ((modifiers_state & mod) != 0) { // released
|
||
|
return MOD_STATE_RELEASED;
|
||
|
} else { // already released
|
||
|
return MOD_STATE_UNCHANGED;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void modifiers_state_update(uint32_t new_state) {
|
||
|
modifiers_state = new_state;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
static uint8_t find_key(uint32_t key_sym, uint32_t key_code, bool update) {
|
||
9 years ago
|
int i;
|
||
9 years ago
|
for (i = 0; i < KEY_STATE_MAX_LENGTH; ++i) {
|
||
9 years ago
|
if (0 == key_sym && 0 == key_code && key_state_array[i].key_sym == 0) {
|
||
|
break;
|
||
|
}
|
||
9 years ago
|
if (key_sym != 0 && (key_state_array[i].key_sym == key_sym
|
||
|
|| key_state_array[i].alt_sym == key_sym)) {
|
||
9 years ago
|
break;
|
||
|
}
|
||
|
if (update && key_state_array[i].key_code == key_code) {
|
||
|
key_state_array[i].alt_sym = key_sym;
|
||
9 years ago
|
break;
|
||
|
}
|
||
9 years ago
|
if (key_sym == 0 && key_code != 0 && key_state_array[i].key_code == key_code) {
|
||
|
break;
|
||
|
}
|
||
9 years ago
|
}
|
||
|
return i;
|
||
|
}
|
||
|
|
||
9 years ago
|
bool check_key(uint32_t key_sym, uint32_t key_code) {
|
||
|
return find_key(key_sym, key_code, false) < KEY_STATE_MAX_LENGTH;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
bool check_released_key(uint32_t key_sym) {
|
||
|
return (key_sym != 0
|
||
|
&& (last_released.key_sym == key_sym
|
||
|
|| last_released.alt_sym == key_sym));
|
||
|
}
|
||
|
|
||
9 years ago
|
void press_key(uint32_t key_sym, uint32_t key_code) {
|
||
|
if (key_code == 0) {
|
||
|
return;
|
||
|
}
|
||
9 years ago
|
// Check if key exists
|
||
9 years ago
|
if (!check_key(key_sym, key_code)) {
|
||
9 years ago
|
// Check that we don't exceed buffer length
|
||
9 years ago
|
int insert = find_key(0, 0, true);
|
||
9 years ago
|
if (insert < KEY_STATE_MAX_LENGTH) {
|
||
9 years ago
|
key_state_array[insert].key_sym = key_sym;
|
||
|
key_state_array[insert].key_code = key_code;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void release_key(uint32_t key_sym, uint32_t key_code) {
|
||
|
uint8_t index = find_key(key_sym, key_code, true);
|
||
9 years ago
|
if (index < KEY_STATE_MAX_LENGTH) {
|
||
9 years ago
|
last_released.key_sym = key_state_array[index].key_sym;
|
||
|
last_released.alt_sym = key_state_array[index].alt_sym;
|
||
|
last_released.key_code = key_state_array[index].key_code;
|
||
9 years ago
|
struct key_state none = { 0, 0, 0 };
|
||
|
key_state_array[index] = none;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// Pointer state and mode
|
||
|
|
||
9 years ago
|
struct pointer_state pointer_state;
|
||
9 years ago
|
|
||
9 years ago
|
static struct mode_state {
|
||
9 years ago
|
// initial view state
|
||
|
double x, y, w, h;
|
||
|
swayc_t *ptr;
|
||
9 years ago
|
// Containers used for resizing horizontally
|
||
9 years ago
|
struct {
|
||
9 years ago
|
double w;
|
||
9 years ago
|
swayc_t *ptr;
|
||
9 years ago
|
struct {
|
||
9 years ago
|
double w;
|
||
9 years ago
|
swayc_t *ptr;
|
||
9 years ago
|
} parent;
|
||
|
} horiz;
|
||
|
// Containers used for resizing vertically
|
||
9 years ago
|
struct {
|
||
9 years ago
|
double h;
|
||
9 years ago
|
swayc_t *ptr;
|
||
9 years ago
|
struct {
|
||
9 years ago
|
double h;
|
||
9 years ago
|
swayc_t *ptr;
|
||
9 years ago
|
} parent;
|
||
|
} vert;
|
||
9 years ago
|
} initial;
|
||
|
|
||
|
static struct {
|
||
9 years ago
|
bool left;
|
||
|
bool top;
|
||
9 years ago
|
} lock;
|
||
|
|
||
9 years ago
|
// initial set/unset
|
||
9 years ago
|
|
||
9 years ago
|
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;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
static void set_initial_sibling(void) {
|
||
|
bool reset = true;
|
||
9 years ago
|
swayc_t *ws = swayc_active_workspace_for(initial.ptr);
|
||
|
if ((initial.horiz.ptr = get_swayc_in_direction_under(initial.ptr,
|
||
|
lock.left ? MOVE_RIGHT: MOVE_LEFT, ws))) {
|
||
9 years ago
|
initial.horiz.w = initial.horiz.ptr->width;
|
||
9 years ago
|
initial.horiz.parent.ptr = get_swayc_in_direction_under(initial.horiz.ptr,
|
||
|
lock.left ? MOVE_LEFT : MOVE_RIGHT, ws);
|
||
9 years ago
|
initial.horiz.parent.w = initial.horiz.parent.ptr->width;
|
||
9 years ago
|
reset = false;
|
||
|
}
|
||
9 years ago
|
if ((initial.vert.ptr = get_swayc_in_direction_under(initial.ptr,
|
||
|
lock.top ? MOVE_DOWN: MOVE_UP, ws))) {
|
||
9 years ago
|
initial.vert.h = initial.vert.ptr->height;
|
||
9 years ago
|
initial.vert.parent.ptr = get_swayc_in_direction_under(initial.vert.ptr,
|
||
|
lock.top ? MOVE_UP : MOVE_DOWN, ws);
|
||
9 years ago
|
initial.vert.parent.h = initial.vert.parent.ptr->height;
|
||
9 years ago
|
reset = false;
|
||
|
}
|
||
9 years ago
|
// If nothing will change just undo the mode
|
||
9 years ago
|
if (reset) {
|
||
|
pointer_state.mode = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
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);
|
||
9 years ago
|
pointer_state.mode = 0;
|
||
|
}
|
||
|
|
||
9 years ago
|
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;
|
||
|
}
|
||
|
|
||
8 years ago
|
void pointer_position_set(double new_x, double new_y, bool force_focus) {
|
||
|
double x, y;
|
||
|
wlc_pointer_get_position_v2(&x, &y);
|
||
|
pointer_state.delta.x = new_x - x;
|
||
|
pointer_state.delta.y = new_y - y;
|
||
9 years ago
|
|
||
8 years ago
|
wlc_pointer_set_position_v2(new_x, new_y);
|
||
9 years ago
|
|
||
9 years ago
|
// Update view under pointer
|
||
|
swayc_t *prev_view = pointer_state.view;
|
||
|
pointer_state.view = container_under_pointer();
|
||
|
|
||
|
// If pointer is in a mode, update it
|
||
|
if (pointer_state.mode) {
|
||
|
pointer_mode_update();
|
||
|
// Otherwise change focus if config is set
|
||
|
} else if (force_focus || (prev_view != pointer_state.view && config->focus_follows_mouse)) {
|
||
|
if (pointer_state.view && pointer_state.view->type == C_VIEW) {
|
||
|
set_focused_container(pointer_state.view);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void center_pointer_on(swayc_t *view) {
|
||
8 years ago
|
pointer_position_set(view->x + view->width/2, view->y + view->height/2, true);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Mode set left/right click
|
||
|
|
||
9 years ago
|
static void pointer_mode_set_dragging(void) {
|
||
|
switch (config->dragging_key) {
|
||
|
case M_LEFT_CLICK:
|
||
|
set_initial_view(pointer_state.left.view);
|
||
|
break;
|
||
|
case M_RIGHT_CLICK:
|
||
|
set_initial_view(pointer_state.right.view);
|
||
|
break;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (initial.ptr->is_floating) {
|
||
9 years ago
|
pointer_state.mode = M_DRAGGING | M_FLOATING;
|
||
|
} else {
|
||
|
pointer_state.mode = M_DRAGGING | M_TILING;
|
||
9 years ago
|
// unset mode if we can't drag tile
|
||
9 years ago
|
if (initial.ptr->parent->type == C_WORKSPACE &&
|
||
|
initial.ptr->parent->children->length == 1) {
|
||
|
pointer_state.mode = 0;
|
||
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
static void pointer_mode_set_resizing(void) {
|
||
|
switch (config->resizing_key) {
|
||
|
case M_LEFT_CLICK:
|
||
|
set_initial_view(pointer_state.left.view);
|
||
|
break;
|
||
|
case M_RIGHT_CLICK:
|
||
|
set_initial_view(pointer_state.right.view);
|
||
|
break;
|
||
|
}
|
||
9 years ago
|
// Setup locking information
|
||
9 years ago
|
int midway_x = initial.ptr->x + initial.ptr->width/2;
|
||
|
int midway_y = initial.ptr->y + initial.ptr->height/2;
|
||
9 years ago
|
|
||
8 years ago
|
double x, y;
|
||
|
wlc_pointer_get_position_v2(&x, &y);
|
||
|
lock.left = x > midway_x;
|
||
|
lock.top = y > midway_y;
|
||
9 years ago
|
|
||
9 years ago
|
if (initial.ptr->is_floating) {
|
||
9 years ago
|
pointer_state.mode = M_RESIZING | M_FLOATING;
|
||
|
} else {
|
||
|
pointer_state.mode = M_RESIZING | M_TILING;
|
||
9 years ago
|
set_initial_sibling();
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// 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:
|
||
9 years ago
|
// end drag mode when 'dragging' click is unpressed
|
||
|
if (config->dragging_key == M_LEFT_CLICK && !pointer_state.left.held) {
|
||
|
pointer_state.mode = 0;
|
||
|
} else if (config->dragging_key == M_RIGHT_CLICK && !pointer_state.right.held) {
|
||
9 years ago
|
pointer_state.mode = 0;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case M_RESIZING:
|
||
9 years ago
|
// end resize mode when 'resizing' click is unpressed
|
||
|
if (config->resizing_key == M_LEFT_CLICK && !pointer_state.left.held) {
|
||
|
pointer_state.mode = 0;
|
||
|
} else if (config->resizing_key == M_RIGHT_CLICK && !pointer_state.right.held) {
|
||
9 years ago
|
pointer_state.mode = 0;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
// No mode case
|
||
|
default:
|
||
|
// return if failed condition, or no view
|
||
|
if (!condition || !pointer_state.view) {
|
||
|
break;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Set mode depending on current button press
|
||
|
switch (button) {
|
||
9 years ago
|
// Start left-click mode
|
||
9 years ago
|
case M_LEFT_CLICK:
|
||
9 years ago
|
// if button release don't do anything
|
||
9 years ago
|
if (pointer_state.left.held) {
|
||
9 years ago
|
if (config->dragging_key == M_LEFT_CLICK) {
|
||
|
pointer_mode_set_dragging();
|
||
|
} else if (config->resizing_key == M_LEFT_CLICK) {
|
||
|
pointer_mode_set_resizing();
|
||
|
}
|
||
9 years ago
|
}
|
||
|
break;
|
||
|
|
||
9 years ago
|
// Start right-click mode
|
||
9 years ago
|
case M_RIGHT_CLICK:
|
||
9 years ago
|
// if button release don't do anyhting
|
||
9 years ago
|
if (pointer_state.right.held) {
|
||
9 years ago
|
if (config->dragging_key == M_RIGHT_CLICK) {
|
||
|
pointer_mode_set_dragging();
|
||
|
} else if (config->resizing_key == M_RIGHT_CLICK) {
|
||
|
pointer_mode_set_resizing();
|
||
|
}
|
||
9 years ago
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void pointer_mode_update(void) {
|
||
9 years ago
|
if (initial.ptr->type != C_VIEW) {
|
||
9 years ago
|
pointer_state.mode = 0;
|
||
|
return;
|
||
|
}
|
||
8 years ago
|
double x, y;
|
||
|
wlc_pointer_get_position_v2(&x, &y);
|
||
|
int dx = x;
|
||
|
int dy = y;
|
||
9 years ago
|
|
||
|
switch (pointer_state.mode) {
|
||
|
case M_FLOATING | M_DRAGGING:
|
||
|
// Update position
|
||
9 years ago
|
switch (config->dragging_key) {
|
||
9 years ago
|
case M_LEFT_CLICK:
|
||
|
dx -= pointer_state.left.x;
|
||
|
dy -= pointer_state.left.y;
|
||
|
break;
|
||
|
case M_RIGHT_CLICK:
|
||
|
dx -= pointer_state.right.x;
|
||
|
dy -= pointer_state.right.y;
|
||
|
break;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (initial.x + dx != initial.ptr->x) {
|
||
|
initial.ptr->x = initial.x + dx;
|
||
9 years ago
|
}
|
||
9 years ago
|
if (initial.y + dy != initial.ptr->y) {
|
||
|
initial.ptr->y = initial.y + dy;
|
||
9 years ago
|
}
|
||
9 years ago
|
update_geometry(initial.ptr);
|
||
9 years ago
|
break;
|
||
|
|
||
|
case M_FLOATING | M_RESIZING:
|
||
9 years ago
|
switch (config->resizing_key) {
|
||
|
case M_LEFT_CLICK:
|
||
|
dx -= pointer_state.left.x;
|
||
|
dy -= pointer_state.left.y;
|
||
|
initial.ptr = pointer_state.left.view;
|
||
|
break;
|
||
|
case M_RIGHT_CLICK:
|
||
|
dx -= pointer_state.right.x;
|
||
|
dy -= pointer_state.right.y;
|
||
|
initial.ptr = pointer_state.right.view;
|
||
|
break;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (lock.left) {
|
||
|
if (initial.w + dx > min_sane_w) {
|
||
|
initial.ptr->width = initial.w + dx;
|
||
9 years ago
|
}
|
||
9 years ago
|
} else { // lock.right
|
||
9 years ago
|
if (initial.w - dx > min_sane_w) {
|
||
|
initial.ptr->width = initial.w - dx;
|
||
|
initial.ptr->x = initial.x + dx;
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
if (lock.top) {
|
||
|
if (initial.h + dy > min_sane_h) {
|
||
|
initial.ptr->height = initial.h + dy;
|
||
9 years ago
|
}
|
||
9 years ago
|
} else { // lock.bottom
|
||
9 years ago
|
if (initial.h - dy > min_sane_h) {
|
||
|
initial.ptr->height = initial.h - dy;
|
||
|
initial.ptr->y = initial.y + dy;
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
update_geometry(initial.ptr);
|
||
9 years ago
|
break;
|
||
|
|
||
|
case M_TILING | M_DRAGGING:
|
||
|
// swap current view under pointer with dragged view
|
||
9 years ago
|
if (pointer_state.view && pointer_state.view->type == C_VIEW
|
||
9 years ago
|
&& pointer_state.view != initial.ptr
|
||
|
&& !pointer_state.view->is_floating) {
|
||
9 years ago
|
// Swap them around
|
||
9 years ago
|
swap_container(pointer_state.view, initial.ptr);
|
||
9 years ago
|
swap_geometry(pointer_state.view, initial.ptr);
|
||
9 years ago
|
update_geometry(pointer_state.view);
|
||
9 years ago
|
update_geometry(initial.ptr);
|
||
9 years ago
|
// Set focus back to initial view
|
||
9 years ago
|
set_focused_container(initial.ptr);
|
||
9 years ago
|
// Arrange the windows
|
||
|
arrange_windows(&root_container, -1, -1);
|
||
9 years ago
|
}
|
||
|
break;
|
||
|
|
||
|
case M_TILING | M_RESIZING:
|
||
9 years ago
|
switch (config->resizing_key) {
|
||
|
case M_LEFT_CLICK:
|
||
|
dx -= pointer_state.left.x;
|
||
|
dy -= pointer_state.left.y;
|
||
|
break;
|
||
|
case M_RIGHT_CLICK:
|
||
|
dx -= pointer_state.right.x;
|
||
|
dy -= pointer_state.right.y;
|
||
|
break;
|
||
|
}
|
||
|
|
||
9 years ago
|
// resize if we can
|
||
9 years ago
|
if (initial.horiz.ptr) {
|
||
9 years ago
|
if (lock.left) {
|
||
|
// Check whether its fine to resize
|
||
9 years ago
|
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;
|
||
9 years ago
|
}
|
||
9 years ago
|
} 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;
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
arrange_windows(initial.horiz.ptr->parent, -1, -1);
|
||
9 years ago
|
}
|
||
9 years ago
|
if (initial.vert.ptr) {
|
||
9 years ago
|
if (lock.top) {
|
||
9 years ago
|
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;
|
||
9 years ago
|
}
|
||
9 years ago
|
} 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;
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
arrange_windows(initial.vert.ptr->parent, -1, -1);
|
||
9 years ago
|
}
|
||
9 years ago
|
default:
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void pointer_mode_reset(void) {
|
||
|
switch (pointer_state.mode) {
|
||
|
case M_FLOATING | M_RESIZING:
|
||
9 years ago
|
case M_FLOATING | M_DRAGGING:
|
||
9 years ago
|
reset_initial_view();
|
||
9 years ago
|
break;
|
||
|
|
||
|
case M_TILING | M_RESIZING:
|
||
9 years ago
|
(void) reset_initial_sibling;
|
||
|
break;
|
||
|
|
||
|
case M_TILING | M_DRAGGING:
|
||
9 years ago
|
default:
|
||
9 years ago
|
break;
|
||
9 years ago
|
}
|
||
|
}
|