diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-08-08 17:01:22 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-08-08 17:01:27 -0400 |
commit | 0427fddb5a919ae6b3a4205e057ae36133bfbc47 (patch) | |
tree | bf4e163872f63ad955148907f4ce03b63ed5384c /sway | |
parent | Destroy outputs when appropriate (diff) | |
download | sway-0427fddb5a919ae6b3a4205e057ae36133bfbc47.tar.gz sway-0427fddb5a919ae6b3a4205e057ae36133bfbc47.tar.zst sway-0427fddb5a919ae6b3a4205e057ae36133bfbc47.zip |
Add logging and new windows into layout tree
Diffstat (limited to 'sway')
-rw-r--r-- | sway/commands.c | 11 | ||||
-rw-r--r-- | sway/handlers.c | 9 | ||||
-rw-r--r-- | sway/layout.c | 74 | ||||
-rw-r--r-- | sway/layout.h | 6 | ||||
-rw-r--r-- | sway/log.c | 43 | ||||
-rw-r--r-- | sway/log.h | 15 | ||||
-rw-r--r-- | sway/main.c | 2 |
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 | ||
10 | int cmd_set(struct sway_config *config, int argc, char **argv) { | 11 | int 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 | ||
24 | int cmd_bindsym(struct sway_config *config, int argc, char **argv) { | 25 | int 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 | ||
19 | bool handle_view_created(wlc_handle view) { | 19 | bool 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 | ||
26 | void handle_view_destroyed(wlc_handle view) { | 24 | void 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 | ||
32 | void handle_view_focus(wlc_handle view, bool focus) { | 29 | void 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 | ||
7 | list_t *outputs; | 8 | list_t *outputs; |
9 | wlc_handle focused_view; | ||
10 | |||
11 | void arrange_windows() { | ||
12 | } | ||
8 | 13 | ||
9 | void init_layout() { | 14 | void init_layout() { |
10 | outputs = create_list(); | 15 | outputs = create_list(); |
16 | focused_view = -1; | ||
11 | } | 17 | } |
12 | 18 | ||
13 | struct sway_container *get_container(wlc_handle output, int *index) { | 19 | struct 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 | ||
30 | struct 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 | |||
49 | struct 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 | |||
61 | void 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 | |||
86 | void 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 | |||
24 | void add_output(wlc_handle output) { | 98 | void 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 | ||
7 | typedef enum { | 7 | typedef 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 | ||
20 | extern list_t *outputs; | 22 | extern list_t *outputs; |
23 | extern wlc_handle focused_view; | ||
21 | 24 | ||
22 | void init_layout(); | 25 | void init_layout(); |
23 | void add_output(wlc_handle output); | 26 | void add_output(wlc_handle output); |
27 | void destroy_output(wlc_handle output); | ||
24 | wlc_handle get_topmost(wlc_handle output, size_t offset); | 28 | wlc_handle get_topmost(wlc_handle output, size_t offset); |
29 | void destroy_view(wlc_handle view); | ||
30 | void 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 | |||
6 | int colored = 1; | ||
7 | int v = 0; | ||
8 | |||
9 | const 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 | |||
16 | void init_log(int verbosity) { | ||
17 | v = verbosity; | ||
18 | } | ||
19 | |||
20 | void 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 | |||
30 | void 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 | |||
4 | typedef enum { | ||
5 | L_SILENT = 0, | ||
6 | L_ERROR = 1, | ||
7 | L_INFO = 2, | ||
8 | L_DEBUG = 3, | ||
9 | } log_importance_t; | ||
10 | |||
11 | void init_log(int verbosity); | ||
12 | void sway_log(int verbosity, char* format, ...); | ||
13 | void 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 | ||
9 | struct sway_config *config; | 10 | struct sway_config *config; |
@@ -27,6 +28,7 @@ void load_config() { | |||
27 | } | 28 | } |
28 | 29 | ||
29 | int main(int argc, char **argv) { | 30 | int 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 | ||