From 7be476c115d4890f15503853aa36cf0059c8fca8 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Thu, 31 Mar 2016 12:17:21 +0200 Subject: [PATCH] Add cmds new_window and new_float Makes it possible to set default layout style for new windows and new floating windows. Close #556 --- include/config.h | 2 + sway/commands.c | 107 ++++++++++++++++++++++++++++++++++++++++------- sway/config.c | 2 + sway/container.c | 10 +++-- sway/sway.5.txt | 17 +++++--- 5 files changed, 114 insertions(+), 24 deletions(-) diff --git a/include/config.h b/include/config.h index 0ef8c5bf9..d7e96e741 100644 --- a/include/config.h +++ b/include/config.h @@ -213,7 +213,9 @@ struct sway_config { const char *current_config; enum swayc_border_types border; + enum swayc_border_types floating_border; int border_thickness; + int floating_border_thickness; enum edge_border_types hide_edge_borders; // border colors diff --git a/sway/commands.c b/sway/commands.c index 381c01333..3cc840621 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -66,6 +66,8 @@ static sway_cmd cmd_log_colors; static sway_cmd cmd_mode; static sway_cmd cmd_mouse_warping; static sway_cmd cmd_move; +static sway_cmd cmd_new_float; +static sway_cmd cmd_new_window; static sway_cmd cmd_orientation; static sway_cmd cmd_output; static sway_cmd cmd_reload; @@ -350,24 +352,21 @@ static struct cmd_results *cmd_bindcode(int argc, char **argv) { static struct cmd_results *cmd_border(int argc, char **argv) { struct cmd_results *error = NULL; + if (!config->active) { + return cmd_results_new(CMD_FAILURE, "border", "Can only be used when sway is running."); + } if ((error = checkarg(argc, "border", EXPECTED_AT_LEAST, 1))) { return error; } if (argc > 2) { - return cmd_results_new(CMD_FAILURE, "border", + return cmd_results_new(CMD_INVALID, "border", "Expected 'border []"); } - enum swayc_border_types border = config->border; - int thickness = config->border_thickness; - - swayc_t *view = NULL; - if (config->active) { - view = get_focused_view(&root_container); - border = view->border_type; - thickness = view->border_thickness; - } + swayc_t *view = get_focused_view(&root_container); + enum swayc_border_types border = view->border_type; + int thickness = view->border_thickness; if (strcasecmp(argv[0], "none") == 0) { border = B_NONE; @@ -388,11 +387,10 @@ static struct cmd_results *cmd_border(int argc, char **argv) { break; } } else { - return cmd_results_new(CMD_FAILURE, "border", + return cmd_results_new(CMD_INVALID, "border", "Expected 'border "); } - if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) { thickness = (int)strtol(argv[1], NULL, 10); if (errno == ERANGE || thickness < 0) { @@ -401,13 +399,10 @@ static struct cmd_results *cmd_border(int argc, char **argv) { } } - if (config->active && view) { + if (view) { view->border_type = border; view->border_thickness = thickness; update_geometry(view); - } else { - config->border = border; - config->border_thickness = thickness; } return cmd_results_new(CMD_SUCCESS, NULL, NULL); @@ -939,6 +934,84 @@ static struct cmd_results *cmd_move(int argc, char **argv) { return cmd_results_new(CMD_SUCCESS, NULL, NULL); } +static struct cmd_results *cmd_new_float(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "new_float", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (argc > 2) { + return cmd_results_new(CMD_INVALID, "new_float", + "Expected 'new_float []"); + } + + enum swayc_border_types border = config->floating_border; + int thickness = config->floating_border_thickness; + + if (strcasecmp(argv[0], "none") == 0) { + border = B_NONE; + } else if (strcasecmp(argv[0], "normal") == 0) { + border = B_NORMAL; + } else if (strcasecmp(argv[0], "pixel") == 0) { + border = B_PIXEL; + } else { + return cmd_results_new(CMD_INVALID, "new_float", + "Expected 'border "); + } + + if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) { + thickness = (int)strtol(argv[1], NULL, 10); + if (errno == ERANGE || thickness < 0) { + errno = 0; + return cmd_results_new(CMD_INVALID, "new_float", "Number is out out of range."); + } + } + + config->floating_border = border; + config->floating_border_thickness = thickness; + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *cmd_new_window(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "new_window", EXPECTED_AT_LEAST, 1))) { + return error; + } + + if (argc > 2) { + return cmd_results_new(CMD_INVALID, "new_window", + "Expected 'new_window []"); + } + + enum swayc_border_types border = config->border; + int thickness = config->border_thickness; + + if (strcasecmp(argv[0], "none") == 0) { + border = B_NONE; + } else if (strcasecmp(argv[0], "normal") == 0) { + border = B_NORMAL; + } else if (strcasecmp(argv[0], "pixel") == 0) { + border = B_PIXEL; + } else { + return cmd_results_new(CMD_INVALID, "new_window", + "Expected 'border "); + } + + if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) { + thickness = (int)strtol(argv[1], NULL, 10); + if (errno == ERANGE || thickness < 0) { + errno = 0; + return cmd_results_new(CMD_INVALID, "new_window", "Number is out out of range."); + } + } + + config->border = border; + config->border_thickness = thickness; + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + static struct cmd_results *cmd_orientation(int argc, char **argv) { struct cmd_results *error = NULL; if (!config->reading) return cmd_results_new(CMD_FAILURE, "orientation", "Can only be used in config file."); @@ -2160,6 +2233,8 @@ static struct cmd_handler handlers[] = { { "mode", cmd_mode }, { "mouse_warping", cmd_mouse_warping }, { "move", cmd_move }, + { "new_float", cmd_new_float }, + { "new_window", cmd_new_window }, { "output", cmd_output }, { "reload", cmd_reload }, { "resize", cmd_resize }, diff --git a/sway/config.c b/sway/config.c index f6241a3d6..a917d348c 100644 --- a/sway/config.c +++ b/sway/config.c @@ -187,7 +187,9 @@ static void config_defaults(struct sway_config *config) { // borders config->border = B_NORMAL; + config->floating_border = B_NORMAL; config->border_thickness = 2; + config->floating_border_thickness = 2; config->hide_edge_borders = E_NONE; // border colors diff --git a/sway/container.c b/sway/container.c index 21a929b90..6b69d6b66 100644 --- a/sway/container.c +++ b/sway/container.c @@ -22,11 +22,8 @@ static swayc_t *new_swayc(enum swayc_types type) { c->gaps = -1; c->layout = L_NONE; c->type = type; - c->border_type = config->border; - c->border_thickness = config->border_thickness; if (type != C_VIEW) { c->children = create_list(); - c->border_type = B_NONE; } return c; } @@ -275,6 +272,9 @@ swayc_t *new_view(swayc_t *sibling, wlc_handle handle) { view->height = 0; view->desired_width = geometry.size.w; view->desired_height = geometry.size.h; + // setup border + view->border_type = config->border; + view->border_thickness = config->border_thickness; view->is_floating = false; @@ -319,6 +319,10 @@ swayc_t *new_floating_view(wlc_handle handle) { view->desired_width = view->width; view->desired_height = view->height; + // setup border + view->border_type = config->floating_border; + view->border_thickness = config->floating_border_thickness; + view->is_floating = true; // Case of focused workspace, just create as child of it diff --git a/sway/sway.5.txt b/sway/sway.5.txt index 2eb0276cb..a2570dcd0 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -44,13 +44,20 @@ The following commands cannot be used directly in the configuration file. They are expected to be used with **bindsym** or at runtime through **swaymsg**(1). **border** []:: - Set border style for windows. _normal_ includes a border of thickness _n_ and - a title bar. _pixel_ is just the border without title bar. Default is _normal_ - with border thickness 2. + Set border style for focused window. _normal_ includes a border of thickness + _n_ and a title bar. _pixel_ is just the border without title bar. Default is + _normal_ with border thickness 2. **border** :: - Set border style to _none_ or _toggle_ between the available border styles: - _normal_, _pixel_, _none_. + Set border style for focused window to _none_ or _toggle_ between the + available border styles: _normal_, _pixel_, _none_. + +**new_window** []:: + Set default border style for new windows. + +**new_float** []:: + Set default border style for new floating windows. This does only apply to + windows that are spawned in floating mode. **exit**:: Exit sway and end your Wayland session.