blob: 46c57a11fcd12e5ea769d838fd0d0d383253a7a9 [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;
60
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050061 struct weston_surface *home_surface;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040062
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050063 struct weston_surface *switcher_surface;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040064 struct wl_listener switcher_listener;
65
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050066 struct tablet_client *current_client;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -040067
68 int state, previous_state;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040069 int long_press_active;
70 struct wl_event_source *long_press_source;
71};
72
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050073struct tablet_client {
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -040074 struct wl_resource resource;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050075 struct tablet_shell *shell;
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -040076 struct wl_client *client;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050077 struct weston_surface *surface;
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -040078 char *name;
79};
80
Kristian Høgsbergd4af3202011-06-21 17:43:31 -040081static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050082tablet_shell_sigchld(struct weston_process *process, int status)
Kristian Høgsbergd4af3202011-06-21 17:43:31 -040083{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050084 struct tablet_shell *shell =
85 container_of(process, struct tablet_shell, process);
Kristian Høgsbergd4af3202011-06-21 17:43:31 -040086
87 shell->process.pid = 0;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040088
Kristian Høgsberg6336e462011-11-26 17:36:23 -050089 fprintf(stderr,
90 "wayland-tablet-daemon crashed, exit code %d\n", status);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040091}
92
93static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -050094tablet_shell_set_state(struct tablet_shell *shell, int state)
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -040095{
96 static const char *states[] = {
97 "STARTING", "LOCKED", "HOME", "SWITCHER", "TASK"
98 };
99
100 fprintf(stderr, "switching to state %s (from %s)\n",
101 states[state], states[shell->state]);
102 shell->previous_state = shell->state;
103 shell->state = state;
104}
105
106static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500107tablet_shell_map(struct weston_shell *base, struct weston_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500108 int32_t width, int32_t height)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400109{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500110 struct tablet_shell *shell =
111 container_of(base, struct tablet_shell, shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400112
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400113 surface->x = 0;
114 surface->y = 0;
115
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400116 if (surface == shell->lockscreen_surface) {
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500117 /* */
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400118 } else if (surface == shell->switcher_surface) {
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400119 /* */
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400120 } else if (surface == shell->home_surface) {
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400121 if (shell->state == STATE_STARTING) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500122 tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400123 shell->previous_state = STATE_HOME;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400124 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500125 TABLET_SHELL_SHOW_LOCKSCREEN);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400126 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400127 } else if (shell->current_client &&
128 shell->current_client->surface != surface &&
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400129 shell->current_client->client == surface->surface.resource.client) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500130 tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400131 shell->current_client->surface = surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500132 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400133 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500134
135 wl_list_insert(&shell->compositor->surface_list, &surface->link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500136 weston_surface_configure(surface, surface->x, surface->y, width, height);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500137}
138
139static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500140tablet_shell_configure(struct weston_shell *base,
141 struct weston_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500142 int32_t x, int32_t y,
143 int32_t width, int32_t height)
144{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500145 weston_surface_configure(surface, x, y, width, height);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400146}
147
148static void
149handle_lockscreen_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200150 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400151{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500152 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400153 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500154 struct tablet_shell, lockscreen_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400155
156 shell->lockscreen_surface = NULL;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500157 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400158}
159
160static void
161tablet_shell_set_lockscreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400162 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400163 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400164{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500165 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500166 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400167
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400168 es->x = 0;
169 es->y = 0;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400170 shell->lockscreen_surface = es;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400171 shell->lockscreen_listener.func = handle_lockscreen_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200172 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400173 &shell->lockscreen_listener.link);
174}
175
176static void
177handle_switcher_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200178 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400179{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500180 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400181 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500182 struct tablet_shell, switcher_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400183
184 shell->switcher_surface = NULL;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400185 if (shell->state != STATE_LOCKED)
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500186 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400187}
188
189static void
190tablet_shell_set_switcher(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400191 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400192 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400193{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500194 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500195 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400196
197 /* FIXME: Switcher should be centered and the compositor
198 * should do the tinting of the background. With the cache
199 * layer idea, we should be able to hit the framerate on the
200 * fade/zoom in. */
201 shell->switcher_surface = es;
202 shell->switcher_surface->x = 0;
203 shell->switcher_surface->y = 0;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400204
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400205 shell->switcher_listener.func = handle_switcher_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200206 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400207 &shell->switcher_listener.link);
208}
209
210static void
211tablet_shell_set_homescreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400212 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400213 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400214{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500215 struct tablet_shell *shell = resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400216
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400217 shell->home_surface = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400218 shell->home_surface->x = 0;
219 shell->home_surface->y = 0;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400220}
221
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400222static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500223minimize_zoom_done(struct weston_zoom *zoom, void *data)
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400224{
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500225 struct tablet_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500226 struct weston_compositor *compositor = shell->compositor;
227 struct weston_input_device *device =
228 (struct weston_input_device *) compositor->input_device;
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400229
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500230 weston_surface_activate(shell->home_surface,
231 device, weston_compositor_get_time());
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400232}
233
234static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500235tablet_shell_switch_to(struct tablet_shell *shell,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500236 struct weston_surface *surface)
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400237{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500238 struct weston_compositor *compositor = shell->compositor;
239 struct weston_input_device *device =
240 (struct weston_input_device *) compositor->input_device;
241 struct weston_surface *current;
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400242
243 if (shell->state == STATE_SWITCHER) {
244 wl_list_remove(&shell->switcher_listener.link);
245 shell->switcher_surface = NULL;
246 };
247
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400248 if (surface == shell->home_surface) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500249 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400250
251 if (shell->current_client && shell->current_client->surface) {
252 current = shell->current_client->surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500253 weston_zoom_run(current, 1.0, 0.3,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500254 minimize_zoom_done, shell);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400255 }
256 } else {
257 fprintf(stderr, "switch to %p\n", surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500258 weston_surface_activate(surface, device,
259 weston_compositor_get_time());
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500260 tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500261 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400262 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400263}
264
265static void
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400266tablet_shell_show_grid(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400267 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400268 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400269{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500270 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500271 struct weston_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400272
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500273 tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400274}
275
276static void
277tablet_shell_show_panels(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400278 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400279 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400280{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500281 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500282 struct weston_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400283
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500284 tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400285}
286
287static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400288destroy_tablet_client(struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400289{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500290 struct tablet_client *tablet_client =
291 container_of(resource, struct tablet_client, resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400292
293 free(tablet_client->name);
294 free(tablet_client);
295}
296
297static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400298tablet_client_destroy(struct wl_client *client,
299 struct wl_resource *resource)
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400300{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500301 wl_resource_destroy(resource, weston_compositor_get_time());
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400302}
303
304static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400305tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400306{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500307 struct tablet_client *tablet_client = resource->data;
308 struct tablet_shell *shell = tablet_client->shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400309
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400310 shell->current_client = tablet_client;
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400311 if (!tablet_client->surface)
312 return;
313
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500314 tablet_shell_switch_to(shell, tablet_client->surface);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400315}
316
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500317static const struct tablet_client_interface tablet_client_implementation = {
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400318 tablet_client_destroy,
319 tablet_client_activate
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400320};
321
322static void
323tablet_shell_create_client(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400324 struct wl_resource *resource,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400325 uint32_t id, const char *name, int fd)
326{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500327 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500328 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500329 struct tablet_client *tablet_client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400330
331 tablet_client = malloc(sizeof *tablet_client);
332 if (tablet_client == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400333 wl_resource_post_no_memory(resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400334 return;
335 }
336
337 tablet_client->client = wl_client_create(compositor->wl_display, fd);
338 tablet_client->shell = shell;
339 tablet_client->name = strdup(name);
340
341 tablet_client->resource.destroy = destroy_tablet_client;
342 tablet_client->resource.object.id = id;
343 tablet_client->resource.object.interface =
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500344 &tablet_client_interface;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400345 tablet_client->resource.object.implementation =
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500346 (void (**)(void)) &tablet_client_implementation;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400347
348 wl_client_add_resource(client, &tablet_client->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400349 tablet_client->surface = NULL;
350 shell->current_client = tablet_client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400351
352 fprintf(stderr, "created client %p, id %d, name %s, fd %d\n",
353 tablet_client->client, id, name, fd);
354}
355
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500356static const struct tablet_shell_interface tablet_shell_implementation = {
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400357 tablet_shell_set_lockscreen,
358 tablet_shell_set_switcher,
359 tablet_shell_set_homescreen,
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400360 tablet_shell_show_grid,
361 tablet_shell_show_panels,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400362 tablet_shell_create_client
363};
364
365static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500366launch_ux_daemon(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400367{
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500368 const char *shell_exe = LIBEXECDIR "/wayland-tablet-shell";
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400369
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500370 shell->client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200371 &shell->process,
372 shell_exe, tablet_shell_sigchld);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400373}
374
375static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500376toggle_switcher(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400377{
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400378 switch (shell->state) {
379 case STATE_SWITCHER:
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400380 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500381 TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400382 break;
383 default:
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400384 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500385 TABLET_SHELL_SHOW_SWITCHER);
386 tablet_shell_set_state(shell, STATE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400387 break;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400388 }
389}
390
391static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500392tablet_shell_lock(struct weston_shell *base)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400393{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500394 struct tablet_shell *shell =
395 container_of(base, struct tablet_shell, shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400396
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400397 if (shell->state == STATE_LOCKED)
398 return;
399 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400400 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500401 TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400402
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400403 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500404 TABLET_SHELL_SHOW_LOCKSCREEN);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400405
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500406 tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400407}
408
409static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500410tablet_shell_unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200411{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200412 struct tablet_shell *shell =
413 container_of(base, struct tablet_shell, shell);
414
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500415 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200416}
417
418static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500419go_home(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400420{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500421 struct weston_input_device *device =
422 (struct weston_input_device *) shell->compositor->input_device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400423
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400424 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400425 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500426 TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400427
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500428 weston_surface_activate(shell->home_surface, device,
429 weston_compositor_get_time());
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400430
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500431 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400432}
433
434static int
435long_press_handler(void *data)
436{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500437 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400438
439 shell->long_press_active = 0;
440 toggle_switcher(shell);
441
442 return 1;
443}
444
445static void
446menu_key_binding(struct wl_input_device *device, uint32_t time,
447 uint32_t key, uint32_t button, uint32_t state, void *data)
448{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500449 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400450
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400451 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400452 return;
453
454 if (state)
455 toggle_switcher(shell);
456}
457
458static void
459home_key_binding(struct wl_input_device *device, uint32_t time,
460 uint32_t key, uint32_t button, uint32_t state, void *data)
461{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500462 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400463
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400464 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400465 return;
466
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500467 shell->device = (struct weston_input_device *) device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400468
469 if (state) {
470 wl_event_source_timer_update(shell->long_press_source, 500);
471 shell->long_press_active = 1;
472 } else if (shell->long_press_active) {
473 wl_event_source_timer_update(shell->long_press_source, 0);
474 shell->long_press_active = 0;
475
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400476 switch (shell->state) {
477 case STATE_HOME:
478 case STATE_SWITCHER:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400479 toggle_switcher(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400480 break;
481 default:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400482 go_home(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400483 break;
484 }
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400485 }
486}
487
488static void
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500489destroy_tablet_shell(struct wl_resource *resource)
490{
491}
492
493static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400494bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400495{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500496 struct tablet_shell *shell = data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400497
498 if (shell->client != client)
499 /* Throw an error or just let the client fail when it
500 * tries to access the object?. */
501 return;
502
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400503 shell->resource.object.id = id;
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500504 shell->resource.object.interface = &tablet_shell_interface;
505 shell->resource.object.implementation =
506 (void (**)(void)) &tablet_shell_implementation;
507 shell->resource.client = client;
508 shell->resource.data = shell;
509 shell->resource.destroy = destroy_tablet_shell;
510
511 wl_client_add_resource(client, &shell->resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400512}
513
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400514void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500515shell_init(struct weston_compositor *compositor);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400516
517WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500518shell_init(struct weston_compositor *compositor)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400519{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500520 struct tablet_shell *shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400521 struct wl_event_loop *loop;
522
523 shell = malloc(sizeof *shell);
524 if (shell == NULL)
525 return;
526
527 memset(shell, 0, sizeof *shell);
528 shell->compositor = compositor;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400529
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400530 /* FIXME: This will make the object available to all clients. */
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400531 wl_display_add_global(compositor->wl_display,
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500532 &tablet_shell_interface, shell, bind_shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400533
534 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400535 shell->long_press_source =
536 wl_event_loop_add_timer(loop, long_press_handler, shell);
537
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500538 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0, 0,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400539 home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500540 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0, 0,
Kristian Høgsbergd28b4d72011-05-02 14:11:15 -0400541 home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500542 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0,
Kristian Høgsbergf77ce462011-05-03 13:11:43 -0400543 MODIFIER_SUPER, home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500544 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0,
Kristian Høgsbergf77ce462011-05-03 13:11:43 -0400545 MODIFIER_SUPER, home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500546 weston_compositor_add_binding(compositor, KEY_COMPOSE, 0, 0,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400547 menu_key_binding, shell);
548
549 compositor->shell = &shell->shell;
550
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500551 shell->shell.lock = tablet_shell_lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200552 shell->shell.unlock = tablet_shell_unlock;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500553 shell->shell.map = tablet_shell_map;
554 shell->shell.configure = tablet_shell_configure;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400555
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400556 launch_ux_daemon(shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400557
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500558 tablet_shell_set_state(shell, STATE_STARTING);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400559}