Merge pull request #204 from sce/misc_stuff

Misc stuff
This commit is contained in:
Drew DeVault 2015-10-25 08:58:34 -04:00
commit eb847a1b1c
8 changed files with 171 additions and 69 deletions

View file

@ -23,4 +23,6 @@ bool _sway_assert(bool condition, const char* format, ...) __attribute__((format
void error_handler(int sig); void error_handler(int sig);
void layout_log(const swayc_t *c, int depth); void layout_log(const swayc_t *c, int depth);
const char *swayc_type_string(enum swayc_types type);
void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, ...) __attribute__((format(printf,3,4)));
#endif #endif

10
include/output.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef _SWAY_OUTPUT_H
#define _SWAY_OUTPUT_H
#include "container.h"
#include "focus.h"
swayc_t *output_by_name(const char* name);
swayc_t *swayc_adjacent_output(swayc_t *output, enum movement_direction dir);
#endif

View file

@ -494,6 +494,8 @@ static struct cmd_results *cmd_move(int argc, char **argv) {
if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
const char* expected_syntax = "Expected 'move <left|right|up|down>' or "
"'move <container|window> to workspace <name>'";
swayc_t *view = get_focused_container(&root_container); swayc_t *view = get_focused_container(&root_container);
if (strcasecmp(argv[0], "left") == 0) { if (strcasecmp(argv[0], "left") == 0) {
@ -505,31 +507,33 @@ static struct cmd_results *cmd_move(int argc, char **argv) {
} else if (strcasecmp(argv[0], "down") == 0) { } else if (strcasecmp(argv[0], "down") == 0) {
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 ...
if ((error = checkarg(argc, "move container/window", EXPECTED_EQUAL_TO, 4))) { if ((error = checkarg(argc, "move container/window", EXPECTED_AT_LEAST, 4))) {
return error; return error;
} else if ( strcasecmp(argv[1], "to") != 0 || strcasecmp(argv[2], "workspace") != 0) { } else if ( strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "workspace") == 0) {
return cmd_results_new(CMD_INVALID, "move", "Expected 'move %s to workspace <name>'", argv[0]); // move container to workspace x
} if (view->type != C_CONTAINER && view->type != C_VIEW) {
return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views.");
}
if (view->type != C_CONTAINER && view->type != C_VIEW) { const char *ws_name = argv[3];
return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views."); if (argc == 5) {
} // move "container to workspace number x"
ws_name = argv[4];
}
const char *ws_name = argv[3]; swayc_t *ws = workspace_by_name(ws_name);
if (argc == 5) { if (ws == NULL) {
// move "container to workspace number x" ws = workspace_create(ws_name);
ws_name = argv[4]; }
move_container_to(view, get_focused_container(ws));
} else {
return cmd_results_new(CMD_INVALID, "move", expected_syntax);
} }
swayc_t *ws = workspace_by_name(ws_name);
if (ws == NULL) {
ws = workspace_create(ws_name);
}
move_container_to(view, get_focused_container(ws));
} else if (strcasecmp(argv[0], "scratchpad") == 0) { } else if (strcasecmp(argv[0], "scratchpad") == 0) {
// move scratchpad ...
if (view->type != C_CONTAINER && view->type != C_VIEW) { if (view->type != C_CONTAINER && view->type != C_VIEW) {
return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views."); return cmd_results_new(CMD_FAILURE, "move scratchpad", "Can only move containers and views.");
} }
swayc_t *view = get_focused_container(&root_container); swayc_t *view = get_focused_container(&root_container);
int i; int i;
@ -554,8 +558,7 @@ static struct cmd_results *cmd_move(int argc, char **argv) {
} }
set_focused_container(focused); set_focused_container(focused);
} else { } else {
return cmd_results_new(CMD_INVALID, "move", return cmd_results_new(CMD_INVALID, "move", expected_syntax);
"Expected 'move <left|right|up|down>' or 'move <container|window> to workspace <name>'");
} }
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }

View file

