summaryrefslogtreecommitdiff
path: root/clients/tablet-shell.c
blob: d6e07897a65702253e6e8d99820219686a976545 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Copyright © 2011 Intel Corporation
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "window.h"
#include "cairo-util.h"
#include "../shared/configparser.h"

#include "tablet-shell-client-protocol.h"

struct tablet_shell {
	struct display *display;
	struct tablet_shell *tablet_shell;
	struct rectangle allocation;
	struct window *lockscreen;
	struct window *switcher;
	struct window *homescreen;
	struct wl_list launcher_list;
};

struct launcher {
	cairo_surface_t *icon;
	char *path;
	struct wl_list link;
};

static char *key_lockscreen_icon;
static char *key_lockscreen_background;
static char *key_homescreen_background;
static char *key_launcher_icon;
static char *key_launcher_path;
static void launcher_section_done(void *data);

static const struct config_key lockscreen_config_keys[] = {
	{ "icon", CONFIG_KEY_STRING, &key_lockscreen_icon },
	{ "background", CONFIG_KEY_STRING, &key_lockscreen_background },
};

static const struct config_key homescreen_config_keys[] = {
	{ "background", CONFIG_KEY_STRING, &key_homescreen_background },
};

static const struct config_key launcher_config_keys[] = {
	{ "icon", CONFIG_KEY_STRING, &key_launcher_icon },
	{ "path", CONFIG_KEY_STRING, &key_launcher_path },
};

static const struct config_section config_sections[] = {
	{ "lockscreen",
	  lockscreen_config_keys, ARRAY_LENGTH(lockscreen_config_keys) },
	{ "homescreen",
	  homescreen_config_keys, ARRAY_LENGTH(homescreen_config_keys) },
	{ "launcher",
	  launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
	  launcher_section_done }
};

static void
paint_background(cairo_t *cr, const char *path, struct rectangle *allocation)
{
	cairo_surface_t *image = NULL;
	cairo_pattern_t *pattern;
	cairo_matrix_t matrix;
	double sx, sy;

	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	if (path)
		image = load_jpeg(path);
	if (image) {
		pattern = cairo_pattern_create_for_surface(image);
		sx = (double) cairo_image_surface_get_width(image) /
			allocation->width;
		sy = (double) cairo_image_surface_get_height(image) /
			allocation->height;
		cairo_matrix_init_scale(&matrix, sx, sy);
		cairo_pattern_set_matrix(pattern, &matrix);
		cairo_set_source(cr, pattern);
		cairo_pattern_destroy (pattern);
		cairo_surface_destroy(image);
		cairo_paint(cr);
	} else {
		fprintf(stderr, "couldn't load backgrond image: %s\n",
			key_lockscreen_background);
		cairo_set_source_rgb(cr, 0.2, 0, 0);
		cairo_paint(cr);
	}
}

static void
homescreen_draw(struct tablet_shell *shell)
{
	cairo_surface_t *surface;
	struct rectangle allocation;
	cairo_pattern_t *pattern;
	cairo_matrix_t matrix;
	cairo_t *cr;
	struct launcher *launcher;
	const int rows = 4, columns = 5, icon_width = 128, icon_height = 128;
	int x, y, i, width, height, vmargin, hmargin, vpadding, hpadding;

	window_draw(shell->homescreen);
	window_get_child_allocation(shell->homescreen, &allocation);
	surface = window_get_surface(shell->homescreen);
	cr = cairo_create(surface);

	paint_background(cr, key_homescreen_background, &allocation);

	cairo_set_operator(cr, CAIRO_OPERATOR_OVER);

	width = allocation.width - columns * icon_width;
	hpadding = width / (columns + 1);
	hmargin = (width - hpadding * (columns - 1)) / 2;

	height = allocation.height - rows * icon_height;
	vpadding = height / (rows + 1);
	vmargin = (height - vpadding * (rows - 1)) / 2;

	x = hmargin;
	y = vmargin;
	i = 0;

	wl_list_for_each(launcher, &shell->launcher_list, link) {
		pattern = cairo_pattern_create_for_surface(launcher->icon);
		cairo_matrix_init_scale(&matrix, 2.0, 2.0);
		cairo_matrix_translate(&matrix, -x, -y);
		cairo_pattern_set_matrix(pattern, &matrix);
		cairo_pattern_set_extend(pattern, CAIRO_EXTEND_NONE);
		cairo_set_source(cr, pattern);
		cairo_pattern_destroy(pattern);
		cairo_paint(cr);
		x += icon_width + hpadding;
		i++;
		if (i == columns) {
			x = hmargin;
			y += icon_height + vpadding;
			i = 0;
		}
	}

	cairo_surface_flush(surface);
	cairo_surface_destroy(surface);
	window_flush(shell->homescreen);
}


