2015-08-06 04:40:38 +02:00
|
|
|
#include <xkbcommon/xkbcommon.h>
|
|
|
|
#include <xkbcommon/xkbcommon-names.h>
|
2015-08-09 01:24:18 +02:00
|
|
|
#include <wlc/wlc.h>
|
2015-08-06 04:10:56 +02:00
|
|
|
#include <stdio.h>
|
2015-08-05 23:30:40 +02:00
|
|
|
#include <stdlib.h>
|
2015-08-18 23:53:57 +02:00
|
|
|
#include <errno.h>
|
2015-08-05 23:30:40 +02:00
|
|
|
#include <string.h>
|
2015-08-09 18:26:32 +02:00
|
|
|
#include <unistd.h>
|
2015-08-05 23:30:40 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include "stringop.h"
|
2015-08-09 20:35:59 +02:00
|
|
|
#include "layout.h"
|
2015-08-18 09:28:44 +02:00
|
|
|
#include "focus.h"
|
2015-08-08 23:01:22 +02:00
|
|
|
#include "log.h"
|
2015-08-10 22:31:23 +02:00
|
|
|
#include "workspace.h"
|
2015-08-05 23:30:40 +02:00
|
|
|
#include "commands.h"
|
2015-08-16 18:54:37 +02:00
|
|
|
#include "container.h"
|
2015-08-17 07:38:34 +02:00
|
|
|
#include "handlers.h"
|
2015-08-20 15:12:34 +02:00
|
|
|
#include "sway.h"
|
2015-08-22 05:26:11 +02:00
|
|
|
#include "resize.h"
|
2015-08-05 23:30:40 +02:00
|
|
|
|
2015-08-09 01:24:18 +02:00
|
|
|
struct modifier_key {
|
|
|
|
char *name;
|
|
|
|
uint32_t mod;
|
|
|
|
};
|
|
|
|
|
2015-08-30 04:14:13 +02:00
|
|
|
swayc_t *sp_view;
|
|
|
|
int sp_index = 0;
|
|
|
|
|
2015-08-13 09:24:03 +02:00
|
|
|
static struct modifier_key modifiers[] = {
|
2015-08-09 01:24:18 +02:00
|
|
|
{ XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT },
|
|
|
|
{ XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS },
|
|
|
|
{ XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL },
|
2015-08-30 22:20:56 +02:00
|
|
|
{ "Ctrl", WLC_BIT_MOD_CTRL },
|
2015-08-09 01:24:18 +02:00
|
|
|
{ XKB_MOD_NAME_ALT, WLC_BIT_MOD_ALT },
|
2015-08-30 22:20:56 +02:00
|
|
|
{ "Alt", WLC_BIT_MOD_ALT },
|
2015-08-09 01:24:18 +02:00
|
|
|
{ XKB_MOD_NAME_NUM, WLC_BIT_MOD_MOD2 },
|
|
|
|
{ "Mod3", WLC_BIT_MOD_MOD3 },
|
|
|
|
{ XKB_MOD_NAME_LOGO, WLC_BIT_MOD_LOGO },
|
|
|
|
{ "Mod5", WLC_BIT_MOD_MOD5 },
|
|
|
|
};
|
|
|
|
|
2015-08-13 06:51:38 +02:00
|
|
|
enum expected_args {
|
2015-08-13 16:48:03 +02:00
|
|
|
EXPECTED_MORE_THAN,
|
2015-08-16 17:01:01 +02:00
|
|
|
EXPECTED_AT_LEAST,
|
2015-08-13 16:48:03 +02:00
|
|
|
EXPECTED_LESS_THAN,
|
2015-08-13 16:50:46 +02:00
|
|
|
EXPECTED_EQUAL_TO
|
2015-08-13 06:51:38 +02:00
|
|
|
};
|
2015-08-13 06:06:09 +02:00
|
|
|
|
2015-08-13 06:51:38 +02:00
|
|
|
static bool checkarg(int argc, char *name, enum expected_args type, int val) {
|
2015-08-13 09:24:03 +02:00
|
|
|
switch (type) {
|
2015-08-18 13:19:20 +02:00
|
|
|
case EXPECTED_MORE_THAN:
|
|
|
|
if (argc > val) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
sway_log(L_ERROR, "Invalid %s command."
|
|
|
|
"(expected more than %d argument%s, got %d",
|
|
|
|
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
|
|
|
break;
|
|
|
|
case EXPECTED_AT_LEAST:
|
|
|
|
if (argc >= val) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
sway_log(L_ERROR, "Invalid %s command."
|
|
|
|
"(expected at least %d argument%s, got %d",
|
|
|
|
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
|
|
|
break;
|
|
|
|
case EXPECTED_LESS_THAN:
|
|
|
|
if (argc < val) {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
sway_log(L_ERROR, "Invalid %s command."
|
|
|
|
"(expected less than %d argument%s, got %d",
|
|
|
|
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
|
|
|
break;
|
|
|
|
case EXPECTED_EQUAL_TO:
|
|
|
|
if (argc == val) {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
sway_log(L_ERROR, "Invalid %s command."
|
|
|
|
"(expected %d arguments, got %d", name, val, argc);
|
|
|
|
break;
|
2015-08-13 06:51:38 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-13 06:06:09 +02:00
|
|
|
|
2015-08-20 03:59:27 +02:00
|
|
|
static int bindsym_sort(const void *_lbind, const void *_rbind) {
|
|
|
|
const struct sway_binding *lbind = *(void **)_lbind;
|
|
|
|
const struct sway_binding *rbind = *(void **)_rbind;
|
|
|
|
unsigned int lmod = 0, rmod = 0, i;
|
|
|
|
|
2015-08-20 14:06:22 +02:00
|
|
|
// Count how any modifiers are pressed
|
2015-08-20 03:59:27 +02:00
|
|
|
for (i = 0; i < 8 * sizeof(lbind->modifiers); ++i) {
|
|
|
|
lmod += lbind->modifiers & 1 << i;
|
|
|
|
rmod += rbind->modifiers & 1 << i;
|
|
|
|
}
|
|
|
|
return (rbind->keys->length + rmod) - (lbind->keys->length + lmod);
|
|
|
|
}
|
2015-08-13 06:51:38 +02:00
|
|
|
|
|
|
|
static bool cmd_bindsym(struct sway_config *config, int argc, char **argv) {
|
2015-08-13 16:48:03 +02:00
|
|
|
if (!checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-13 06:51:38 +02:00
|
|
|
};
|
2015-08-06 04:40:38 +02:00
|
|
|
|
|
|
|
struct sway_binding *binding = malloc(sizeof(struct sway_binding));
|
|
|
|
binding->keys = create_list();
|
|
|
|
binding->modifiers = 0;
|
|
|
|
binding->command = join_args(argv + 1, argc - 1);
|
2015-08-10 20:53:43 +02:00
|
|
|
|
2015-08-06 04:40:38 +02:00
|
|
|
list_t *split = split_string(argv[0], "+");
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < split->length; ++i) {
|
2015-08-09 01:24:18 +02:00
|
|
|
// Check for a modifier key
|
|
|
|
int j;
|
|
|
|
bool is_mod = false;
|
2015-08-21 16:53:11 +02:00
|
|
|
for (j = 0; j < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++j) {
|
2015-08-09 01:24:18 +02:00
|
|
|
if (strcasecmp(modifiers[j].name, split->items[i]) == 0) {
|
|
|
|
binding->modifiers |= modifiers[j].mod;
|
|
|
|
is_mod = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_mod) continue;
|
|
|
|
// Check for xkb key
|
2015-08-06 04:40:38 +02:00
|
|
|
xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE);
|
|
|
|
if (!sym) {
|
2015-08-08 23:01:22 +02:00
|
|
|
sway_log(L_ERROR, "bindsym - unknown key %s", (char *)split->items[i]);
|
2015-08-19 11:27:48 +02:00
|
|
|
list_free(binding->keys);
|
|
|
|
free(binding->command);
|
|
|
|
free(binding);
|
|
|
|
list_free(split);
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-06 04:40:38 +02:00
|
|
|
}
|
2015-08-06 04:41:46 +02:00
|
|
|
xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
|
|
|
|
*key = sym;
|
|
|
|
list_add(binding->keys, key);
|
2015-08-06 04:40:38 +02:00
|
|
|
}
|
2015-08-24 04:09:18 +02:00
|
|
|
free_flat_list(split);
|
2015-08-06 04:40:38 +02:00
|
|
|
|
2015-08-06 04:55:51 +02:00
|
|
|
// TODO: Check if there are other commands with this key binding
|
2015-08-20 03:59:27 +02:00
|
|
|
struct sway_mode *mode = config->current_mode;
|
|
|
|
list_add(mode->bindings, binding);
|
|
|
|
qsort(mode->bindings->items, mode->bindings->length,
|
|
|
|
sizeof(mode->bindings->items[0]), bindsym_sort);
|
2015-08-06 04:40:38 +02:00
|
|
|
|
2015-08-08 23:01:22 +02:00
|
|
|
sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command);
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-05 23:30:40 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_exec_always(struct sway_config *config, int argc, char **argv) {
|
2015-08-13 16:48:03 +02:00
|
|
|
if (!checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-09 18:26:32 +02:00
|
|
|
}
|
2015-08-10 20:53:43 +02:00
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
pid_t pid = fork();
|
|
|
|
/* Failed to fork */
|
|
|
|
if (pid < 0) {
|
|
|
|
sway_log(L_ERROR, "exec command failed, sway did not fork");
|
|
|
|
return false;
|
2015-08-10 21:09:51 +02:00
|
|
|
}
|
2015-08-13 06:06:09 +02:00
|
|
|
/* Child process */
|
|
|
|
if (pid == 0) {
|
2015-08-10 20:53:43 +02:00
|
|
|
char *args = join_args(argv, argc);
|
|
|
|
sway_log(L_DEBUG, "Executing %s", args);
|
|
|
|
execl("/bin/sh", "sh", "-c", args, (char *)NULL);
|
2015-08-13 06:06:09 +02:00
|
|
|
/* Execl doesnt return unless failure */
|
|
|
|
sway_log(L_ERROR, "could not find /bin/sh");
|
2015-08-10 20:53:43 +02:00
|
|
|
free(args);
|
2015-08-13 06:06:09 +02:00
|
|
|
exit(-1);
|
2015-08-10 20:53:43 +02:00
|
|
|
}
|
2015-08-13 06:06:09 +02:00
|
|
|
/* Parent */
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-10 20:53:43 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_exec(struct sway_config *config, int argc, char **argv) {
|
|
|
|
if (config->reloading) {
|
2015-08-09 18:26:32 +02:00
|
|
|
char *args = join_args(argv, argc);
|
2015-08-13 06:06:09 +02:00
|
|
|
sway_log(L_DEBUG, "Ignoring exec %s due to reload", args);
|
2015-08-09 18:26:32 +02:00
|
|
|
free(args);
|
2015-08-13 06:06:09 +02:00
|
|
|
return true;
|
2015-08-09 18:26:32 +02:00
|
|
|
}
|
2015-08-13 06:06:09 +02:00
|
|
|
return cmd_exec_always(config, argc, argv);
|
2015-08-09 18:26:32 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 18:54:37 +02:00
|
|
|
static void kill_views(swayc_t *container, void *data) {
|
|
|
|
if (container->type == C_VIEW) {
|
|
|
|
wlc_view_close(container->handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_exit(struct sway_config *config, int argc, char **argv) {
|
2015-08-13 16:50:46 +02:00
|
|
|
if (!checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-09 15:23:10 +02:00
|
|
|
}
|
2015-08-16 18:54:37 +02:00
|
|
|
// Close all views
|
|
|
|
container_map(&root_container, kill_views, NULL);
|
2015-08-20 15:12:34 +02:00
|
|
|
sway_terminate();
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-09 15:23:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 07:38:34 +02:00
|
|
|
static bool cmd_floating(struct sway_config *config, int argc, char **argv) {
|
2015-08-18 08:33:15 +02:00
|
|
|
if (!checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-17 07:38:34 +02:00
|
|
|
if (strcasecmp(argv[0], "toggle") == 0) {
|
|
|
|
swayc_t *view = get_focused_container(&root_container);
|
|
|
|
// Prevent running floating commands on things like workspaces
|
|
|
|
if (view->type != C_VIEW) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Change from nonfloating to floating
|
|
|
|
if (!view->is_floating) {
|
2015-08-20 14:06:22 +02:00
|
|
|
// Remove view from its current location
|
2015-08-19 10:06:15 +02:00
|
|
|
destroy_container(remove_child(view));
|
2015-08-20 23:29:36 +02:00
|
|
|
|
2015-08-20 14:06:22 +02:00
|
|
|
// and move it into workspace floating
|
2015-08-28 20:14:59 +02:00
|
|
|
add_floating(swayc_active_workspace(), view);
|
2015-08-21 19:28:37 +02:00
|
|
|
view->x = (swayc_active_workspace()->width - view->width)/2;
|
|
|
|
view->y = (swayc_active_workspace()->height - view->height)/2;
|
2015-08-19 09:28:53 +02:00
|
|
|
if (view->desired_width != -1) {
|
|
|
|
view->width = view->desired_width;
|
2015-08-17 07:38:34 +02:00
|
|
|
}
|
2015-08-19 09:28:53 +02:00
|
|
|
if (view->desired_height != -1) {
|
|
|
|
view->height = view->desired_height;
|
2015-08-17 07:38:34 +02:00
|
|
|
}
|
2015-08-21 19:28:37 +02:00
|
|
|
arrange_windows(swayc_active_workspace(), -1, -1);
|
2015-08-17 07:38:34 +02:00
|
|
|
} else {
|
|
|
|
// Delete the view from the floating list and unset its is_floating flag
|
|
|
|
// Using length-1 as the index is safe because the view must be the currently
|
|
|
|
// focused floating output
|
2015-08-19 09:28:53 +02:00
|
|
|
remove_child(view);
|
2015-08-17 07:38:34 +02:00
|
|
|
view->is_floating = false;
|
|
|
|
// Get the properly focused container, and add in the view there
|
2015-08-18 09:28:44 +02:00
|
|
|
swayc_t *focused = container_under_pointer();
|
2015-08-17 07:38:34 +02:00
|
|
|
// If focused is null, it's because the currently focused container is a workspace
|
|
|
|
if (focused == NULL) {
|
2015-08-21 19:28:37 +02:00
|
|
|
focused = swayc_active_workspace();
|
2015-08-17 07:38:34 +02:00
|
|
|
}
|
2015-08-18 09:28:44 +02:00
|
|
|
set_focused_container(focused);
|
2015-08-17 07:38:34 +02:00
|
|
|
|
|
|
|
sway_log(L_DEBUG, "Non-floating focused container is %p", focused);
|
|
|
|
|
2015-08-20 14:06:22 +02:00
|
|
|
// Case of focused workspace, just create as child of it
|
2015-08-17 07:38:34 +02:00
|
|
|
if (focused->type == C_WORKSPACE) {
|
|
|
|
add_child(focused, view);
|
|
|
|
}
|
2015-08-20 14:06:22 +02:00
|
|
|
// Regular case, create as sibling of current container
|
2015-08-17 07:38:34 +02:00
|
|
|
else {
|
|
|
|
add_sibling(focused, view);
|
|
|
|
}
|
2015-08-17 17:22:00 +02:00
|
|
|
// Refocus on the view once its been put back into the layout
|
2015-08-20 13:47:36 +02:00
|
|
|
view->width = view->height = 0;
|
2015-08-21 19:28:37 +02:00
|
|
|
arrange_windows(swayc_active_workspace(), -1, -1);
|
2015-08-30 04:14:13 +02:00
|
|
|
remove_view_from_scratchpad(view);
|
2015-08-17 07:38:34 +02:00
|
|
|
}
|
2015-08-19 10:06:15 +02:00
|
|
|
set_focused_container(view);
|
2015-08-17 07:38:34 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-18 08:33:15 +02:00
|
|
|
static bool cmd_floating_mod(struct sway_config *config, int argc, char **argv) {
|
|
|
|
if (!checkarg(argc, "floating_modifier", EXPECTED_EQUAL_TO, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-19 18:09:35 +02:00
|
|
|
int i, j;
|
|
|
|
list_t *split = split_string(argv[0], "+");
|
|
|
|
config->floating_mod = 0;
|
|
|
|
|
2015-08-20 14:06:22 +02:00
|
|
|
// set modifer keys
|
2015-08-19 18:09:35 +02:00
|
|
|
for (i = 0; i < split->length; ++i) {
|
2015-08-21 16:53:11 +02:00
|
|
|
for (j = 0; j < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++j) {
|
2015-08-19 18:09:35 +02:00
|
|
|
if (strcasecmp(modifiers[j].name, split->items[i]) == 0) {
|
|
|
|
config->floating_mod |= modifiers[j].mod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-24 04:09:18 +02:00
|
|
|
free_flat_list(split);
|
2015-08-19 18:09:35 +02:00
|
|
|
if (!config->floating_mod) {
|
|
|
|
sway_log(L_ERROR, "bindsym - unknown keys %s", argv[0]);
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-18 08:33:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_focus(struct sway_config *config, int argc, char **argv) {
|
2015-08-20 02:57:24 +02:00
|
|
|
static int floating_toggled_index = 0;
|
|
|
|
static int tiled_toggled_index = 0;
|
2015-08-13 16:50:46 +02:00
|
|
|
if (!checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-10 02:10:26 +02:00
|
|
|
}
|
|
|
|
if (strcasecmp(argv[0], "left") == 0) {
|
2015-08-10 05:04:37 +02:00
|
|
|
return move_focus(MOVE_LEFT);
|
2015-08-10 02:10:26 +02:00
|
|
|
} else if (strcasecmp(argv[0], "right") == 0) {
|
2015-08-10 05:04:37 +02:00
|
|
|
return move_focus(MOVE_RIGHT);
|
2015-08-10 02:10:26 +02:00
|
|
|
} else if (strcasecmp(argv[0], "up") == 0) {
|
2015-08-10 05:04:37 +02:00
|
|
|
return move_focus(MOVE_UP);
|
2015-08-10 02:10:26 +02:00
|
|
|
} else if (strcasecmp(argv[0], "down") == 0) {
|
2015-08-10 05:04:37 +02:00
|
|
|
return move_focus(MOVE_DOWN);
|
2015-08-10 02:20:53 +02:00
|
|
|
} else if (strcasecmp(argv[0], "parent") == 0) {
|
2015-08-10 09:05:44 +02:00
|
|
|
return move_focus(MOVE_PARENT);
|
2015-08-20 02:57:24 +02:00
|
|
|
} else if (strcasecmp(argv[0], "mode_toggle") == 0) {
|
|
|
|
int i;
|
2015-08-21 19:28:37 +02:00
|
|
|
swayc_t *workspace = swayc_active_workspace();
|
|
|
|
swayc_t *focused = get_focused_view(workspace);
|
2015-08-20 02:57:24 +02:00
|
|
|
if (focused->is_floating) {
|
2015-08-21 19:28:37 +02:00
|
|
|
if (workspace->children->length > 0) {
|
|
|
|
for (i = 0;i < workspace->floating->length; i++) {
|
|
|
|
if (workspace->floating->items[i] == focused) {
|
2015-08-20 02:57:24 +02:00
|
|
|
floating_toggled_index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 19:28:37 +02:00
|
|
|
if (workspace->children->length > tiled_toggled_index) {
|
|
|
|
set_focused_container(get_focused_view(workspace->children->items[tiled_toggled_index]));
|
2015-08-20 02:57:24 +02:00
|
|
|
} else {
|
2015-08-21 19:28:37 +02:00
|
|
|
set_focused_container(get_focused_view(workspace->children->items[0]));
|
2015-08-20 02:57:24 +02:00
|
|
|
tiled_toggled_index = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-21 19:28:37 +02:00
|
|
|
if (workspace->floating->length > 0) {
|
|
|
|
for (i = 0;i < workspace->children->length; i++) {
|
|
|
|
if (workspace->children->items[i] == focused) {
|
2015-08-20 02:57:24 +02:00
|
|
|
tiled_toggled_index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 19:28:37 +02:00
|
|
|
if (workspace->floating->length > floating_toggled_index) {
|
|
|
|
swayc_t *floating = workspace->floating->items[floating_toggled_index];
|
2015-08-20 02:57:24 +02:00
|
|
|
set_focused_container(get_focused_view(floating));
|
|
|
|
} else {
|
2015-08-21 19:28:37 +02:00
|
|
|
swayc_t *floating = workspace->floating->items[workspace->floating->length - 1];
|
2015-08-20 02:57:24 +02:00
|
|
|
set_focused_container(get_focused_view(floating));
|
2015-08-21 19:28:37 +02:00
|
|
|
tiled_toggled_index = workspace->floating->length - 1;
|
2015-08-20 02:57:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-10 02:10:26 +02:00
|
|
|
}
|
2015-08-20 02:57:24 +02:00
|
|
|
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-10 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) {
|
2015-08-13 16:50:46 +02:00
|
|
|
if (!checkarg(argc, "focus_follows_mouse", EXPECTED_EQUAL_TO, 1)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-09 15:23:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
config->focus_follows_mouse = !strcasecmp(argv[0], "yes");
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-09 15:23:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-30 04:14:13 +02:00
|
|
|
static void hide_view_in_scratchpad(swayc_t *sp_view) {
|
|
|
|
if(sp_view == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlc_view_set_mask(sp_view->handle, 0);
|
|
|
|
sp_view->visible = false;
|
|
|
|
swayc_t *ws = sp_view->parent;
|
|
|
|
remove_child(sp_view);
|
|
|
|
if (swayc_active_workspace() != ws && ws->floating->length == 0 && ws->children->length == 0) {
|
|
|
|
destroy_workspace(ws);
|
|
|
|
}
|
|
|
|
arrange_windows(ws, -1, -1);
|
|
|
|
set_focused_container(container_under_pointer());
|
|
|
|
}
|
|
|
|
|
2015-08-18 13:50:15 +02:00
|
|
|
static bool cmd_move(struct sway_config *config, int argc, char **argv) {
|
2015-08-29 06:39:12 +02:00
|
|
|
if (!checkarg(argc, "move", EXPECTED_AT_LEAST, 1)) {
|
2015-08-20 22:27:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *view = get_focused_container(&root_container);
|
|
|
|
|
|
|
|
if (strcasecmp(argv[0], "left") == 0) {
|
2015-08-28 20:14:59 +02:00
|
|
|
move_container(view, MOVE_LEFT);
|
2015-08-20 22:27:38 +02:00
|
|
|
} else if (strcasecmp(argv[0], "right") == 0) {
|
2015-08-28 20:14:59 +02:00
|
|
|
move_container(view, MOVE_RIGHT);
|
2015-08-20 22:27:38 +02:00
|
|
|
} else if (strcasecmp(argv[0], "up") == 0) {
|
2015-08-28 20:14:59 +02:00
|
|
|
move_container(view, MOVE_UP);
|
2015-08-20 22:27:38 +02:00
|
|
|
} else if (strcasecmp(argv[0], "down") == 0) {
|
2015-08-28 20:14:59 +02:00
|
|
|
move_container(view, MOVE_DOWN);
|
2015-08-25 18:25:36 +02:00
|
|
|
} else if (strcasecmp(argv[0], "container") == 0 || strcasecmp(argv[0], "window") == 0) {
|
|
|
|
// "move container to workspace x"
|
|
|
|
if (!checkarg(argc, "move container/window", EXPECTED_EQUAL_TO, 4) ||
|
|
|
|
strcasecmp(argv[1], "to") != 0 ||
|
|
|
|
strcasecmp(argv[2], "workspace") != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view->type != C_CONTAINER && view->type != C_VIEW) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ws_name = argv[3];
|
|
|
|
if (argc == 5) {
|
|
|
|
// move "container to workspace number x"
|
|
|
|
ws_name = argv[4];
|
|
|
|
}
|
|
|
|
|
|
|
|
swayc_t *ws = workspace_by_name(ws_name);
|
|
|
|
if (ws == NULL) {
|
|
|
|
ws = workspace_create(ws_name);
|
|
|
|
}
|
2015-08-26 20:01:26 +02:00
|
|
|
move_container_to(view, get_focused_container(ws));
|
2015-08-29 06:39:12 +02:00
|
|
|
} else if (strcasecmp(argv[0], "scratchpad") == 0) {
|
2015-08-29 07:33:33 +02:00
|
|
|
if (view->type != C_CONTAINER && view->type != C_VIEW) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-29 06:39:12 +02:00
|
|
|
swayc_t *view = get_focused_container(&root_container);
|
2015-08-30 04:14:13 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < scratchpad->length; i++) {
|
|
|
|
if (scratchpad->items[i] == view) {
|
|
|
|
hide_view_in_scratchpad(view);
|
|
|
|
sp_view = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2015-08-29 06:39:12 +02:00
|
|
|
list_add(scratchpad, view);
|
2015-08-29 07:33:33 +02:00
|
|
|
if (!view->is_floating) {
|
|
|
|
destroy_container(remove_child(view));
|
2015-08-29 06:39:12 +02:00
|
|
|
} else {
|
2015-08-29 07:33:33 +02:00
|
|
|
remove_child(view);
|
|
|
|
}
|
|
|
|
wlc_view_set_mask(view->handle, 0);
|
|
|
|
arrange_windows(swayc_active_workspace(), -1, -1);
|
|
|
|
swayc_t *focused = container_under_pointer();
|
|
|
|
if (focused == NULL) {
|
|
|
|
focused = swayc_active_workspace();
|
2015-08-29 06:39:12 +02:00
|
|
|
}
|
2015-08-29 07:33:33 +02:00
|
|
|
set_focused_container(focused);
|
2015-08-20 23:29:36 +02:00
|
|
|
} else {
|
2015-08-20 22:27:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-18 13:50:15 +02:00
|
|
|
return true;
|
2015-08-20 22:45:00 +02:00
|
|
|
}
|
2015-08-20 22:27:38 +02:00
|
|
|
|
2015-08-28 04:52:59 +02:00
|
|
|
static bool cmd_orientation(struct sway_config *config, int argc, char **argv) {
|
2015-08-28 20:14:59 +02:00
|
|
|
if (strcasecmp(argv[0], "horizontal") == 0) {
|
2015-08-28 04:52:59 +02:00
|
|
|
config->default_orientation = L_HORIZ;
|
|
|
|
} else if (strcasecmp(argv[0], "vertical") == 0) {
|
|
|
|
config->default_orientation = L_VERT;
|
|
|
|
} else if (strcasecmp(argv[0], "auto") == 0) {
|
|
|
|
// Do nothing
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-22 17:18:55 +02:00
|
|
|
static bool cmd_output(struct sway_config *config, int argc, char **argv) {
|
|
|
|
if (!checkarg(argc, "output", EXPECTED_AT_LEAST, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct output_config *output = calloc(1, sizeof(struct output_config));
|
|
|
|
output->x = output->y = output->width = output->height = -1;
|
|
|
|
output->name = strdup(argv[0]);
|
2015-08-23 19:22:45 +02:00
|
|
|
output->enabled = true;
|
2015-08-22 17:18:55 +02:00
|
|
|
|
|
|
|
// TODO: atoi doesn't handle invalid numbers
|
2015-08-23 19:22:45 +02:00
|
|
|
|
2015-08-23 20:32:47 +02:00
|
|
|
if (strcasecmp(argv[1], "disable") == 0) {
|
2015-08-23 19:22:45 +02:00
|
|
|
output->enabled = false;
|
|
|
|
}
|
2015-08-22 17:18:55 +02:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
if (strcasecmp(argv[i], "resolution") == 0 || strcasecmp(argv[i], "res") == 0) {
|
|
|
|
char *res = argv[++i];
|
|
|
|
char *x = strchr(res, 'x');
|
|
|
|
int width = -1, height = -1;
|
|
|
|
if (x != NULL) {
|
|
|
|
// Format is 1234x4321
|
|
|
|
*x = '\0';
|
|
|
|
width = atoi(res);
|
|
|
|
height = atoi(x + 1);
|
|
|
|
*x = 'x';
|
|
|
|
} else {
|
|
|
|
// Format is 1234 4321
|
|
|
|
width = atoi(res);
|
|
|
|
res = argv[++i];
|
|
|
|
height = atoi(res);
|
|
|
|
}
|
|
|
|
output->width = width;
|
|
|
|
output->height = height;
|
|
|
|
} else if (strcasecmp(argv[i], "position") == 0 || strcasecmp(argv[i], "pos") == 0) {
|
|
|
|
char *res = argv[++i];
|
|
|
|
char *c = strchr(res, ',');
|
|
|
|
int x = -1, y = -1;
|
|
|
|
if (c != NULL) {
|
|
|
|
// Format is 1234,4321
|
|
|
|
*c = '\0';
|
|
|
|
x = atoi(res);
|
|
|
|
y = atoi(c + 1);
|
|
|
|
*c = ',';
|
|
|
|
} else {
|
|
|
|
// Format is 1234 4321
|
|
|
|
x = atoi(res);
|
|
|
|
res = argv[++i];
|
|
|
|
y = atoi(res);
|
|
|
|
}
|
|
|
|
output->x = x;
|
|
|
|
output->y = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add(config->output_configs, output);
|
|
|
|
|
|
|
|
sway_log(L_DEBUG, "Configured output %s to %d x %d @ %d, %d",
|
|
|
|
output->name, output->width, output->height, output->x, output->y);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-18 23:53:57 +02:00
|
|
|
static bool cmd_gaps(struct sway_config *config, int argc, char **argv) {
|
|
|
|
if (!checkarg(argc, "gaps", EXPECTED_AT_LEAST, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 1) {
|
2015-09-05 01:14:59 +02:00
|
|
|
int amount = (int)strtol(argv[0], NULL, 10);
|
2015-08-18 23:53:57 +02:00
|
|
|
if (errno == ERANGE || amount == 0) {
|
|
|
|
errno = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (config->gaps_inner == 0) {
|
|
|
|
config->gaps_inner = amount;
|
|
|
|
}
|
|
|
|
if (config->gaps_outer == 0) {
|
|
|
|
config->gaps_outer = amount;
|
|
|
|
}
|
|
|
|
} else if (argc == 2) {
|
2015-09-05 01:14:59 +02:00
|
|
|
int amount = (int)strtol(argv[1], NULL, 10);
|
2015-08-18 23:53:57 +02:00
|
|
|
if (errno == ERANGE || amount == 0) {
|
|
|
|
errno = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-19 00:44:50 +02:00
|
|
|
if (strcasecmp(argv[0], "inner") == 0) {
|
2015-08-18 23:53:57 +02:00
|
|
|
config->gaps_inner = amount;
|
2015-08-19 00:44:50 +02:00
|
|
|
} else if (strcasecmp(argv[0], "outer") == 0) {
|
2015-08-18 23:53:57 +02:00
|
|
|
config->gaps_outer = amount;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-05 01:14:59 +02:00
|
|
|
arrange_windows(&root_container, -1, -1);
|
2015-08-18 23:53:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-16 18:41:41 +02:00
|
|
|
static bool cmd_kill(struct sway_config *config, int argc, char **argv) {
|
|
|
|
swayc_t *view = get_focused_container(&root_container);
|
2015-08-16 18:54:37 +02:00
|
|
|
wlc_view_close(view->handle);
|
|
|
|
return true;
|
2015-08-16 18:41:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_layout(struct sway_config *config, int argc, char **argv) {
|
2015-08-13 16:48:03 +02:00
|
|
|
if (!checkarg(argc, "layout", EXPECTED_MORE_THAN, 0)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-09 20:35:59 +02:00
|
|
|
}
|
|
|
|
swayc_t *parent = get_focused_container(&root_container);
|
|
|
|
while (parent->type == C_VIEW) {
|
|
|
|
parent = parent->parent;
|
|
|
|
}
|
2015-08-18 13:19:20 +02:00
|
|
|
|
2015-08-09 20:35:59 +02:00
|
|
|
if (strcasecmp(argv[0], "splith") == 0) {
|
|
|
|
parent->layout = L_HORIZ;
|
|
|
|
} else if (strcasecmp(argv[0], "splitv") == 0) {
|
|
|
|
parent->layout = L_VERT;
|
|
|
|
} else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) {
|
|
|
|
if (parent->layout == L_VERT) {
|
|
|
|
parent->layout = L_HORIZ;
|
|
|
|
} else {
|
|
|
|
parent->layout = L_VERT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arrange_windows(parent, parent->width, parent->height);
|
|
|
|
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-09 20:35:59 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_reload(struct sway_config *config, int argc, char **argv) {
|
2015-08-13 16:50:46 +02:00
|
|
|
if (!checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-10 20:53:43 +02:00
|
|
|
}
|
2015-08-20 14:37:09 +02:00
|
|
|
if (!load_config(NULL)) { // TODO: Use config given from -c
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-10 21:22:22 +02:00
|
|
|
}
|
2015-08-10 21:38:15 +02:00
|
|
|
arrange_windows(&root_container, -1, -1);
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-10 20:53:43 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 18:30:32 +02:00
|
|
|
static bool cmd_resize(struct sway_config *config, int argc, char **argv) {
|
|
|
|
if (!checkarg(argc, "resize", EXPECTED_AT_LEAST, 3)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
char *end;
|
|
|
|
int amount = (int)strtol(argv[2], &end, 10);
|
|
|
|
if (errno == ERANGE || amount == 0) {
|
|
|
|
errno = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-22 05:26:11 +02:00
|
|
|
|
2015-08-20 18:30:32 +02:00
|
|
|
if (strcmp(argv[0], "shrink") != 0 && strcmp(argv[0], "grow") != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-22 05:26:11 +02:00
|
|
|
|
2015-08-20 18:30:32 +02:00
|
|
|
if (strcmp(argv[0], "shrink") == 0) {
|
|
|
|
amount *= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "width") == 0) {
|
2015-08-22 05:26:11 +02:00
|
|
|
return resize_tiled(amount, true);
|
2015-08-20 18:30:32 +02:00
|
|
|
} else if (strcmp(argv[1], "height") == 0) {
|
2015-08-22 05:26:11 +02:00
|
|
|
return resize_tiled(amount, false);
|
2015-08-20 18:30:32 +02:00
|
|
|
}
|
2015-08-22 05:26:11 +02:00
|
|
|
return false;
|
2015-08-20 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
2015-08-30 04:14:13 +02:00
|
|
|
static swayc_t *fetch_view_from_scratchpad() {
|
|
|
|
if (sp_index >= scratchpad->length) {
|
|
|
|
sp_index = 0;
|
|
|
|
}
|
|
|
|
swayc_t *view = scratchpad->items[sp_index++];
|
|
|
|
|
|
|
|
if (wlc_view_get_output(view->handle) != swayc_active_output()->handle) {
|
|
|
|
wlc_view_set_output(view->handle, swayc_active_output()->handle);
|
|
|
|
}
|
|
|
|
if (!view->is_floating) {
|
|
|
|
view->width = swayc_active_workspace()->width/2;
|
|
|
|
view->height = swayc_active_workspace()->height/2;
|
|
|
|
view->x = (swayc_active_workspace()->width - view->width)/2;
|
|
|
|
view->y = (swayc_active_workspace()->height - view->height)/2;
|
|
|
|
}
|
|
|
|
if (swayc_active_workspace()->width < view->x + 20 || view->x + view->width < 20) {
|
|
|
|
view->x = (swayc_active_workspace()->width - view->width)/2;
|
|
|
|
}
|
|
|
|
if (swayc_active_workspace()->height < view->y + 20 || view->y + view->height < 20) {
|
|
|
|
view->y = (swayc_active_workspace()->height - view->height)/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_floating(swayc_active_workspace(), view);
|
|
|
|
wlc_view_set_mask(view->handle, VISIBLE);
|
|
|
|
view->visible = true;
|
|
|
|
arrange_windows(swayc_active_workspace(), -1, -1);
|
|
|
|
set_focused_container(view);
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_view_from_scratchpad(swayc_t *view) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < scratchpad->length; i++) {
|
|
|
|
if (scratchpad->items[i] == view) {
|
|
|
|
if (sp_index == 0) {
|
|
|
|
sp_index = scratchpad->length - 1;
|
|
|
|
} else {
|
|
|
|
sp_index--;
|
|
|
|
}
|
|
|
|
list_del(scratchpad, sp_index);
|
|
|
|
sp_view = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 07:33:33 +02:00
|
|
|
static bool cmd_scratchpad(struct sway_config *config, int argc, char **argv) {
|
|
|
|
if (!checkarg(argc, "scratchpad", EXPECTED_EQUAL_TO, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-30 04:14:13 +02:00
|
|
|
|
|
|
|
if (strcasecmp(argv[0], "show") == 0 && scratchpad->length > 0) {
|
|
|
|
if (!sp_view) {
|
|
|
|
sp_view = fetch_view_from_scratchpad();
|
|
|
|
} else {
|
|
|
|
if (swayc_active_workspace() != sp_view->parent) {
|
|
|
|
hide_view_in_scratchpad(sp_view);
|
|
|
|
if (sp_index == 0) {
|
|
|
|
sp_index = scratchpad->length;
|
|
|
|
}
|
|
|
|
sp_index--;
|
|
|
|
sp_view = fetch_view_from_scratchpad();
|
|
|
|
} else {
|
|
|
|
hide_view_in_scratchpad(sp_view);
|
|
|
|
sp_view = NULL;
|
2015-08-29 07:33:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_set(struct sway_config *config, int argc, char **argv) {
|
2015-08-13 16:50:46 +02:00
|
|
|
if (!checkarg(argc, "set", EXPECTED_EQUAL_TO, 2)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-09 15:23:10 +02:00
|
|
|
}
|
|
|
|
struct sway_variable *var = malloc(sizeof(struct sway_variable));
|
|
|
|
var->name = malloc(strlen(argv[0]) + 1);
|
|
|
|
strcpy(var->name, argv[0]);
|
|
|
|
var->value = malloc(strlen(argv[1]) + 1);
|
|
|
|
strcpy(var->value, argv[1]);
|
|
|
|
list_add(config->symbols, var);
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-09 15:23:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool _do_split(struct sway_config *config, int argc, char **argv, int layout) {
|
2015-08-14 21:42:19 +02:00
|
|
|
char *name = layout == L_VERT ? "splitv" :
|
|
|
|
layout == L_HORIZ ? "splith" : "split";
|
2015-08-13 16:50:46 +02:00
|
|
|
if (!checkarg(argc, name, EXPECTED_EQUAL_TO, 0)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-09 20:35:56 +02:00
|
|
|
}
|
2015-08-10 01:27:25 +02:00
|
|
|
swayc_t *focused = get_focused_container(&root_container);
|
2015-08-14 21:42:19 +02:00
|
|
|
|
2015-08-21 19:28:37 +02:00
|
|
|
// Case of floating window, dont split
|
|
|
|
if (focused->is_floating) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/* Case that focus is on an workspace with 0/1 children.change its layout */
|
2015-08-15 20:19:44 +02:00
|
|
|
if (focused->type == C_WORKSPACE && focused->children->length <= 1) {
|
|
|
|
sway_log(L_DEBUG, "changing workspace layout");
|
2015-08-14 21:42:19 +02:00
|
|
|
focused->layout = layout;
|
2015-08-18 13:19:20 +02:00
|
|
|
} else if (focused->type != C_WORKSPACE && focused->parent->children->length == 1) {
|
|
|
|
/* Case of no siblings. change parent layout */
|
2015-08-15 20:19:44 +02:00
|
|
|
sway_log(L_DEBUG, "changing container layout");
|
2015-08-14 21:42:19 +02:00
|
|
|
focused->parent->layout = layout;
|
2015-08-18 13:19:20 +02:00
|
|
|
} else {
|
|
|
|
/* regular case where new split container is build around focused container
|
|
|
|
* or in case of workspace, container inherits its children */
|
2015-08-15 20:19:44 +02:00
|
|
|
sway_log(L_DEBUG, "Adding new container around current focused container");
|
|
|
|
swayc_t *parent = new_container(focused, layout);
|
2015-08-18 09:28:44 +02:00
|
|
|
set_focused_container(focused);
|
2015-08-15 20:19:44 +02:00
|
|
|
arrange_windows(parent, -1, -1);
|
|
|
|
}
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-09 20:35:56 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 17:01:01 +02:00
|
|
|
static bool cmd_split(struct sway_config *config, int argc, char **argv) {
|
|
|
|
if (!checkarg(argc, "split", EXPECTED_EQUAL_TO, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-18 13:19:20 +02:00
|
|
|
|
2015-08-16 17:01:01 +02:00
|
|
|
if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) {
|
2015-08-16 22:38:54 +02:00
|
|
|
_do_split(config, argc - 1, argv + 1, L_VERT);
|
2015-08-16 17:01:01 +02:00
|
|
|
} else if (strcasecmp(argv[0], "h") == 0 || strcasecmp(argv[0], "horizontal") == 0) {
|
2015-08-16 22:38:54 +02:00
|
|
|
_do_split(config, argc - 1, argv + 1, L_HORIZ);
|
2015-08-16 17:01:01 +02:00
|
|
|
} else {
|
|
|
|
sway_log(L_ERROR, "Invalid split command (expected either horiziontal or vertical).");
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-18 13:19:20 +02:00
|
|
|
|
2015-08-16 17:01:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_splitv(struct sway_config *config, int argc, char **argv) {
|
2015-08-10 01:27:25 +02:00
|
|
|
return _do_split(config, argc, argv, L_VERT);
|
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_splith(struct sway_config *config, int argc, char **argv) {
|
2015-08-10 01:27:25 +02:00
|
|
|
return _do_split(config, argc, argv, L_HORIZ);
|
|
|
|
}
|
2015-08-09 20:35:56 +02:00
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) {
|
2015-08-13 16:50:46 +02:00
|
|
|
if (!checkarg(argc, "log_colors", EXPECTED_EQUAL_TO, 1)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-10 02:20:40 +02:00
|
|
|
}
|
|
|
|
if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) {
|
|
|
|
sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]);
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-10 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sway_log_colors(!strcasecmp(argv[0], "yes"));
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-10 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) {
|
2015-08-16 17:01:01 +02:00
|
|
|
if (!checkarg(argc, "fullscreen", EXPECTED_AT_LEAST, 0)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-10 02:23:56 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 20:22:52 +02:00
|
|
|
swayc_t *container = get_focused_view(&root_container);
|
2015-08-21 19:45:18 +02:00
|
|
|
bool current = swayc_is_fullscreen(container);
|
2015-08-10 02:49:19 +02:00
|
|
|
wlc_view_set_state(container->handle, WLC_BIT_FULLSCREEN, !current);
|
2015-08-20 14:06:22 +02:00
|
|
|
// Resize workspace if going from fullscreen -> notfullscreen
|
|
|
|
// otherwise just resize container
|
2015-08-15 23:32:14 +02:00
|
|
|
if (current) {
|
2015-08-20 18:52:54 +02:00
|
|
|
container = swayc_parent_by_type(container, C_WORKSPACE);
|
2015-08-15 23:32:14 +02:00
|
|
|
}
|
2015-08-20 14:06:22 +02:00
|
|
|
// Only resize container when going into fullscreen
|
2015-08-10 02:23:56 +02:00
|
|
|
arrange_windows(container, -1, -1);
|
|
|
|
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-10 02:23:56 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 06:06:09 +02:00
|
|
|
static bool cmd_workspace(struct sway_config *config, int argc, char **argv) {
|
2015-08-18 02:34:53 +02:00
|
|
|
if (!checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1)) {
|
2015-08-10 23:51:18 +02:00
|
|
|
return false;
|
2015-08-10 22:31:23 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 02:34:53 +02:00
|
|
|
if (argc == 1) {
|
|
|
|
// Handle workspace next/prev
|
2015-08-31 02:59:14 +02:00
|
|
|
if (strcasecmp(argv[0], "next") == 0) {
|
2015-08-25 19:00:20 +02:00
|
|
|
workspace_switch(workspace_next());
|
2015-08-18 02:34:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-16 21:50:01 +02:00
|
|
|
|
2015-08-31 02:59:14 +02:00
|
|
|
if (strcasecmp(argv[0], "prev") == 0) {
|
2015-08-25 19:00:20 +02:00
|
|
|
workspace_switch(workspace_prev());
|
2015-08-18 02:34:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-16 21:50:01 +02:00
|
|
|
|
2015-08-18 02:34:53 +02:00
|
|
|
// Handle workspace output_next/prev
|
2015-08-31 02:59:14 +02:00
|
|
|
if (strcasecmp(argv[0], "next_on_output") == 0) {
|
2015-08-25 19:00:20 +02:00
|
|
|
workspace_switch(workspace_output_next());
|
2015-08-18 02:34:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-16 21:50:01 +02:00
|
|
|
|
2015-08-31 02:59:14 +02:00
|
|
|
if (strcasecmp(argv[0], "prev_on_output") == 0) {
|
2015-08-25 19:00:20 +02:00
|
|
|
workspace_switch(workspace_output_prev());
|
2015-08-18 02:34:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-31 02:59:14 +02:00
|
|
|
if (strcasecmp(argv[0], "back_and_forth") == 0) {
|
|
|
|
if (prev_workspace_name) {
|
2015-09-01 18:02:26 +02:00
|
|
|
swayc_t *ws = workspace_by_name(prev_workspace_name);
|
|
|
|
workspace_switch(ws ? ws : workspace_create(prev_workspace_name));
|
2015-08-31 02:59:14 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-16 21:50:01 +02:00
|
|
|
|
2015-08-21 19:28:37 +02:00
|
|
|
swayc_t *workspace = workspace_by_name(argv[0]);
|
2015-08-18 02:34:53 +02:00
|
|
|
if (!workspace) {
|
|
|
|
workspace = workspace_create(argv[0]);
|
|
|
|
}
|
|
|
|
workspace_switch(workspace);
|
|
|
|
} else {
|
|
|
|
if (strcasecmp(argv[1], "output") == 0) {
|
|
|
|
if (!checkarg(argc, "workspace", EXPECTED_EQUAL_TO, 3)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct workspace_output *wso = calloc(1, sizeof(struct workspace_output));
|
|
|
|
sway_log(L_DEBUG, "Assigning workspace %s to output %s", argv[0], argv[2]);
|
|
|
|
wso->workspace = strdup(argv[0]);
|
|
|
|
wso->output = strdup(argv[2]);
|
|
|
|
list_add(config->workspace_outputs, wso);
|
|
|
|
// TODO: Consider moving any existing workspace to that output? This might be executed sometime after config load
|
|
|
|
}
|
2015-08-14 21:42:19 +02:00
|
|
|
}
|
2015-08-10 23:51:18 +02:00
|
|
|
return true;
|
2015-08-10 22:31:23 +02:00
|
|
|
}
|
|
|
|
|
2015-08-31 04:34:10 +02:00
|
|
|
static bool cmd_ws_auto_back_and_forth(struct sway_config *config, int argc, char **argv) {
|
|
|
|
if (!checkarg(argc, "workspace_auto_back_and_forth", EXPECTED_EQUAL_TO, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (strcasecmp(argv[0], "yes") == 0) {
|
|
|
|
config->auto_back_and_forth = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-05 23:30:40 +02:00
|
|
|
/* Keep alphabetized */
|
2015-08-13 06:51:38 +02:00
|
|
|
static struct cmd_handler handlers[] = {
|
2015-08-06 04:40:38 +02:00
|
|
|
{ "bindsym", cmd_bindsym },
|
2015-08-28 04:52:59 +02:00
|
|
|
{ "default_orientation", cmd_orientation },
|
2015-08-09 18:26:32 +02:00
|
|
|
{ "exec", cmd_exec },
|
2015-08-10 20:53:43 +02:00
|
|
|
{ "exec_always", cmd_exec_always },
|
2015-08-09 01:24:18 +02:00
|
|
|
{ "exit", cmd_exit },
|
2015-08-17 07:38:34 +02:00
|
|
|
{ "floating", cmd_floating },
|
2015-08-18 08:33:15 +02:00
|
|
|
{ "floating_modifier", cmd_floating_mod },
|
2015-08-10 02:10:26 +02:00
|
|
|
{ "focus", cmd_focus },
|
2015-08-09 15:23:10 +02:00
|
|
|
{ "focus_follows_mouse", cmd_focus_follows_mouse },
|
2015-08-10 02:23:56 +02:00
|
|
|
{ "fullscreen", cmd_fullscreen },
|
2015-08-18 23:53:57 +02:00
|
|
|
{ "gaps", cmd_gaps },
|
2015-08-16 18:41:41 +02:00
|
|
|
{ "kill", cmd_kill },
|
2015-08-09 20:35:59 +02:00
|
|
|
{ "layout", cmd_layout },
|
2015-08-10 02:20:40 +02:00
|
|
|
{ "log_colors", cmd_log_colors },
|
2015-08-20 23:29:36 +02:00
|
|
|
{ "move", cmd_move},
|
2015-08-22 17:18:55 +02:00
|
|
|
{ "output", cmd_output},
|
2015-08-10 20:53:43 +02:00
|
|
|
{ "reload", cmd_reload },
|
2015-08-20 18:30:32 +02:00
|
|
|
{ "resize", cmd_resize },
|
2015-08-29 06:39:12 +02:00
|
|
|
{ "scratchpad", cmd_scratchpad },
|
2015-08-09 01:24:18 +02:00
|
|
|
{ "set", cmd_set },
|
2015-08-16 17:01:01 +02:00
|
|
|
{ "split", cmd_split },
|
2015-08-10 01:27:25 +02:00
|
|
|
{ "splith", cmd_splith },
|
2015-08-10 22:31:23 +02:00
|
|
|
{ "splitv", cmd_splitv },
|
2015-08-31 04:34:10 +02:00
|
|
|
{ "workspace", cmd_workspace },
|
|
|
|
{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }
|
2015-08-05 23:30:40 +02:00
|
|
|
};
|
|
|
|
|
2015-08-13 06:51:38 +02:00
|
|
|
static char **split_directive(char *line, int *argc) {
|
2015-08-06 04:10:56 +02:00
|
|
|
const char *delimiters = " ";
|
2015-08-05 23:30:40 +02:00
|
|
|
*argc = 0;
|
|
|
|
while (isspace(*line) && *line) ++line;
|
|
|
|
|
|
|
|
int capacity = 10;
|
|
|
|
char **parts = malloc(sizeof(char *) * capacity);
|
|
|
|
|
|
|
|
if (!*line) return parts;
|
|
|
|
|
|
|
|
int in_string = 0, in_character = 0;
|
2015-08-18 14:39:26 +02:00
|
|
|
int i, j, _;
|
2015-08-05 23:30:40 +02:00
|
|
|
for (i = 0, j = 0; line[i]; ++i) {
|
|
|
|
if (line[i] == '\\') {
|
|
|
|
++i;
|
|
|
|
} else if (line[i] == '"' && !in_character) {
|
|
|
|
in_string = !in_string;
|
|
|
|
} else if (line[i] == '\'' && !in_string) {
|
|
|
|
in_character = !in_character;
|
|
|
|
} else if (!in_character && !in_string) {
|
|
|
|
if (strchr(delimiters, line[i]) != NULL) {
|
|
|
|
char *item = malloc(i - j + 1);
|
|
|
|
strncpy(item, line + j, i - j);
|
|
|
|
item[i - j] = '\0';
|
2015-08-18 14:39:26 +02:00
|
|
|
item = strip_whitespace(item, &_);
|
2015-08-05 23:30:40 +02:00
|
|
|
if (item[0] == '\0') {
|
|
|
|
free(item);
|
|
|
|
} else {
|
|
|
|
if (*argc == capacity) {
|
|
|
|
capacity *= 2;
|
|
|
|
parts = realloc(parts, sizeof(char *) * capacity);
|
|
|
|
}
|
|
|
|
parts[*argc] = item;
|
|
|
|
j = i + 1;
|
|
|
|
++*argc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char *item = malloc(i - j + 1);
|
|
|
|
strncpy(item, line + j, i - j);
|
|
|
|
item[i - j] = '\0';
|
2015-08-18 14:39:26 +02:00
|
|
|
item = strip_whitespace(item, &_);
|
2015-08-05 23:30:40 +02:00
|
|
|
if (*argc == capacity) {
|
|
|
|
capacity++;
|
|
|
|
parts = realloc(parts, sizeof(char *) * capacity);
|
|
|
|
}
|
|
|
|
parts[*argc] = item;
|
|
|
|
++*argc;
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2015-08-13 06:51:38 +02:00
|
|
|
static int handler_compare(const void *_a, const void *_b) {
|
2015-08-05 23:30:40 +02:00
|
|
|
const struct cmd_handler *a = _a;
|
|
|
|
const struct cmd_handler *b = _b;
|
|
|
|
return strcasecmp(a->command, b->command);
|
|
|
|
}
|
|
|
|
|
2015-08-13 06:51:38 +02:00
|
|
|
static struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) {
|
2015-08-05 23:30:40 +02:00
|
|
|
struct cmd_handler d = { .command=line };
|
|
|
|
struct cmd_handler *res = bsearch(&d, handlers, l, sizeof(struct cmd_handler), handler_compare);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-08-11 02:32:50 +02:00
|
|
|
bool handle_command(struct sway_config *config, char *exec) {
|
2015-08-09 01:24:18 +02:00
|
|
|
sway_log(L_INFO, "Handling command '%s'", exec);
|
2015-08-05 23:30:40 +02:00
|
|
|
char *ptr, *cmd;
|
2015-08-10 23:51:18 +02:00
|
|
|
bool exec_success;
|
2015-08-10 22:16:38 +02:00
|
|
|
|
2015-08-05 23:30:40 +02:00
|
|
|
if ((ptr = strchr(exec, ' ')) == NULL) {
|
2015-08-10 22:16:38 +02:00
|
|
|
cmd = exec;
|
2015-08-05 23:30:40 +02:00
|
|
|
} else {
|
|
|
|
int index = ptr - exec;
|
|
|
|
cmd = malloc(index + 1);
|
|
|
|
strncpy(cmd, exec, index);
|
|
|
|
cmd[index] = '\0';
|
|
|
|
}
|
|
|
|
struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd);
|
|
|
|
if (handler == NULL) {
|
2015-08-08 23:01:22 +02:00
|
|
|
sway_log(L_ERROR, "Unknown command '%s'", cmd);
|
2015-08-10 23:51:18 +02:00
|
|
|
exec_success = false; // TODO: return error, probably
|
2015-08-10 22:16:38 +02:00
|
|
|
} else {
|
|
|
|
int argc;
|
|
|
|
char **argv = split_directive(exec + strlen(handler->command), &argc);
|
|
|
|
int i;
|
2015-08-11 02:50:22 +02:00
|
|
|
|
2015-08-20 14:06:22 +02:00
|
|
|
// Perform var subs on all parts of the command
|
2015-08-11 02:50:22 +02:00
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
argv[i] = do_var_replacement(config, argv[i]);
|
|
|
|
}
|
|
|
|
|
2015-08-10 23:51:18 +02:00
|
|
|
exec_success = handler->handle(config, argc, argv);
|
2015-08-10 22:16:38 +02:00
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
free(argv[i]);
|
|
|
|
}
|
|
|
|
free(argv);
|
2015-08-10 23:51:18 +02:00
|
|
|
if (!exec_success) {
|
2015-08-10 22:16:38 +02:00
|
|
|
sway_log(L_ERROR, "Command failed: %s", cmd);
|
|
|
|
}
|
2015-08-06 04:10:56 +02:00
|
|
|
}
|
2015-08-13 09:24:03 +02:00
|
|
|
if (ptr) {
|
2015-08-10 22:16:38 +02:00
|
|
|
free(cmd);
|
2015-08-10 21:45:36 +02:00
|
|
|
}
|
2015-08-10 23:51:18 +02:00
|
|
|
return exec_success;
|
2015-08-05 23:30:40 +02:00
|
|
|
}
|