implement an emit command and resolve #4937

Command Syntax: emit <press|release|press-release> <criteria> <keys>
* action: specifies whether to press, release the keys or do both
* criteria: first window that matches the criteria will get the key input
* keys: modifiers and keys separated by +
Examples:
* emit press-release [app_id=com.obsproject.Studio] ALT+F8
* bindsym --no-repeat Ctrl+T emit press [app_id=wev] Ctrl+A
* bindsym --no-repeat --release Ctrl+T emit release [app_id=wev] Ctrl+A
This commit is contained in:
Tert0 2024-12-11 22:00:39 +01:00
parent f293418d9d
commit 8689627774
No known key found for this signature in database
GPG key ID: 5CD17B02E0EFD576
7 changed files with 181 additions and 6 deletions

View file

@ -141,3 +141,9 @@ bool sway_set_cloexec(int fd, bool cloexec) {
} }
return true; return true;
} }
uint32_t get_current_time_msec(void) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return now.tv_sec * 1000 + now.tv_nsec / 1000000;
}

View file

@ -201,6 +201,7 @@ sway_cmd cmd_workspace;
sway_cmd cmd_workspace_layout; sway_cmd cmd_workspace_layout;
sway_cmd cmd_ws_auto_back_and_forth; sway_cmd cmd_ws_auto_back_and_forth;
sway_cmd cmd_xwayland; sway_cmd cmd_xwayland;
sway_cmd cmd_emit;
sway_cmd bar_cmd_bindcode; sway_cmd bar_cmd_bindcode;
sway_cmd bar_cmd_binding_mode_indicator; sway_cmd bar_cmd_binding_mode_indicator;

View file

@ -61,4 +61,6 @@ const char *sway_wl_output_subpixel_to_string(enum wl_output_subpixel subpixel);
bool sway_set_cloexec(int fd, bool cloexec); bool sway_set_cloexec(int fd, bool cloexec);
uint32_t get_current_time_msec(void);
#endif #endif

View file

