blob: 2c325db006fa24a6d73fadf2786e5ef700dd6a98 [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,
Kristian Høgsberg9724b512012-01-03 14:35:49 -050090 "weston-tablet-shell 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,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +0200108 int32_t width, int32_t height, int32_t sx, int32_t sy)
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
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200113 weston_surface_configure(surface, 0, 0, width, height);
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400114
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400115 if (surface == shell->lockscreen_surface) {
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500116 /* */
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400117 } else if (surface == shell->switcher_surface) {
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400118 /* */
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400119 } else if (surface == shell->home_surface) {
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400120 if (shell->state == STATE_STARTING) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500121 tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400122 shell->previous_state = STATE_HOME;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400123 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500124 TABLET_SHELL_SHOW_LOCKSCREEN);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400125 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400126 } else if (shell->current_client &&
127 shell->current_client->surface != surface &&
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400128 shell->current_client->client == surface->surface.resource.client) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500129 tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400130 shell->current_client->surface = surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500131 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400132 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500133
134 wl_list_insert(&shell->compositor->surface_list, &surface->link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200135 weston_surface_assign_output(surface);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500136}
137
138static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500139tablet_shell_configure(struct weston_shell *base,
140 struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200141 GLfloat x, GLfloat y,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500142 int32_t width, int32_t height)
143{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500144 weston_surface_configure(surface, x, y, width, height);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400145}
146
147static void
148handle_lockscreen_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200149 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400150{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500151 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400152 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500153 struct tablet_shell, lockscreen_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400154
155 shell->lockscreen_surface = NULL;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500156 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400157}
158
159static void
160tablet_shell_set_lockscreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400161 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400162 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400163{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500164 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500165 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400166
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200167 weston_surface_set_position(es, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400168 shell->lockscreen_surface = es;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400169 shell->lockscreen_listener.func = handle_lockscreen_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200170 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400171 &shell->lockscreen_listener.link);
172}
173
174static void
175handle_switcher_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200176 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400177{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500178 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400179 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500180 struct tablet_shell, switcher_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400181
182 shell->switcher_surface = NULL;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400183 if (shell->state != STATE_LOCKED)
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500184 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400185}
186
187static void
188tablet_shell_set_switcher(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400189 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400190 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400191{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500192 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500193 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400194
195 /* FIXME: Switcher should be centered and the compositor
196 * should do the tinting of the background. With the cache
197 * layer idea, we should be able to hit the framerate on the
198 * fade/zoom in. */
199 shell->switcher_surface = es;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200200 weston_surface_set_position(shell->switcher_surface, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400201
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400202 shell->switcher_listener.func = handle_switcher_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200203 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400204 &shell->switcher_listener.link);
205}
206
207static void
208tablet_shell_set_homescreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400209 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400210 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400211{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500212 struct tablet_shell *shell = resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400213
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400214 shell->home_surface = surface_resource->data;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200215 weston_surface_set_position(shell->home_surface, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400216}
217
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400218static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500219minimize_zoom_done(struct weston_zoom *zoom, void *data)
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400220{
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500221 struct tablet_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500222 struct weston_compositor *compositor = shell->compositor;
223 struct weston_input_device *device =
224 (struct weston_input_device *) compositor->input_device;
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400225
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500226 weston_surface_activate(shell->home_surface,
227 device, weston_compositor_get_time());
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400228}
229
230static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500231tablet_shell_switch_to(struct tablet_shell *shell,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500232 struct weston_surface *surface)
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400233{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500234 struct weston_compositor *compositor = shell->compositor;
235 struct weston_input_device *device =
236 (struct weston_input_device *) compositor->input_device;
237 struct weston_surface *current;
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400238
239 if (shell->state == STATE_SWITCHER) {
240 wl_list_remove(&shell->switcher_listener.link);
241 shell->switcher_surface = NULL;
242 };
243
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400244 if (surface == shell->home_surface) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500245 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400246
247 if (shell->current_client && shell->current_client->surface) {
248 current = shell->current_client->surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500249 weston_zoom_run(current, 1.0, 0.3,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500250 minimize_zoom_done, shell);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400251 }
252 } else {
253 fprintf(stderr, "switch to %p\n", surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500254 weston_surface_activate(surface, device,
255 weston_compositor_get_time());
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500256 tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500257 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400258 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400259}
260
261static void
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400262tablet_shell_show_grid(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400263 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400264 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400265{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500266 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500267 struct weston_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400268
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500269 tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400270}
271
272static void
273tablet_shell_show_panels(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400274 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400275 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400276{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500277 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500278 struct weston_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400279
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500280 tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400281}
282
283static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400284destroy_tablet_client(struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400285{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500286 struct tablet_client *tablet_client =
287 container_of(resource, struct tablet_client, resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400288
289 free(tablet_client->name);
290 free(tablet_client);
291}
292
293static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400294tablet_client_destroy(struct wl_client *client,
295 struct wl_resource *resource)
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400296{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500297 wl_resource_destroy(resource, weston_compositor_get_time());
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400298}
299
300static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400301tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400302{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500303 struct tablet_client *tablet_client = resource->data;
304 struct tablet_shell *shell = tablet_client->shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400305
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400306 shell->current_client = tablet_client;
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400307 if (!tablet_client->surface)
308 return;
309
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500310 tablet_shell_switch_to(shell, tablet_client->surface);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400311}
312
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500313static const struct tablet_client_interface tablet_client_implementation = {
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400314 tablet_client_destroy,
315 tablet_client_activate
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400316};
317
318static void
319tablet_shell_create_client(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400320 struct wl_resource *resource,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400321 uint32_t id, const char *name, int fd)
322{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500323 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500324 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500325 struct tablet_client *tablet_client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400326
327 tablet_client = malloc(sizeof *tablet_client);
328 if (tablet_client == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400329 wl_resource_post_no_memory(resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400330 return;
331 }
332
333 tablet_client->client = wl_client_create(compositor->wl_display, fd);
334 tablet_client->shell = shell;
335 tablet_client->name = strdup(name);
336
337 tablet_client->resource.destroy = destroy_tablet_client;
338 tablet_client->resource.object.id = id;
339 tablet_client->resource.object.interface =
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500340 &tablet_client_interface;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400341 tablet_client->resource.object.implementation =
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500342 (void (**)(void)) &tablet_client_implementation;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400343
344 wl_client_add_resource(client, &tablet_client->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400345 tablet_client->surface = NULL;
346 shell->current_client = tablet_client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400347
348 fprintf(stderr, "created client %p, id %d, name %s, fd %d\n",
349 tablet_client->client, id, name, fd);
350}
351
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500352static const struct tablet_shell_interface tablet_shell_implementation = {
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400353 tablet_shell_set_lockscreen,
354 tablet_shell_set_switcher,
355 tablet_shell_set_homescreen,
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400356 tablet_shell_show_grid,
357 tablet_shell_show_panels,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400358 tablet_shell_create_client
359};
360
361static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500362launch_ux_daemon(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400363{
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500364 const char *shell_exe = LIBEXECDIR "/weston-tablet-shell";
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400365
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500366 shell->client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200367 &shell->process,
368 shell_exe, tablet_shell_sigchld);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400369}
370
371static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500372toggle_switcher(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400373{
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400374 switch (shell->state) {
375 case STATE_SWITCHER:
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400376 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500377 TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400378 break;
379 default:
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_SHOW_SWITCHER);
382 tablet_shell_set_state(shell, STATE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400383 break;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400384 }
385}
386
387static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500388tablet_shell_lock(struct weston_shell *base)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400389{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500390 struct tablet_shell *shell =
391 container_of(base, struct tablet_shell, shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400392
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400393 if (shell->state == STATE_LOCKED)
394 return;
395 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400396 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500397 TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400398
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400399 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500400 TABLET_SHELL_SHOW_LOCKSCREEN);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400401
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500402 tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400403}
404
405static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500406tablet_shell_unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200407{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200408 struct tablet_shell *shell =
409 container_of(base, struct tablet_shell, shell);
410
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500411 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200412}
413
414static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500415go_home(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400416{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500417 struct weston_input_device *device =
418 (struct weston_input_device *) shell->compositor->input_device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400419
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400420 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400421 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500422 TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400423
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500424 weston_surface_activate(shell->home_surface, device,
425 weston_compositor_get_time());
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400426
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500427 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400428}
429
430static int
431long_press_handler(void *data)
432{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500433 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400434
435 shell->long_press_active = 0;
436 toggle_switcher(shell);
437
438 return 1;
439}
440
441static void
442menu_key_binding(struct wl_input_device *device, uint32_t time,
443 uint32_t key, uint32_t button, uint32_t state, void *data)
444{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500445 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400446
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400447 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400448 return;
449
450 if (state)
451 toggle_switcher(shell);
452}
453
454static void
455home_key_binding(struct wl_input_device *device, uint32_t time,
456 uint32_t key, uint32_t button, uint32_t state, void *data)
457{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500458 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400459
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400460 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400461 return;
462
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500463 shell->device = (struct weston_input_device *) device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400464
465 if (state) {
466 wl_event_source_timer_update(shell->long_press_source, 500);
467 shell->long_press_active = 1;
468 } else if (shell->long_press_active) {
469 wl_event_source_timer_update(shell->long_press_source, 0);
470 shell->long_press_active = 0;
471
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400472 switch (shell->state) {
473 case STATE_HOME:
474 case STATE_SWITCHER:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400475 toggle_switcher(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400476 break;
477 default:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400478 go_home(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400479 break;
480 }
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400481 }
482}
483
484static void
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500485destroy_tablet_shell(struct wl_resource *resource)
486{
487}
488
489static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400490bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400491{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500492 struct tablet_shell *shell = data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400493
494 if (shell->client != client)
495 /* Throw an error or just let the client fail when it
496 * tries to access the object?. */
497 return;
498
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400499 shell->resource.object.id = id;
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500500 shell->resource.object.interface = &tablet_shell_interface;
501 shell->resource.object.implementation =
502 (void (**)(void)) &tablet_shell_implementation;
503 shell->resource.client = client;
504 shell->resource.data = shell;
505 shell->resource.destroy = destroy_tablet_shell;
506
507 wl_client_add_resource(client, &shell->resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400508}
509
Pekka Paalanen3c647232011-12-22 13:43:43 +0200510static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500511tablet_shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +0200512{
513 struct tablet_shell *shell =
514 container_of(base, struct tablet_shell, shell);
515
516 wl_event_source_remove(shell->long_press_source);
517 free(shell);
518}
519
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400520void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500521shell_init(struct weston_compositor *compositor);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400522
523WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500524shell_init(struct weston_compositor *compositor)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400525{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500526 struct tablet_shell *shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400527 struct wl_event_loop *loop;
528
529 shell = malloc(sizeof *shell);
530 if (shell == NULL)
531 return;
532
533 memset(shell, 0, sizeof *shell);
534 shell->compositor = compositor;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400535
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400536 /* FIXME: This will make the object available to all clients. */
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400537 wl_display_add_global(compositor->wl_display,
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500538 &tablet_shell_interface, shell, bind_shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400539
540 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400541 shell->long_press_source =
542 wl_event_loop_add_timer(loop, long_press_handler, shell);
543
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500544 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0, 0,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400545 home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500546 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0, 0,
Kristian Høgsbergd28b4d72011-05-02 14:11:15 -0400547 home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500548 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0,
Kristian Høgsbergf77ce462011-05-03 13:11:43 -0400549 MODIFIER_SUPER, home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500550 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0,
Kristian Høgsbergf77ce462011-05-03 13:11:43 -0400551 MODIFIER_SUPER, home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500552 weston_compositor_add_binding(compositor, KEY_COMPOSE, 0, 0,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400553 menu_key_binding, shell);
554
555 compositor->shell = &shell->shell;
556
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500557 shell->shell.lock = tablet_shell_lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200558 shell->shell.unlock = tablet_shell_unlock;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500559 shell->shell.map = tablet_shell_map;
560 shell->shell.configure = tablet_shell_configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +0200561 shell->shell.destroy = tablet_shell_destroy;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400562
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400563 launch_ux_daemon(shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400564
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500565 tablet_shell_set_state(shell, STATE_STARTING);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400566}