blob: fe215abdf47e91191cbe2c39a0677ab90db846d1 [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øgsberg0b5cd0c2012-03-04 21:57:37 -0500123 tablet_shell_send_show_lockscreen(&shell->resource);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400124 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400125 } else if (shell->current_client &&
126 shell->current_client->surface != surface &&
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400127 shell->current_client->client == surface->surface.resource.client) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500128 tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400129 shell->current_client->surface = surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500130 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400131 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500132
133 wl_list_insert(&shell->compositor->surface_list, &surface->link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200134 weston_surface_assign_output(surface);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500135}
136
137static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500138tablet_shell_configure(struct weston_shell *base,
139 struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200140 GLfloat x, GLfloat y,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500141 int32_t width, int32_t height)
142{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500143 weston_surface_configure(surface, x, y, width, height);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400144}
145
146static void
147handle_lockscreen_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200148 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400149{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500150 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400151 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500152 struct tablet_shell, lockscreen_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400153
154 shell->lockscreen_surface = NULL;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500155 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400156}
157
158static void
159tablet_shell_set_lockscreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400160 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400161 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400162{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500163 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500164 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400165
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200166 weston_surface_set_position(es, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400167 shell->lockscreen_surface = es;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400168 shell->lockscreen_listener.func = handle_lockscreen_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200169 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400170 &shell->lockscreen_listener.link);
171}
172
173static void
174handle_switcher_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200175 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400176{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500177 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400178 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500179 struct tablet_shell, switcher_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400180
181 shell->switcher_surface = NULL;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400182 if (shell->state != STATE_LOCKED)
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500183 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400184}
185
186static void
187tablet_shell_set_switcher(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400188 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400189 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400190{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500191 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500192 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400193
194 /* FIXME: Switcher should be centered and the compositor
195 * should do the tinting of the background. With the cache
196 * layer idea, we should be able to hit the framerate on the
197 * fade/zoom in. */
198 shell->switcher_surface = es;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200199 weston_surface_set_position(shell->switcher_surface, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400200
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400201 shell->switcher_listener.func = handle_switcher_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200202 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400203 &shell->switcher_listener.link);
204}
205
206static void
207tablet_shell_set_homescreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400208 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400209 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400210{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500211 struct tablet_shell *shell = resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400212
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400213 shell->home_surface = surface_resource->data;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200214 weston_surface_set_position(shell->home_surface, 0, 0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400215}
216
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400217static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500218minimize_zoom_done(struct weston_zoom *zoom, void *data)
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400219{
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500220 struct tablet_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500221 struct weston_compositor *compositor = shell->compositor;
222 struct weston_input_device *device =
223 (struct weston_input_device *) compositor->input_device;
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400224
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500225 weston_surface_activate(shell->home_surface,
226 device, weston_compositor_get_time());
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400227}
228
229static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500230tablet_shell_switch_to(struct tablet_shell *shell,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500231 struct weston_surface *surface)
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400232{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233 struct weston_compositor *compositor = shell->compositor;
234 struct weston_input_device *device =
235 (struct weston_input_device *) compositor->input_device;
236 struct weston_surface *current;
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400237
238 if (shell->state == STATE_SWITCHER) {
239 wl_list_remove(&shell->switcher_listener.link);
240 shell->switcher_surface = NULL;
241 };
242
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400243 if (surface == shell->home_surface) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500244 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400245
246 if (shell->current_client && shell->current_client->surface) {
247 current = shell->current_client->surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500248 weston_zoom_run(current, 1.0, 0.3,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500249 minimize_zoom_done, shell);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400250 }
251 } else {
252 fprintf(stderr, "switch to %p\n", surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500253 weston_surface_activate(surface, device,
254 weston_compositor_get_time());
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500255 tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500256 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400257 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400258}
259
260static void
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400261tablet_shell_show_grid(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400262 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400263 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400264{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500265 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500266 struct weston_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400267
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500268 tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400269}
270
271static void
272tablet_shell_show_panels(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400273 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400274 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400275{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500276 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500277 struct weston_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400278
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500279 tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400280}
281
282static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400283destroy_tablet_client(struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400284{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500285 struct tablet_client *tablet_client =
286 container_of(resource, struct tablet_client, resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400287
288 free(tablet_client->name);
289 free(tablet_client);
290}
291
292static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400293tablet_client_destroy(struct wl_client *client,
294 struct wl_resource *resource)
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400295{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500296 wl_resource_destroy(resource, weston_compositor_get_time());
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400297}
298
299static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400300tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400301{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500302 struct tablet_client *tablet_client = resource->data;
303 struct tablet_shell *shell = tablet_client->shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400304
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400305 shell->current_client = tablet_client;
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400306 if (!tablet_client->surface)
307 return;
308
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500309 tablet_shell_switch_to(shell, tablet_client->surface);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400310}
311
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500312static const struct tablet_client_interface tablet_client_implementation = {
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400313 tablet_client_destroy,
314 tablet_client_activate
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400315};
316
317static void
318tablet_shell_create_client(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400319 struct wl_resource *resource,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400320 uint32_t id, const char *name, int fd)
321{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500322 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500323 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500324 struct tablet_client *tablet_client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400325
326 tablet_client = malloc(sizeof *tablet_client);
327 if (tablet_client == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400328 wl_resource_post_no_memory(resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400329 return;
330 }
331
332 tablet_client->client = wl_client_create(compositor->wl_display, fd);
333 tablet_client->shell = shell;
334 tablet_client->name = strdup(name);
335
336 tablet_client->resource.destroy = destroy_tablet_client;
337 tablet_client->resource.object.id = id;
338 tablet_client->resource.object.interface =
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500339 &tablet_client_interface;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400340 tablet_client->resource.object.implementation =
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500341 (void (**)(void)) &tablet_client_implementation;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400342
343 wl_client_add_resource(client, &tablet_client->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400344 tablet_client->surface = NULL;
345 shell->current_client = tablet_client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400346
347 fprintf(stderr, "created client %p, id %d, name %s, fd %d\n",
348 tablet_client->client, id, name, fd);
349}
350
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500351static const struct tablet_shell_interface tablet_shell_implementation = {
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400352 tablet_shell_set_lockscreen,
353 tablet_shell_set_switcher,
354 tablet_shell_set_homescreen,
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400355 tablet_shell_show_grid,
356 tablet_shell_show_panels,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400357 tablet_shell_create_client
358};
359
360static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500361launch_ux_daemon(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400362{
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500363 const char *shell_exe = LIBEXECDIR "/weston-tablet-shell";
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400364
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500365 shell->client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200366 &shell->process,
367 shell_exe, tablet_shell_sigchld);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400368}
369
370static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500371toggle_switcher(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400372{
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400373 switch (shell->state) {
374 case STATE_SWITCHER:
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500375 tablet_shell_send_hide_switcher(&shell->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400376 break;
377 default:
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500378 tablet_shell_send_show_switcher(&shell->resource);
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500379 tablet_shell_set_state(shell, STATE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400380 break;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400381 }
382}
383
384static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500385tablet_shell_lock(struct weston_shell *base)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400386{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500387 struct tablet_shell *shell =
388 container_of(base, struct tablet_shell, shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400389
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400390 if (shell->state == STATE_LOCKED)
391 return;
392 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500393 tablet_shell_send_hide_switcher(&shell->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400394
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500395 tablet_shell_send_show_lockscreen(&shell->resource);
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500396 tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400397}
398
399static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500400tablet_shell_unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200401{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200402 struct tablet_shell *shell =
403 container_of(base, struct tablet_shell, shell);
404
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500405 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200406}
407
408static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500409go_home(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400410{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500411 struct weston_input_device *device =
412 (struct weston_input_device *) shell->compositor->input_device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400413
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400414 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500415 tablet_shell_send_hide_switcher(&shell->resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400416
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500417 weston_surface_activate(shell->home_surface, device,
418 weston_compositor_get_time());
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400419
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500420 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400421}
422
423static int
424long_press_handler(void *data)
425{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500426 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400427
428 shell->long_press_active = 0;
429 toggle_switcher(shell);
430
431 return 1;
432}
433
434static void
435menu_key_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600436 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400437{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500438 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400439
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400440 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400441 return;
442
443 if (state)
444 toggle_switcher(shell);
445}
446
447static void
448home_key_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600449 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400450{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500451 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400452
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400453 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400454 return;
455
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500456 shell->device = (struct weston_input_device *) device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400457
458 if (state) {
459 wl_event_source_timer_update(shell->long_press_source, 500);
460 shell->long_press_active = 1;
461 } else if (shell->long_press_active) {
462 wl_event_source_timer_update(shell->long_press_source, 0);
463 shell->long_press_active = 0;
464
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400465 switch (shell->state) {
466 case STATE_HOME:
467 case STATE_SWITCHER:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400468 toggle_switcher(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400469 break;
470 default:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400471 go_home(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400472 break;
473 }
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400474 }
475}
476
477static void
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500478destroy_tablet_shell(struct wl_resource *resource)
479{
480}
481
482static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400483bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400484{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500485 struct tablet_shell *shell = data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400486
487 if (shell->client != client)
488 /* Throw an error or just let the client fail when it
489 * tries to access the object?. */
490 return;
491
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400492 shell->resource.object.id = id;
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500493 shell->resource.object.interface = &tablet_shell_interface;
494 shell->resource.object.implementation =
495 (void (**)(void)) &tablet_shell_implementation;
496 shell->resource.client = client;
497 shell->resource.data = shell;
498 shell->resource.destroy = destroy_tablet_shell;
499
500 wl_client_add_resource(client, &shell->resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400501}
502
Pekka Paalanen3c647232011-12-22 13:43:43 +0200503static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500504tablet_shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +0200505{
506 struct tablet_shell *shell =
507 container_of(base, struct tablet_shell, shell);
508
509 wl_event_source_remove(shell->long_press_source);
510 free(shell);
511}
512
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400513void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500514shell_init(struct weston_compositor *compositor);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400515
516WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500517shell_init(struct weston_compositor *compositor)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400518{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500519 struct tablet_shell *shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400520 struct wl_event_loop *loop;
521
522 shell = malloc(sizeof *shell);
523 if (shell == NULL)
524 return;
525
526 memset(shell, 0, sizeof *shell);
527 shell->compositor = compositor;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400528
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400529 /* FIXME: This will make the object available to all clients. */
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400530 wl_display_add_global(compositor->wl_display,
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500531 &tablet_shell_interface, shell, bind_shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400532
533 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400534 shell->long_press_source =
535 wl_event_loop_add_timer(loop, long_press_handler, shell);
536
Scott Moreau6a3633d2012-03-20 08:47:59 -0600537 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0, 0, 0,
538 home_key_binding, shell);
539 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0, 0, 0,
540 home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500541 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -0600542 MODIFIER_SUPER, home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500543 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0, 0,
Kristian Høgsbergf77ce462011-05-03 13:11:43 -0400544 MODIFIER_SUPER, home_key_binding, shell);
Scott Moreau6a3633d2012-03-20 08:47:59 -0600545 weston_compositor_add_binding(compositor, KEY_COMPOSE, 0, 0, 0,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400546 menu_key_binding, shell);
547
548 compositor->shell = &shell->shell;
549
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500550 shell->shell.lock = tablet_shell_lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200551 shell->shell.unlock = tablet_shell_unlock;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500552 shell->shell.map = tablet_shell_map;
553 shell->shell.configure = tablet_shell_configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +0200554 shell->shell.destroy = tablet_shell_destroy;
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}