@ -76,7 +76,7 @@ bool set_focused_container(swayc_t *c) {
if (locked_container_focus || !c) { if (locked_container_focus || !c) {
return false; return false;
} }
sway_log(L_DEBUG, "Setting focus to %p:%ld", c, c->handle); swayc_log(L_DEBUG, c, "Setting focus to %p:%ld", c, c->handle);
// Get workspace for c, get that workspaces current focused container. // Get workspace for c, get that workspaces current focused container.
swayc_t *workspace = swayc_active_workspace_for(c); swayc_t *workspace = swayc_active_workspace_for(c);

View file

@ -13,6 +13,7 @@
#include "stringop.h" #include "stringop.h"
#include "workspace.h" #include "workspace.h"
#include "container.h" #include "container.h"
#include "output.h"
#include "focus.h" #include "focus.h"
#include "input_state.h" #include "input_state.h"
#include "resize.h" #include "resize.h"
@ -364,62 +365,30 @@ static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct
// don't do the switch if the pointer is in a mode. // don't do the switch if the pointer is in a mode.
if (config->seamless_mouse && !pointer_state.mode && if (config->seamless_mouse && !pointer_state.mode &&
!pointer_state.left.held && !pointer_state.right.held && !pointer_state.scroll.held) { !pointer_state.left.held && !pointer_state.right.held && !pointer_state.scroll.held) {
swayc_t *output = swayc_active_output();
// TODO: This implementation is naïve: We assume all outputs are swayc_t *output = swayc_active_output(), *adjacent = NULL;
// perfectly aligned (ie. only a single output per edge which covers
// the whole edge).
if (origin->x == 0) { // Left edge if (origin->x == 0) { // Left edge
for(int i = 0; i < root_container.children->length; ++i) { if ((adjacent = swayc_adjacent_output(output, MOVE_LEFT))) {
swayc_t *c = root_container.children->items[i]; if (workspace_switch(adjacent)) {
if (c == output || c->type != C_OUTPUT) { new_origin.x = adjacent->width;
continue;
}
if (c->y == output->y && c->x + c->width == output->x) {
sway_log(L_DEBUG, "%s is right of %s", output->name, c->name);
if (workspace_switch(c)) {
new_origin.x = c->width;
}
} }
} }
} else if ((double)origin->x == output->width) { // Right edge } else if ((double)origin->x == output->width) { // Right edge
for(int i = 0; i < root_container.children->length; ++i) { if ((adjacent = swayc_adjacent_output(output, MOVE_RIGHT))) {
swayc_t *c = root_container.children->items[i]; if (workspace_switch(adjacent)) {
if (c == output || c->type != C_OUTPUT) { new_origin.x = 0;
continue;
}
if (c->y == output->y && output->x + output->width == c->x) {
sway_log(L_DEBUG, "%s is left of %s", output->name, c->name);
if (workspace_switch(c)) {
new_origin.x = 0;
}
} }
} }
} } else if (origin->y == 0) { // Top edge
if (origin->y == 0) { // Top edge if ((adjacent = swayc_adjacent_output(output, MOVE_UP))) {
for(int i = 0; i < root_container.children->length; ++i) { if (workspace_switch(adjacent)) {
swayc_t *c = root_container.children->items[i]; new_origin.y = adjacent->height;
if (c == output || c->type != C_OUTPUT) {
continue;
}
if (output->x == c->x && c->y + c->height == output->y) {
sway_log(L_DEBUG, "%s is below %s", output->name, c->name);
if (workspace_switch(c)) {
new_origin.y = c->height;
}
} }
} }
} else if ((double)origin->y == output->height) { // Bottom edge } else if ((double)origin->y == output->height) { // Bottom edge
for(int i = 0; i < root_container.children->length; ++i) { if ((adjacent = swayc_adjacent_output(output, MOVE_DOWN))) {
swayc_t *c = root_container.children->items[i]; if (workspace_switch(adjacent)) {
if (c == output || c->type != C_OUTPUT) { new_origin.y = 0;
continue;
}
if (output->x == c->x && output->y + output->height == c->y) {
sway_log(L_DEBUG, "%s is above %s", output->name, c->name);
if (workspace_switch(c)) {
new_origin.y = 0;
}
} }
} }
} }

View file

@ -349,7 +349,7 @@ void update_geometry(swayc_t *container) {
static void arrange_windows_r(swayc_t *container, double width, double height) { static void arrange_windows_r(swayc_t *container, double width, double height) {
int i; int i;
if (width == -1 || height == -1) { if (width == -1 || height == -1) {
sway_log(L_DEBUG, "Arranging layout for %p", container); swayc_log(L_DEBUG, container, "Arranging layout for %p", container);
width = container->width; width = container->width;
height = container->height; height = container->height;
} }

View file

@ -187,4 +187,31 @@ void layout_log(const swayc_t *c, int depth) {
} }
} }
} }
const char *swayc_type_string(enum swayc_types type) {
return type == C_ROOT ? "ROOT" :
type == C_OUTPUT ? "OUTPUT" :
type == C_WORKSPACE ? "WORKSPACE" :
type == C_CONTAINER ? "CONTAINER" :
type == C_VIEW ? "VIEW" :
"UNKNOWN";
}
// Like sway_log, but also appends some info about given container to log output.
void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, ...) {
sway_assert(cont, "swayc_log: no container ...");
va_list args;
va_start(args, format);
char *txt = malloc(128);
vsprintf(txt, format, args);
va_end(args);
char *debug_txt = malloc(32);
snprintf(debug_txt, 32, "%s '%s'", swayc_type_string(cont->type), cont->name);
sway_log(verbosity, "%s (%s)", txt, debug_txt);
free(txt);
free(debug_txt);
}
/* XXX:DEBUG:XXX */ /* XXX:DEBUG:XXX */