@ -115,6 +115,7 @@ static const struct cmd_handler command_handlers[] = {
{ "allow_tearing", cmd_allow_tearing }, { "allow_tearing", cmd_allow_tearing },
{ "border", cmd_border }, { "border", cmd_border },
{ "create_output", cmd_create_output }, { "create_output", cmd_create_output },
{ "emit", cmd_emit },
{ "exit", cmd_exit }, { "exit", cmd_exit },
{ "floating", cmd_floating }, { "floating", cmd_floating },
{ "fullscreen", cmd_fullscreen }, { "fullscreen", cmd_fullscreen },

170
sway/commands/emit.c Normal file
View file

@ -0,0 +1,170 @@
#include "list.h"
#include "sway/commands.h"
#include "sway/criteria.h"
#include "sway/input/keyboard.h"
#include "sway/input/seat.h"
#include "sway/tree/container.h"
#include "stringop.h"
struct keycode_matches {
xkb_keysym_t keysym;
xkb_keycode_t keycode;
int count;
};
static void find_keycode(struct xkb_keymap *keymap,
xkb_keycode_t keycode, void *data) {
xkb_keysym_t keysym = xkb_state_key_get_one_sym(
config->keysym_translation_state, keycode);
if (keysym == XKB_KEY_NoSymbol) {
return;
}
struct keycode_matches *matches = data;
if (matches->keysym == keysym) {
matches->keycode = keycode;
matches->count++;
}
}
static struct keycode_matches get_keycode_for_keysym(xkb_keysym_t keysym) {
struct keycode_matches matches = {
.keysym = keysym,
.keycode = XKB_KEYCODE_INVALID,
.count = 0,
};
xkb_keymap_key_for_each(
xkb_state_get_keymap(config->keysym_translation_state),
find_keycode, &matches);
return matches;
}
enum emit_action {
EMIT_ACTION_RELEASE = 1 << 0,
EMIT_ACTION_PRESS = 1 << 1
};
struct cmd_results *cmd_emit(int argc, char **argv) {
struct cmd_results *error = NULL;
if((error = checkarg(argc, "emit", EXPECTED_EQUAL_TO, 3))) {
return error;
}
enum emit_action action = 0;
if(strcmp("press", argv[0]) == 0) {
action = EMIT_ACTION_PRESS;
} else if(strcmp("release", argv[0]) == 0) {
action = EMIT_ACTION_RELEASE;
} else if(strcmp("press-release", argv[0]) == 0) {
action = EMIT_ACTION_PRESS | EMIT_ACTION_RELEASE;
}
if(!action) {
return cmd_results_new(CMD_INVALID, "Unknown emit action '%s'", argv[0]);
}
char *err_str = NULL;
struct criteria *criteria = criteria_parse(argv[1], &err_str);
if(!criteria) {
error = cmd_results_new(CMD_INVALID, "%s", err_str);
free(err_str);
return error;
}
list_t *keycodes = create_list();
list_t *split = split_string(argv[2], "+");
uint32_t raw_modifiers = 0;
for(int i = 0; i < split->length; i++) {
uint32_t current_mod;
// parse modifier key
if((current_mod = get_modifier_mask_by_name(split->items[i])) > 0) {
raw_modifiers |= current_mod;
continue;
}
xkb_keysym_t keysym = xkb_keysym_from_name(split->items[i],
XKB_KEYSYM_CASE_INSENSITIVE);
if(!keysym) {
criteria_destroy(criteria);
list_free_items_and_destroy(keycodes);
error = cmd_results_new(CMD_FAILURE,
"Unknown key '%s'", (char*) split->items[i]);
list_free_items_and_destroy(split);
return error;
}
struct keycode_matches matches = get_keycode_for_keysym(keysym);
if(matches.count == 0) {
criteria_destroy(criteria);
list_free_items_and_destroy(keycodes);
list_free_items_and_destroy(split);
return cmd_results_new(CMD_FAILURE, "Unable to convert key to keycode");
}
uint32_t *keycode = malloc(sizeof(uint32_t));
if(!keycode) {
criteria_destroy(criteria);
list_free_items_and_destroy(keycodes);
list_free_items_and_destroy(split);
return cmd_results_new(CMD_FAILURE, "Unable to allocate key");
}
*keycode = matches.keycode;
list_add(keycodes, keycode);
}
list_t *containers = criteria_get_containers(criteria);
struct sway_seat *seat = config->handler_context.seat;
struct sway_container *original_container = seat_get_focused_container(seat);
if(containers->length == 0) {
return cmd_results_new(CMD_FAILURE, "no matching container found: %s", argv[1]);
}
struct sway_container *container = containers->items[0];
bool is_same_surface = container->view->surface == original_container->view->surface;
if(!is_same_surface) {
wlr_seat_keyboard_enter(seat->wlr_seat, container->view->surface, 0, 0, NULL);
}
struct wlr_keyboard_modifiers wlr_modifiers = {
.depressed = raw_modifiers
};
wlr_seat_keyboard_send_modifiers(seat->wlr_seat, &wlr_modifiers);
// send key presses
if(action & EMIT_ACTION_PRESS) {
for(int i = 0; i < keycodes->length; i++) {
wlr_seat_keyboard_notify_key(seat->wlr_seat, get_current_time_msec(), *((uint32_t*) keycodes->items[i])-8, 1);
}
}
// send key releases
if(action & EMIT_ACTION_RELEASE) {
for(int i = 0; i < keycodes->length; i++) {
wlr_seat_keyboard_notify_key(seat->wlr_seat, get_current_time_msec(), *((uint32_t*) keycodes->items[i])-8, 0);
}
}
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->wlr_seat);
if(is_same_surface) {
wlr_seat_keyboard_send_modifiers(seat->wlr_seat, &keyboard->modifiers);
} else {
wlr_seat_keyboard_enter(seat->wlr_seat, original_container->view->surface, keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers);
}
list_free(containers);
criteria_destroy(criteria);
return cmd_results_new(CMD_SUCCESS, NULL);
}

View file

@ -32,12 +32,6 @@
#include "sway/tree/workspace.h" #include "sway/tree/workspace.h"
#include "wlr-layer-shell-unstable-v1-protocol.h" #include "wlr-layer-shell-unstable-v1-protocol.h"
static uint32_t get_current_time_msec(void) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return now.tv_sec * 1000 + now.tv_nsec / 1000000;
}
/** /**
* Returns the node at the cursor's position. If there is a surface at that * Returns the node at the cursor's position. If there is a surface at that
* location, it is stored in **surface (it may not be a view). * location, it is stored in **surface (it may not be a view).

View file

@ -124,6 +124,7 @@ sway_sources = files(
'commands/workspace_layout.c', 'commands/workspace_layout.c',
'commands/ws_auto_back_and_forth.c', 'commands/ws_auto_back_and_forth.c',
'commands/xwayland.c', 'commands/xwayland.c',
'commands/emit.c',
'commands/bar/bind.c', 'commands/bar/bind.c',
'commands/bar/binding_mode_indicator.c', 'commands/bar/binding_mode_indicator.c',