diff --git a/sway/commands.c b/sway/commands.c index 4eb733ba..89dd0936 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -5,11 +5,12 @@ #include #include #include "stringop.h" +#include "log.h" #include "commands.h" int cmd_set(struct sway_config *config, int argc, char **argv) { if (argc != 2) { - fprintf(stderr, "Invalid set command (expected 2 arguments, got %d)\n", argc); + sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc); return 1; } struct sway_variable *var = malloc(sizeof(struct sway_variable)); @@ -23,7 +24,7 @@ int cmd_set(struct sway_config *config, int argc, char **argv) { int cmd_bindsym(struct sway_config *config, int argc, char **argv) { if (argc < 2) { - fprintf(stderr, "Invalid bindsym command (expected 2 arguments, got %d)\n", argc); + sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc); return 1; } argv[0] = do_var_replacement(config, argv[0]); @@ -39,7 +40,7 @@ int cmd_bindsym(struct sway_config *config, int argc, char **argv) { // TODO: Parse modifier keys xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE); if (!sym) { - fprintf(stderr, "bindsym - unknown key '%s'\n", (char *)split->items[i]); + sway_log(L_ERROR, "bindsym - unknown key %s", (char *)split->items[i]); // Ignore for now, we need to deal with modifier keys // return 1; } @@ -52,7 +53,7 @@ int cmd_bindsym(struct sway_config *config, int argc, char **argv) { // TODO: Check if there are other commands with this key binding list_add(config->current_mode->bindings, binding); - fprintf(stderr, "bindsym - Bound %s to command %s\n", argv[0], binding->command); + sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command); return 0; } @@ -139,7 +140,7 @@ int handle_command(struct sway_config *config, char *exec) { } struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd); if (handler == NULL) { - fprintf(stderr, "Unknown command '%s'\n", cmd); + sway_log(L_ERROR, "Unknown command '%s'", cmd); return 0; // TODO: return error, probably } int argc; diff --git a/sway/handlers.c b/sway/handlers.c index 9addc847..af33f785 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -17,18 +17,17 @@ void handle_output_resolution_change(wlc_handle output, const struct wlc_size *f } bool handle_view_created(wlc_handle view) { - printf("View created, focusing"); - wlc_view_focus(view); - wlc_view_bring_to_front(view); + add_view(view); return true; } void handle_view_destroyed(wlc_handle view) { - printf("View destroyed"); - wlc_view_focus(get_topmost(wlc_view_get_output(view), 0)); + destroy_view(view); return true; } void handle_view_focus(wlc_handle view, bool focus) { + printf("View focused\n"); wlc_view_set_state(view, WLC_BIT_ACTIVATED, focus); + focused_view = view; } diff --git a/sway/layout.c b/sway/layout.c index e61094e2..e95ee423 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -2,12 +2,18 @@ #include #include #include "list.h" +#include "log.h" #include "layout.h" list_t *outputs; +wlc_handle focused_view; + +void arrange_windows() { +} void init_layout() { outputs = create_list(); + focused_view = -1; } struct sway_container *get_container(wlc_handle output, int *index) { @@ -21,6 +27,74 @@ struct sway_container *get_container(wlc_handle output, int *index) { return NULL; } +struct sway_container *get_container_for_view_recurse(wlc_handle handle, int *index, struct sway_container *parent) { + int j; + for (j = 0; j < parent->children->length; ++j) { + struct sway_container *child = parent->children->items[j]; + if (child->layout == LAYOUT_IS_VIEW) { + if (child->output == handle) { + *index = j; + return parent; + } + } else { + struct sway_container *res; + if ((res = get_container_for_view_recurse(handle, index, child))) { + return res; + } + } + } + return NULL; +} + +struct sway_container *get_container_for_view(wlc_handle handle, int *index) { + int i; + for (i = 0; i < outputs->length; ++i) { + struct sway_container *c = outputs->items[i]; + struct sway_container *res; + if ((res = get_container_for_view_recurse(handle, index, c))) { + return res; + } + } + return NULL; +} + +void add_view(wlc_handle view_handle) { + struct sway_container *container; + int _; + + if (focused_view == -1) { // Add it to the output container + sway_log(L_DEBUG, "Adding initial view for output", view_handle); + wlc_handle output = wlc_get_focused_output(); + container = get_container(output, &_); + } else { + sway_log(L_DEBUG, "Adding view %d to output", view_handle); + // TODO + } + + // Create "container" for this view + struct sway_container *view = malloc(sizeof(struct sway_container)); + view->layout = LAYOUT_IS_VIEW; + view->children = NULL; + view->output = view_handle; + list_add(container->children, view); + + wlc_view_focus(view_handle); + + arrange_windows(); +} + +void destroy_view(wlc_handle view) { + sway_log(L_DEBUG, "Destroying view %d", view); + + int index; + struct sway_container *container = get_container_for_view(view, &index); + list_del(container->children, index); + + wlc_view_focus(get_topmost(wlc_view_get_output(view), 0)); + + arrange_windows(); +} + void add_output(wlc_handle output) { struct sway_container *container = malloc(sizeof(struct sway_container)); // TODO: Get default layout from config diff --git a/sway/layout.h b/sway/layout.h index 24d214d8..3d14252b 100644 --- a/sway/layout.h +++ b/sway/layout.h @@ -5,6 +5,7 @@ #include "list.h" typedef enum { + LAYOUT_IS_VIEW, LAYOUT_TILE_HORIZ, LAYOUT_TILE_VERT, LAYOUT_TABBED, @@ -15,12 +16,17 @@ struct sway_container { wlc_handle output; list_t *children; container_layout_t layout; + struct sway_container *parent; }; extern list_t *outputs; +extern wlc_handle focused_view; void init_layout(); void add_output(wlc_handle output); +void destroy_output(wlc_handle output); wlc_handle get_topmost(wlc_handle output, size_t offset); +void destroy_view(wlc_handle view); +void add_view(wlc_handle view); #endif diff --git a/sway/log.c b/sway/log.c new file mode 100644 index 00000000..6ac7026f --- /dev/null +++ b/sway/log.c @@ -0,0 +1,43 @@ +#include "log.h" +#include +#include +#include + +int colored = 1; +int v = 0; + +const char *verbosity_colors[] = { + "", // L_SILENT + "\x1B[1;31m", // L_ERROR + "\x1B[1;34m", // L_INFO + "\x1B[1;30m", // L_DEBUG +}; + +void init_log(int verbosity) { + v = verbosity; +} + +void sway_abort(char *format, ...) { + fprintf(stderr, "ERROR: "); + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + fprintf(stderr, "\n"); + exit(1); +} + +void sway_log(int verbosity, char* format, ...) { + if (verbosity <= v) { + int c = verbosity; + if (c > sizeof(verbosity_colors) / sizeof(char *)) { + c = sizeof(verbosity_colors) / sizeof(char *) - 1; + } + fprintf(stderr, verbosity_colors[c]); + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + fprintf(stderr, "\x1B[0m\n"); + } +} diff --git a/sway/log.h b/sway/log.h new file mode 100644 index 00000000..019012d3 --- /dev/null +++ b/sway/log.h @@ -0,0 +1,15 @@ +#ifndef _SWAY_LOG_H +#define _SWAY_LOG_H + +typedef enum { + L_SILENT = 0, + L_ERROR = 1, + L_INFO = 2, + L_DEBUG = 3, +} log_importance_t; + +void init_log(int verbosity); +void sway_log(int verbosity, char* format, ...); +void sway_abort(char* format, ...); + +#endif diff --git a/sway/main.c b/sway/main.c index 3a6c3f9d..e8603cab 100644 --- a/sway/main.c +++ b/sway/main.c @@ -4,6 +4,7 @@ #include #include "layout.h" #include "config.h" +#include "log.h" #include "handlers.h" struct sway_config *config; @@ -27,6 +28,7 @@ void load_config() { } int main(int argc, char **argv) { + init_log(L_DEBUG); // TODO: Control this with command line arg load_config(); init_layout();