Merge pull request #177 from taiyu-len/master

cmd status + workspace ws output op
This commit is contained in:
Drew DeVault 2015-09-10 14:18:56 -04:00
commit f5343adae4
7 changed files with 297 additions and 251 deletions

View File

@ -5,17 +5,16 @@
struct cmd_handler { struct cmd_handler {
char *command; char *command;
bool (*handle)(int argc, char **argv); enum cmd_status {
enum { CMD_SUCCESS,
CMD_COMPOSITOR_READY, CMD_FAILURE,
CMD_KEYBIND, CMD_DEFER,
CMD_ANYTIME } (*handle)(int argc, char **argv);
} config_type;
}; };
bool handle_command(char *command); enum cmd_status handle_command(char *command);
// Handles commands during config // Handles commands during config
bool config_command(char *command); enum cmd_status config_command(char *command);
void remove_view_from_scratchpad(); void remove_view_from_scratchpad();

View File

@ -52,6 +52,7 @@ struct sway_config {
bool active; bool active;
bool failed; bool failed;
bool reloading; bool reloading;
bool reading;
bool auto_back_and_forth; bool auto_back_and_forth;
int gaps_inner; int gaps_inner;

View File

@ -18,15 +18,13 @@
#include "sway.h" #include "sway.h"
#include "resize.h" #include "resize.h"
struct modifier_key {
char *name;
uint32_t mod;
};
swayc_t *sp_view; swayc_t *sp_view;
int sp_index = 0; int sp_index = 0;
static struct modifier_key modifiers[] = { static struct modifier_key {
char *name;
uint32_t mod;
} modifiers[] = {
{ XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT }, { XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT },
{ XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS }, { XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS },
{ XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL }, { XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL },
@ -46,14 +44,14 @@ enum expected_args {
EXPECTED_EQUAL_TO EXPECTED_EQUAL_TO
}; };
static bool checkarg(int argc, char *name, enum expected_args type, int val) { static bool checkarg(int argc, const char *name, enum expected_args type, int val) {
switch (type) { switch (type) {
case EXPECTED_MORE_THAN: case EXPECTED_MORE_THAN:
if (argc > val) { if (argc > val) {
return true; return true;
} }
sway_log(L_ERROR, "Invalid %s command." sway_log(L_ERROR, "Invalid %s command."
"(expected more than %d argument%s, got %d", "(expected more than %d argument%s, got %d)",
name, val, (char*[2]){"s", ""}[argc==1], argc); name, val, (char*[2]){"s", ""}[argc==1], argc);
break; break;
case EXPECTED_AT_LEAST: case EXPECTED_AT_LEAST:
@ -61,7 +59,7 @@ static bool checkarg(int argc, char *name, enum expected_args type, int val) {
return true; return true;
} }
sway_log(L_ERROR, "Invalid %s command." sway_log(L_ERROR, "Invalid %s command."
"(expected at least %d argument%s, got %d", "(expected at least %d argument%s, got %d)",
name, val, (char*[2]){"s", ""}[argc==1], argc); name, val, (char*[2]){"s", ""}[argc==1], argc);
break; break;
case EXPECTED_LESS_THAN: case EXPECTED_LESS_THAN:
@ -69,7 +67,7 @@ static bool checkarg(int argc, char *name, enum expected_args type, int val) {
return true; return true;
}; };
sway_log(L_ERROR, "Invalid %s command." sway_log(L_ERROR, "Invalid %s command."
"(expected less than %d argument%s, got %d", "(expected less than %d argument%s, got %d)",
name, val, (char*[2]){"s", ""}[argc==1], argc); name, val, (char*[2]){"s", ""}[argc==1], argc);
break; break;
case EXPECTED_EQUAL_TO: case EXPECTED_EQUAL_TO:
@ -77,7 +75,7 @@ static bool checkarg(int argc, char *name, enum expected_args type, int val) {
return true; return true;
}; };
sway_log(L_ERROR, "Invalid %s command." sway_log(L_ERROR, "Invalid %s command."
"(expected %d arguments, got %d", name, val, argc); "(expected %d arguments, got %d)", name, val, argc);
break; break;
} }
return false; return false;
@ -96,9 +94,10 @@ static int bindsym_sort(const void *_lbind, const void *_rbind) {
return (rbind->keys->length + rmod) - (lbind->keys->length + lmod); return (rbind->keys->length + rmod) - (lbind->keys->length + lmod);
} }
static bool cmd_bindsym(int argc, char **argv) { static enum cmd_status cmd_bindsym(int argc, char **argv) {
if (!checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1)) { if (!checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1)
return false; || !config->reading) {
return CMD_FAILURE;
}; };
struct sway_binding *binding = malloc(sizeof(struct sway_binding)); struct sway_binding *binding = malloc(sizeof(struct sway_binding));
@ -128,7 +127,7 @@ static bool cmd_bindsym(int argc, char **argv) {
free(binding->command); free(binding->command);
free(binding); free(binding);
list_free(split); list_free(split);
return false; return CMD_FAILURE;
} }
xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
*key = sym; *key = sym;
@ -142,19 +141,22 @@ static bool cmd_bindsym(int argc, char **argv) {
list_sort(mode->bindings, bindsym_sort); list_sort(mode->bindings, bindsym_sort);
sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command); sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command);
return true; return CMD_SUCCESS;
} }
static bool cmd_exec_always(int argc, char **argv) { static enum cmd_status cmd_exec_always(int argc, char **argv) {
if (!checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0)) { if (!checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0)) {
return false; return CMD_FAILURE;
}
if (!config->active) {
return CMD_DEFER;
} }
pid_t pid = fork(); pid_t pid = fork();
/* Failed to fork */ /* Failed to fork */
if (pid < 0) { if (pid < 0) {
sway_log(L_ERROR, "exec command failed, sway did not fork"); sway_log(L_ERROR, "exec command failed, sway did not fork");
return false; return CMD_FAILURE;
} }
/* Child process */ /* Child process */
if (pid == 0) { if (pid == 0) {
@ -167,15 +169,18 @@ static bool cmd_exec_always(int argc, char **argv) {
exit(-1); exit(-1);
} }
/* Parent */ /* Parent */
return true; return CMD_SUCCESS;
} }
static bool cmd_exec(int argc, char **argv) { static enum cmd_status cmd_exec(int argc, char **argv) {
if (!config->active) {
return CMD_DEFER;
}
if (config->reloading) { if (config->reloading) {
char *args = join_args(argv, argc); char *args = join_args(argv, argc);
sway_log(L_DEBUG, "Ignoring exec %s due to reload", args); sway_log(L_DEBUG, "Ignoring 'exec %s' due to reload", args);
free(args); free(args);
return true; return CMD_SUCCESS;
} }
return cmd_exec_always(argc, argv); return cmd_exec_always(argc, argv);
} }
@ -186,26 +191,28 @@ static void kill_views(swayc_t *container, void *data) {
} }
} }
static bool cmd_exit(int argc, char **argv) { static enum cmd_status cmd_exit(int argc, char **argv) {
if (!checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0)) { if (!checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0)
return false; || config->reading || !config->active) {
return CMD_FAILURE;
} }
// Close all views // Close all views
container_map(&root_container, kill_views, NULL); container_map(&root_container, kill_views, NULL);
sway_terminate(); sway_terminate();
return true; return CMD_SUCCESS;
} }
static bool cmd_floating(int argc, char **argv) { static enum cmd_status cmd_floating(int argc, char **argv) {
if (!checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1)) { if (!checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1)
return false; || config->reading || !config->active) {
return CMD_FAILURE;
} }
if (strcasecmp(argv[0], "toggle") == 0) { if (strcasecmp(argv[0], "toggle") == 0) {
swayc_t *view = get_focused_container(&root_container); swayc_t *view = get_focused_container(&root_container);
// Prevent running floating commands on things like workspaces // Prevent running floating commands on things like workspaces
if (view->type != C_VIEW) { if (view->type != C_VIEW) {
return true; return CMD_SUCCESS;
} }
// Change from nonfloating to floating // Change from nonfloating to floating
if (!view->is_floating) { if (!view->is_floating) {
@ -254,12 +261,13 @@ static bool cmd_floating(int argc, char **argv) {
} }
set_focused_container(view); set_focused_container(view);
} }
return true; return CMD_SUCCESS;
} }
static bool cmd_floating_mod(int argc, char **argv) { static enum cmd_status cmd_floating_mod(int argc, char **argv) {
if (!checkarg(argc, "floating_modifier", EXPECTED_EQUAL_TO, 1)) { if (!checkarg(argc, "floating_modifier", EXPECTED_EQUAL_TO, 1)
return false; || !config->reading) {
return CMD_FAILURE;
} }
int i, j; int i, j;
list_t *split = split_string(argv[0], "+"); list_t *split = split_string(argv[0], "+");
@ -276,27 +284,28 @@ static bool cmd_floating_mod(int argc, char **argv) {
free_flat_list(split); free_flat_list(split);
if (!config->floating_mod) { if (!config->floating_mod) {
sway_log(L_ERROR, "bindsym - unknown keys %s", argv[0]); sway_log(L_ERROR, "bindsym - unknown keys %s", argv[0]);
return false; return CMD_FAILURE;
} }
return true; return CMD_SUCCESS;
} }
static bool cmd_focus(int argc, char **argv) { static enum cmd_status cmd_focus(int argc, char **argv) {
static int floating_toggled_index = 0; static int floating_toggled_index = 0;
static int tiled_toggled_index = 0; static int tiled_toggled_index = 0;
if (!checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1)) { if (!checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1)
return false; || config->reading || !config->active) {
return CMD_FAILURE;
} }
if (strcasecmp(argv[0], "left") == 0) { if (strcasecmp(argv[0], "left") == 0) {
return move_focus(MOVE_LEFT); move_focus(MOVE_LEFT);
} else if (strcasecmp(argv[0], "right") == 0) { } else if (strcasecmp(argv[0], "right") == 0) {
return move_focus(MOVE_RIGHT); move_focus(MOVE_RIGHT);
} else if (strcasecmp(argv[0], "up") == 0) { } else if (strcasecmp(argv[0], "up") == 0) {
return move_focus(MOVE_UP); move_focus(MOVE_UP);
} else if (strcasecmp(argv[0], "down") == 0) { } else if (strcasecmp(argv[0], "down") == 0) {
return move_focus(MOVE_DOWN); move_focus(MOVE_DOWN);
} else if (strcasecmp(argv[0], "parent") == 0) { } else if (strcasecmp(argv[0], "parent") == 0) {
return move_focus(MOVE_PARENT); move_focus(MOVE_PARENT);
} else if (strcasecmp(argv[0], "mode_toggle") == 0) { } else if (strcasecmp(argv[0], "mode_toggle") == 0) {
int i; int i;
swayc_t *workspace = swayc_active_workspace(); swayc_t *workspace = swayc_active_workspace();
@ -335,17 +344,16 @@ static bool cmd_focus(int argc, char **argv) {
} }
} }
} }
return CMD_SUCCESS;
return true;
} }
static bool cmd_focus_follows_mouse(int argc, char **argv) { static enum cmd_status cmd_focus_follows_mouse(int argc, char **argv) {
if (!checkarg(argc, "focus_follows_mouse", EXPECTED_EQUAL_TO, 1)) { if (!checkarg(argc, "focus_follows_mouse", EXPECTED_EQUAL_TO, 1)) {
return false; return CMD_FAILURE;
} }
config->focus_follows_mouse = !strcasecmp(argv[0], "yes"); config->focus_follows_mouse = !strcasecmp(argv[0], "yes");
return true; return CMD_SUCCESS;
} }
static void hide_view_in_scratchpad(swayc_t *sp_view) { static void hide_view_in_scratchpad(swayc_t *sp_view) {
@ -364,12 +372,16 @@ static void hide_view_in_scratchpad(swayc_t *sp_view) {
set_focused_container(container_under_pointer()); set_focused_container(container_under_pointer());
} }
static bool cmd_mode(int argc, char **argv) { static enum cmd_status cmd_mode(int argc, char **argv) {
if (!checkarg(argc, "move", EXPECTED_AT_LEAST, 1)) { if (!checkarg(argc, "move", EXPECTED_AT_LEAST, 1)) {
return false; return CMD_FAILURE;
} }
bool mode_make = strcmp(argv[argc-1], "{") == 0; bool mode_make = !strcmp(argv[argc-1], "{");
const char *mode_name = join_args(argv, argc - mode_make); if (mode_make && !config->reading) {
return CMD_FAILURE;
}
char *mode_name = join_args(argv, argc - mode_make);
struct sway_mode *mode = NULL; struct sway_mode *mode = NULL;
// Find mode // Find mode
int i, len = config->modes->length; int i, len = config->modes->length;
@ -389,17 +401,20 @@ static bool cmd_mode(int argc, char **argv) {
} }
if (!mode) { if (!mode) {
sway_log(L_ERROR, "Unknown mode `%s'", mode_name); sway_log(L_ERROR, "Unknown mode `%s'", mode_name);
return false; free(mode_name);
return CMD_FAILURE;
} }
sway_log(L_DEBUG, "Switching to mode `%s'",mode->name); sway_log(L_DEBUG, "Switching to mode `%s'",mode->name);
free(mode_name);
// Set current mode // Set current mode
config->current_mode = mode; config->current_mode = mode;
return true; return CMD_SUCCESS;
} }
static bool cmd_move(int argc, char **argv) { static enum cmd_status cmd_move(int argc, char **argv) {
if (!checkarg(argc, "move", EXPECTED_AT_LEAST, 1)) { if (!checkarg(argc, "move", EXPECTED_AT_LEAST, 1)
return false; || config->reading || !config->active) {
return CMD_FAILURE;
} }
swayc_t *view = get_focused_container(&root_container); swayc_t *view = get_focused_container(&root_container);
@ -414,14 +429,14 @@ static bool cmd_move(int argc, char **argv) {
move_container(view, MOVE_DOWN); move_container(view, MOVE_DOWN);
} else if (strcasecmp(argv[0], "container") == 0 || strcasecmp(argv[0], "window") == 0) { } else if (strcasecmp(argv[0], "container") == 0 || strcasecmp(argv[0], "window") == 0) {
// "move container to workspace x" // "move container to workspace x"
if (!checkarg(argc, "move container/window", EXPECTED_EQUAL_TO, 4) || if (!checkarg(argc, "move container/window", EXPECTED_EQUAL_TO, 4)
strcasecmp(argv[1], "to") != 0 || || strcasecmp(argv[1], "to") != 0
strcasecmp(argv[2], "workspace") != 0) { || strcasecmp(argv[2], "workspace") != 0) {
return false; return CMD_FAILURE;
} }
if (view->type != C_CONTAINER && view->type != C_VIEW) { if (view->type != C_CONTAINER && view->type != C_VIEW) {
return false; return CMD_FAILURE;
} }
const char *ws_name = argv[3]; const char *ws_name = argv[3];
@ -437,7 +452,7 @@ static bool cmd_move(int argc, char **argv) {
move_container_to(view, get_focused_container(ws)); move_container_to(view, get_focused_container(ws));
} else if (strcasecmp(argv[0], "scratchpad") == 0) { } else if (strcasecmp(argv[0], "scratchpad") == 0) {
if (view->type != C_CONTAINER && view->type != C_VIEW) { if (view->type != C_CONTAINER && view->type != C_VIEW) {
return false; return CMD_FAILURE;
} }
swayc_t *view = get_focused_container(&root_container); swayc_t *view = get_focused_container(&root_container);
int i; int i;
@ -445,7 +460,7 @@ static bool cmd_move(int argc, char **argv) {
if (scratchpad->items[i] == view) { if (scratchpad->items[i] == view) {
hide_view_in_scratchpad(view); hide_view_in_scratchpad(view);
sp_view = NULL; sp_view = NULL;
return true; return CMD_SUCCESS;
} }
} }
list_add(scratchpad, view); list_add(scratchpad, view);
@ -462,12 +477,16 @@ static bool cmd_move(int argc, char **argv) {
} }
set_focused_container(focused); set_focused_container(focused);
} else { } else {
return false; return CMD_FAILURE;
} }
return true; return CMD_SUCCESS;
} }
static bool cmd_orientation(int argc, char **argv) { static enum cmd_status cmd_orientation(int argc, char **argv) {
if (!checkarg(argc, "orientation", EXPECTED_EQUAL_TO, 1)
|| !config->reading) {
return CMD_FAILURE;
}
if (strcasecmp(argv[0], "horizontal") == 0) { if (strcasecmp(argv[0], "horizontal") == 0) {
config->default_orientation = L_HORIZ; config->default_orientation = L_HORIZ;
} else if (strcasecmp(argv[0], "vertical") == 0) { } else if (strcasecmp(argv[0], "vertical") == 0) {
@ -475,14 +494,14 @@ static bool cmd_orientation(int argc, char **argv) {
} else if (strcasecmp(argv[0], "auto") == 0) { } else if (strcasecmp(argv[0], "auto") == 0) {
// Do nothing // Do nothing
} else { } else {
return false; return CMD_FAILURE;
} }
return true; return CMD_SUCCESS;
} }
static bool cmd_output(int argc, char **argv) { static enum cmd_status cmd_output(int argc, char **argv) {
if (!checkarg(argc, "output", EXPECTED_AT_LEAST, 1)) { if (!checkarg(argc, "output", EXPECTED_AT_LEAST, 1)) {
return false; return CMD_FAILURE;
} }
struct output_config *output = calloc(1, sizeof(struct output_config)); struct output_config *output = calloc(1, sizeof(struct output_config));
@ -542,12 +561,12 @@ static bool cmd_output(int argc, char **argv) {
sway_log(L_DEBUG, "Configured output %s to %d x %d @ %d, %d", sway_log(L_DEBUG, "Configured output %s to %d x %d @ %d, %d",
output->name, output->width, output->height, output->x, output->y); output->name, output->width, output->height, output->x, output->y);
return true; return CMD_SUCCESS;
} }
static bool cmd_gaps(int argc, char **argv) { static enum cmd_status cmd_gaps(int argc, char **argv) {
if (!checkarg(argc, "gaps", EXPECTED_AT_LEAST, 1)) { if (!checkarg(argc, "gaps", EXPECTED_AT_LEAST, 1)) {
return false; return CMD_FAILURE;
} }
const char *amount_str = argv[0]; const char *amount_str = argv[0];
// gaps amount // gaps amount
@ -555,7 +574,7 @@ static bool cmd_gaps(int argc, char **argv) {
int amount = (int)strtol(amount_str, NULL, 10); int amount = (int)strtol(amount_str, NULL, 10);
if (errno == ERANGE || amount == 0) { if (errno == ERANGE || amount == 0) {
errno = 0; errno = 0;
return false; return CMD_FAILURE;
} }
if (config->gaps_inner == 0) { if (config->gaps_inner == 0) {
config->gaps_inner = amount; config->gaps_inner = amount;
@ -563,14 +582,14 @@ static bool cmd_gaps(int argc, char **argv) {
if (config->gaps_outer == 0) { if (config->gaps_outer == 0) {
config->gaps_outer = amount; config->gaps_outer = amount;
} }
return true; return CMD_SUCCESS;
} }
// gaps inner|outer n // gaps inner|outer n
else if (argc >= 2 && isdigit((amount_str = argv[1])[0])) { else if (argc >= 2 && isdigit((amount_str = argv[1])[0])) {
int amount = (int)strtol(amount_str, NULL, 10); int amount = (int)strtol(amount_str, NULL, 10);
if (errno == ERANGE || amount == 0) { if (errno == ERANGE || amount == 0) {
errno = 0; errno = 0;
return false; return CMD_FAILURE;
} }
const char *target_str = argv[0]; const char *target_str = argv[0];
if (strcasecmp(target_str, "inner") == 0) { if (strcasecmp(target_str, "inner") == 0) {
@ -578,11 +597,11 @@ static bool cmd_gaps(int argc, char **argv) {
} else if (strcasecmp(target_str, "outer") == 0) { } else if (strcasecmp(target_str, "outer") == 0) {
config->gaps_outer = amount; config->gaps_outer = amount;
} }
return true; return CMD_SUCCESS;
} }
// gaps inner|outer current|all set|plus|minus n // gaps inner|outer current|all set|plus|minus n
if (argc < 4) { if (argc < 4 || config->reading) {
return false; return CMD_FAILURE;
} }
// gaps inner|outer ... // gaps inner|outer ...
const char *inout_str = argv[0]; const char *inout_str = argv[0];
@ -592,7 +611,7 @@ static bool cmd_gaps(int argc, char **argv) {
} else if (strcasecmp(inout_str, "outer") == 0) { } else if (strcasecmp(inout_str, "outer") == 0) {
inout = OUTER; inout = OUTER;
} else { } else {
return false; return CMD_FAILURE;
} }
// gaps ... current|all ... // gaps ... current|all ...
@ -610,7 +629,7 @@ static bool cmd_gaps(int argc, char **argv) {
target = WORKSPACE; target = WORKSPACE;
} }
} else { } else {
return false; return CMD_FAILURE;
} }
// gaps ... n // gaps ... n
@ -618,7 +637,7 @@ static bool cmd_gaps(int argc, char **argv) {
int amount = (int)strtol(amount_str, NULL, 10); int amount = (int)strtol(amount_str, NULL, 10);
if (errno == ERANGE || amount == 0) { if (errno == ERANGE || amount == 0) {
errno = 0; errno = 0;
return false; return CMD_FAILURE;
} }
// gaps ... set|plus|minus ... // gaps ... set|plus|minus ...
@ -632,18 +651,18 @@ static bool cmd_gaps(int argc, char **argv) {
method = ADD; method = ADD;
amount *= -1; amount *= -1;
} else { } else {
return false; return CMD_FAILURE;
} }
if (target == CURRENT) { if (target == CURRENT) {
swayc_t *cont; swayc_t *cont;
if (inout == OUTER) { if (inout == OUTER) {
if ((cont = swayc_active_workspace()) == NULL) { if ((cont = swayc_active_workspace()) == NULL) {
return false; return CMD_FAILURE;
} }
} else { } else {
if ((cont = get_focused_view(&root_container))->type != C_VIEW) { if ((cont = get_focused_view(&root_container))->type != C_VIEW) {
return false; return CMD_FAILURE;
} }
} }
cont->gaps = swayc_gap(cont); cont->gaps = swayc_gap(cont);
@ -673,7 +692,7 @@ static bool cmd_gaps(int argc, char **argv) {
swayc_t *top; swayc_t *top;
if (target == WORKSPACE) { if (target == WORKSPACE) {
if ((top = swayc_active_workspace()) == NULL) { if ((top = swayc_active_workspace()) == NULL) {
return false; return CMD_FAILURE;
} }
} else { } else {
top = &root_container; top = &root_container;
@ -684,18 +703,22 @@ static bool cmd_gaps(int argc, char **argv) {
arrange_windows(top, -1, -1); arrange_windows(top, -1, -1);
} }
return true; return CMD_SUCCESS;
} }
static bool cmd_kill(int argc, char **argv) { static enum cmd_status cmd_kill(int argc, char **argv) {
if (config->reading || !config->active) {
return CMD_FAILURE;
}
swayc_t *view = get_focused_container(&root_container); swayc_t *view = get_focused_container(&root_container);
wlc_view_close(view->handle); wlc_view_close(view->handle);
return true; return CMD_SUCCESS;
} }
static bool cmd_layout(int argc, char **argv) { static enum cmd_status cmd_layout(int argc, char **argv) {
if (!checkarg(argc, "layout", EXPECTED_MORE_THAN, 0)) { if (!checkarg(argc, "layout", EXPECTED_MORE_THAN, 0)
return false; || config->reading || !config->active) {
return CMD_FAILURE;
} }
swayc_t *parent = get_focused_container(&root_container); swayc_t *parent = get_focused_container(&root_container);
while (parent->type == C_VIEW) { while (parent->type == C_VIEW) {
@ -715,33 +738,33 @@ static bool cmd_layout(int argc, char **argv) {
} }
arrange_windows(parent, parent->width, parent->height); arrange_windows(parent, parent->width, parent->height);
return true; return CMD_SUCCESS;
} }
static bool cmd_reload(int argc, char **argv) { static enum cmd_status cmd_reload(int argc, char **argv) {
if (!checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0)) { if (!checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0)
return false; || config->reading
} || !load_config(NULL)) {
if (!load_config(NULL)) { // TODO: Use config given from -c return CMD_FAILURE;
return false;
} }
arrange_windows(&root_container, -1, -1); arrange_windows(&root_container, -1, -1);
return true; return CMD_SUCCESS;
} }
static bool cmd_resize(int argc, char **argv) { static enum cmd_status cmd_resize(int argc, char **argv) {
if (!checkarg(argc, "resize", EXPECTED_AT_LEAST, 3)) { if (!checkarg(argc, "resize", EXPECTED_AT_LEAST, 3)
return false; || config->reading || !config->active) {
return CMD_FAILURE;
} }
char *end; char *end;
int amount = (int)strtol(argv[2], &end, 10); int amount = (int)strtol(argv[2], &end, 10);
if (errno == ERANGE || amount == 0) { if (errno == ERANGE || amount == 0) {
errno = 0; errno = 0;
return false; return CMD_FAILURE;
} }
if (strcmp(argv[0], "shrink") != 0 && strcmp(argv[0], "grow") != 0) { if (strcmp(argv[0], "shrink") != 0 && strcmp(argv[0], "grow") != 0) {
return false; return CMD_FAILURE;
} }
if (strcmp(argv[0], "shrink") == 0) { if (strcmp(argv[0], "shrink") == 0) {
@ -749,11 +772,13 @@ static bool cmd_resize(int argc, char **argv) {
} }
if (strcmp(argv[1], "width") == 0) { if (strcmp(argv[1], "width") == 0) {
return resize_tiled(amount, true); resize_tiled(amount, true);
} else if (strcmp(argv[1], "height") == 0) { } else if (strcmp(argv[1], "height") == 0) {
return resize_tiled(amount, false); resize_tiled(amount, false);
} else {
return CMD_FAILURE;
} }
return false; return CMD_SUCCESS;
} }
static swayc_t *fetch_view_from_scratchpad() { static swayc_t *fetch_view_from_scratchpad() {
@ -801,9 +826,10 @@ void remove_view_from_scratchpad(swayc_t *view) {
} }
} }
static bool cmd_scratchpad(int argc, char **argv) { static enum cmd_status cmd_scratchpad(int argc, char **argv) {
if (!checkarg(argc, "scratchpad", EXPECTED_EQUAL_TO, 1)) { if (!checkarg(argc, "scratchpad", EXPECTED_EQUAL_TO, 1)
return false; || config->reading || !config->active) {
return CMD_FAILURE;
} }
if (strcasecmp(argv[0], "show") == 0 && scratchpad->length > 0) { if (strcasecmp(argv[0], "show") == 0 && scratchpad->length > 0) {
@ -822,10 +848,9 @@ static bool cmd_scratchpad(int argc, char **argv) {
sp_view = NULL; sp_view = NULL;
} }
} }
return true; return CMD_SUCCESS;
} else {
return false;
} }
return CMD_FAILURE;
} }
// sort in order of longest->shortest // sort in order of longest->shortest
@ -835,9 +860,10 @@ static int compare_set(const void *_l, const void *_r) {
return strlen((*r)->name) - strlen((*l)->name); return strlen((*r)->name) - strlen((*l)->name);
} }
static bool cmd_set(int argc, char **argv) { static enum cmd_status cmd_set(int argc, char **argv) {
if (!checkarg(argc, "set", EXPECTED_AT_LEAST, 2)) { if (!checkarg(argc, "set", EXPECTED_AT_LEAST, 2)
return false; || !config->reading) {
return CMD_FAILURE;
} }
struct sway_variable *var = NULL; struct sway_variable *var = NULL;
// Find old variable if it exists // Find old variable if it exists
@ -858,20 +884,21 @@ static bool cmd_set(int argc, char **argv) {
list_sort(config->symbols, compare_set); list_sort(config->symbols, compare_set);
} }
var->value = join_args(argv + 1, argc - 1); var->value = join_args(argv + 1, argc - 1);
return true; return CMD_SUCCESS;
} }
static bool _do_split(int argc, char **argv, int layout) { static enum cmd_status _do_split(int argc, char **argv, int layout) {
char *name = layout == L_VERT ? "splitv" : char *name = layout == L_VERT ? "splitv" :
layout == L_HORIZ ? "splith" : "split"; layout == L_HORIZ ? "splith" : "split";
if (!checkarg(argc, name, EXPECTED_EQUAL_TO, 0)) { if (!checkarg(argc, name, EXPECTED_EQUAL_TO, 0)
return false; || config->reading || !config->active) {
return CMD_FAILURE;
} }
swayc_t *focused = get_focused_container(&root_container); swayc_t *focused = get_focused_container(&root_container);
// Case of floating window, dont split // Case of floating window, dont split
if (focused->is_floating) { if (focused->is_floating) {
return true; return CMD_SUCCESS;
} }
/* Case that focus is on an workspace with 0/1 children.change its layout */ /* Case that focus is on an workspace with 0/1 children.change its layout */
if (focused->type == C_WORKSPACE && focused->children->length <= 1) { if (focused->type == C_WORKSPACE && focused->children->length <= 1) {
@ -889,12 +916,13 @@ static bool _do_split(int argc, char **argv, int layout) {
set_focused_container(focused); set_focused_container(focused);
arrange_windows(parent, -1, -1); arrange_windows(parent, -1, -1);
} }
return true; return CMD_SUCCESS;
} }
static bool cmd_split(int argc, char **argv) { static enum cmd_status cmd_split(int argc, char **argv) {
if (!checkarg(argc, "split", EXPECTED_EQUAL_TO, 1)) { if (!checkarg(argc, "split", EXPECTED_EQUAL_TO, 1)
return false; || config->reading || !config->active) {
return CMD_FAILURE;
} }
if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) { if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) {
@ -903,36 +931,39 @@ static bool cmd_split(int argc, char **argv) {
_do_split(argc - 1, argv + 1, L_HORIZ); _do_split(argc - 1, argv + 1, L_HORIZ);
} else { } else {
sway_log(L_ERROR, "Invalid split command (expected either horiziontal or vertical)."); sway_log(L_ERROR, "Invalid split command (expected either horiziontal or vertical).");
return false; return CMD_FAILURE;
} }
return CMD_SUCCESS;
return true;
} }
static bool cmd_splitv(int argc, char **argv) { static enum cmd_status cmd_splitv(int argc, char **argv) {
return _do_split(argc, argv, L_VERT); return _do_split(argc, argv, L_VERT);
} }
static bool cmd_splith(int argc, char **argv) { static enum cmd_status cmd_splith(int argc, char **argv) {
return _do_split(argc, argv, L_HORIZ); return _do_split(argc, argv, L_HORIZ);
} }
static bool cmd_log_colors(int argc, char **argv) { static enum cmd_status cmd_log_colors(int argc, char **argv) {
if (!checkarg(argc, "log_colors", EXPECTED_EQUAL_TO, 1)) { if (!checkarg(argc, "log_colors", EXPECTED_EQUAL_TO, 1)
return false; || !config->reading) {
return CMD_FAILURE;
} }
if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) { if (strcasecmp(argv[0], "no") == 0) {
sway_log_colors(0);
} else if(strcasecmp(argv[0], "yes") == 0) {
sway_log_colors(1);
} else {
sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]); sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]);
return false; return CMD_FAILURE;
} }
return CMD_SUCCESS;
sway_log_colors(!strcasecmp(argv[0], "yes"));
return true;
} }
static bool cmd_fullscreen(int argc, char **argv) { static enum cmd_status cmd_fullscreen(int argc, char **argv) {
if (!checkarg(argc, "fullscreen", EXPECTED_AT_LEAST, 0)) { if (!checkarg(argc, "fullscreen", EXPECTED_AT_LEAST, 0)
return false; || config->reading || !config->active) {
return CMD_FAILURE;
} }
swayc_t *container = get_focused_view(&root_container); swayc_t *container = get_focused_view(&root_container);
@ -946,103 +977,100 @@ static bool cmd_fullscreen(int argc, char **argv) {
// Only resize container when going into fullscreen // Only resize container when going into fullscreen
arrange_windows(container, -1, -1); arrange_windows(container, -1, -1);
return true; return CMD_SUCCESS;
} }
static bool cmd_workspace(int argc, char **argv) { static enum cmd_status cmd_workspace(int argc, char **argv) {
if (!checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1)) { if (!checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1)) {
return false; return CMD_FAILURE;
} }
if (argc == 1) { if (argc == 1) {
if (config->reading || !config->active) {
return CMD_DEFER;
}
// Handle workspace next/prev // Handle workspace next/prev
swayc_t *ws = NULL;
if (strcasecmp(argv[0], "next") == 0) { if (strcasecmp(argv[0], "next") == 0) {
workspace_switch(workspace_next()); ws = workspace_next();
return true; } else if (strcasecmp(argv[0], "prev") == 0) {
} ws = workspace_prev();
} else if (strcasecmp(argv[0], "next_on_output") == 0) {
if (strcasecmp(argv[0], "prev") == 0) { ws = workspace_output_next();
workspace_switch(workspace_prev()); } else if (strcasecmp(argv[0], "prev_on_output") == 0) {
return true; ws = workspace_output_prev();
} } else if (strcasecmp(argv[0], "back_and_forth") == 0) {
// Handle workspace output_next/prev
if (strcasecmp(argv[0], "next_on_output") == 0) {
workspace_switch(workspace_output_next());
return true;
}
if (strcasecmp(argv[0], "prev_on_output") == 0) {
workspace_switch(workspace_output_prev());
return true;
}
if (strcasecmp(argv[0], "back_and_forth") == 0) {
if (prev_workspace_name) { if (prev_workspace_name) {
swayc_t *ws = workspace_by_name(prev_workspace_name); if (!(ws = workspace_by_name(prev_workspace_name))) {
workspace_switch(ws ? ws : workspace_create(prev_workspace_name)); ws = workspace_create(prev_workspace_name);
}
}
} else {
if (!(ws= workspace_by_name(argv[0]))) {
ws = workspace_create(argv[0]);
} }
return true;
} }
workspace_switch(ws);
swayc_t *workspace = workspace_by_name(argv[0]);
if (!workspace) {
workspace = workspace_create(argv[0]);
}
workspace_switch(workspace);
} else { } else {
if (strcasecmp(argv[1], "output") == 0) { if (strcasecmp(argv[1], "output") == 0) {
if (!checkarg(argc, "workspace", EXPECTED_EQUAL_TO, 3)) { if (!checkarg(argc, "workspace", EXPECTED_EQUAL_TO, 3)) {
return false; return CMD_FAILURE;
} }
struct workspace_output *wso = calloc(1, sizeof(struct workspace_output)); struct workspace_output *wso = calloc(1, sizeof(struct workspace_output));
sway_log(L_DEBUG, "Assigning workspace %s to output %s", argv[0], argv[2]); sway_log(L_DEBUG, "Assigning workspace %s to output %s", argv[0], argv[2]);
wso->workspace = strdup(argv[0]); wso->workspace = strdup(argv[0]);
wso->output = strdup(argv[2]); wso->output = strdup(argv[2]);
list_add(config->workspace_outputs, wso); list_add(config->workspace_outputs, wso);
// TODO: Consider moving any existing workspace to that output? This might be executed sometime after config load if (!config->reading) {
// TODO: Move workspace to output. (dont do so when reloading)
}
} }
} }
return true; return CMD_SUCCESS;
} }
static bool cmd_ws_auto_back_and_forth(int argc, char **argv) { static enum cmd_status cmd_ws_auto_back_and_forth(int argc, char **argv) {
if (!checkarg(argc, "workspace_auto_back_and_forth", EXPECTED_EQUAL_TO, 1)) { if (!checkarg(argc, "workspace_auto_back_and_forth", EXPECTED_EQUAL_TO, 1)) {
return false; return CMD_FAILURE;
} }
if (strcasecmp(argv[0], "yes") == 0) { if (strcasecmp(argv[0], "yes") == 0) {
config->auto_back_and_forth = true; config->auto_back_and_forth = true;
} else if (strcasecmp(argv[0], "no") == 0) {
config->auto_back_and_forth = false;
} else {
return CMD_FAILURE;
} }
return true; return CMD_SUCCESS;
} }
/* Keep alphabetized */ /* Keep alphabetized */
static struct cmd_handler handlers[] = { static struct cmd_handler handlers[] = {
{ "bindsym", cmd_bindsym, CMD_ANYTIME }, { "bindsym", cmd_bindsym },
{ "default_orientation", cmd_orientation, CMD_ANYTIME}, { "default_orientation", cmd_orientation },
{ "exec", cmd_exec, CMD_COMPOSITOR_READY }, { "exec", cmd_exec },
{ "exec_always", cmd_exec_always, CMD_COMPOSITOR_READY }, { "exec_always", cmd_exec_always },
{ "exit", cmd_exit, CMD_KEYBIND }, { "exit", cmd_exit },
{ "floating", cmd_floating, CMD_KEYBIND }, { "floating", cmd_floating },
{ "floating_modifier", cmd_floating_mod, CMD_ANYTIME }, { "floating_modifier", cmd_floating_mod },
{ "focus", cmd_focus, CMD_KEYBIND }, { "focus", cmd_focus },
{ "focus_follows_mouse", cmd_focus_follows_mouse, CMD_ANYTIME }, { "focus_follows_mouse", cmd_focus_follows_mouse },
{ "fullscreen", cmd_fullscreen, CMD_KEYBIND }, { "fullscreen", cmd_fullscreen },
{ "gaps", cmd_gaps, CMD_ANYTIME }, { "gaps", cmd_gaps },
{ "kill", cmd_kill, CMD_KEYBIND }, { "kill", cmd_kill },
{ "layout", cmd_layout, CMD_KEYBIND }, { "layout", cmd_layout },
{ "log_colors", cmd_log_colors, CMD_ANYTIME }, { "log_colors", cmd_log_colors },
{ "mode", cmd_mode, CMD_ANYTIME }, { "mode", cmd_mode },
{ "move", cmd_move, CMD_KEYBIND }, { "move", cmd_move },
{ "output", cmd_output, CMD_ANYTIME }, { "output", cmd_output },
{ "reload", cmd_reload, CMD_KEYBIND }, { "reload", cmd_reload },
{ "resize", cmd_resize, CMD_KEYBIND }, { "resize", cmd_resize },
{ "scratchpad", cmd_scratchpad, CMD_KEYBIND }, { "scratchpad", cmd_scratchpad },
{ "set", cmd_set, CMD_ANYTIME }, { "set", cmd_set },
{ "split", cmd_split, CMD_KEYBIND }, { "split", cmd_split },
{ "splith", cmd_splith, CMD_KEYBIND }, { "splith", cmd_splith },
{ "splitv", cmd_splitv, CMD_KEYBIND }, { "splitv", cmd_splitv },
{ "workspace", cmd_workspace, CMD_COMPOSITOR_READY }, { "workspace", cmd_workspace },
{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth, CMD_ANYTIME }, { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth },
}; };
static int handler_compare(const void *_a, const void *_b) { static int handler_compare(const void *_a, const void *_b) {
@ -1059,63 +1087,58 @@ static struct cmd_handler *find_handler(char *line) {
return res; return res;
} }
bool handle_command(char *exec) { enum cmd_status handle_command(char *exec) {
sway_log(L_INFO, "Handling command '%s'", exec); sway_log(L_INFO, "Handling command '%s'", exec);
int argc; int argc;
char **argv = split_args(exec, &argc); char **argv = split_args(exec, &argc);
if (argc == 0) { enum cmd_status status = CMD_FAILURE;
return false; struct cmd_handler *handler;
if (!argc) {
return status;
} }
struct cmd_handler *handler = find_handler(argv[0]); if ((handler = find_handler(argv[0])) == NULL
bool exec_success = false; || (status = handler->handle(argc - 1, argv + 1)) != CMD_SUCCESS) {
if (handler && !(handler->handle(argc - 1, argv + 1))) {
sway_log(L_ERROR, "Command failed: %s", argv[0]); sway_log(L_ERROR, "Command failed: %s", argv[0]);
} }
free_argv(argc, argv); free_argv(argc, argv);
return exec_success; return status;
} }
bool config_command(char *exec) { enum cmd_status config_command(char *exec) {
sway_log(L_INFO, "handling config command '%s'", exec); sway_log(L_INFO, "handling config command '%s'", exec);
struct cmd_handler *handler;
int argc; int argc;
char **argv = split_args(exec, &argc); char **argv = split_args(exec, &argc);
bool res = false; enum cmd_status status = CMD_FAILURE;
struct cmd_handler *handler;
if (!argc) { if (!argc) {
return true; status = CMD_SUCCESS;
goto cleanup;
} }
//TODO make this better, it only handles modes right now, and very // TODO better block handling
//simply at that
if (strncmp(argv[0], "}", 1) == 0) { if (strncmp(argv[0], "}", 1) == 0) {
config->current_mode = config->modes->items[0]; config->current_mode = config->modes->items[0];
res = true; status = CMD_SUCCESS;
goto cleanup; goto cleanup;
} }
if ((handler = find_handler(argv[0]))) { if ((handler = find_handler(argv[0]))) {
int i = 1, e = argc; // Dont replace first argument in cmd_set
// dont var replace first argument int i = handler->handle == cmd_set ? 2 : 1;
if (handler->handle == cmd_set) { int e = argc;
i = 2;
}
for (; i < e; ++i) { for (; i < e; ++i) {
argv[i] = do_var_replacement(argv[i]); argv[i] = do_var_replacement(argv[i]);
} }
if (handler->config_type == CMD_KEYBIND) { status = handler->handle(argc - 1, argv + 1);
sway_log(L_ERROR, "Invalid command during config `%s'", exec); if (status == CMD_FAILURE) {
} else if (handler->config_type == CMD_COMPOSITOR_READY && !config->active) { sway_log(L_ERROR, "Config load failed for line `%s'", exec);
} else if (status == CMD_DEFER) {
sway_log(L_DEBUG, "Defferring command `%s'", exec); sway_log(L_DEBUG, "Defferring command `%s'", exec);
char *cmd = join_args(argv, argc); list_add(config->cmd_queue, strdup(exec));
list_add(config->cmd_queue, cmd); status = CMD_SUCCESS;
res = true;
} else if (!handler->handle(argc-1, argv+1)) {
sway_log(L_DEBUG, "Config load failed for line `%s'", exec);
} else {
res = true;
} }
} else { } else {
sway_log(L_ERROR, "Unknown command `%s'", exec); sway_log(L_ERROR, "Unknown command `%s'", exec);
} }
cleanup: cleanup:
free_argv(argc, argv); free_argv(argc, argv);
return res; return status;
} }

View File

@ -102,6 +102,7 @@ static void config_defaults(struct sway_config *config) {
config->active = false; config->active = false;
config->failed = false; config->failed = false;
config->auto_back_and_forth = false; config->auto_back_and_forth = false;
config->reading = false;
config->gaps_inner = 0; config->gaps_inner = 0;
config->gaps_outer = 0; config->gaps_outer = 0;
@ -217,6 +218,7 @@ bool read_config(FILE *file, bool is_active) {
config = malloc(sizeof(struct sway_config)); config = malloc(sizeof(struct sway_config));
config_defaults(config); config_defaults(config);
config->reading = true;
if (is_active) { if (is_active) {
sway_log(L_DEBUG, "Performing configuration file reload"); sway_log(L_DEBUG, "Performing configuration file reload");
config->reloading = true; config->reloading = true;
@ -228,7 +230,8 @@ bool read_config(FILE *file, bool is_active) {
while (!feof(file)) { while (!feof(file)) {
line = read_line(file); line = read_line(file);
line = strip_comments(line); line = strip_comments(line);
if (!config_command(line)) { if (config_command(line) == CMD_FAILURE) {
sway_log(L_ERROR, "Error on line '%s'", line);
success = false; success = false;
} }
free(line); free(line);
@ -242,6 +245,7 @@ bool read_config(FILE *file, bool is_active) {
free_config(old_config); free_config(old_config);
} }
config->reading = false;
return success; return success;
} }

View File

@ -478,12 +478,12 @@ static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct w
static void handle_wlc_ready(void) { static void handle_wlc_ready(void) {
sway_log(L_DEBUG, "Compositor is ready, executing cmds in queue"); sway_log(L_DEBUG, "Compositor is ready, executing cmds in queue");
// Execute commands until there are none left // Execute commands until there are none left
config->active = true;
while (config->cmd_queue->length) { while (config->cmd_queue->length) {
handle_command(config->cmd_queue->items[0]); handle_command(config->cmd_queue->items[0]);
free(config->cmd_queue->items[0]); free(config->cmd_queue->items[0]);
list_del(config->cmd_queue, 0); list_del(config->cmd_queue, 0);
} }
config->active = true;
} }
struct wlc_interface interface = { struct wlc_interface interface = {

View File

@ -145,7 +145,7 @@ char **split_args(const char *start, int *argc) {
} }
void free_argv(int argc, char **argv) { void free_argv(int argc, char **argv) {
while (--argc) { while (--argc > 0) {
free(argv[argc]); free(argv[argc]);
} }
free(argv); free(argv);

View File

@ -76,7 +76,26 @@ char *workspace_next_name(void) {
} }
swayc_t *workspace_create(const char* name) { swayc_t *workspace_create(const char* name) {
swayc_t *parent = get_focused_container(&root_container); swayc_t *parent;
// Search for workspace<->output pair
int i, e = config->workspace_outputs->length;
for (i = 0; i < e; ++i) {
struct workspace_output *wso = config->workspace_outputs->items[i];
if (strcasecmp(wso->workspace, name) == 0)
{
// Find output to use if it exists
e = root_container.children->length;
for (i = 0; i < e; ++i) {
parent = root_container.children->items[i];
if (strcmp(parent->name, wso->output) == 0) {
return new_workspace(parent, name);
}
}
break;
}
}
// Otherwise create a new one
parent = get_focused_container(&root_container);
parent = swayc_parent_by_type(parent, C_OUTPUT); parent = swayc_parent_by_type(parent, C_OUTPUT);
return new_workspace(parent, name); return new_workspace(parent, name);
} }