summaryrefslogtreecommitdiffstats
path: root/sway/extensions.c
blob: 7769f86e083ecf3a31c8453cda40614deeb846e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdlib.h>
#include <wlc/wlc.h>
#include <wlc/wlc-wayland.h>
#include "wayland-desktop-shell-server-protocol.h"
#include "wayland-swaylock-server-protocol.h"
#include "layout.h"
#include "log.h"
#include "extensions.h"

struct desktop_shell_state desktop_shell;

void background_surface_destructor(struct wl_resource *resource) {
	sway_log(L_DEBUG, "Background surface killed");
	int i;
	for (i = 0; i < desktop_shell.backgrounds->length; ++i) {
		struct background_config *config = desktop_shell.backgrounds->items[i];
		if (config->resource == resource) {
			list_del(desktop_shell.backgrounds, i);
			break;
		}
	}
}

void panel_surface_destructor(struct wl_resource *resource) {
	sway_log(L_DEBUG, "Panel surface killed");
	int i;
	for (i = 0; i < desktop_shell.panels->length; ++i) {
		struct panel_config *config = desktop_shell.panels->items[i];
		if (config->resource == resource) {
			list_del(desktop_shell.panels, i);
			arrange_windows(&root_container, -1, -1);
			break;
		}
	}
}

void lock_surface_destructor(struct wl_resource *resource) {
	sway_log(L_DEBUG, "Lock surface killed");
	int i;
	for (i = 0; i < desktop_shell.lock_surfaces->length; ++i) {
		struct wl_resource *surface = desktop_shell.lock_surfaces->items[i];
		if (surface == resource) {
			list_del(desktop_shell.lock_surfaces, i);
			arrange_windows(&root_container, -1, -1);
			desktop_shell.is_locked = false;
			break;
		}
	}
}

static void set_background(struct wl_client *client, struct wl_resource *resource,
		struct wl_resource *_output, struct wl_resource *surface) {
	wlc_handle output = wlc_handle_from_wl_output_resource(_output);
	if (!output) {
		return;
	}
	sway_log(L_DEBUG, "Setting surface %p as background for output %d", surface, (int)output);
	struct background_config *config = malloc(sizeof(struct background_config));
	config->output = output;
	config->surface = wlc_resource_from_wl_surface_resource(surface);
	config->resource = surface;
	list_add(desktop_shell.backgrounds, config);
	wl_resource_set_destructor(surface, background_surface_destructor);
}

static void set_panel(struct wl_client *client, struct wl_resource *resource,
			  struct wl_resource *_output, struct wl_resource *surface) {
	wlc_handle output = wlc_handle_from_wl_output_resource(_output);
	if (!output) {
		return;
	}
	sway_log(L_DEBUG, "Setting surface %p as panel for output %d", surface, (int)output);
	struct panel_config *config = malloc(sizeof(struct panel_config));
	config->output = output;
	config->surface = wlc_resource_from_wl_surface_resource(surface);
	config->resource = surface;
	list_add(desktop_shell.panels, config);
	wl_resource_set_destructor(surface, panel_surface_destructor);
	desktop_shell.panel_size = *wlc_surface_get_size(config->surface);
	arrange_windows(&root_container, -1, -1);
}

static void desktop_set_lock_surface(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface) {
	sway_log(L_ERROR, "desktop_set_lock_surface is not currently supported");
}

static void desktop_unlock(struct wl_client *client, struct wl_resource *resource) {
	sway_log(L_ERROR, "desktop_unlock is not currently supported");
}

static void set_lock_surface(struct wl_client *client, struct wl_resource *resource,
		struct wl_resource *output, struct wl_resource *surface) {
	sway_log(L_ERROR, "set_lock_surface is not currently supported");
}

static void unlock(struct wl_client *client, struct wl_resource *resource) {
	sway_log(L_ERROR, "unlock is not currently supported");
}

static void set_grab_surface(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface) {
	sway_log(L_ERROR, "desktop_set_grab_surface is not currently supported");
}

static void desktop_ready(struct wl_client *client, struct wl_resource *resource) {
	// nop
}

static void set_panel_position(struct wl_client *client, struct wl_resource *resource, uint32_t position) {
	desktop_shell.panel_position = position;
	arrange_windows(&root_container, -1, -1);
}

static struct desktop_shell_interface desktop_shell_implementation = {
	.set_background = set_background,
	.set_panel = set_panel,
	.set_lock_surface = desktop_set_lock_surface,
	.unlock = desktop_unlock,
	.set_grab_surface = set_grab_surface,
	.desktop_ready = desktop_ready,
	.set_panel_position = set_panel_position
};

static struct lock_interface swaylock_implementation = {
	.set_lock_surface = set_lock_surface,
	.unlock = unlock
};

static void desktop_shell_bind(struct wl_client *client, void *data,
		unsigned int version, unsigned int id) {
	if (version > 3) {
		// Unsupported version
		return;
	}

	struct wl_resource *resource = wl_resource_create(client, &desktop_shell_interface, version, id);
	if (!resource) {
		wl_client_post_no_memory(client);
	}

	wl_resource_set_implementation(resource, &desktop_shell_implementation, NULL, NULL);
}

static void swaylock_bind(struct wl_client *client, void *data,
		unsigned int version, unsigned int id) {
	if (version > 1) {
		// Unsupported version
		return;
	}

	struct wl_resource *resource = wl_resource_create(client, &lock_interface, version, id);
	if (!resource) {
		wl_client_post_no_memory(client);
	}

	wl_resource_set_implementation(resource, &swaylock_implementation, NULL, NULL);
}

void register_extensions(void) {
	wl_global_create(wlc_get_wl_display(), &desktop_shell_interface, 3, NULL, desktop_shell_bind);
	desktop_shell.backgrounds = create_list();
	desktop_shell.panels = create_list();
	desktop_shell.panel_position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
	desktop_shell.lock_surfaces = create_list();
	desktop_shell.is_locked = false;
	wl_global_create(wlc_get_wl_display(), &lock_interface, 3, NULL, swaylock_bind);
}