blob: cb7211cae7a0e829f4c708ef85f9bfb2e4b49bc8 [file] [log] [blame]
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include <sys/wait.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040028#include <linux/input.h>
29
30#include "compositor.h"
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050031#include "tablet-shell-server-protocol.h"
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040032
33/*
34 * TODO: Don't fade back from black until we've received a lockscreen
35 * attachment.
36 */
37
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -040038enum {
39 STATE_STARTING,
40 STATE_LOCKED,
41 STATE_HOME,
42 STATE_SWITCHER,
43 STATE_TASK
44};
45
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050046struct tablet_shell {
Kristian Høgsberg904055a2011-08-18 17:55:30 -040047 struct wl_resource resource;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040048
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050049 struct weston_shell shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040050
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050051 struct weston_compositor *compositor;
52 struct weston_process process;
53 struct weston_input_device *device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040054 struct wl_client *client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040055
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050056 struct weston_surface *surface;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040057
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050058 struct weston_surface *lockscreen_surface;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040059 struct wl_listener lockscreen_listener;
Tiago Vignatti0a386112012-03-28 13:04:02 +030060 struct weston_layer lockscreen_layer;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040061
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050062 struct weston_surface *home_surface;
Tiago Vignatti0a386112012-03-28 13:04:02 +030063 struct weston_layer homescreen_layer;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040064
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050065 struct weston_surface *switcher_surface;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040066 struct wl_listener switcher_listener;
67
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050068 struct tablet_client *current_client;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -040069
70 int state, previous_state;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040071 int long_press_active;
72 struct wl_event_source *long_press_source;
73};
74
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050075struct tablet_client {
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -040076 struct wl_resource resource;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050077 struct tablet_shell *shell;
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -040078 struct wl_client *client;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050079 struct weston_surface *surface;
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -040080 char *name;
81};
82
Kristian Høgsbergd4af3202011-06-21 17:43:31 -040083static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050084tablet_shell_sigchld(struct weston_process *process, int status)
Kristian Høgsbergd4af3202011-06-21 17:43:31 -040085{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050086 struct tablet_shell *shell =
87 container_of(process, struct tablet_shell, process);
Kristian Høgsbergd4af3202011-06-21 17:43:31 -040088
89 shell->process.pid = 0;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040090
Kristian Høgsberg6336e462011-11-26 17:36:23 -050091 fprintf(stderr,
Kristian Høgsberg9724b512012-01-03 14:35:49 -050092 "weston-tablet-shell crashed, exit code %d\n", status);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040093}
94
95static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050096tablet_shell_set_state(struct tablet_shell *shell, int state)
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -040097{
98 static const char *states[] = {
99 "STARTING", "LOCKED", "HOME", "SWITCHER", "TASK"
100 };
101
102 fprintf(stderr, "switching to state %s (from %s)\n",
103 states[state], states[shell->state]);
104 shell->previous_state = shell->state;
105 shell->state = state;
106}
107
108static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500109tablet_shell_map(struct weston_shell *base, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +0200110 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400111{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500112 struct tablet_shell *shell =
113 container_of(base, struct tablet_shell, shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400114
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200115 weston_surface_configure(surface, 0, 0, width, height);
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400116
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400117 if (surface == shell->lockscreen_surface) {
Tiago Vignatti0a386112012-03-28 13:04:02 +0300118 wl_list_insert(&shell->lockscreen_layer.surface_list,
119 &surface->layer_link);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400120 } else if (surface == shell->switcher_surface) {
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400121 /* */
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400122 } else if (surface == shell->home_surface) {
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400123 if (shell->state == STATE_STARTING) {
Tiago Vignatti0a386112012-03-28 13:04:02 +0300124 /* homescreen always visible, at the bottom */
125 wl_list_insert(&shell->homescreen_layer.surface_list,
126 &surface->layer_link);
127
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500128 tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400129 shell->previous_state = STATE_HOME;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500130 tablet_shell_send_show_lockscreen(&shell->resource);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400131 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400132 } else if (shell->current_client &&
133 shell->current_client->surface != surface &&
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400134 shell->current_client->client == surface->surface.resource.client) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500135 tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400136 shell->current_client->surface = surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500137 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400138 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500139
140 wl_list_insert(&shell->compositor->surface_list, &surface->link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200141 weston_surface_assign_output(surface);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500142}
143
144static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500145tablet_shell_configure(struct weston_shell *base,
146 struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200147 GLfloat x, GLfloat y,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500148 int32_t width, int32_t height)
149{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500150 weston_surface_configure(surface, x, y, width, height);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400151}
152
153static void
Tiago Vignatti0a386112012-03-28 13:04:02 +0300154tablet_shell_surface_configure(struct weston_surface *es, int32_t sx,
155 int32_t sy)
156{
157 struct weston_shell *shell = es->compositor->shell;
158
159 if (!weston_surface_is_mapped(es))
160 tablet_shell_map(shell, es, es->buffer->width,
161 es->buffer->height, sx, sy);
162}
163
164static void
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400165handle_lockscreen_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200166 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400167{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500168 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400169 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500170 struct tablet_shell, lockscreen_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400171
172 shell->lockscreen_surface = NULL;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500173 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400174}
175
176static void
177tablet_shell_set_lockscreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400178 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400179 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400180{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500181 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500182 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400183
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200184 weston_surface_set_position(es, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400185 shell->lockscreen_surface = es;
Tiago Vignatti0a386112012-03-28 13:04:02 +0300186 shell->lockscreen_surface->configure = tablet_shell_surface_configure;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400187 shell->lockscreen_listener.func = handle_lockscreen_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200188 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400189 &shell->lockscreen_listener.link);
190}
191
192static void
193handle_switcher_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200194 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400195{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500196 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400197 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500198 struct tablet_shell, switcher_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400199
200 shell->switcher_surface = NULL;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400201 if (shell->state != STATE_LOCKED)
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500202 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400203}
204
205static void
206tablet_shell_set_switcher(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400207 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400208 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400209{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500210 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500211 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400212
213 /* FIXME: Switcher should be centered and the compositor
214 * should do the tinting of the background. With the cache
215 * layer idea, we should be able to hit the framerate on the
216 * fade/zoom in. */
217 shell->switcher_surface = es;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200218 weston_surface_set_position(shell->switcher_surface, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400219
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400220 shell->switcher_listener.func = handle_switcher_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200221 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400222 &shell->switcher_listener.link);
223}
224
225static void
226tablet_shell_set_homescreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400227 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400228 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400229{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500230 struct tablet_shell *shell = resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400231
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400232 shell->home_surface = surface_resource->data;
Tiago Vignatti0a386112012-03-28 13:04:02 +0300233 shell->home_surface->configure = tablet_shell_surface_configure;
234
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200235 weston_surface_set_position(shell->home_surface, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400236}
237
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400238static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500239minimize_zoom_done(struct weston_zoom *zoom, void *data)
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400240{
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500241 struct tablet_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500242 struct weston_compositor *compositor = shell->compositor;
243 struct weston_input_device *device =
244 (struct weston_input_device *) compositor->input_device;
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400245
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500246 weston_surface_activate(shell->home_surface,
247 device, weston_compositor_get_time());
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400248}
249
250static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500251tablet_shell_switch_to(struct tablet_shell *shell,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500252 struct weston_surface *surface)
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400253{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500254 struct weston_compositor *compositor = shell->compositor;
255 struct weston_input_device *device =
256 (struct weston_input_device *) compositor->input_device;
257 struct weston_surface *current;
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400258
259 if (shell->state == STATE_SWITCHER) {
260 wl_list_remove(&shell->switcher_listener.link);
261 shell->switcher_surface = NULL;
262 };
263
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400264 if (surface == shell->home_surface) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500265 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400266
267 if (shell->current_client && shell->current_client->surface) {
268 current = shell->current_client->surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500269 weston_zoom_run(current, 1.0, 0.3,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500270 minimize_zoom_done, shell);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400271 }
272 } else {
273 fprintf(stderr, "switch to %p\n", surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500274 weston_surface_activate(surface, device,
275 weston_compositor_get_time());
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500276 tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500277 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400278 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400279}
280
281static void
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400282tablet_shell_show_grid(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400283 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400284 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400285{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500286 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500287 struct weston_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400288
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500289 tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400290}
291
292static void
293tablet_shell_show_panels(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400294 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400295 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400296{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500297 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500298 struct weston_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400299
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500300 tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400301}
302
303static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400304destroy_tablet_client(struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400305{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500306 struct tablet_client *tablet_client =
307 container_of(resource, struct tablet_client, resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400308
309 free(tablet_client->name);
310 free(tablet_client);
311}
312
313static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400314tablet_client_destroy(struct wl_client *client,
315 struct wl_resource *resource)
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400316{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 wl_resource_destroy(resource, weston_compositor_get_time());
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400318}
319
320static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400321tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400322{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500323 struct tablet_client *tablet_client = resource->data;
324 struct tablet_shell *shell = tablet_client->shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400325
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400326 shell->current_client = tablet_client;
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400327 if (!tablet_client->surface)
328 return;
329
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500330 tablet_shell_switch_to(shell, tablet_client->surface);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400331}
332
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500333static const struct tablet_client_interface tablet_client_implementation = {
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400334 tablet_client_destroy,
335 tablet_client_activate
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400336};
337
338static void
339tablet_shell_create_client(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400340 struct wl_resource *resource,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400341 uint32_t id, const char *name, int fd)
342{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500343 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500344 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500345 struct tablet_client *tablet_client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400346
347 tablet_client = malloc(sizeof *tablet_client);
348 if (tablet_client == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400349 wl_resource_post_no_memory(resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400350 return;
351 }
352
353 tablet_client->client = wl_client_create(compositor->wl_display, fd);
354 tablet_client->shell = shell;
355 tablet_client->name = strdup(name);
356
357 tablet_client->resource.destroy = destroy_tablet_client;
358 tablet_client->resource.object.id = id;
359 tablet_client->resource.object.interface =
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500360 &tablet_client_interface;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400361 tablet_client->resource.object.implementation =
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500362 (void (**)(void)) &tablet_client_implementation;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400363
364 wl_client_add_resource(client, &tablet_client->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400365 tablet_client->surface = NULL;
366 shell->current_client = tablet_client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400367
368 fprintf(stderr, "created client %p, id %d, name %s, fd %d\n",
369 tablet_client->client, id, name, fd);
370}
371
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500372static const struct tablet_shell_interface tablet_shell_implementation = {
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400373 tablet_shell_set_lockscreen,
374 tablet_shell_set_switcher,
375 tablet_shell_set_homescreen,
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400376 tablet_shell_show_grid,
377 tablet_shell_show_panels,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400378 tablet_shell_create_client
379};
380
381static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500382launch_ux_daemon(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400383{
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500384 const char *shell_exe = LIBEXECDIR "/weston-tablet-shell";
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400385
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500386 shell->client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200387 &shell->process,
388 shell_exe, tablet_shell_sigchld);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400389}
390
391static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500392toggle_switcher(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400393{
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400394 switch (shell->state) {
395 case STATE_SWITCHER:
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500396 tablet_shell_send_hide_switcher(&shell->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400397 break;
398 default:
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500399 tablet_shell_send_show_switcher(&shell->resource);
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500400 tablet_shell_set_state(shell, STATE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400401 break;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400402 }
403}
404
405static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500406tablet_shell_lock(struct weston_shell *base)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400407{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500408 struct tablet_shell *shell =
409 container_of(base, struct tablet_shell, shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400410
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400411 if (shell->state == STATE_LOCKED)
412 return;
413 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500414 tablet_shell_send_hide_switcher(&shell->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400415
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500416 tablet_shell_send_show_lockscreen(&shell->resource);
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500417 tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400418}
419
420static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500421tablet_shell_unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200422{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200423 struct tablet_shell *shell =
424 container_of(base, struct tablet_shell, shell);
425
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500426 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200427}
428
429static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500430go_home(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400431{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500432 struct weston_input_device *device =
433 (struct weston_input_device *) shell->compositor->input_device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400434
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400435 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500436 tablet_shell_send_hide_switcher(&shell->resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400437
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500438 weston_surface_activate(shell->home_surface, device,
439 weston_compositor_get_time());
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400440
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500441 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400442}
443
444static int
445long_press_handler(void *data)
446{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500447 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400448
449 shell->long_press_active = 0;
450 toggle_switcher(shell);
451
452 return 1;
453}
454
455static void
456menu_key_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600457 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400458{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500459 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400460
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400461 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400462 return;
463
464 if (state)
465 toggle_switcher(shell);
466}
467
468static void
469home_key_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600470 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400471{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500472 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400473
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400474 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400475 return;
476
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500477 shell->device = (struct weston_input_device *) device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400478
479 if (state) {
480 wl_event_source_timer_update(shell->long_press_source, 500);
481 shell->long_press_active = 1;
482 } else if (shell->long_press_active) {
483 wl_event_source_timer_update(shell->long_press_source, 0);
484 shell->long_press_active = 0;
485
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400486 switch (shell->state) {
487 case STATE_HOME:
488 case STATE_SWITCHER:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400489 toggle_switcher(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400490 break;
491 default:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400492 go_home(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400493 break;
494 }
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400495 }
496}
497
498static void
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500499destroy_tablet_shell(struct wl_resource *resource)
500{
501}
502
503static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400504bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400505{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500506 struct tablet_shell *shell = data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400507
508 if (shell->client != client)
509 /* Throw an error or just let the client fail when it
510 * tries to access the object?. */
511 return;
512
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400513 shell->resource.object.id = id;
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500514 shell->resource.object.interface = &tablet_shell_interface;
515 shell->resource.object.implementation =
516 (void (**)(void)) &tablet_shell_implementation;
517 shell->resource.client = client;
518 shell->resource.data = shell;
519 shell->resource.destroy = destroy_tablet_shell;
520
521 wl_client_add_resource(client, &shell->resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400522}
523
Pekka Paalanen3c647232011-12-22 13:43:43 +0200524static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500525tablet_shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +0200526{
527 struct tablet_shell *shell =
528 container_of(base, struct tablet_shell, shell);
529
Tiago Vignatti0a386112012-03-28 13:04:02 +0300530 if (shell->home_surface)
531 shell->home_surface->configure = NULL;
532
533 if (shell->lockscreen_surface)
534 shell->lockscreen_surface->configure = NULL;
535
Pekka Paalanen3c647232011-12-22 13:43:43 +0200536 wl_event_source_remove(shell->long_press_source);
537 free(shell);
538}
539
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400540void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500541shell_init(struct weston_compositor *compositor);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400542
543WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500544shell_init(struct weston_compositor *compositor)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400545{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500546 struct tablet_shell *shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400547 struct wl_event_loop *loop;
548
549 shell = malloc(sizeof *shell);
550 if (shell == NULL)
551 return;
552
553 memset(shell, 0, sizeof *shell);
554 shell->compositor = compositor;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400555
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400556 /* FIXME: This will make the object available to all clients. */
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400557 wl_display_add_global(compositor->wl_display,
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500558 &tablet_shell_interface, shell, bind_shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400559
560 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400561 shell->long_press_source =
562 wl_event_loop_add_timer(loop, long_press_handler, shell);
563
Scott Moreau6a3633d2012-03-20 08:47:59 -0600564 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0, 0, 0,
565 home_key_binding, shell);
566 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0, 0, 0,
567 home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500568 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600569 MODIFIER_SUPER, home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500570 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0, 0,
Kristian Høgsbergf77ce462011-05-03 13:11:43 -0400571 MODIFIER_SUPER, home_key_binding, shell);
Scott Moreau6a3633d2012-03-20 08:47:59 -0600572 weston_compositor_add_binding(compositor, KEY_COMPOSE, 0, 0, 0,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400573 menu_key_binding, shell);
574
575 compositor->shell = &shell->shell;
576
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500577 shell->shell.lock = tablet_shell_lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200578 shell->shell.unlock = tablet_shell_unlock;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500579 shell->shell.map = tablet_shell_map;
580 shell->shell.configure = tablet_shell_configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +0200581 shell->shell.destroy = tablet_shell_destroy;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400582
Tiago Vignatti0a386112012-03-28 13:04:02 +0300583 weston_layer_init(&shell->homescreen_layer,
584 &compositor->cursor_layer.link);
585 weston_layer_init(&shell->lockscreen_layer,
586 &compositor->cursor_layer.link);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400587 launch_ux_daemon(shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400588
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500589 tablet_shell_set_state(shell, STATE_STARTING);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400590}