static void
lockscreen_draw(struct tablet_shell *shell)
{
	cairo_surface_t *surface;
	cairo_surface_t *icon;
	struct rectangle allocation;
	cairo_t *cr;
	int width, height;

	window_draw(shell->lockscreen);
	window_get_child_allocation(shell->lockscreen, &allocation);
	surface = window_get_surface(shell->lockscreen);
	cr = cairo_create(surface);

	paint_background(cr, key_lockscreen_background, &allocation);

	cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
	icon = cairo_image_surface_create_from_png(key_lockscreen_icon);
	width = cairo_image_surface_get_width(icon);
	height = cairo_image_surface_get_height(icon);
	cairo_set_source_surface(cr, icon,
				 allocation.x + (allocation.width - width) / 2,
				 allocation.y + (allocation.height - height) / 2);
	cairo_paint(cr);
	cairo_surface_destroy(icon);

	cairo_surface_flush(surface);
	cairo_surface_destroy(surface);
	window_flush(shell->lockscreen);
}

static int
lockscreen_motion_handler(struct window *window,
			  struct input *input, uint32_t time,
			  int32_t x, int32_t y,
			  int32_t sx, int32_t sy, void *data)
{
	return POINTER_LEFT_PTR;
}

static void
lockscreen_button_handler(struct window *window,
			  struct input *input, uint32_t time,
			  int button, int state, void *data)
{
	struct tablet_shell *shell = data;

	window_destroy(shell->lockscreen);
	shell->lockscreen = NULL;
}

static void
show_lockscreen(void *data, struct tablet_shell *tablet_shell)
{
	struct tablet_shell *shell = data;

	shell->lockscreen = window_create(shell->display,
					  shell->allocation.width,
					  shell->allocation.height);
	window_set_user_data(shell->lockscreen, shell);
	window_set_decoration(shell->lockscreen, 0);
	window_set_custom(shell->lockscreen);
	window_set_button_handler(shell->lockscreen,
				  lockscreen_button_handler);
	window_set_motion_handler(shell->lockscreen,
				  lockscreen_motion_handler);


	tablet_shell_set_lockscreen(shell->tablet_shell,
				    window_get_wl_surface(shell->lockscreen));
	lockscreen_draw(shell);
}

static void
show_switcher(void *data, struct tablet_shell *tablet_shell)
{
	struct tablet_shell *shell = data;

	shell->switcher = window_create(shell->display, 0, 0);
	window_set_user_data(shell->switcher, shell);
	window_set_decoration(shell->switcher, 0);
	window_set_custom(shell->switcher);
	tablet_shell_set_switcher(shell->tablet_shell,
				  window_get_wl_surface(shell->switcher));
}

static void
hide_switcher(void *data, struct tablet_shell *tablet_shell)
{
}

static const struct tablet_shell_listener tablet_shell_listener = {
	show_lockscreen,
	show_switcher,
	hide_switcher
};

static struct tablet_shell *
tablet_shell_create(struct display *display, uint32_t id)
{
	struct tablet_shell *shell;
	struct output *output;

	shell = malloc(sizeof *shell);

	shell->display = display;
	shell->tablet_shell =
		wl_display_bind(display_get_display(display),
				id, &tablet_shell_interface);
	tablet_shell_add_listener(shell->tablet_shell,
				  &tablet_shell_listener, shell);
	output = display_get_output(display);
	output_get_allocation(output, &shell->allocation);

	shell->homescreen = window_create(display,
					  shell->allocation.width,
					  shell->allocation.height);
	window_set_user_data(shell->homescreen, shell);
	window_set_decoration(shell->homescreen, 0);
	window_set_custom(shell->homescreen);

	tablet_shell_set_homescreen(shell->tablet_shell,
				    window_get_wl_surface(shell->homescreen));
	wl_list_init(&shell->launcher_list);

	return shell;
}

static void
tablet_shell_add_launcher(struct tablet_shell *shell,
			  const char *icon, const char *path)
{
	struct launcher *launcher;

	launcher = malloc(sizeof *launcher);
	launcher->path = strdup(path);
	launcher->icon = cairo_image_surface_create_from_png(icon);
	if (cairo_surface_status (launcher->icon) != CAIRO_STATUS_SUCCESS) {
		fprintf(stderr, "couldn't load %s\n", icon);
		free(launcher);
		return;
	}

	wl_list_insert(&shell->launcher_list, &launcher->link);
}

static void
launcher_section_done(void *data)
{
	struct tablet_shell *shell = data;

	if (key_launcher_icon == NULL || key_launcher_path == NULL) {
		fprintf(stderr, "invalid launcher section\n");
		return;
	}

	tablet_shell_add_launcher(shell, key_launcher_icon, key_launcher_path);

	free(key_launcher_icon);
	key_launcher_icon = NULL;
	free(key_launcher_path);
	key_launcher_path = NULL;
}

int main(int argc, char *argv[])
{
	struct display *display;
	char *config_file;
	uint32_t id;
	struct tablet_shell *shell;

	display = display_create(&argc, &argv, NULL);
	if (display == NULL) {
		fprintf(stderr, "failed to create display: %m\n");
		return -1;
	}

	wl_display_roundtrip(display_get_display(display));
	id = wl_display_get_global(display_get_display(display),
				   "tablet_shell", 1);
	shell = tablet_shell_create(display, id);

	config_file = config_file_path("wayland-tablet-shell.ini");
	parse_config_file(config_file,
			  config_sections, ARRAY_LENGTH(config_sections),
			  shell);
	free(config_file);

	homescreen_draw(shell);

	display_run(display);

	return 0;
}