blob: 450888b2c22ee1878e9c7a40e146111772ba56c0 [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>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <fcntl.h>
31#include <linux/input.h>
32
33#include "compositor.h"
34#include "meego-tablet-server-protocol.h"
35
36/*
37 * TODO: Don't fade back from black until we've received a lockscreen
38 * attachment.
39 */
40
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -040041enum {
42 STATE_STARTING,
43 STATE_LOCKED,
44 STATE_HOME,
45 STATE_SWITCHER,
46 STATE_TASK
47};
48
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040049struct meego_tablet_shell {
Kristian Høgsberg904055a2011-08-18 17:55:30 -040050 struct wl_resource resource;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040051
52 struct wlsc_shell shell;
53
54 struct wlsc_compositor *compositor;
Kristian Høgsbergd4af3202011-06-21 17:43:31 -040055 struct wlsc_process process;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040056 struct wlsc_input_device *device;
57 struct wl_client *client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040058
59 struct wlsc_surface *surface;
60
61 struct wlsc_surface *lockscreen_surface;
62 struct wl_listener lockscreen_listener;
63
64 struct wlsc_surface *home_surface;
65
66 struct wlsc_surface *switcher_surface;
67 struct wl_listener switcher_listener;
68
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -040069 struct meego_tablet_client *current_client;
70
71 int state, previous_state;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040072 int long_press_active;
73 struct wl_event_source *long_press_source;
74};
75
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -040076struct meego_tablet_client {
77 struct wl_resource resource;
78 struct meego_tablet_shell *shell;
79 struct wl_client *client;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -040080 struct wlsc_surface *surface;
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -040081 char *name;
82};
83
84struct meego_tablet_zoom {
85 struct wlsc_surface *surface;
86 struct wlsc_animation animation;
Kristian Høgsberg269c7822011-05-02 14:38:18 -040087 struct wlsc_spring spring;
Kristian Høgsberg0bc0e242011-05-02 14:35:40 -040088 struct wlsc_transform transform;
Kristian Høgsberg132c6532011-05-03 12:41:03 -040089 struct wl_listener listener;
Kristian Høgsberg57eca742011-06-21 16:40:56 -040090 struct meego_tablet_shell *shell;
91 void (*done)(struct meego_tablet_zoom *zoom);
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -040092};
93
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -040094
Kristian Høgsbergd4af3202011-06-21 17:43:31 -040095static void
96meego_tablet_shell_sigchld(struct wlsc_process *process, int status)
97{
98 struct meego_tablet_shell *shell =
99 container_of(process, struct meego_tablet_shell, process);
100
101 shell->process.pid = 0;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400102
103 fprintf(stderr, "meego-ux-daemon crashed, exit code %d\n", status);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400104}
105
106static void
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400107meego_tablet_shell_set_state(struct meego_tablet_shell *shell, int state)
108{
109 static const char *states[] = {
110 "STARTING", "LOCKED", "HOME", "SWITCHER", "TASK"
111 };
112
113 fprintf(stderr, "switching to state %s (from %s)\n",
114 states[state], states[shell->state]);
115 shell->previous_state = shell->state;
116 shell->state = state;
117}
118
119static void
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400120meego_tablet_zoom_destroy(struct meego_tablet_zoom *zoom)
121{
122 wl_list_remove(&zoom->animation.link);
123 zoom->surface->transform = NULL;
124 if (zoom->done)
125 zoom->done(zoom);
126 free(zoom);
127}
128
129static void
Kristian Høgsberg132c6532011-05-03 12:41:03 -0400130handle_zoom_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200131 struct wl_resource *resource, uint32_t time)
Kristian Høgsberg132c6532011-05-03 12:41:03 -0400132{
133 struct meego_tablet_zoom *zoom =
134 container_of(listener, struct meego_tablet_zoom, listener);
135
Kristian Høgsberg132c6532011-05-03 12:41:03 -0400136 fprintf(stderr, "animation surface gone\n");
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400137 meego_tablet_zoom_destroy(zoom);
Kristian Høgsberg132c6532011-05-03 12:41:03 -0400138}
139
140static void
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400141meego_tablet_zoom_frame(struct wlsc_animation *animation,
142 struct wlsc_output *output, uint32_t msecs)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400143{
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400144 struct meego_tablet_zoom *zoom =
145 container_of(animation, struct meego_tablet_zoom, animation);
146 struct wlsc_surface *es = zoom->surface;
147 GLfloat scale;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400148
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400149 wlsc_spring_update(&zoom->spring, msecs);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400150
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400151 if (wlsc_spring_done(&zoom->spring)) {
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400152 fprintf(stderr, "animation done\n");
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400153 meego_tablet_zoom_destroy(zoom);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400154 }
155
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400156 scale = zoom->spring.current;
Kristian Høgsberg0bc0e242011-05-02 14:35:40 -0400157 wlsc_matrix_init(&zoom->transform.matrix);
158 wlsc_matrix_translate(&zoom->transform.matrix,
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400159 -es->width / 2.0, -es->height / 2.0, 0);
Kristian Høgsberg0bc0e242011-05-02 14:35:40 -0400160 wlsc_matrix_scale(&zoom->transform.matrix, scale, scale, scale);
161 wlsc_matrix_translate(&zoom->transform.matrix,
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400162 es->width / 2.0, es->height / 2.0, 0);
163
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400164 scale = 1.0 / zoom->spring.current;
Kristian Høgsberg0bc0e242011-05-02 14:35:40 -0400165 wlsc_matrix_init(&zoom->transform.inverse);
166 wlsc_matrix_scale(&zoom->transform.inverse, scale, scale, scale);
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400167
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400168 wlsc_surface_damage(es);
169}
170
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400171static struct meego_tablet_zoom *
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400172meego_tablet_zoom_run(struct meego_tablet_shell *shell,
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400173 struct wlsc_surface *surface,
174 GLfloat start, GLfloat stop)
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400175{
176 struct meego_tablet_zoom *zoom;
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400177
178 fprintf(stderr, "starting animation for surface %p\n", surface);
179
180 zoom = malloc(sizeof *zoom);
181 if (!zoom)
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400182 return NULL;
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400183
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400184 zoom->shell = shell;
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400185 zoom->surface = surface;
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400186 zoom->done = NULL;
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400187 surface->transform = &zoom->transform;
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400188 wlsc_spring_init(&zoom->spring, 200.0, start, stop);
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400189 zoom->spring.timestamp = wlsc_compositor_get_time();
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400190 zoom->animation.frame = meego_tablet_zoom_frame;
Kristian Høgsberg0bc0e242011-05-02 14:35:40 -0400191 meego_tablet_zoom_frame(&zoom->animation, NULL,
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400192 zoom->spring.timestamp);
Kristian Høgsberg0bc0e242011-05-02 14:35:40 -0400193
Kristian Høgsberg132c6532011-05-03 12:41:03 -0400194 zoom->listener.func = handle_zoom_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200195 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
Kristian Høgsberg132c6532011-05-03 12:41:03 -0400196 &zoom->listener.link);
197
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400198 wl_list_insert(shell->compositor->animation_list.prev,
199 &zoom->animation.link);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400200
201 return zoom;
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400202}
203
Kristian Høgsberg3a53efe2011-05-02 14:21:03 -0400204/* FIXME: We should be handling map, not attach... Map is when the
205 * surface becomes visible, which is what we want to catch. Attach
206 * will happen whenever the surface changes. */
207
208static void
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400209meego_tablet_shell_attach(struct wlsc_shell *base,
210 struct wlsc_surface *surface)
211{
212 struct meego_tablet_shell *shell =
213 container_of(base, struct meego_tablet_shell, shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400214
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400215 surface->x = 0;
216 surface->y = 0;
217
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400218 if (surface == shell->lockscreen_surface) {
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400219 wlsc_compositor_fade(shell->compositor, 0.0);
220 wlsc_compositor_wake(shell->compositor);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400221 } else if (surface == shell->switcher_surface) {
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400222 /* */
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400223 } else if (surface == shell->home_surface) {
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400224 if (shell->state == STATE_STARTING) {
225 meego_tablet_shell_set_state(shell, STATE_LOCKED);
226 shell->previous_state = STATE_HOME;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400227 wl_resource_post_event(&shell->resource,
228 MEEGO_TABLET_SHELL_SHOW_LOCKSCREEN);
Kristian Høgsbergb79216e2011-05-08 21:28:45 -0400229 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400230 } else if (shell->current_client &&
231 shell->current_client->surface != surface &&
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400232 shell->current_client->client == surface->surface.resource.client) {
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400233 meego_tablet_shell_set_state(shell, STATE_TASK);
234 shell->current_client->surface = surface;
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400235 meego_tablet_zoom_run(shell, surface, 0.3, 1.0);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400236 }
237}
238
239static void
240handle_lockscreen_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200241 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400242{
243 struct meego_tablet_shell *shell =
244 container_of(listener,
245 struct meego_tablet_shell, lockscreen_listener);
246
247 shell->lockscreen_surface = NULL;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400248 meego_tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400249}
250
251static void
252tablet_shell_set_lockscreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400253 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400254 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400255{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400256 struct meego_tablet_shell *shell = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400257 struct wlsc_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400258 struct wlsc_input_device *device =
259 (struct wlsc_input_device *) shell->compositor->input_device;
260
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400261 es->x = 0;
262 es->y = 0;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400263 wlsc_surface_activate(es, device, wlsc_compositor_get_time());
264 shell->lockscreen_surface = es;
265
266 shell->lockscreen_listener.func = handle_lockscreen_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200267 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400268 &shell->lockscreen_listener.link);
269}
270
271static void
272handle_switcher_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200273 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400274{
275 struct meego_tablet_shell *shell =
276 container_of(listener,
277 struct meego_tablet_shell, switcher_listener);
278
279 shell->switcher_surface = NULL;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400280 if (shell->state != STATE_LOCKED)
281 meego_tablet_shell_set_state(shell, shell->previous_state);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400282}
283
284static void
285tablet_shell_set_switcher(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400286 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400287 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400288{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400289 struct meego_tablet_shell *shell = resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400290 struct wlsc_input_device *device =
291 (struct wlsc_input_device *) shell->compositor->input_device;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400292 struct wlsc_surface *es = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400293
294 /* FIXME: Switcher should be centered and the compositor
295 * should do the tinting of the background. With the cache
296 * layer idea, we should be able to hit the framerate on the
297 * fade/zoom in. */
298 shell->switcher_surface = es;
299 shell->switcher_surface->x = 0;
300 shell->switcher_surface->y = 0;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400301
302 wlsc_surface_activate(es, device, wlsc_compositor_get_time());
303
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400304 shell->switcher_listener.func = handle_switcher_surface_destroy;
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200305 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400306 &shell->switcher_listener.link);
307}
308
309static void
310tablet_shell_set_homescreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400311 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400312 struct wl_resource *surface_resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400313{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400314 struct meego_tablet_shell *shell = resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400315 struct wlsc_input_device *device =
316 (struct wlsc_input_device *) shell->compositor->input_device;
317
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400318 shell->home_surface = surface_resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400319 shell->home_surface->x = 0;
320 shell->home_surface->y = 0;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400321
322 wlsc_surface_activate(shell->home_surface, device,
323 wlsc_compositor_get_time());
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400324}
325
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400326static void
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400327minimize_zoom_done(struct meego_tablet_zoom *zoom)
328{
329 struct meego_tablet_shell *shell = zoom->shell;
330 struct wlsc_compositor *compositor = shell->compositor;
331 struct wlsc_input_device *device =
332 (struct wlsc_input_device *) compositor->input_device;
333
334 wlsc_surface_activate(shell->home_surface,
335 device, wlsc_compositor_get_time());
336}
337
338static void
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400339meego_tablet_shell_switch_to(struct meego_tablet_shell *shell,
340 struct wlsc_surface *surface)
341{
342 struct wlsc_compositor *compositor = shell->compositor;
343 struct wlsc_input_device *device =
344 (struct wlsc_input_device *) compositor->input_device;
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400345 struct wlsc_surface *current;
346 struct meego_tablet_zoom *zoom;
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400347
348 if (shell->state == STATE_SWITCHER) {
349 wl_list_remove(&shell->switcher_listener.link);
350 shell->switcher_surface = NULL;
351 };
352
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400353 if (surface == shell->home_surface) {
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400354 meego_tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400355
356 if (shell->current_client && shell->current_client->surface) {
357 current = shell->current_client->surface;
358 zoom = meego_tablet_zoom_run(shell, current, 1.0, 0.3);
359 zoom->done = minimize_zoom_done;
360 }
361 } else {
362 fprintf(stderr, "switch to %p\n", surface);
363 wlsc_surface_activate(surface, device,
364 wlsc_compositor_get_time());
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400365 meego_tablet_shell_set_state(shell, STATE_TASK);
Kristian Høgsberg57eca742011-06-21 16:40:56 -0400366 meego_tablet_zoom_run(shell, surface, 0.3, 1.0);
367 }
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400368}
369
370static void
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400371tablet_shell_show_grid(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400372 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400373 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400374{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400375 struct meego_tablet_shell *shell = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400376 struct wlsc_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400377
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400378 meego_tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400379}
380
381static void
382tablet_shell_show_panels(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400383 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400384 struct wl_resource *surface_resource)
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400385{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400386 struct meego_tablet_shell *shell = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400387 struct wlsc_surface *es = surface_resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400388
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400389 meego_tablet_shell_switch_to(shell, es);
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400390}
391
392static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400393destroy_tablet_client(struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400394{
395 struct meego_tablet_client *tablet_client =
396 container_of(resource, struct meego_tablet_client, resource);
397
398 free(tablet_client->name);
399 free(tablet_client);
400}
401
402static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400403tablet_client_destroy(struct wl_client *client,
404 struct wl_resource *resource)
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400405{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400406 wl_resource_destroy(resource, wlsc_compositor_get_time());
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400407}
408
409static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400410tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400411{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400412 struct meego_tablet_client *tablet_client = resource->data;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400413 struct meego_tablet_shell *shell = tablet_client->shell;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400414
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400415 shell->current_client = tablet_client;
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400416 if (!tablet_client->surface)
417 return;
418
Kristian Høgsbergbfcf07c2011-05-05 15:25:28 -0400419 meego_tablet_shell_switch_to(shell, tablet_client->surface);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400420}
421
422static const struct meego_tablet_client_interface tablet_client_interface = {
Kristian Høgsberg81e8c262011-05-04 16:17:20 -0400423 tablet_client_destroy,
424 tablet_client_activate
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400425};
426
427static void
428tablet_shell_create_client(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400429 struct wl_resource *resource,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400430 uint32_t id, const char *name, int fd)
431{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400432 struct meego_tablet_shell *shell = resource->data;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400433 struct wlsc_compositor *compositor = shell->compositor;
434 struct meego_tablet_client *tablet_client;
435
436 tablet_client = malloc(sizeof *tablet_client);
437 if (tablet_client == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400438 wl_resource_post_no_memory(resource);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400439 return;
440 }
441
442 tablet_client->client = wl_client_create(compositor->wl_display, fd);
443 tablet_client->shell = shell;
444 tablet_client->name = strdup(name);
445
446 tablet_client->resource.destroy = destroy_tablet_client;
447 tablet_client->resource.object.id = id;
448 tablet_client->resource.object.interface =
449 &meego_tablet_client_interface;
450 tablet_client->resource.object.implementation =
451 (void (**)(void)) &tablet_client_interface;
452
453 wl_client_add_resource(client, &tablet_client->resource);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400454 tablet_client->surface = NULL;
455 shell->current_client = tablet_client;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400456
457 fprintf(stderr, "created client %p, id %d, name %s, fd %d\n",
458 tablet_client->client, id, name, fd);
459}
460
461static const struct meego_tablet_shell_interface tablet_shell_interface = {
462 tablet_shell_set_lockscreen,
463 tablet_shell_set_switcher,
464 tablet_shell_set_homescreen,
Kristian Høgsberg92fb0e92011-05-04 16:16:43 -0400465 tablet_shell_show_grid,
466 tablet_shell_show_panels,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400467 tablet_shell_create_client
468};
469
470static void
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400471launch_ux_daemon(struct meego_tablet_shell *shell)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400472{
473 struct wlsc_compositor *compositor = shell->compositor;
474 char s[32];
475 int sv[2], flags;
476
477 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
478 fprintf(stderr, "socketpair failed\n");
479 return;
480 }
481
Kristian Høgsbergd4af3202011-06-21 17:43:31 -0400482 shell->process.pid = fork();
483 shell->process.cleanup = meego_tablet_shell_sigchld;
484
485 switch (shell->process.pid) {
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400486 case 0:
Kristian Høgsberg3d1cad82011-05-02 13:57:38 -0400487 /* SOCK_CLOEXEC closes both ends, so we need to unset
488 * the flag on the client fd. */
489 flags = fcntl(sv[1], F_GETFD);
490 if (flags != -1)
491 fcntl(sv[1], F_SETFD, flags & ~FD_CLOEXEC);
492
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400493 snprintf(s, sizeof s, "%d", sv[1]);
494 setenv("WAYLAND_SOCKET", s, 1);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400495 setenv("QT_QPA_PLATFORM", "waylandgl", 1);
496 if (execl("/usr/libexec/meego-ux-daemon",
497 "/usr/libexec/meego-ux-daemon", NULL) < 0)
498 fprintf(stderr, "exec failed: %m\n");
499 exit(-1);
500
501 default:
502 close(sv[1]);
503 shell->client =
504 wl_client_create(compositor->wl_display, sv[0]);
Kristian Høgsbergd4af3202011-06-21 17:43:31 -0400505 wlsc_watch_process(&shell->process);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400506 break;
507
508 case -1:
509 fprintf(stderr, "failed to fork\n");
510 break;
511 }
512}
513
514static void
515toggle_switcher(struct meego_tablet_shell *shell)
516{
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400517 switch (shell->state) {
518 case STATE_SWITCHER:
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400519 wl_resource_post_event(&shell->resource,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400520 MEEGO_TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400521 break;
522 default:
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400523 wl_resource_post_event(&shell->resource,
524 MEEGO_TABLET_SHELL_SHOW_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400525 meego_tablet_shell_set_state(shell, STATE_SWITCHER);
526 break;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400527 }
528}
529
530static void
531meego_tablet_shell_lock(struct wlsc_shell *base)
532{
533 struct meego_tablet_shell *shell =
534 container_of(base, struct meego_tablet_shell, shell);
535
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400536 if (shell->state == STATE_LOCKED)
537 return;
538 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400539 wl_resource_post_event(&shell->resource,
540 MEEGO_TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400541
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400542 wl_resource_post_event(&shell->resource,
543 MEEGO_TABLET_SHELL_SHOW_LOCKSCREEN);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400544
545 meego_tablet_shell_set_state(shell, STATE_LOCKED);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400546}
547
548static void
549go_home(struct meego_tablet_shell *shell)
550{
551 struct wlsc_input_device *device =
552 (struct wlsc_input_device *) shell->compositor->input_device;
553
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400554 if (shell->state == STATE_SWITCHER)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400555 wl_resource_post_event(&shell->resource,
556 MEEGO_TABLET_SHELL_HIDE_SWITCHER);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400557
558 wlsc_surface_activate(shell->home_surface, device,
559 wlsc_compositor_get_time());
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400560
561 meego_tablet_shell_set_state(shell, STATE_HOME);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400562}
563
564static int
565long_press_handler(void *data)
566{
567 struct meego_tablet_shell *shell = data;
568
569 shell->long_press_active = 0;
570 toggle_switcher(shell);
571
572 return 1;
573}
574
575static void
576menu_key_binding(struct wl_input_device *device, uint32_t time,
577 uint32_t key, uint32_t button, uint32_t state, void *data)
578{
579 struct meego_tablet_shell *shell = data;
580
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400581 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400582 return;
583
584 if (state)
585 toggle_switcher(shell);
586}
587
588static void
589home_key_binding(struct wl_input_device *device, uint32_t time,
590 uint32_t key, uint32_t button, uint32_t state, void *data)
591{
592 struct meego_tablet_shell *shell = data;
593
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400594 if (shell->state == STATE_LOCKED)
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400595 return;
596
597 shell->device = (struct wlsc_input_device *) device;
598
599 if (state) {
600 wl_event_source_timer_update(shell->long_press_source, 500);
601 shell->long_press_active = 1;
602 } else if (shell->long_press_active) {
603 wl_event_source_timer_update(shell->long_press_source, 0);
604 shell->long_press_active = 0;
605
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400606 switch (shell->state) {
607 case STATE_HOME:
608 case STATE_SWITCHER:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400609 toggle_switcher(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400610 break;
611 default:
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400612 go_home(shell);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400613 break;
614 }
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400615 }
616}
617
618static void
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400619meego_tablet_shell_set_selection_focus(struct wlsc_shell *shell,
620 struct wl_selection *selection,
621 struct wl_surface *surface,
622 uint32_t time)
623{
624}
625
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400626static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400627bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400628{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400629 struct meego_tablet_shell *shell = data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400630
631 if (shell->client != client)
632 /* Throw an error or just let the client fail when it
633 * tries to access the object?. */
634 return;
635
636 shell->resource.client = client;
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400637 shell->resource.object.id = id;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400638}
639
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400640void
641shell_init(struct wlsc_compositor *compositor);
642
643WL_EXPORT void
644shell_init(struct wlsc_compositor *compositor)
645{
646 struct meego_tablet_shell *shell;
647 struct wl_event_loop *loop;
648
649 shell = malloc(sizeof *shell);
650 if (shell == NULL)
651 return;
652
653 memset(shell, 0, sizeof *shell);
654 shell->compositor = compositor;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400655
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400656 shell->resource.object.interface = &meego_tablet_shell_interface;
657 shell->resource.object.implementation =
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400658 (void (**)(void)) &tablet_shell_interface;
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400659
660 /* FIXME: This will make the object available to all clients. */
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400661 wl_display_add_global(compositor->wl_display,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400662 &wl_shell_interface, shell, bind_shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400663
664 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400665 shell->long_press_source =
666 wl_event_loop_add_timer(loop, long_press_handler, shell);
667
Kristian Høgsbergd28b4d72011-05-02 14:11:15 -0400668 wlsc_compositor_add_binding(compositor, KEY_LEFTMETA, 0, 0,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400669 home_key_binding, shell);
Kristian Høgsbergd28b4d72011-05-02 14:11:15 -0400670 wlsc_compositor_add_binding(compositor, KEY_RIGHTMETA, 0, 0,
671 home_key_binding, shell);
Kristian Høgsbergf77ce462011-05-03 13:11:43 -0400672 wlsc_compositor_add_binding(compositor, KEY_LEFTMETA, 0,
673 MODIFIER_SUPER, home_key_binding, shell);
674 wlsc_compositor_add_binding(compositor, KEY_RIGHTMETA, 0,
675 MODIFIER_SUPER, home_key_binding, shell);
Kristian Høgsberg9d097772011-05-02 15:24:11 -0400676 wlsc_compositor_add_binding(compositor, KEY_COMPOSE, 0, 0,
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400677 menu_key_binding, shell);
678
679 compositor->shell = &shell->shell;
680
681 shell->shell.lock = meego_tablet_shell_lock;
682 shell->shell.attach = meego_tablet_shell_attach;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400683 shell->shell.set_selection_focus =
684 meego_tablet_shell_set_selection_focus;
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400685 launch_ux_daemon(shell);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400686
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400687 wlsc_spring_init(&compositor->fade.spring, 40.0, 1.0, 1.0);
Kristian Høgsberg1258a4a2011-05-04 11:35:02 -0400688 meego_tablet_shell_set_state(shell, STATE_STARTING);
Kristian Høgsbergeb7e12c2011-04-23 13:17:43 -0400689}