aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/sway/container.h1
-rw-r--r--include/sway/layout.h2
-rw-r--r--include/sway/workspace.h6
-rw-r--r--sway/CMakeLists.txt3
-rw-r--r--sway/tree/container.c34
-rw-r--r--sway/tree/layout.c37
-rw-r--r--sway/tree/workspace.c26
7 files changed, 104 insertions, 5 deletions
diff --git a/include/sway/container.h b/include/sway/container.h
index 35d1c146..2a96165f 100644
--- a/include/sway/container.h
+++ b/include/sway/container.h
@@ -126,5 +126,6 @@ struct sway_container {
126}; 126};
127 127
128swayc_t *new_output(struct sway_output *sway_output); 128swayc_t *new_output(struct sway_output *sway_output);
129swayc_t *new_workspace(swayc_t *output, const char *name);
129 130
130#endif 131#endif
diff --git a/include/sway/layout.h b/include/sway/layout.h
index 7e7a9c35..6356ad00 100644
--- a/include/sway/layout.h
+++ b/include/sway/layout.h
@@ -5,5 +5,7 @@ struct sway_container;
5 5
6void init_layout(void); 6void init_layout(void);
7void add_child(struct sway_container *parent, struct sway_container *child); 7void add_child(struct sway_container *parent, struct sway_container *child);
8enum swayc_layouts default_layout(struct sway_container *output);
9void sort_workspaces(struct sway_container *output);
8 10
9#endif 11#endif
diff --git a/include/sway/workspace.h b/include/sway/workspace.h
new file mode 100644
index 00000000..04b2ea4e
--- /dev/null
+++ b/include/sway/workspace.h
@@ -0,0 +1,6 @@
1#ifndef _SWAY_WORKSPACE_H
2#define _SWAY_WORKSPACE_H
3
4char *workspace_next_name(const char *output_name);
5
6#endif
diff --git a/sway/CMakeLists.txt b/sway/CMakeLists.txt
index 9a92466c..6d520e76 100644
--- a/sway/CMakeLists.txt
+++ b/sway/CMakeLists.txt
@@ -14,8 +14,9 @@ add_executable(sway
14 desktop/output.c 14 desktop/output.c
15 desktop/xdg_shell_v6.c 15 desktop/xdg_shell_v6.c
16 16
17 tree/layout.c
18 tree/container.c 17 tree/container.c
18 tree/layout.c
19 tree/workspace.c
19 20
20 base64.c 21 base64.c
21 main.c 22 main.c
diff --git a/sway/tree/container.c b/sway/tree/container.c
index 54bcf478..ac79356a 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -5,6 +5,8 @@
5#include "sway/container.h" 5#include "sway/container.h"
6#include "sway/layout.h" 6#include "sway/layout.h"
7#include "sway/output.h" 7#include "sway/output.h"
8#include "sway/workspace.h"
9#include "log.h"
8 10
9static swayc_t *new_swayc(enum swayc_types type) { 11static swayc_t *new_swayc(enum swayc_types type) {
10 // next id starts at 1 because 0 is assigned to root_container in layout.c 12 // next id starts at 1 because 0 is assigned to root_container in layout.c
@@ -27,8 +29,8 @@ static swayc_t *new_swayc(enum swayc_types type) {
27 29
28swayc_t *new_output(struct sway_output *sway_output) { 30swayc_t *new_output(struct sway_output *sway_output) {
29 struct wlr_box size; 31 struct wlr_box size;
30 wlr_output_effective_resolution(sway_output->wlr_output, 32 wlr_output_effective_resolution(
31 &size.width, &size.height); 33 sway_output->wlr_output, &size.width, &size.height);
32 const char *name = sway_output->wlr_output->name; 34 const char *name = sway_output->wlr_output->name;
33 35
34 swayc_t *output = new_swayc(C_OUTPUT); 36 swayc_t *output = new_swayc(C_OUTPUT);
@@ -39,7 +41,31 @@ swayc_t *new_output(struct sway_output *sway_output) {
39 41
40 add_child(&root_container, output); 42 add_child(&root_container, output);
41 43
42 // TODO: Create workspace 44 // Create workspace
43 45 char *ws_name = workspace_next_name(output->name);
46 sway_log(L_DEBUG, "Creating default workspace %s", ws_name);
47 new_workspace(output, ws_name);
48 free(ws_name);
44 return output; 49 return output;
45} 50}
51
52swayc_t *new_workspace(swayc_t *output, const char *name) {
53 if (!sway_assert(output, "new_workspace called with null output")) {
54 return NULL;
55 }
56 sway_log(L_DEBUG, "Added workspace %s for output %s", name, output->name);
57 swayc_t *workspace = new_swayc(C_WORKSPACE);
58
59 workspace->x = output->x;
60 workspace->y = output->y;
61 workspace->width = output->width;
62 workspace->height = output->height;
63 workspace->name = !name ? NULL : strdup(name);
64 workspace->prev_layout = L_NONE;
65 workspace->layout = default_layout(output);
66 workspace->workspace_layout = default_layout(output);
67
68 add_child(output, workspace);
69 sort_workspaces(output);
70 return workspace;
71}
diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index 06200bbf..5a70c570 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -1,5 +1,7 @@
1#define _POSIX_C_SOURCE 200809L 1#define _POSIX_C_SOURCE 200809L
2#include <ctype.h>
2#include <stdbool.h> 3#include <stdbool.h>
4#include <stdlib.h>
3#include <string.h> 5#include <string.h>
4#include <wlr/types/wlr_output_layout.h> 6#include <wlr/types/wlr_output_layout.h>
5#include "sway/container.h" 7#include "sway/container.h"
@@ -33,3 +35,38 @@ void add_child(swayc_t *parent, swayc_t *child) {
33 } 35 }
34 */ 36 */
35} 37}
38
39enum swayc_layouts default_layout(swayc_t *output) {
40 /* TODO WLR
41 if (config->default_layout != L_NONE) {
42 //return config->default_layout;
43 } else if (config->default_orientation != L_NONE) {
44 return config->default_orientation;
45 } else */if (output->width >= output->height) {
46 return L_HORIZ;
47 } else {
48 return L_VERT;
49 }
50}
51
52static int sort_workspace_cmp_qsort(const void *_a, const void *_b) {
53 swayc_t *a = *(void **)_a;
54 swayc_t *b = *(void **)_b;
55 int retval = 0;
56
57 if (isdigit(a->name[0]) && isdigit(b->name[0])) {
58 int a_num = strtol(a->name, NULL, 10);
59 int b_num = strtol(b->name, NULL, 10);
60 retval = (a_num < b_num) ? -1 : (a_num > b_num);
61 } else if (isdigit(a->name[0])) {
62 retval = -1;
63 } else if (isdigit(b->name[0])) {
64 retval = 1;
65 }
66
67 return retval;
68}
69
70void sort_workspaces(swayc_t *output) {
71 list_stable_sort(output->children, sort_workspace_cmp_qsort);
72}
diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c
new file mode 100644
index 00000000..e8ed4102
--- /dev/null
+++ b/sway/tree/workspace.c
@@ -0,0 +1,26 @@
1#define _XOPEN_SOURCE 500
2#include <stdbool.h>
3#include <stdlib.h>
4#include <stdio.h>
5#include "sway/container.h"
6#include "log.h"
7
8void next_name_map(swayc_t *ws, void *data) {
9 int *count = data;
10 ++count;
11}
12
13char *workspace_next_name(const char *output_name) {
14 sway_log(L_DEBUG, "Workspace: Generating new workspace name for output %s",
15 output_name);
16 int count = 0;
17 next_name_map(&root_container, &count);
18 ++count;
19 int len = snprintf(NULL, 0, "%d", count);
20 char *name = malloc(len + 1);
21 if (!sway_assert(name, "Failed to allocate workspace name")) {
22 return NULL;
23 }
24 snprintf(name, len + 1, "%d", count);
25 return name;
26}