aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2015-08-08 17:01:22 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2015-08-08 17:01:27 -0400
commit0427fddb5a919ae6b3a4205e057ae36133bfbc47 (patch)
treebf4e163872f63ad955148907f4ce03b63ed5384c
parentDestroy outputs when appropriate (diff)
downloadsway-0427fddb5a919ae6b3a4205e057ae36133bfbc47.tar.gz
sway-0427fddb5a919ae6b3a4205e057ae36133bfbc47.tar.zst
sway-0427fddb5a919ae6b3a4205e057ae36133bfbc47.zip
Add logging and new windows into layout tree
-rw-r--r--sway/commands.c11
-rw-r--r--sway/handlers.c9
-rw-r--r--sway/layout.c74
-rw-r--r--sway/layout.h6
-rw-r--r--sway/log.c43
-rw-r--r--sway/log.h15
-rw-r--r--sway/main.c2
7 files changed, 150 insertions, 10 deletions
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 @@
5#include <string.h> 5#include <string.h>
6#include <ctype.h> 6#include <ctype.h>
7#include "stringop.h" 7#include "stringop.h"
8#include "log.h"
8#include "commands.h" 9#include "commands.h"
9 10
10int cmd_set(struct sway_config *config, int argc, char **argv) { 11int cmd_set(struct sway_config *config, int argc, char **argv) {
11 if (argc != 2) { 12 if (argc != 2) {
12 fprintf(stderr, "Invalid set command (expected 2 arguments, got %d)\n", argc); 13 sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc);
13 return 1; 14 return 1;
14 } 15 }
15 struct sway_variable *var = malloc(sizeof(struct sway_variable)); 16 struct sway_variable *var = malloc(sizeof(struct sway_variable));
@@ -23,7 +24,7 @@ int cmd_set(struct sway_config *config, int argc, char **argv) {
23 24
24int cmd_bindsym(struct sway_config *config, int argc, char **argv) { 25int cmd_bindsym(struct sway_config *config, int argc, char **argv) {
25 if (argc < 2) { 26 if (argc < 2) {
26 fprintf(stderr, "Invalid bindsym command (expected 2 arguments, got %d)\n", argc); 27 sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc);
27 return 1; 28 return 1;
28 } 29 }
29 argv[0] = do_var_replacement(config, argv[0]); 30 argv[0] = do_var_replacement(config, argv[0]);
@@ -39,7 +40,7 @@ int cmd_bindsym(struct sway_config *config, int argc, char **argv) {
39 // TODO: Parse modifier keys 40 // TODO: Parse modifier keys
40 xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE); 41 xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE);
41 if (!sym) { 42 if (!sym) {
42 fprintf(stderr, "bindsym - unknown key '%s'\n", (char *)split->items[i]); 43 sway_log(L_ERROR, "bindsym - unknown key %s", (char *)split->items[i]);
43 // Ignore for now, we need to deal with modifier keys 44 // Ignore for now, we need to deal with modifier keys
44 // return 1; 45 // return 1;
45 } 46 }
@@ -52,7 +53,7 @@ int cmd_bindsym(struct sway_config *config, int argc, char **argv) {
52 // TODO: Check if there are other commands with this key binding 53 // TODO: Check if there are other commands with this key binding
53 list_add(config->current_mode->bindings, binding); 54 list_add(config->current_mode->bindings, binding);
54 55
55 fprintf(stderr, "bindsym - Bound %s to command %s\n", argv[0], binding->command); 56 sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command);
56 return 0; 57 return 0;
57} 58}
58 59
@@ -139,7 +140,7 @@ int handle_command(struct sway_config *config, char *exec) {
139 } 140 }
140 struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd); 141 struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd);
141 if (handler == NULL) { 142 if (handler == NULL) {
142 fprintf(stderr, "Unknown command '%s'\n", cmd); 143 sway_log(L_ERROR, "Unknown command '%s'", cmd);
143 return 0; // TODO: return error, probably 144 return 0; // TODO: return error, probably
144 } 145 }
145 int argc; 146 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
17} 17}
18 18
19bool handle_view_created(wlc_handle view) { 19bool handle_view_created(wlc_handle view) {
20 printf("View created, focusing"); 20 add_view(view);
21 wlc_view_focus(view);
22 wlc_view_bring_to_front(view);
23 return true; 21 return true;
24} 22}
25 23
26void handle_view_destroyed(wlc_handle view) { 24void handle_view_destroyed(wlc_handle view) {
27 printf("View destroyed"); 25 destroy_view(view);
28 wlc_view_focus(get_topmost(wlc_view_get_output(view), 0));
29 return true; 26 return true;
30} 27}
31 28
32void handle_view_focus(wlc_handle view, bool focus) { 29void handle_view_focus(wlc_handle view, bool focus) {
30 printf("View focused\n");
33 wlc_view_set_state(view, WLC_BIT_ACTIVATED, focus); 31 wlc_view_set_state(view, WLC_BIT_ACTIVATED, focus);
32 focused_view = view;
34} 33}
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 @@
2#include <stdbool.h> 2#include <stdbool.h>
3#include <wlc/wlc.h> 3#include <wlc/wlc.h>
4#include "list.h" 4#include "list.h"
5#include "log.h"
5#include "layout.h" 6#include "layout.h"
6 7
7list_t *outputs; 8list_t *outputs;
9wlc_handle focused_view;
10
11void arrange_windows() {
12}
8 13
9void init_layout() { 14void init_layout() {
10 outputs = create_list(); 15 outputs = create_list();
16 focused_view = -1;
11} 17}
12 18
13struct sway_container *get_container(wlc_handle output, int *index) { 19struct sway_container *get_container(wlc_handle output, int *index) {
@@ -21,6 +27,74 @@ struct sway_container *get_container(wlc_handle output, int *index) {
21 return NULL; 27 return NULL;
22} 28}
23 29
30struct sway_container *get_container_for_view_recurse(wlc_handle handle, int *index, struct sway_container *parent) {
31 int j;
32 for (j = 0; j < parent->children->length; ++j) {
33 struct sway_container *child = parent->children->items[j];
34 if (child->layout == LAYOUT_IS_VIEW) {
35 if (child->output == handle) {
36 *index = j;
37 return parent;
38 }
39 } else {
40 struct sway_container *res;
41 if ((res = get_container_for_view_recurse(handle, index, child))) {
42 return res;
43 }
44 }
45 }
46 return NULL;
47}
48
49struct sway_container *get_container_for_view(wlc_handle handle, int *index) {
50 int i;
51 for (i = 0; i < outputs->length; ++i) {
52 struct sway_container *c = outputs->items[i];
53 struct sway_container *res;
54 if ((res = get_container_for_view_recurse(handle, index, c))) {
55 return res;
56 }
57 }
58 return NULL;
59}
60
61void add_view(wlc_handle view_handle) {
62 struct sway_container *container;
63 int _;
64
65 if (focused_view == -1) { // Add it to the output container
66 sway_log(L_DEBUG, "Adding initial view for output", view_handle);
67 wlc_handle output = wlc_get_focused_output();
68 container = get_container(output, &_);
69 } else {
70 sway_log(L_DEBUG, "Adding view %d to output", view_handle);
71 // TODO
72 }
73
74 // Create "container" for this view
75 struct sway_container *view = malloc(sizeof(struct sway_container));
76 view->layout = LAYOUT_IS_VIEW;
77 view->children = NULL;
78 view->output = view_handle;
79 list_add(container->children, view);
80
81 wlc_view_focus(view_handle);
82
83 arrange_windows();
84}
85
86void destroy_view(wlc_handle view) {
87 sway_log(L_DEBUG, "Destroying view %d", view);
88
89 int index;
90 struct sway_container *container = get_container_for_view(view, &index);
91 list_del(container->children, index);
92
93 wlc_view_focus(get_topmost(wlc_view_get_output(view), 0));
94
95 arrange_windows();
96}
97
24void add_output(wlc_handle output) { 98void add_output(wlc_handle output) {
25 struct sway_container *container = malloc(sizeof(struct sway_container)); 99 struct sway_container *container = malloc(sizeof(struct sway_container));
26 // TODO: Get default layout from config 100 // 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 @@
5#include "list.h" 5#include "list.h"
6 6
7typedef enum { 7typedef enum {
8 LAYOUT_IS_VIEW,
8 LAYOUT_TILE_HORIZ, 9 LAYOUT_TILE_HORIZ,
9 LAYOUT_TILE_VERT, 10 LAYOUT_TILE_VERT,
10 LAYOUT_TABBED, 11 LAYOUT_TABBED,
@@ -15,12 +16,17 @@ struct sway_container {
15 wlc_handle output; 16 wlc_handle output;
16 list_t *children; 17 list_t *children;
17 container_layout_t layout; 18 container_layout_t layout;
19 struct sway_container *parent;
18}; 20};
19 21
20extern list_t *outputs; 22extern list_t *outputs;
23extern wlc_handle focused_view;
21 24
22void init_layout(); 25void init_layout();
23void add_output(wlc_handle output); 26void add_output(wlc_handle output);
27void destroy_output(wlc_handle output);
24wlc_handle get_topmost(wlc_handle output, size_t offset); 28wlc_handle get_topmost(wlc_handle output, size_t offset);
29void destroy_view(wlc_handle view);
30void add_view(wlc_handle view);
25 31
26#endif 32#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 @@
1#include "log.h"
2#include <stdarg.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6int colored = 1;
7int v = 0;
8
9const char *verbosity_colors[] = {
10 "", // L_SILENT
11 "\x1B[1;31m", // L_ERROR
12 "\x1B[1;34m", // L_INFO
13 "\x1B[1;30m", // L_DEBUG
14};
15
16void init_log(int verbosity) {
17 v = verbosity;
18}
19
20void sway_abort(char *format, ...) {
21 fprintf(stderr, "ERROR: ");
22 va_list args;
23 va_start(args, format);
24 vfprintf(stderr, format, args);
25 va_end(args);
26 fprintf(stderr, "\n");
27 exit(1);
28}
29
30void sway_log(int verbosity, char* format, ...) {
31 if (verbosity <= v) {
32 int c = verbosity;
33 if (c > sizeof(verbosity_colors) / sizeof(char *)) {
34 c = sizeof(verbosity_colors) / sizeof(char *) - 1;
35 }
36 fprintf(stderr, verbosity_colors[c]);
37 va_list args;
38 va_start(args, format);
39 vfprintf(stderr, format, args);
40 va_end(args);
41 fprintf(stderr, "\x1B[0m\n");
42 }
43}
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 @@
1#ifndef _SWAY_LOG_H
2#define _SWAY_LOG_H
3
4typedef enum {
5 L_SILENT = 0,
6 L_ERROR = 1,
7 L_INFO = 2,
8 L_DEBUG = 3,
9} log_importance_t;
10
11void init_log(int verbosity);
12void sway_log(int verbosity, char* format, ...);
13void sway_abort(char* format, ...);
14
15#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 @@
4#include <wlc/wlc.h> 4#include <wlc/wlc.h>
5#include "layout.h" 5#include "layout.h"
6#include "config.h" 6#include "config.h"
7#include "log.h"
7#include "handlers.h" 8#include "handlers.h"
8 9
9struct sway_config *config; 10struct sway_config *config;
@@ -27,6 +28,7 @@ void load_config() {
27} 28}
28 29
29int main(int argc, char **argv) { 30int main(int argc, char **argv) {
31 init_log(L_DEBUG); // TODO: Control this with command line arg
30 load_config(); 32 load_config();
31 init_layout(); 33 init_layout();
32 34