91
sway/output.c Normal file
View file

@ -0,0 +1,91 @@
#include <strings.h>
#include "output.h"
#include "log.h"
swayc_t *output_by_name(const char* name) {
if (strcasecmp(name, "left") == 0) {
return swayc_adjacent_output(NULL, MOVE_LEFT);
} else if (strcasecmp(name, "right") == 0) {
return swayc_adjacent_output(NULL, MOVE_RIGHT);
} else if (strcasecmp(name, "up") == 0) {
return swayc_adjacent_output(NULL, MOVE_UP);
} else if (strcasecmp(name, "down") == 0) {
return swayc_adjacent_output(NULL, MOVE_DOWN);
} else {
for(int i = 0; i < root_container.children->length; ++i) {
swayc_t *c = root_container.children->items[i];
if (c->type == C_OUTPUT && strcasecmp(c->name, name) == 0) {
return c;
}
}
}
return NULL;
}
swayc_t *swayc_adjacent_output(swayc_t *output, enum movement_direction dir) {
// TODO: This implementation is naïve: We assume all outputs are
// perfectly aligned (ie. only a single output per edge which covers
// the whole edge).
if (!output) {
output = swayc_active_output();
}
swayc_t *adjacent = NULL;
switch(dir) {
case MOVE_LEFT:
for(int i = 0; i < root_container.children->length; ++i) {
swayc_t *c = root_container.children->items[i];
if (c == output || c->type != C_OUTPUT) {
continue;
}
if (c->y == output->y && c->x + c->width == output->x) {
sway_log(L_DEBUG, "%s is left of current output %s", c->name, output->name);
adjacent = c;
break;
}
}
break;
case MOVE_RIGHT:
for(int i = 0; i < root_container.children->length; ++i) {
swayc_t *c = root_container.children->items[i];
if (c == output || c->type != C_OUTPUT) {
continue;
}
if (c->y == output->y && output->x + output->width == c->x) {
sway_log(L_DEBUG, "%s is right of current output %s", c->name, output->name);
adjacent = c;
break;
}
}
break;
case MOVE_UP:
for(int i = 0; i < root_container.children->length; ++i) {
swayc_t *c = root_container.children->items[i];
if (c == output || c->type != C_OUTPUT) {
continue;
}
if (output->x == c->x && c->y + c->height == output->y) {
sway_log(L_DEBUG, "%s is above current output %s", c->name, output->name);
adjacent = c;
break;
}
}
break;
case MOVE_DOWN:
for(int i = 0; i < root_container.children->length; ++i) {
swayc_t *c = root_container.children->items[i];
if (c == output || c->type != C_OUTPUT) {
continue;
}
if (output->x == c->x && output->y + output->height == c->y) {
sway_log(L_DEBUG, "%s is below current output %s", c->name, output->name);
adjacent = c;
break;
}
}
break;
default:
sway_abort("Function called with invalid argument.");
break;
}
return adjacent;
}