blob: 0ec31ea1f2a9aab3335eb58b56fda86a032a54cf [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øgsberg34486162012-03-29 13:08:32 -0400109tablet_shell_surface_configure(struct weston_surface *surface,
110 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 =
Kristian Høgsberg34486162012-03-29 13:08:32 -0400113 container_of(surface->compositor->shell,
114 struct tablet_shell, shell);
115 int32_t width, height;
116
117 if (weston_surface_is_mapped(surface))
118 return;
119
120 width = surface->buffer->width;
121 height = surface->buffer->height;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400122
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200123 weston_surface_configure(surface, 0, 0, width, height);
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400124
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400125 if (surface == shell->lockscreen_surface) {
Tiago Vignatti0a386112012-03-28 13:04:02 +0300126 wl_list_insert(&shell->lockscreen_layer.surface_list,
127 &surface->layer_link);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400128 } else if (surface == shell->switcher_surface) {
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400129 /* */
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400130 } else if (surface == shell->home_surface) {
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400131 if (shell->state == STATE_STARTING) {
Tiago Vignatti0a386112012-03-28 13:04:02 +0300132 /* homescreen always visible, at the bottom */
133 wl_list_insert(&shell->homescreen_layer.surface_list,
134 &surface->layer_link);
135
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500136 tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400137 shell->previous_state = STATE_HOME;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500138 tablet_shell_send_show_lockscreen(&shell->resource);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400139 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400140 } else if (shell->current_client &&
141 shell->current_client->surface != surface &&
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400142 shell->current_client->client == surface->surface.resource.client) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500143 tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400144 shell->current_client->surface = surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500145 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400146 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500147
148 wl_list_insert(&shell->compositor->surface_list, &surface->link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200149 weston_surface_assign_output(surface);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500150}
151
152static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400153handle_lockscreen_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400154{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500155 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400156 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500157 struct tablet_shell, lockscreen_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400158
159 shell->lockscreen_surface = NULL;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500160 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400161}
162
163static void
164tablet_shell_set_lockscreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400165 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400166 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400167{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500168 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500169 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400170
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200171 weston_surface_set_position(es, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400172 shell->lockscreen_surface = es;
Tiago Vignatti0a386112012-03-28 13:04:02 +0300173 shell->lockscreen_surface->configure = tablet_shell_surface_configure;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400174 shell->lockscreen_listener.notify = handle_lockscreen_surface_destroy;
175 wl_signal_add(&es->surface.resource.destroy_signal,
176 &shell->lockscreen_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400177}
178
179static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400180handle_switcher_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400181{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500182 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400183 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500184 struct tablet_shell, switcher_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400185
186 shell->switcher_surface = NULL;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400187 if (shell->state != STATE_LOCKED)
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500188 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400189}
190
191static void
192tablet_shell_set_switcher(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400193 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400194 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400195{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500196 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500197 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400198
199 /* FIXME: Switcher should be centered and the compositor
200 * should do the tinting of the background. With the cache
201 * layer idea, we should be able to hit the framerate on the
202 * fade/zoom in. */
203 shell->switcher_surface = es;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200204 weston_surface_set_position(shell->switcher_surface, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400205
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400206 shell->switcher_listener.notify = handle_switcher_surface_destroy;
207 wl_signal_add(&es->surface.resource.destroy_signal,
208 &shell->switcher_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400209}
210
211static void
212tablet_shell_set_homescreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400213 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400214 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400215{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500216 struct tablet_shell *shell = resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400217
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400218 shell->home_surface = surface_resource->data;
Tiago Vignatti0a386112012-03-28 13:04:02 +0300219 shell->home_surface->configure = tablet_shell_surface_configure;
220
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200221 weston_surface_set_position(shell->home_surface, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400222}
223
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400224static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500225minimize_zoom_done(struct weston_zoom *zoom, void *data)
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400226{
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500227 struct tablet_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228 struct weston_compositor *compositor = shell->compositor;
229 struct weston_input_device *device =
230 (struct weston_input_device *) compositor->input_device;
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400231
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400232 weston_surface_activate(shell->home_surface, device);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400233}
234
235static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500236tablet_shell_switch_to(struct tablet_shell *shell,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500237 struct weston_surface *surface)
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400238{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500239 struct weston_compositor *compositor = shell->compositor;
240 struct weston_input_device *device =
241 (struct weston_input_device *) compositor->input_device;
242 struct weston_surface *current;
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400243
244 if (shell->state == STATE_SWITCHER) {
245 wl_list_remove(&shell->switcher_listener.link);
246 shell->switcher_surface = NULL;
247 };
248
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400249 if (surface == shell->home_surface) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500250 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400251
252 if (shell->current_client && shell->current_client->surface) {
253 current = shell->current_client->surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500254 weston_zoom_run(current, 1.0, 0.3,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500255 minimize_zoom_done, shell);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400256 }
257 } else {
258 fprintf(stderr, "switch to %p\n", surface);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400259 weston_surface_activate(surface, device);
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øgsbergeae5de72012-04-11 22:42:15 -0400301 wl_resource_destroy(resource);
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øgsberg9724b512012-01-03 14:35:49 -0500368 const char *shell_exe = LIBEXECDIR "/weston-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øgsberg0b5cd0c2012-03-04 21:57:37 -0500380 tablet_shell_send_hide_switcher(&shell->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400381 break;
382 default:
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500383 tablet_shell_send_show_switcher(&shell->resource);
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500384 tablet_shell_set_state(shell, STATE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400385 break;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400386 }
387}
388
389static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500390tablet_shell_lock(struct weston_shell *base)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400391{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500392 struct tablet_shell *shell =
393 container_of(base, struct tablet_shell, shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400394
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400395 if (shell->state == STATE_LOCKED)
396 return;
397 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500398 tablet_shell_send_hide_switcher(&shell->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400399
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500400 tablet_shell_send_show_lockscreen(&shell->resource);
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500401 tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400402}
403
404static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500405tablet_shell_unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200406{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200407 struct tablet_shell *shell =
408 container_of(base, struct tablet_shell, shell);
409
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500410 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200411}
412
413static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500414go_home(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400415{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500416 struct weston_input_device *device =
417 (struct weston_input_device *) shell->compositor->input_device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400418
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400419 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500420 tablet_shell_send_hide_switcher(&shell->resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400421
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400422 weston_surface_activate(shell->home_surface, device);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400423
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500424 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400425}
426
427static int
428long_press_handler(void *data)
429{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500430 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400431
432 shell->long_press_active = 0;
433 toggle_switcher(shell);
434
435 return 1;
436}
437
438static void
439menu_key_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600440 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400441{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500442 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400443
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400444 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400445 return;
446
447 if (state)
448 toggle_switcher(shell);
449}
450
451static void
452home_key_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600453 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400454{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500455 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400456
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400457 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400458 return;
459
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500460 shell->device = (struct weston_input_device *) device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400461
462 if (state) {
463 wl_event_source_timer_update(shell->long_press_source, 500);
464 shell->long_press_active = 1;
465 } else if (shell->long_press_active) {
466 wl_event_source_timer_update(shell->long_press_source, 0);
467 shell->long_press_active = 0;
468
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400469 switch (shell->state) {
470 case STATE_HOME:
471 case STATE_SWITCHER:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400472 toggle_switcher(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400473 break;
474 default:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400475 go_home(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400476 break;
477 }
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400478 }
479}
480
481static void
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500482destroy_tablet_shell(struct wl_resource *resource)
483{
484}
485
486static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400487bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400488{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500489 struct tablet_shell *shell = data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400490
491 if (shell->client != client)
492 /* Throw an error or just let the client fail when it
493 * tries to access the object?. */
494 return;
495
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400496 shell->resource.object.id = id;
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500497 shell->resource.object.interface = &tablet_shell_interface;
498 shell->resource.object.implementation =
499 (void (**)(void)) &tablet_shell_implementation;
500 shell->resource.client = client;
501 shell->resource.data = shell;
502 shell->resource.destroy = destroy_tablet_shell;
503
504 wl_client_add_resource(client, &shell->resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400505}
506
Pekka Paalanen3c647232011-12-22 13:43:43 +0200507static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500508tablet_shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +0200509{
510 struct tablet_shell *shell =
511 container_of(base, struct tablet_shell, shell);
512
Tiago Vignatti0a386112012-03-28 13:04:02 +0300513 if (shell->home_surface)
514 shell->home_surface->configure = NULL;
515
516 if (shell->lockscreen_surface)
517 shell->lockscreen_surface->configure = NULL;
518
Pekka Paalanen3c647232011-12-22 13:43:43 +0200519 wl_event_source_remove(shell->long_press_source);
520 free(shell);
521}
522
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400523void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500524shell_init(struct weston_compositor *compositor);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400525
526WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500527shell_init(struct weston_compositor *compositor)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400528{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500529 struct tablet_shell *shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400530 struct wl_event_loop *loop;
531
532 shell = malloc(sizeof *shell);
533 if (shell == NULL)
534 return;
535
536 memset(shell, 0, sizeof *shell);
537 shell->compositor = compositor;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400538
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400539 /* FIXME: This will make the object available to all clients. */
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400540 wl_display_add_global(compositor->wl_display,
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500541 &tablet_shell_interface, shell, bind_shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400542
543 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400544 shell->long_press_source =
545 wl_event_loop_add_timer(loop, long_press_handler, shell);
546
Scott Moreau6a3633d2012-03-20 08:47:59 -0600547 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0, 0, 0,
548 home_key_binding, shell);
549 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0, 0, 0,
550 home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500551 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600552 MODIFIER_SUPER, home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500553 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0, 0,
Kristian Høgsbergf77ce462011-05-03 13:11:43 -0400554 MODIFIER_SUPER, home_key_binding, shell);
Scott Moreau6a3633d2012-03-20 08:47:59 -0600555 weston_compositor_add_binding(compositor, KEY_COMPOSE, 0, 0, 0,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400556 menu_key_binding, shell);
557
558 compositor->shell = &shell->shell;
559
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500560 shell->shell.lock = tablet_shell_lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200561 shell->shell.unlock = tablet_shell_unlock;
Pekka Paalanen3c647232011-12-22 13:43:43 +0200562 shell->shell.destroy = tablet_shell_destroy;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400563
Tiago Vignatti0a386112012-03-28 13:04:02 +0300564 weston_layer_init(&shell->homescreen_layer,
565 &compositor->cursor_layer.link);
566 weston_layer_init(&shell->lockscreen_layer,
567 &compositor->cursor_layer.link);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400568 launch_ux_daemon(shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400569
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500570 tablet_shell_set_state(shell, STATE_STARTING);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400571}