blob: 14ac8895b7d6ec1a63081f09f7e39d4d8c7e6b4c [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,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500108 int32_t width, int32_t height)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400109{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500110 struct tablet_shell *shell =
111 container_of(base, struct tablet_shell, shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400112
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200113 surface->geometry.x = 0;
114 surface->geometry.y = 0;
115 surface->geometry.dirty = 1;
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400116
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400117 if (surface == shell->lockscreen_surface) {
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500118 /* */
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400119 } else if (surface == shell->switcher_surface) {
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400120 /* */
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400121 } else if (surface == shell->home_surface) {
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400122 if (shell->state == STATE_STARTING) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500123 tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400124 shell->previous_state = STATE_HOME;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400125 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500126 TABLET_SHELL_SHOW_LOCKSCREEN);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400127 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400128 } else if (shell->current_client &&
129 shell->current_client->surface != surface &&
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400130 shell->current_client->client == surface->surface.resource.client) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500131 tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400132 shell->current_client->surface = surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500133 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400134 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500135
136 wl_list_insert(&shell->compositor->surface_list, &surface->link);
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200137 weston_surface_configure(surface, surface->geometry.x,
138 surface->geometry.y, width, height);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500139}
140
141static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500142tablet_shell_configure(struct weston_shell *base,
143 struct weston_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500144 int32_t x, int32_t y,
145 int32_t width, int32_t height)
146{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500147 weston_surface_configure(surface, x, y, width, height);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400148}
149
150static void
151handle_lockscreen_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200152 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400153{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500154 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400155 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500156 struct tablet_shell, lockscreen_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400157
158 shell->lockscreen_surface = NULL;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500159 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400160}
161
162static void
163tablet_shell_set_lockscreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400164 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400165 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400166{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500167 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500168 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400169
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200170 es->geometry.x = 0;
171 es->geometry.y = 0;
172 es->geometry.dirty = 1;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400173 shell->lockscreen_surface = es;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400174 shell->lockscreen_listener.func = handle_lockscreen_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200175 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400176 &shell->lockscreen_listener.link);
177}
178
179static void
180handle_switcher_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200181 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400182{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500183 struct tablet_shell *shell =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400184 container_of(listener,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500185 struct tablet_shell, switcher_listener);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400186
187 shell->switcher_surface = NULL;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400188 if (shell->state != STATE_LOCKED)
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500189 tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400190}
191
192static void
193tablet_shell_set_switcher(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400194 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400195 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400196{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500197 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500198 struct weston_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400199
200 /* FIXME: Switcher should be centered and the compositor
201 * should do the tinting of the background. With the cache
202 * layer idea, we should be able to hit the framerate on the
203 * fade/zoom in. */
204 shell->switcher_surface = es;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200205 shell->switcher_surface->geometry.x = 0;
206 shell->switcher_surface->geometry.y = 0;
207 shell->switcher_surface->geometry.dirty = 1;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400208
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400209 shell->switcher_listener.func = handle_switcher_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200210 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400211 &shell->switcher_listener.link);
212}
213
214static void
215tablet_shell_set_homescreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400216 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400217 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400218{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500219 struct tablet_shell *shell = resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400220
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400221 shell->home_surface = surface_resource->data;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200222 shell->home_surface->geometry.x = 0;
223 shell->home_surface->geometry.y = 0;
224 shell->home_surface->geometry.dirty = 1;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400225}
226
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400227static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228minimize_zoom_done(struct weston_zoom *zoom, void *data)
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400229{
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500230 struct tablet_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500231 struct weston_compositor *compositor = shell->compositor;
232 struct weston_input_device *device =
233 (struct weston_input_device *) compositor->input_device;
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400234
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500235 weston_surface_activate(shell->home_surface,
236 device, weston_compositor_get_time());
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400237}
238
239static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500240tablet_shell_switch_to(struct tablet_shell *shell,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500241 struct weston_surface *surface)
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400242{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500243 struct weston_compositor *compositor = shell->compositor;
244 struct weston_input_device *device =
245 (struct weston_input_device *) compositor->input_device;
246 struct weston_surface *current;
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400247
248 if (shell->state == STATE_SWITCHER) {
249 wl_list_remove(&shell->switcher_listener.link);
250 shell->switcher_surface = NULL;
251 };
252
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400253 if (surface == shell->home_surface) {
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500254 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400255
256 if (shell->current_client && shell->current_client->surface) {
257 current = shell->current_client->surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500258 weston_zoom_run(current, 1.0, 0.3,
Kristian Høgsberg698c0582011-12-04 15:20:19 -0500259 minimize_zoom_done, shell);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400260 }
261 } else {
262 fprintf(stderr, "switch to %p\n", surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500263 weston_surface_activate(surface, device,
264 weston_compositor_get_time());
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500265 tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500266 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400267 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400268}
269
270static void
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400271tablet_shell_show_grid(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400272 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400273 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400274{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500275 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500276 struct weston_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400277
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500278 tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400279}
280
281static void
282tablet_shell_show_panels(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400283 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400284 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400285{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500286 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500287 struct weston_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400288
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500289 tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400290}
291
292static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400293destroy_tablet_client(struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400294{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500295 struct tablet_client *tablet_client =
296 container_of(resource, struct tablet_client, resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400297
298 free(tablet_client->name);
299 free(tablet_client);
300}
301
302static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400303tablet_client_destroy(struct wl_client *client,
304 struct wl_resource *resource)
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400305{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500306 wl_resource_destroy(resource, weston_compositor_get_time());
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400307}
308
309static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400310tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400311{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500312 struct tablet_client *tablet_client = resource->data;
313 struct tablet_shell *shell = tablet_client->shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400314
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400315 shell->current_client = tablet_client;
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400316 if (!tablet_client->surface)
317 return;
318
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500319 tablet_shell_switch_to(shell, tablet_client->surface);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400320}
321
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500322static const struct tablet_client_interface tablet_client_implementation = {
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400323 tablet_client_destroy,
324 tablet_client_activate
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400325};
326
327static void
328tablet_shell_create_client(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400329 struct wl_resource *resource,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400330 uint32_t id, const char *name, int fd)
331{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500332 struct tablet_shell *shell = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500333 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500334 struct tablet_client *tablet_client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400335
336 tablet_client = malloc(sizeof *tablet_client);
337 if (tablet_client == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400338 wl_resource_post_no_memory(resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400339 return;
340 }
341
342 tablet_client->client = wl_client_create(compositor->wl_display, fd);
343 tablet_client->shell = shell;
344 tablet_client->name = strdup(name);
345
346 tablet_client->resource.destroy = destroy_tablet_client;
347 tablet_client->resource.object.id = id;
348 tablet_client->resource.object.interface =
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500349 &tablet_client_interface;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400350 tablet_client->resource.object.implementation =
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500351 (void (**)(void)) &tablet_client_implementation;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400352
353 wl_client_add_resource(client, &tablet_client->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400354 tablet_client->surface = NULL;
355 shell->current_client = tablet_client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400356
357 fprintf(stderr, "created client %p, id %d, name %s, fd %d\n",
358 tablet_client->client, id, name, fd);
359}
360
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500361static const struct tablet_shell_interface tablet_shell_implementation = {
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400362 tablet_shell_set_lockscreen,
363 tablet_shell_set_switcher,
364 tablet_shell_set_homescreen,
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400365 tablet_shell_show_grid,
366 tablet_shell_show_panels,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400367 tablet_shell_create_client
368};
369
370static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500371launch_ux_daemon(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400372{
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500373 const char *shell_exe = LIBEXECDIR "/weston-tablet-shell";
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400374
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500375 shell->client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200376 &shell->process,
377 shell_exe, tablet_shell_sigchld);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400378}
379
380static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500381toggle_switcher(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400382{
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400383 switch (shell->state) {
384 case STATE_SWITCHER:
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400385 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500386 TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400387 break;
388 default:
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400389 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500390 TABLET_SHELL_SHOW_SWITCHER);
391 tablet_shell_set_state(shell, STATE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400392 break;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400393 }
394}
395
396static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500397tablet_shell_lock(struct weston_shell *base)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400398{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500399 struct tablet_shell *shell =
400 container_of(base, struct tablet_shell, shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400401
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400402 if (shell->state == STATE_LOCKED)
403 return;
404 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400405 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500406 TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400407
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400408 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500409 TABLET_SHELL_SHOW_LOCKSCREEN);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400410
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500411 tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400412}
413
414static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500415tablet_shell_unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200416{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200417 struct tablet_shell *shell =
418 container_of(base, struct tablet_shell, shell);
419
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500420 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200421}
422
423static void
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500424go_home(struct tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400425{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500426 struct weston_input_device *device =
427 (struct weston_input_device *) shell->compositor->input_device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400428
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400429 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400430 wl_resource_post_event(&shell->resource,
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500431 TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400432
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500433 weston_surface_activate(shell->home_surface, device,
434 weston_compositor_get_time());
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400435
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500436 tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400437}
438
439static int
440long_press_handler(void *data)
441{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500442 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400443
444 shell->long_press_active = 0;
445 toggle_switcher(shell);
446
447 return 1;
448}
449
450static void
451menu_key_binding(struct wl_input_device *device, uint32_t time,
452 uint32_t key, uint32_t button, uint32_t state, void *data)
453{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500454 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400455
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400456 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400457 return;
458
459 if (state)
460 toggle_switcher(shell);
461}
462
463static void
464home_key_binding(struct wl_input_device *device, uint32_t time,
465 uint32_t key, uint32_t button, uint32_t state, void *data)
466{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500467 struct tablet_shell *shell = data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400468
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400469 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400470 return;
471
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500472 shell->device = (struct weston_input_device *) device;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400473
474 if (state) {
475 wl_event_source_timer_update(shell->long_press_source, 500);
476 shell->long_press_active = 1;
477 } else if (shell->long_press_active) {
478 wl_event_source_timer_update(shell->long_press_source, 0);
479 shell->long_press_active = 0;
480
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400481 switch (shell->state) {
482 case STATE_HOME:
483 case STATE_SWITCHER:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400484 toggle_switcher(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400485 break;
486 default:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400487 go_home(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400488 break;
489 }
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400490 }
491}
492
493static void
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500494destroy_tablet_shell(struct wl_resource *resource)
495{
496}
497
498static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400499bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400500{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500501 struct tablet_shell *shell = data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400502
503 if (shell->client != client)
504 /* Throw an error or just let the client fail when it
505 * tries to access the object?. */
506 return;
507
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400508 shell->resource.object.id = id;
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500509 shell->resource.object.interface = &tablet_shell_interface;
510 shell->resource.object.implementation =
511 (void (**)(void)) &tablet_shell_implementation;
512 shell->resource.client = client;
513 shell->resource.data = shell;
514 shell->resource.destroy = destroy_tablet_shell;
515
516 wl_client_add_resource(client, &shell->resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400517}
518
Pekka Paalanen3c647232011-12-22 13:43:43 +0200519static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500520tablet_shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +0200521{
522 struct tablet_shell *shell =
523 container_of(base, struct tablet_shell, shell);
524
525 wl_event_source_remove(shell->long_press_source);
526 free(shell);
527}
528
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400529void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500530shell_init(struct weston_compositor *compositor);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400531
532WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500533shell_init(struct weston_compositor *compositor)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400534{
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500535 struct tablet_shell *shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400536 struct wl_event_loop *loop;
537
538 shell = malloc(sizeof *shell);
539 if (shell == NULL)
540 return;
541
542 memset(shell, 0, sizeof *shell);
543 shell->compositor = compositor;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400544
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400545 /* FIXME: This will make the object available to all clients. */
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400546 wl_display_add_global(compositor->wl_display,
Kristian Høgsberg6336e462011-11-26 17:36:23 -0500547 &tablet_shell_interface, shell, bind_shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400548
549 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400550 shell->long_press_source =
551 wl_event_loop_add_timer(loop, long_press_handler, shell);
552
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500553 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0, 0,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400554 home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500555 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0, 0,
Kristian Høgsbergd28b4d72011-05-02 14:11:15 -0400556 home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500557 weston_compositor_add_binding(compositor, KEY_LEFTMETA, 0,
Kristian Høgsbergf77ce462011-05-03 13:11:43 -0400558 MODIFIER_SUPER, home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500559 weston_compositor_add_binding(compositor, KEY_RIGHTMETA, 0,
Kristian Høgsbergf77ce462011-05-03 13:11:43 -0400560 MODIFIER_SUPER, home_key_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500561 weston_compositor_add_binding(compositor, KEY_COMPOSE, 0, 0,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400562 menu_key_binding, shell);
563
564 compositor->shell = &shell->shell;
565
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500566 shell->shell.lock = tablet_shell_lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200567 shell->shell.unlock = tablet_shell_unlock;
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500568 shell->shell.map = tablet_shell_map;
569 shell->shell.configure = tablet_shell_configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +0200570 shell->shell.destroy = tablet_shell_destroy;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400571
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400572 launch_ux_daemon(shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400573
Kristian Høgsberg64f1c3f2011-11-14 15:50:03 -0500574 tablet_shell_set_state(shell, STATE_STARTING);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400575}