blob: cd5717f317d4fc3da81844728bd861648856013c [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040033#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020038#include "../shared/config-parser.h"
Martin Minarik6d118362012-06-07 18:01:59 +020039#include "log.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040
Jonas Ådahle3cddce2012-06-13 00:01:22 +020041#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020042#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043
Juan Zhaoe10d2792012-04-25 19:09:52 +080044enum animation_type {
45 ANIMATION_NONE,
46
47 ANIMATION_ZOOM,
48 ANIMATION_FADE
49};
50
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
52 struct weston_seat *seat;
53 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Jonas Ådahle3cddce2012-06-13 00:01:22 +020059struct workspace {
60 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020061
62 struct wl_list focus_list;
63 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020064};
65
Tiago Vignattibe143262012-04-16 17:31:41 +030066struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040068
69 struct wl_listener lock_listener;
70 struct wl_listener unlock_listener;
71 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020072 struct wl_listener show_input_panel_listener;
73 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020074
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050075 struct weston_layer fullscreen_layer;
76 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050077 struct weston_layer background_layer;
78 struct weston_layer lock_layer;
79
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040080 struct wl_listener pointer_focus_listener;
81 struct weston_surface *busy_surface;
82
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020083 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050084 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020085 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020086 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020087
88 unsigned deathcount;
89 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020090 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020091
92 bool locked;
93 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020094
Kristian Høgsberg730c94d2012-06-26 21:44:35 -040095 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050096 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010097
Pekka Paalanen77346a62011-11-30 16:26:35 +020098 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +020099 struct wl_array array;
100 unsigned int current;
101 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200102
103 struct weston_animation animation;
104 int anim_dir;
105 uint32_t anim_timestamp;
106 double anim_current;
107 struct workspace *anim_from;
108 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200109 } workspaces;
110
111 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200112 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200113 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200114 struct wl_resource *binding;
115 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500116 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200117 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500118
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200119 struct {
120 struct wl_resource *binding;
121 struct wl_list surfaces;
122 } input_panel;
123
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300124 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800125 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500126 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400127};
128
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500129enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200130 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500131
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200132 SHELL_SURFACE_INPUT_PANEL,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500133
134 SHELL_SURFACE_TOPLEVEL,
135 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500136 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800137 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500138 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200139};
140
Scott Moreauff1db4a2012-04-17 19:06:18 -0600141struct ping_timer {
142 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600143 uint32_t serial;
144};
145
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200146struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200147 struct wl_resource resource;
148
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500149 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200150 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500151 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300152 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200153
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400154 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400155 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500156 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800157 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800158 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600159 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500161 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200162 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500163 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200164 } rotation;
165
166 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700167 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500168 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200169 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500170 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100171 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400172 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173 } popup;
174
Alex Wu4539b082012-03-01 12:57:46 +0800175 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300176 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400177 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300178 } transient;
179
180 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800181 enum wl_shell_surface_fullscreen_method type;
182 struct weston_transform transform; /* matrix from x, y */
183 uint32_t framerate;
184 struct weston_surface *black_surface;
185 } fullscreen;
186
Scott Moreauff1db4a2012-04-17 19:06:18 -0600187 struct ping_timer *ping_timer;
188
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200189 struct weston_transform workspace_transform;
190
Jonas Ådahl04769742012-06-13 00:01:24 +0200191 struct focus_state *focus_state;
192
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500193 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500194 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100195 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400196
197 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200198};
199
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300200struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700201 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300202 struct shell_surface *shsurf;
203 struct wl_listener shsurf_destroy_listener;
204};
205
206struct weston_move_grab {
207 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100208 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500209};
210
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300212 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500213 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200214 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400215 GLfloat x;
216 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200217 } center;
218};
219
Alex Wubd3354b2012-04-17 17:20:49 +0800220static struct shell_surface *
221get_shell_surface(struct weston_surface *surface);
222
223static struct desktop_shell *
224shell_surface_get_shell(struct shell_surface *shsurf);
225
226static bool
227shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
228{
229 struct desktop_shell *shell;
230 struct weston_surface *top_fs_es;
231
232 shell = shell_surface_get_shell(shsurf);
233
234 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
235 return false;
236
237 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
238 struct weston_surface,
239 layer_link);
240 return (shsurf == get_shell_surface(top_fs_es));
241}
242
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500243static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400244destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300245{
246 struct shell_grab *grab;
247
248 grab = container_of(listener, struct shell_grab,
249 shsurf_destroy_listener);
250
251 grab->shsurf = NULL;
252}
253
254static void
255shell_grab_init(struct shell_grab *grab,
256 const struct wl_pointer_grab_interface *interface,
257 struct shell_surface *shsurf)
258{
259 grab->grab.interface = interface;
260 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400261 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
262 wl_signal_add(&shsurf->resource.destroy_signal,
263 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300264
265}
266
267static void
268shell_grab_finish(struct shell_grab *grab)
269{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400270 if (grab->shsurf)
271 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300272}
273
274static void
Alex Wu4539b082012-03-01 12:57:46 +0800275center_on_output(struct weston_surface *surface,
276 struct weston_output *output);
277
Daniel Stone496ca172012-05-30 16:31:42 +0100278static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300279get_modifier(char *modifier)
280{
281 if (!modifier)
282 return MODIFIER_SUPER;
283
284 if (!strcmp("ctrl", modifier))
285 return MODIFIER_CTRL;
286 else if (!strcmp("alt", modifier))
287 return MODIFIER_ALT;
288 else if (!strcmp("super", modifier))
289 return MODIFIER_SUPER;
290 else
291 return MODIFIER_SUPER;
292}
293
Juan Zhaoe10d2792012-04-25 19:09:52 +0800294static enum animation_type
295get_animation_type(char *animation)
296{
297 if (!animation)
298 return ANIMATION_NONE;
299
300 if (!strcmp("zoom", animation))
301 return ANIMATION_ZOOM;
302 else if (!strcmp("fade", animation))
303 return ANIMATION_FADE;
304 else
305 return ANIMATION_NONE;
306}
307
Alex Wu4539b082012-03-01 12:57:46 +0800308static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300309shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200310{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200311 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200312 char *path = NULL;
313 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200314 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300315 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800316 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200317
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400318 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300319 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800320 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200321 { "num-workspaces",
322 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200323 };
324
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400325 struct config_key saver_keys[] = {
326 { "path", CONFIG_KEY_STRING, &path },
327 { "duration", CONFIG_KEY_INTEGER, &duration },
328 };
329
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200330 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400331 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200332 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
333 };
334
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200335 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500336 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200337 free(config_file);
338
Pekka Paalanen7296e792011-12-07 16:22:00 +0200339 shell->screensaver.path = path;
340 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300341 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800342 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200343 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
344}
345
346static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200347focus_state_destroy(struct focus_state *state)
348{
349 wl_list_remove(&state->seat_destroy_listener.link);
350 wl_list_remove(&state->surface_destroy_listener.link);
351 free(state);
352}
353
354static void
355focus_state_seat_destroy(struct wl_listener *listener, void *data)
356{
357 struct focus_state *state = container_of(listener,
358 struct focus_state,
359 seat_destroy_listener);
360
361 wl_list_remove(&state->link);
362 focus_state_destroy(state);
363}
364
365static void
366focus_state_surface_destroy(struct wl_listener *listener, void *data)
367{
368 struct focus_state *state = container_of(listener,
369 struct focus_state,
370 seat_destroy_listener);
371
372 wl_list_remove(&state->link);
373 focus_state_destroy(state);
374}
375
376static struct focus_state *
377focus_state_create(struct weston_seat *seat)
378{
379 struct wl_keyboard *keyboard = seat->seat.keyboard;
380 struct focus_state *state;
381 struct wl_surface *surface;
382 struct shell_surface *shsurf;
383
384 state = malloc(sizeof *state);
385 if (state == NULL)
386 return NULL;
387
388 surface = keyboard->focus;
389 shsurf = get_shell_surface((struct weston_surface *)keyboard->focus);
390 shsurf->focus_state = state;
391
392 state->seat = seat;
393 state->keyboard_focus = shsurf->surface;
394 wl_list_init(&state->link);
395
396 state->seat_destroy_listener.notify = focus_state_seat_destroy;
397 state->surface_destroy_listener.notify = focus_state_surface_destroy;
398 wl_signal_add(&seat->seat.destroy_signal,
399 &state->seat_destroy_listener);
400 wl_signal_add(&surface->resource.destroy_signal,
401 &state->surface_destroy_listener);
402
403 return state;
404}
405
406static void
407pop_focus_state(struct desktop_shell *shell, struct workspace *ws)
408{
409 struct focus_state *state, *next;
410
411 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
412 if (state->keyboard_focus)
413 wl_keyboard_set_focus(state->seat->seat.keyboard,
414 &state->keyboard_focus->surface);
415
416 focus_state_destroy(state);
417 }
418 wl_list_init(&ws->focus_list);
419}
420
421static void
422push_focus_state(struct desktop_shell *shell, struct workspace *ws)
423{
424 struct weston_seat *seat;
425 struct focus_state *state;
426 struct wl_keyboard *keyboard;
427
428 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
429 keyboard = seat->seat.keyboard;
430 if (keyboard && keyboard->focus) {
431 state = focus_state_create(seat);
432 if (state == NULL)
433 return;
434
435 wl_list_insert(&ws->focus_list, &state->link);
436
437 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
438 }
439 }
440}
441
442static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200443workspace_destroy(struct workspace *ws)
444{
Jonas Ådahl04769742012-06-13 00:01:24 +0200445 struct focus_state *state, *next;
446
447 wl_list_for_each_safe(state, next, &ws->focus_list, link)
448 focus_state_destroy(state);
449
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200450 free(ws);
451}
452
Jonas Ådahl04769742012-06-13 00:01:24 +0200453static void
454seat_destroyed(struct wl_listener *listener, void *data)
455{
456 struct weston_seat *seat = data;
457 struct focus_state *state, *next;
458 struct workspace *ws = container_of(listener,
459 struct workspace,
460 seat_destroyed_listener);
461
462 wl_list_for_each_safe(state, next, &ws->focus_list, link)
463 if (state->seat == seat)
464 wl_list_remove(&state->link);
465}
466
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200467static struct workspace *
468workspace_create(void)
469{
470 struct workspace *ws = malloc(sizeof *ws);
471 if (ws == NULL)
472 return NULL;
473
474 weston_layer_init(&ws->layer, NULL);
475
Jonas Ådahl04769742012-06-13 00:01:24 +0200476 wl_list_init(&ws->focus_list);
477 wl_list_init(&ws->seat_destroyed_listener.link);
478 ws->seat_destroyed_listener.notify = seat_destroyed;
479
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200480 return ws;
481}
482
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200483static int
484workspace_is_empty(struct workspace *ws)
485{
486 return wl_list_empty(&ws->layer.surface_list);
487}
488
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200489static struct workspace *
490get_workspace(struct desktop_shell *shell, unsigned int index)
491{
492 struct workspace **pws = shell->workspaces.array.data;
493 pws += index;
494 return *pws;
495}
496
497static struct workspace *
498get_current_workspace(struct desktop_shell *shell)
499{
500 return get_workspace(shell, shell->workspaces.current);
501}
502
503static void
504activate_workspace(struct desktop_shell *shell, unsigned int index)
505{
506 struct workspace *ws;
507
508 ws = get_workspace(shell, index);
509 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
510
511 shell->workspaces.current = index;
512}
513
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200514static unsigned int
515get_output_height(struct weston_output *output)
516{
517 return abs(output->region.extents.y1 - output->region.extents.y2);
518}
519
520static void
521surface_translate(struct weston_surface *surface, double d)
522{
523 struct shell_surface *shsurf = get_shell_surface(surface);
524 struct weston_transform *transform;
525
526 transform = &shsurf->workspace_transform;
527 if (wl_list_empty(&transform->link))
528 wl_list_insert(surface->geometry.transformation_list.prev,
529 &shsurf->workspace_transform.link);
530
531 weston_matrix_init(&shsurf->workspace_transform.matrix);
532 weston_matrix_translate(&shsurf->workspace_transform.matrix,
533 0.0, d, 0.0);
534 surface->geometry.dirty = 1;
535}
536
537static void
538workspace_translate_out(struct workspace *ws, double fraction)
539{
540 struct weston_surface *surface;
541 unsigned int height;
542 double d;
543
544 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
545 height = get_output_height(surface->output);
546 d = height * fraction;
547
548 surface_translate(surface, d);
549 }
550}
551
552static void
553workspace_translate_in(struct workspace *ws, double fraction)
554{
555 struct weston_surface *surface;
556 unsigned int height;
557 double d;
558
559 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
560 height = get_output_height(surface->output);
561
562 if (fraction > 0)
563 d = -(height - height * fraction);
564 else
565 d = height + height * fraction;
566
567 surface_translate(surface, d);
568 }
569}
570
571static void
572workspace_damage_all_surfaces(struct workspace *ws)
573{
574 struct weston_surface *surface;
575
576 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
577 weston_surface_damage(surface);
578}
579
580static void
581reverse_workspace_change_animation(struct desktop_shell *shell,
582 unsigned int index,
583 struct workspace *from,
584 struct workspace *to)
585{
586 shell->workspaces.current = index;
587
588 shell->workspaces.anim_to = to;
589 shell->workspaces.anim_from = from;
590 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
591 shell->workspaces.anim_timestamp = 0;
592
Jonas Ådahl04769742012-06-13 00:01:24 +0200593 push_focus_state(shell, from);
594 pop_focus_state(shell, to);
595
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200596 workspace_damage_all_surfaces(from);
597 workspace_damage_all_surfaces(to);
598}
599
600static void
601workspace_deactivate_transforms(struct workspace *ws)
602{
603 struct weston_surface *surface;
604 struct shell_surface *shsurf;
605
606 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
607 shsurf = get_shell_surface(surface);
608 wl_list_remove(&shsurf->workspace_transform.link);
609 wl_list_init(&shsurf->workspace_transform.link);
610 shsurf->surface->geometry.dirty = 1;
611 }
612}
613
614static void
615finish_workspace_change_animation(struct desktop_shell *shell,
616 struct workspace *from,
617 struct workspace *to)
618{
619 workspace_damage_all_surfaces(from);
620 workspace_damage_all_surfaces(to);
621
622 wl_list_remove(&shell->workspaces.animation.link);
623 workspace_deactivate_transforms(from);
624 workspace_deactivate_transforms(to);
625 shell->workspaces.anim_to = NULL;
626
627 wl_list_remove(&shell->workspaces.anim_from->layer.link);
628}
629
630static void
631animate_workspace_change_frame(struct weston_animation *animation,
632 struct weston_output *output, uint32_t msecs)
633{
634 struct desktop_shell *shell =
635 container_of(animation, struct desktop_shell,
636 workspaces.animation);
637 struct workspace *from = shell->workspaces.anim_from;
638 struct workspace *to = shell->workspaces.anim_to;
639 uint32_t t;
640 double x, y;
641
642 if (workspace_is_empty(from) && workspace_is_empty(to)) {
643 finish_workspace_change_animation(shell, from, to);
644 return;
645 }
646
647 if (shell->workspaces.anim_timestamp == 0) {
648 if (shell->workspaces.anim_current == 0.0)
649 shell->workspaces.anim_timestamp = msecs;
650 else
651 shell->workspaces.anim_timestamp =
652 msecs -
653 /* Invers of movement function 'y' below. */
654 (asin(1.0 - shell->workspaces.anim_current) *
655 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
656 M_2_PI);
657 }
658
659 t = msecs - shell->workspaces.anim_timestamp;
660
661 /*
662 * x = [0, π/2]
663 * y(x) = sin(x)
664 */
665 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
666 y = sin(x);
667
668 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
669 workspace_damage_all_surfaces(from);
670 workspace_damage_all_surfaces(to);
671
672 workspace_translate_out(from, shell->workspaces.anim_dir * y);
673 workspace_translate_in(to, shell->workspaces.anim_dir * y);
674 shell->workspaces.anim_current = y;
675
676 workspace_damage_all_surfaces(from);
677 workspace_damage_all_surfaces(to);
678 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200679 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200680 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200681}
682
683static void
684animate_workspace_change(struct desktop_shell *shell,
685 unsigned int index,
686 struct workspace *from,
687 struct workspace *to)
688{
689 struct weston_output *output;
690
691 int dir;
692
693 if (index > shell->workspaces.current)
694 dir = -1;
695 else
696 dir = 1;
697
698 shell->workspaces.current = index;
699
700 shell->workspaces.anim_dir = dir;
701 shell->workspaces.anim_from = from;
702 shell->workspaces.anim_to = to;
703 shell->workspaces.anim_current = 0.0;
704 shell->workspaces.anim_timestamp = 0;
705
706 output = container_of(shell->compositor->output_list.next,
707 struct weston_output, link);
708 wl_list_insert(&output->animation_list,
709 &shell->workspaces.animation.link);
710
711 wl_list_insert(&from->layer.link, &to->layer.link);
712
713 workspace_translate_in(to, 0);
714
Jonas Ådahl04769742012-06-13 00:01:24 +0200715 push_focus_state(shell, from);
716 pop_focus_state(shell, to);
717
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200718 workspace_damage_all_surfaces(from);
719 workspace_damage_all_surfaces(to);
720}
721
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200722static void
723change_workspace(struct desktop_shell *shell, unsigned int index)
724{
725 struct workspace *from;
726 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200727
728 if (index == shell->workspaces.current)
729 return;
730
731 /* Don't change workspace when there is any fullscreen surfaces. */
732 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
733 return;
734
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200735 from = get_current_workspace(shell);
736 to = get_workspace(shell, index);
737
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200738 if (shell->workspaces.anim_from == to &&
739 shell->workspaces.anim_to == from) {
740 reverse_workspace_change_animation(shell, index, from, to);
741 return;
742 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200743
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200744 if (shell->workspaces.anim_to != NULL)
745 finish_workspace_change_animation(shell,
746 shell->workspaces.anim_from,
747 shell->workspaces.anim_to);
748
749 if (workspace_is_empty(to) && workspace_is_empty(from)) {
750 shell->workspaces.current = index;
751 wl_list_insert(&from->layer.link, &to->layer.link);
752 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200753
754 push_focus_state(shell, from);
755 pop_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200756 }
757 else
758 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200759}
760
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200761static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400762noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100763 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500764{
765 grab->focus = NULL;
766}
767
768static void
Scott Moreau447013d2012-02-18 05:05:29 -0700769move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100770 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500771{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500772 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100773 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300774 struct shell_surface *shsurf = move->base.shsurf;
775 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100776 int dx = wl_fixed_to_int(pointer->x + move->dx);
777 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300778
779 if (!shsurf)
780 return;
781
782 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500783
Daniel Stone103db7f2012-05-08 17:17:55 +0100784 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200785 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400786
787 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500788}
789
790static void
Scott Moreau447013d2012-02-18 05:05:29 -0700791move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100792 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500793{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300794 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
795 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100796 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100797 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500798
Daniel Stone4dbadb12012-05-30 16:31:51 +0100799 if (pointer->button_count == 0 &&
800 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300801 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100802 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500803 free(grab);
804 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500805}
806
Scott Moreau447013d2012-02-18 05:05:29 -0700807static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500808 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500809 move_grab_motion,
810 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500811};
812
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600813static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400814busy_cursor_grab_focus(struct wl_pointer_grab *base,
815 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600816{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400817 struct shell_grab *grab = (struct shell_grab *) base;
818 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600819
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400820 if (grab->grab.focus != surface) {
821 shell_grab_finish(grab);
822 wl_pointer_end_grab(pointer);
823 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600824 }
825}
826
827static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400828busy_cursor_grab_motion(struct wl_pointer_grab *grab,
829 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600830{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400831}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600832
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400833static void
834busy_cursor_grab_button(struct wl_pointer_grab *grab,
835 uint32_t time, uint32_t button, uint32_t state)
836{
837}
838
839static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
840 busy_cursor_grab_focus,
841 busy_cursor_grab_motion,
842 busy_cursor_grab_button,
843};
844
845static void
846set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
847{
848 struct shell_grab *grab;
849 struct desktop_shell *shell = shsurf->shell;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400850
851 grab = malloc(sizeof *grab);
852 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600853 return;
854
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400855 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
856 grab->grab.focus = &shsurf->surface->surface;
857 wl_pointer_start_grab(pointer, &grab->grab);
858 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400859}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600860
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400861static void
862end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
863{
864 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
865
866 if (grab->grab.interface == &busy_cursor_grab_interface) {
867 shell_grab_finish(grab);
868 wl_pointer_end_grab(pointer);
869 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600870 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600871}
872
Scott Moreau9521d5e2012-04-19 13:06:17 -0600873static void
874ping_timer_destroy(struct shell_surface *shsurf)
875{
876 if (!shsurf || !shsurf->ping_timer)
877 return;
878
879 if (shsurf->ping_timer->source)
880 wl_event_source_remove(shsurf->ping_timer->source);
881
882 free(shsurf->ping_timer);
883 shsurf->ping_timer = NULL;
884}
885
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400886static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600887ping_timeout_handler(void *data)
888{
889 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400890 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600891
Scott Moreau9521d5e2012-04-19 13:06:17 -0600892 /* Client is not responding */
893 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400894
895 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
896 if (seat->seat.pointer->focus == &shsurf->surface->surface)
897 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600898
899 return 1;
900}
901
902static void
903ping_handler(struct weston_surface *surface, uint32_t serial)
904{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400905 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600906 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400907 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600908
909 if (!shsurf)
910 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400911 if (!shsurf->resource.client)
912 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600913
914 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300915 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600916 if (!shsurf->ping_timer)
917 return;
918
919 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600920 loop = wl_display_get_event_loop(surface->compositor->wl_display);
921 shsurf->ping_timer->source =
922 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
923 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
924
925 wl_shell_surface_send_ping(&shsurf->resource, serial);
926 }
927}
928
929static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400930handle_pointer_focus(struct wl_listener *listener, void *data)
931{
932 struct wl_pointer *pointer = data;
933 struct weston_surface *surface =
934 (struct weston_surface *) pointer->focus;
935 struct weston_compositor *compositor;
936 struct shell_surface *shsurf;
937 uint32_t serial;
938
939 if (!surface)
940 return;
941
942 compositor = surface->compositor;
943 shsurf = get_shell_surface(surface);
944
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300945 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400946 set_busy_cursor(shsurf, pointer);
947 } else {
948 serial = wl_display_next_serial(compositor->wl_display);
949 ping_handler(surface, serial);
950 }
951}
952
953static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600954shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
955 uint32_t serial)
956{
957 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400958 struct desktop_shell *shell = shsurf->shell;
959 struct weston_seat *seat;
960 struct weston_compositor *ec = shsurf->surface->compositor;
961 struct wl_pointer *pointer;
962 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600963
Scott Moreauff1db4a2012-04-17 19:06:18 -0600964 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400965 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600966 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400967 if (was_unresponsive) {
968 /* Received pong from previously unresponsive client */
969 wl_list_for_each(seat, &ec->seat_list, link) {
970 pointer = seat->seat.pointer;
971 if (pointer->focus ==
972 &shell->busy_surface->surface &&
973 pointer->current ==
974 &shsurf->surface->surface)
975 end_busy_cursor(shsurf, pointer);
976 }
977 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600978 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600979 }
980}
981
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400982static void
983shell_surface_set_title(struct wl_client *client,
984 struct wl_resource *resource, const char *title)
985{
986 struct shell_surface *shsurf = resource->data;
987
988 free(shsurf->title);
989 shsurf->title = strdup(title);
990}
991
992static void
993shell_surface_set_class(struct wl_client *client,
994 struct wl_resource *resource, const char *class)
995{
996 struct shell_surface *shsurf = resource->data;
997
998 free(shsurf->class);
999 shsurf->class = strdup(class);
1000}
1001
Scott Moreauff1db4a2012-04-17 19:06:18 -06001002static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001003surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001004{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001005 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001006
1007 if (!shsurf)
1008 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001009
1010 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001011 if (!move)
1012 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001013
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001014 shell_grab_init(&move->base, &move_grab_interface, shsurf);
1015
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001016 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001017 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001018 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001019 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001020
Daniel Stone37816df2012-05-16 18:45:18 +01001021 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001022
Daniel Stone37816df2012-05-16 18:45:18 +01001023 wl_pointer_set_focus(ws->seat.pointer, NULL,
1024 wl_fixed_from_int(0),
1025 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001026
1027 return 0;
1028}
1029
1030static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001031shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001032 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001033{
Daniel Stone37816df2012-05-16 18:45:18 +01001034 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001035 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001036
Daniel Stone37816df2012-05-16 18:45:18 +01001037 if (ws->seat.pointer->button_count == 0 ||
1038 ws->seat.pointer->grab_serial != serial ||
1039 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001040 return;
1041
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001042 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001043 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001044}
1045
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001046struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001047 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001048 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001049 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001050};
1051
1052static void
Scott Moreau447013d2012-02-18 05:05:29 -07001053resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001054 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001055{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001056 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001057 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001058 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001059 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001060 wl_fixed_t from_x, from_y;
1061 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001062
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001063 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001064 return;
1065
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001066 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001067 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001068 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001069 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001070 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001071
Daniel Stone103db7f2012-05-08 17:17:55 +01001072 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001073 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001074 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001075 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001076 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001077 }
1078
Daniel Stone103db7f2012-05-08 17:17:55 +01001079 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001080 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001081 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001082 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001083 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001084 }
1085
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001086 shsurf->client->send_configure(shsurf->surface,
1087 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001088}
1089
1090static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001091send_configure(struct weston_surface *surface,
1092 uint32_t edges, int32_t width, int32_t height)
1093{
1094 struct shell_surface *shsurf = get_shell_surface(surface);
1095
1096 wl_shell_surface_send_configure(&shsurf->resource,
1097 edges, width, height);
1098}
1099
1100static const struct weston_shell_client shell_client = {
1101 send_configure
1102};
1103
1104static void
Scott Moreau447013d2012-02-18 05:05:29 -07001105resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001106 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001107{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001108 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001109 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001110 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001111
Daniel Stone4dbadb12012-05-30 16:31:51 +01001112 if (pointer->button_count == 0 &&
1113 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001114 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001115 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001116 free(grab);
1117 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001118}
1119
Scott Moreau447013d2012-02-18 05:05:29 -07001120static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001121 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001122 resize_grab_motion,
1123 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001124};
1125
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001126static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001127surface_resize(struct shell_surface *shsurf,
1128 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001129{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001130 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001131
Alex Wu4539b082012-03-01 12:57:46 +08001132 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1133 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001134
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001135 if (edges == 0 || edges > 15 ||
1136 (edges & 3) == 3 || (edges & 12) == 12)
1137 return 0;
1138
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001139 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001140 if (!resize)
1141 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001142
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001143 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
1144
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001145 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001146 resize->width = shsurf->surface->geometry.width;
1147 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001148
Daniel Stone37816df2012-05-16 18:45:18 +01001149 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001150
Daniel Stone37816df2012-05-16 18:45:18 +01001151 wl_pointer_set_focus(ws->seat.pointer, NULL,
1152 wl_fixed_from_int(0),
1153 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001154
1155 return 0;
1156}
1157
1158static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001159shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001160 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001161 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001162{
Daniel Stone37816df2012-05-16 18:45:18 +01001163 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001164 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001165
Alex Wu4539b082012-03-01 12:57:46 +08001166 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1167 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001168
Daniel Stone37816df2012-05-16 18:45:18 +01001169 if (ws->seat.pointer->button_count == 0 ||
1170 ws->seat.pointer->grab_serial != serial ||
1171 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001172 return;
1173
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001174 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001175 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001176}
1177
Juan Zhao96879df2012-02-07 08:45:41 +08001178static struct weston_output *
1179get_default_output(struct weston_compositor *compositor)
1180{
1181 return container_of(compositor->output_list.next,
1182 struct weston_output, link);
1183}
1184
Alex Wu4539b082012-03-01 12:57:46 +08001185static void
1186shell_unset_fullscreen(struct shell_surface *shsurf)
1187{
1188 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001189 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1190 shell_surface_is_top_fullscreen(shsurf)) {
1191 weston_output_switch_mode(shsurf->fullscreen_output,
1192 shsurf->fullscreen_output->origin);
1193 }
Alex Wu4539b082012-03-01 12:57:46 +08001194 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1195 shsurf->fullscreen.framerate = 0;
1196 wl_list_remove(&shsurf->fullscreen.transform.link);
1197 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001198 if (shsurf->fullscreen.black_surface)
1199 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001200 shsurf->fullscreen.black_surface = NULL;
1201 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001202 weston_surface_set_position(shsurf->surface,
1203 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001204 if (shsurf->saved_rotation_valid) {
1205 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1206 &shsurf->rotation.transform.link);
1207 shsurf->saved_rotation_valid = false;
1208 }
Alex Wu4539b082012-03-01 12:57:46 +08001209}
1210
Pekka Paalanen98262232011-12-01 10:42:22 +02001211static int
1212reset_shell_surface_type(struct shell_surface *surface)
1213{
1214 switch (surface->type) {
1215 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001216 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001217 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001218 case SHELL_SURFACE_MAXIMIZED:
1219 surface->output = get_default_output(surface->surface->compositor);
1220 weston_surface_set_position(surface->surface,
1221 surface->saved_x,
1222 surface->saved_y);
1223 break;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001224 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen98262232011-12-01 10:42:22 +02001225 wl_list_remove(&surface->link);
1226 wl_list_init(&surface->link);
1227 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001228 case SHELL_SURFACE_NONE:
1229 case SHELL_SURFACE_TOPLEVEL:
1230 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001231 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001232 break;
1233 }
1234
1235 surface->type = SHELL_SURFACE_NONE;
1236 return 0;
1237}
1238
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001239static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001240set_surface_type(struct shell_surface *shsurf)
1241{
1242 struct weston_surface *surface = shsurf->surface;
1243 struct shell_surface *pshsurf = shsurf->parent;
1244 struct weston_surface *pes;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001245
1246 reset_shell_surface_type(shsurf);
1247
1248 shsurf->type = shsurf->next_type;
1249 shsurf->next_type = SHELL_SURFACE_NONE;
1250
1251 switch (shsurf->type) {
1252 case SHELL_SURFACE_TOPLEVEL:
1253 break;
1254 case SHELL_SURFACE_TRANSIENT:
1255 pes = pshsurf->surface;
1256 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001257 pes->geometry.x + shsurf->transient.x,
1258 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001259 break;
1260
1261 case SHELL_SURFACE_MAXIMIZED:
1262 shsurf->saved_x = surface->geometry.x;
1263 shsurf->saved_y = surface->geometry.y;
1264 shsurf->saved_position_valid = true;
1265 break;
1266
1267 case SHELL_SURFACE_FULLSCREEN:
1268 shsurf->saved_x = surface->geometry.x;
1269 shsurf->saved_y = surface->geometry.y;
1270 shsurf->saved_position_valid = true;
1271
1272 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1273 wl_list_remove(&shsurf->rotation.transform.link);
1274 wl_list_init(&shsurf->rotation.transform.link);
1275 shsurf->surface->geometry.dirty = 1;
1276 shsurf->saved_rotation_valid = true;
1277 }
1278 break;
1279
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001280 default:
1281 break;
1282 }
1283}
1284
1285static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001286set_toplevel(struct shell_surface *shsurf)
1287{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001288 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001289}
1290
1291static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001292shell_surface_set_toplevel(struct wl_client *client,
1293 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001294{
Pekka Paalanen98262232011-12-01 10:42:22 +02001295 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001296
Tiago Vignattibc052c92012-04-19 16:18:18 +03001297 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001298}
1299
1300static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001301set_transient(struct shell_surface *shsurf,
1302 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
1303{
1304 /* assign to parents output */
1305 shsurf->parent = pshsurf;
1306 shsurf->transient.x = x;
1307 shsurf->transient.y = y;
1308 shsurf->transient.flags = flags;
1309 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1310}
1311
1312static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001313shell_surface_set_transient(struct wl_client *client,
1314 struct wl_resource *resource,
1315 struct wl_resource *parent_resource,
1316 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001317{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001318 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001319 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001320
Tiago Vignatti491bac12012-05-18 16:37:43 -04001321 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001322}
1323
Tiago Vignattibe143262012-04-16 17:31:41 +03001324static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001325shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001326{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001327 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001328}
1329
1330static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001331get_output_panel_height(struct desktop_shell *shell,
1332 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001333{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001334 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001335 int panel_height = 0;
1336
1337 if (!output)
1338 return 0;
1339
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001340 wl_list_for_each(surface, &shell->panel_layer.surface_list, link) {
1341 if (surface->output == output) {
1342 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001343 break;
1344 }
1345 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001346
Juan Zhao96879df2012-02-07 08:45:41 +08001347 return panel_height;
1348}
1349
1350static void
1351shell_surface_set_maximized(struct wl_client *client,
1352 struct wl_resource *resource,
1353 struct wl_resource *output_resource )
1354{
1355 struct shell_surface *shsurf = resource->data;
1356 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001357 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001358 uint32_t edges = 0, panel_height = 0;
1359
1360 /* get the default output, if the client set it as NULL
1361 check whether the ouput is available */
1362 if (output_resource)
1363 shsurf->output = output_resource->data;
1364 else
1365 shsurf->output = get_default_output(es->compositor);
1366
Tiago Vignattibe143262012-04-16 17:31:41 +03001367 shell = shell_surface_get_shell(shsurf);
1368 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001369 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001370
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001371 shsurf->client->send_configure(shsurf->surface, edges,
1372 es->output->current->width,
1373 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001374
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001375 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001376}
1377
Alex Wu21858432012-04-01 20:13:08 +08001378static void
1379black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1380
Alex Wu4539b082012-03-01 12:57:46 +08001381static struct weston_surface *
1382create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001383 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001384 GLfloat x, GLfloat y, int w, int h)
1385{
1386 struct weston_surface *surface = NULL;
1387
1388 surface = weston_surface_create(ec);
1389 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001390 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001391 return NULL;
1392 }
1393
Alex Wu21858432012-04-01 20:13:08 +08001394 surface->configure = black_surface_configure;
1395 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001396 weston_surface_configure(surface, x, y, w, h);
1397 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1398 return surface;
1399}
1400
1401/* Create black surface and append it to the associated fullscreen surface.
1402 * Handle size dismatch and positioning according to the method. */
1403static void
1404shell_configure_fullscreen(struct shell_surface *shsurf)
1405{
1406 struct weston_output *output = shsurf->fullscreen_output;
1407 struct weston_surface *surface = shsurf->surface;
1408 struct weston_matrix *matrix;
1409 float scale;
1410
1411 center_on_output(surface, output);
1412
1413 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001414 shsurf->fullscreen.black_surface =
1415 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001416 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001417 output->x, output->y,
1418 output->current->width,
1419 output->current->height);
1420
1421 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1422 wl_list_insert(&surface->layer_link,
1423 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001424 shsurf->fullscreen.black_surface->output = output;
1425
1426 switch (shsurf->fullscreen.type) {
1427 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1428 break;
1429 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1430 matrix = &shsurf->fullscreen.transform.matrix;
1431 weston_matrix_init(matrix);
1432 scale = (float)output->current->width/(float)surface->geometry.width;
1433 weston_matrix_scale(matrix, scale, scale, 1);
1434 wl_list_remove(&shsurf->fullscreen.transform.link);
1435 wl_list_insert(surface->geometry.transformation_list.prev,
1436 &shsurf->fullscreen.transform.link);
1437 weston_surface_set_position(surface, output->x, output->y);
1438 break;
1439 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001440 if (shell_surface_is_top_fullscreen(shsurf)) {
1441 struct weston_mode mode = {0,
1442 surface->geometry.width,
1443 surface->geometry.height,
1444 shsurf->fullscreen.framerate};
1445
1446 if (weston_output_switch_mode(output, &mode) == 0) {
1447 weston_surface_configure(shsurf->fullscreen.black_surface,
1448 output->x, output->y,
1449 output->current->width,
1450 output->current->height);
1451 weston_surface_set_position(surface, output->x, output->y);
1452 break;
1453 }
1454 }
Alex Wu4539b082012-03-01 12:57:46 +08001455 break;
1456 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1457 break;
1458 default:
1459 break;
1460 }
1461}
1462
1463/* make the fullscreen and black surface at the top */
1464static void
1465shell_stack_fullscreen(struct shell_surface *shsurf)
1466{
Alex Wubd3354b2012-04-17 17:20:49 +08001467 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001468 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001469 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001470
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001471 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001472 wl_list_insert(&shell->fullscreen_layer.surface_list,
1473 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001474 weston_surface_damage(surface);
1475
1476 if (!shsurf->fullscreen.black_surface)
1477 shsurf->fullscreen.black_surface =
1478 create_black_surface(surface->compositor,
1479 surface,
1480 output->x, output->y,
1481 output->current->width,
1482 output->current->height);
1483
1484 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001485 wl_list_insert(&surface->layer_link,
1486 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001487 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001488}
1489
1490static void
1491shell_map_fullscreen(struct shell_surface *shsurf)
1492{
Alex Wu4539b082012-03-01 12:57:46 +08001493 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001494 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001495}
1496
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001497static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001498shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001499 struct wl_resource *resource,
1500 uint32_t method,
1501 uint32_t framerate,
1502 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001503{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001504 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001505 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001506
1507 if (output_resource)
1508 shsurf->output = output_resource->data;
1509 else
1510 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001511
Alex Wu4539b082012-03-01 12:57:46 +08001512 shsurf->fullscreen_output = shsurf->output;
1513 shsurf->fullscreen.type = method;
1514 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001515 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001516
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001517 shsurf->client->send_configure(shsurf->surface, 0,
1518 shsurf->output->current->width,
1519 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001520}
1521
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001522static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001523popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001524 struct wl_surface *surface,
1525 wl_fixed_t x,
1526 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001527{
Daniel Stone37816df2012-05-16 18:45:18 +01001528 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001529 struct shell_surface *priv =
1530 container_of(grab, struct shell_surface, popup.grab);
1531 struct wl_client *client = priv->surface->surface.resource.client;
1532
Pekka Paalanencb108432012-01-19 16:25:40 +02001533 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001534 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001535 grab->focus = surface;
1536 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001537 wl_pointer_set_focus(pointer, NULL,
1538 wl_fixed_from_int(0),
1539 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001540 grab->focus = NULL;
1541 }
1542}
1543
1544static void
Scott Moreau447013d2012-02-18 05:05:29 -07001545popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001546 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001547{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001548 struct wl_resource *resource;
1549
Daniel Stone37816df2012-05-16 18:45:18 +01001550 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001551 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001552 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001553}
1554
1555static void
Scott Moreau447013d2012-02-18 05:05:29 -07001556popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001557 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001558{
1559 struct wl_resource *resource;
1560 struct shell_surface *shsurf =
1561 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001562 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001563 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001564 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001565
Daniel Stone37816df2012-05-16 18:45:18 +01001566 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001567 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001568 display = wl_client_get_display(resource->client);
1569 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001570 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001571 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001572 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001573 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001574 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001575 wl_pointer_end_grab(grab->pointer);
1576 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001577 }
1578
Daniel Stone4dbadb12012-05-30 16:31:51 +01001579 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001580 shsurf->popup.initial_up = 1;
1581}
1582
Scott Moreau447013d2012-02-18 05:05:29 -07001583static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001584 popup_grab_focus,
1585 popup_grab_motion,
1586 popup_grab_button,
1587};
1588
1589static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001590shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001591{
Daniel Stone37816df2012-05-16 18:45:18 +01001592 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001593 struct weston_surface *es = shsurf->surface;
1594 struct weston_surface *parent = shsurf->parent->surface;
1595
1596 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001597 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001598
Pekka Paalanen938269a2012-02-07 14:19:01 +02001599 weston_surface_update_transform(parent);
1600 if (parent->transform.enabled) {
1601 shsurf->popup.parent_transform.matrix =
1602 parent->transform.matrix;
1603 } else {
1604 /* construct x, y translation matrix */
1605 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1606 shsurf->popup.parent_transform.matrix.d[12] =
1607 parent->geometry.x;
1608 shsurf->popup.parent_transform.matrix.d[13] =
1609 parent->geometry.y;
1610 }
1611 wl_list_insert(es->geometry.transformation_list.prev,
1612 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001613 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001614
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001615 shsurf->popup.initial_up = 0;
1616
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001617 /* We don't require the grab to still be active, but if another
1618 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001619 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1620 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001621 } else {
1622 wl_shell_surface_send_popup_done(&shsurf->resource);
1623 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001624}
1625
1626static void
1627shell_surface_set_popup(struct wl_client *client,
1628 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001629 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001630 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001631 struct wl_resource *parent_resource,
1632 int32_t x, int32_t y, uint32_t flags)
1633{
1634 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001635
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001636 shsurf->type = SHELL_SURFACE_POPUP;
1637 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001638 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001639 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001640 shsurf->popup.x = x;
1641 shsurf->popup.y = y;
1642}
1643
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001644static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001645 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001646 shell_surface_move,
1647 shell_surface_resize,
1648 shell_surface_set_toplevel,
1649 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001650 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001651 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001652 shell_surface_set_maximized,
1653 shell_surface_set_title,
1654 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001655};
1656
1657static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001658destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001659{
Daniel Stone37816df2012-05-16 18:45:18 +01001660 if (shsurf->popup.grab.pointer)
1661 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001662
Alex Wubd3354b2012-04-17 17:20:49 +08001663 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1664 shell_surface_is_top_fullscreen(shsurf)) {
1665 weston_output_switch_mode(shsurf->fullscreen_output,
1666 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001667 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001668
Alex Wuaa08e2d2012-03-05 11:01:40 +08001669 if (shsurf->fullscreen.black_surface)
1670 weston_surface_destroy(shsurf->fullscreen.black_surface);
1671
Alex Wubd3354b2012-04-17 17:20:49 +08001672 /* As destroy_resource() use wl_list_for_each_safe(),
1673 * we can always remove the listener.
1674 */
1675 wl_list_remove(&shsurf->surface_destroy_listener.link);
1676 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001677 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001678
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001679 wl_list_remove(&shsurf->link);
1680 free(shsurf);
1681}
1682
1683static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001684shell_destroy_shell_surface(struct wl_resource *resource)
1685{
1686 struct shell_surface *shsurf = resource->data;
1687
1688 destroy_shell_surface(shsurf);
1689}
1690
1691static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001692shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001693{
1694 struct shell_surface *shsurf = container_of(listener,
1695 struct shell_surface,
1696 surface_destroy_listener);
1697
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001698 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001699 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001700 } else {
1701 wl_signal_emit(&shsurf->resource.destroy_signal,
1702 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001703 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001704 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001705}
1706
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001707static void
1708shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1709
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001710static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001711get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001712{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001713 if (surface->configure == shell_surface_configure)
1714 return surface->private;
1715 else
1716 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001717}
1718
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001719static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001720create_shell_surface(void *shell, struct weston_surface *surface,
1721 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001722{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001723 struct shell_surface *shsurf;
1724
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001725 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001726 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001727 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001728 }
1729
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001730 shsurf = calloc(1, sizeof *shsurf);
1731 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001732 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001733 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001734 }
1735
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001736 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001737 surface->private = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001738 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001739
Tiago Vignattibc052c92012-04-19 16:18:18 +03001740 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001741 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001742 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001743 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001744 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001745 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1746 shsurf->fullscreen.framerate = 0;
1747 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001748 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001749 wl_list_init(&shsurf->fullscreen.transform.link);
1750
Tiago Vignattibc052c92012-04-19 16:18:18 +03001751 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001752 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1753 wl_signal_add(&surface->surface.resource.destroy_signal,
1754 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001755
1756 /* init link so its safe to always remove it in destroy_shell_surface */
1757 wl_list_init(&shsurf->link);
1758
Pekka Paalanen460099f2012-01-20 16:48:25 +02001759 /* empty when not in use */
1760 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001761 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001762
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001763 wl_list_init(&shsurf->workspace_transform.link);
1764
Pekka Paalanen98262232011-12-01 10:42:22 +02001765 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001766 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001767
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001768 shsurf->client = client;
1769
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001770 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001771}
1772
1773static void
1774shell_get_shell_surface(struct wl_client *client,
1775 struct wl_resource *resource,
1776 uint32_t id,
1777 struct wl_resource *surface_resource)
1778{
1779 struct weston_surface *surface = surface_resource->data;
1780 struct desktop_shell *shell = resource->data;
1781 struct shell_surface *shsurf;
1782
1783 if (get_shell_surface(surface)) {
1784 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001785 WL_DISPLAY_ERROR_INVALID_OBJECT,
1786 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001787 return;
1788 }
1789
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001790 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001791 if (!shsurf) {
1792 wl_resource_post_error(surface_resource,
1793 WL_DISPLAY_ERROR_INVALID_OBJECT,
1794 "surface->configure already set");
1795 return;
1796 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001797
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001798 shsurf->resource.destroy = shell_destroy_shell_surface;
1799 shsurf->resource.object.id = id;
1800 shsurf->resource.object.interface = &wl_shell_surface_interface;
1801 shsurf->resource.object.implementation =
1802 (void (**)(void)) &shell_surface_implementation;
1803 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001804
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001805 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001806}
1807
1808static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001809 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001810};
1811
Kristian Høgsberg07937562011-04-12 17:25:42 -04001812static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001813handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001814{
1815 proc->pid = 0;
1816}
1817
1818static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001819launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001820{
1821 if (shell->screensaver.binding)
1822 return;
1823
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001824 if (!shell->screensaver.path)
1825 return;
1826
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001827 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001828 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001829 return;
1830 }
1831
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001832 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001833 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001834 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001835 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001836}
1837
1838static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001839terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001840{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001841 if (shell->screensaver.process.pid == 0)
1842 return;
1843
1844 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001845}
1846
1847static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001848show_input_panel(struct desktop_shell *shell, struct shell_surface *surface)
1849{
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04001850 if (weston_surface_is_mapped(surface->surface))
1851 return;
1852
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001853 wl_list_remove(&surface->surface->layer_link);
1854 wl_list_insert(&shell->panel_layer.surface_list, &surface->surface->layer_link);
1855 surface->surface->output = surface->output;
1856 weston_surface_damage(surface->surface);
Kristian Høgsberg414bd422012-06-21 22:07:30 -04001857
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04001858 weston_slide_run(surface->surface,
1859 surface->surface->geometry.height, 0,
1860 NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001861}
1862
1863static void
1864hide_input_panel(struct desktop_shell *shell, struct shell_surface *surface)
1865{
1866 weston_surface_damage_below(surface->surface);
1867 wl_list_remove(&surface->surface->layer_link);
1868 wl_list_init(&surface->surface->layer_link);
1869 surface->surface->output = NULL;
1870
1871 weston_compositor_schedule_repaint(surface->surface->compositor);
1872}
1873
1874static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001875configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001876{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001877 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001878
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001879 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1880 if (s->output == es->output && s != es) {
1881 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001882 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001883 }
1884 }
1885
1886 weston_surface_configure(es, es->output->x, es->output->y,
1887 es->buffer->width, es->buffer->height);
1888
1889 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001890 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001891 weston_surface_assign_output(es);
1892 }
1893}
1894
1895static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001896background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1897{
1898 struct desktop_shell *shell = es->private;
1899
1900 configure_static_surface(es, &shell->background_layer);
1901}
1902
1903static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001904desktop_shell_set_background(struct wl_client *client,
1905 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001906 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001907 struct wl_resource *surface_resource)
1908{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001909 struct desktop_shell *shell = resource->data;
1910 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001911
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001912 if (surface->configure) {
1913 wl_resource_post_error(surface_resource,
1914 WL_DISPLAY_ERROR_INVALID_OBJECT,
1915 "surface role already assigned");
1916 return;
1917 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001918
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001919 surface->configure = background_configure;
1920 surface->private = shell;
1921 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001922 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001923 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001924 surface->output->current->width,
1925 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001926}
1927
1928static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001929panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1930{
1931 struct desktop_shell *shell = es->private;
1932
1933 configure_static_surface(es, &shell->panel_layer);
1934}
1935
1936static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001937desktop_shell_set_panel(struct wl_client *client,
1938 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001939 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001940 struct wl_resource *surface_resource)
1941{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001942 struct desktop_shell *shell = resource->data;
1943 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001944
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001945 if (surface->configure) {
1946 wl_resource_post_error(surface_resource,
1947 WL_DISPLAY_ERROR_INVALID_OBJECT,
1948 "surface role already assigned");
1949 return;
1950 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001951
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001952 surface->configure = panel_configure;
1953 surface->private = shell;
1954 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001955 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001956 surface_resource,
1957 surface->output->current->width,
1958 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001959}
1960
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001961static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001962lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1963{
1964 struct desktop_shell *shell = surface->private;
1965
1966 center_on_output(surface, get_default_output(shell->compositor));
1967
1968 if (!weston_surface_is_mapped(surface)) {
1969 wl_list_insert(&shell->lock_layer.surface_list,
1970 &surface->layer_link);
1971 weston_surface_assign_output(surface);
1972 weston_compositor_wake(shell->compositor);
1973 }
1974}
1975
1976static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001977handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001978{
Tiago Vignattibe143262012-04-16 17:31:41 +03001979 struct desktop_shell *shell =
1980 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001981
Martin Minarik6d118362012-06-07 18:01:59 +02001982 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001983 shell->lock_surface = NULL;
1984}
1985
1986static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001987desktop_shell_set_lock_surface(struct wl_client *client,
1988 struct wl_resource *resource,
1989 struct wl_resource *surface_resource)
1990{
Tiago Vignattibe143262012-04-16 17:31:41 +03001991 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001992 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001993
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001994 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001995
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001996 if (!shell->locked)
1997 return;
1998
Pekka Paalanen98262232011-12-01 10:42:22 +02001999 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002000
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002001 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2002 wl_signal_add(&surface_resource->destroy_signal,
2003 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002004
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002005 surface->configure = lock_surface_configure;
2006 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002007}
2008
2009static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002010resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002011{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002012 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002013 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002014
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002015 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2016 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002017
2018 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002019
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002020 wl_list_remove(&shell->lock_layer.link);
2021 wl_list_insert(&shell->compositor->cursor_layer.link,
2022 &shell->fullscreen_layer.link);
2023 wl_list_insert(&shell->fullscreen_layer.link,
2024 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002025 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002026
Jonas Ådahl04769742012-06-13 00:01:24 +02002027 pop_focus_state(shell, get_current_workspace(shell));
2028
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002029 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002030 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002031 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002032 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002033}
2034
2035static void
2036desktop_shell_unlock(struct wl_client *client,
2037 struct wl_resource *resource)
2038{
Tiago Vignattibe143262012-04-16 17:31:41 +03002039 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002040
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002041 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002042
2043 if (shell->locked)
2044 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002045}
2046
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002047static void
2048desktop_shell_set_busy_surface(struct wl_client *client,
2049 struct wl_resource *resource,
2050 struct wl_resource *surface_resource)
2051{
2052 struct desktop_shell *shell = resource->data;
2053
2054 shell->busy_surface = surface_resource->data;
2055}
2056
Kristian Høgsberg75840622011-09-06 13:48:16 -04002057static const struct desktop_shell_interface desktop_shell_implementation = {
2058 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002059 desktop_shell_set_panel,
2060 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002061 desktop_shell_unlock,
2062 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002063};
2064
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002065static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002066get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002067{
2068 struct shell_surface *shsurf;
2069
2070 shsurf = get_shell_surface(surface);
2071 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002072 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002073 return shsurf->type;
2074}
2075
Kristian Høgsberg75840622011-09-06 13:48:16 -04002076static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002077move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002078{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002079 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002080 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002081 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002082
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002083 if (surface == NULL)
2084 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002085
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002086 shsurf = get_shell_surface(surface);
2087 if (shsurf == NULL)
2088 return;
2089
2090 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002091 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002092 case SHELL_SURFACE_INPUT_PANEL:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002093 return;
2094 default:
2095 break;
2096 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002097
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002098 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002099}
2100
2101static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002102resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002103{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002104 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002105 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002106 uint32_t edges = 0;
2107 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002108 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002109
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002110 if (surface == NULL)
2111 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002112
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002113 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002114 if (!shsurf)
2115 return;
2116
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002117 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002118 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002119 case SHELL_SURFACE_INPUT_PANEL:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002120 return;
2121 default:
2122 break;
2123 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002124
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002125 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002126 wl_fixed_to_int(seat->pointer->grab_x),
2127 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002128 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002129
Pekka Paalanen60921e52012-01-25 15:55:43 +02002130 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002131 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002132 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002133 edges |= 0;
2134 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002135 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002136
Pekka Paalanen60921e52012-01-25 15:55:43 +02002137 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002138 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002139 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002140 edges |= 0;
2141 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002142 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002143
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002144 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002145}
2146
2147static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002148surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002149 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002150{
Scott Moreau02709af2012-05-22 01:54:10 -06002151 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002152 struct shell_surface *shsurf;
2153 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002154 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002155
2156 if (surface == NULL)
2157 return;
2158
2159 shsurf = get_shell_surface(surface);
2160 if (!shsurf)
2161 return;
2162
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002163 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002164
Scott Moreau02709af2012-05-22 01:54:10 -06002165 if (surface->alpha > 1.0)
2166 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002167 if (surface->alpha < step)
2168 surface->alpha = step;
2169
2170 surface->geometry.dirty = 1;
2171 weston_surface_damage(surface);
2172}
2173
2174static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002175do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002176 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002177{
Daniel Stone37816df2012-05-16 18:45:18 +01002178 struct weston_seat *ws = (struct weston_seat *) seat;
2179 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002180 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002181 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002182
2183 wl_list_for_each(output, &compositor->output_list, link) {
2184 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002185 wl_fixed_to_double(seat->pointer->x),
2186 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002187 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002188 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002189 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002190 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002191 increment = -output->zoom.increment;
2192 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002193 increment = output->zoom.increment *
2194 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002195 else
2196 increment = 0;
2197
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002198 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002199
Scott Moreaue6603982012-06-11 13:07:51 -06002200 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002201 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002202 else if (output->zoom.level > output->zoom.max_level)
2203 output->zoom.level = output->zoom.max_level;
2204 else
2205 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002206
Scott Moreaue6603982012-06-11 13:07:51 -06002207 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002208
Scott Moreau8dacaab2012-06-17 18:10:58 -06002209 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002210 }
2211 }
2212}
2213
Scott Moreauccbf29d2012-02-22 14:21:41 -07002214static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002215zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002216 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002217{
2218 do_zoom(seat, time, 0, axis, value);
2219}
2220
2221static void
2222zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2223 void *data)
2224{
2225 do_zoom(seat, time, key, 0, 0);
2226}
2227
2228static void
2229terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2230 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002231{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002232 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002233
Daniel Stone325fc2d2012-05-30 16:31:58 +01002234 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002235}
2236
2237static void
Scott Moreau447013d2012-02-18 05:05:29 -07002238rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002239 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002240{
2241 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002242 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002243 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002244 struct shell_surface *shsurf = rotate->base.shsurf;
2245 struct weston_surface *surface;
2246 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2247
2248 if (!shsurf)
2249 return;
2250
2251 surface = shsurf->surface;
2252
2253 cx = 0.5f * surface->geometry.width;
2254 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002255
Daniel Stone37816df2012-05-16 18:45:18 +01002256 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2257 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002258 r = sqrtf(dx * dx + dy * dy);
2259
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002260 wl_list_remove(&shsurf->rotation.transform.link);
2261 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002262
2263 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002264 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002265 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002266
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002267 weston_matrix_init(&rotate->rotation);
2268 rotate->rotation.d[0] = dx / r;
2269 rotate->rotation.d[4] = -dy / r;
2270 rotate->rotation.d[1] = -rotate->rotation.d[4];
2271 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002272
2273 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002274 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002275 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002276 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002277 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002278
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002279 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002280 &shsurf->surface->geometry.transformation_list,
2281 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002282 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002283 wl_list_init(&shsurf->rotation.transform.link);
2284 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002285 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002286 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002287
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002288 /* We need to adjust the position of the surface
2289 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002290 cposx = surface->geometry.x + cx;
2291 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002292 dposx = rotate->center.x - cposx;
2293 dposy = rotate->center.y - cposy;
2294 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002295 weston_surface_set_position(surface,
2296 surface->geometry.x + dposx,
2297 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002298 }
2299
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002300 /* Repaint implies weston_surface_update_transform(), which
2301 * lazily applies the damage due to rotation update.
2302 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002303 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002304}
2305
2306static void
Scott Moreau447013d2012-02-18 05:05:29 -07002307rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002308 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002309{
2310 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002311 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002312 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002313 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002314 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002315
Daniel Stone4dbadb12012-05-30 16:31:51 +01002316 if (pointer->button_count == 0 &&
2317 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002318 if (shsurf)
2319 weston_matrix_multiply(&shsurf->rotation.rotation,
2320 &rotate->rotation);
2321 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002322 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002323 free(rotate);
2324 }
2325}
2326
Scott Moreau447013d2012-02-18 05:05:29 -07002327static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002328 noop_grab_focus,
2329 rotate_grab_motion,
2330 rotate_grab_button,
2331};
2332
2333static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002334rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002335 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002336{
2337 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002338 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002339 struct shell_surface *surface;
2340 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002341 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002342 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002343
2344 if (base_surface == NULL)
2345 return;
2346
2347 surface = get_shell_surface(base_surface);
2348 if (!surface)
2349 return;
2350
2351 switch (surface->type) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002352 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002353 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen460099f2012-01-20 16:48:25 +02002354 return;
2355 default:
2356 break;
2357 }
2358
Pekka Paalanen460099f2012-01-20 16:48:25 +02002359 rotate = malloc(sizeof *rotate);
2360 if (!rotate)
2361 return;
2362
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002363 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02002364
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002365 weston_surface_to_global_float(surface->surface,
2366 surface->surface->geometry.width / 2,
2367 surface->surface->geometry.height / 2,
2368 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002369
Daniel Stone37816df2012-05-16 18:45:18 +01002370 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002371
Daniel Stone37816df2012-05-16 18:45:18 +01002372 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2373 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002374 r = sqrtf(dx * dx + dy * dy);
2375 if (r > 20.0f) {
2376 struct weston_matrix inverse;
2377
2378 weston_matrix_init(&inverse);
2379 inverse.d[0] = dx / r;
2380 inverse.d[4] = dy / r;
2381 inverse.d[1] = -inverse.d[4];
2382 inverse.d[5] = inverse.d[0];
2383 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002384
2385 weston_matrix_init(&rotate->rotation);
2386 rotate->rotation.d[0] = dx / r;
2387 rotate->rotation.d[4] = -dy / r;
2388 rotate->rotation.d[1] = -rotate->rotation.d[4];
2389 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002390 } else {
2391 weston_matrix_init(&surface->rotation.rotation);
2392 weston_matrix_init(&rotate->rotation);
2393 }
2394
Daniel Stone37816df2012-05-16 18:45:18 +01002395 wl_pointer_set_focus(seat->pointer, NULL,
2396 wl_fixed_from_int(0),
2397 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02002398}
2399
2400static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002401lower_fullscreen_layer(struct desktop_shell *shell)
2402{
2403 struct workspace *ws;
2404 struct weston_surface *surface, *prev;
2405
2406 ws = get_current_workspace(shell);
2407 wl_list_for_each_reverse_safe(surface, prev,
2408 &shell->fullscreen_layer.surface_list,
2409 layer_link)
2410 weston_surface_restack(surface, &ws->layer.surface_list);
2411}
2412
2413static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002414activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002415 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002416{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002417 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002418
Daniel Stone37816df2012-05-16 18:45:18 +01002419 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002420
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002421 switch (get_shell_surface_type(es)) {
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002422 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002423 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002424
Alex Wu4539b082012-03-01 12:57:46 +08002425 case SHELL_SURFACE_FULLSCREEN:
2426 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002427 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002428 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002429 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002430 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002431 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002432 lower_fullscreen_layer(shell);
2433 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002434 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002435 }
2436}
2437
Alex Wu21858432012-04-01 20:13:08 +08002438/* no-op func for checking black surface */
2439static void
2440black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2441{
2442}
2443
2444static bool
2445is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2446{
2447 if (es->configure == black_surface_configure) {
2448 if (fs_surface)
2449 *fs_surface = (struct weston_surface *)es->private;
2450 return true;
2451 }
2452 return false;
2453}
2454
Kristian Høgsberg75840622011-09-06 13:48:16 -04002455static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002456click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002457 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002458{
Daniel Stone37816df2012-05-16 18:45:18 +01002459 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002460 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002461 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002462 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002463
Daniel Stone37816df2012-05-16 18:45:18 +01002464 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002465 if (!focus)
2466 return;
2467
Alex Wu21858432012-04-01 20:13:08 +08002468 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002469 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002470
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002471 switch (get_shell_surface_type(focus)) {
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002472 case SHELL_SURFACE_INPUT_PANEL:
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002473 case SHELL_SURFACE_NONE:
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002474 return;
2475 default:
2476 break;
2477 }
2478
Daniel Stone325fc2d2012-05-30 16:31:58 +01002479 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002480 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002481}
2482
2483static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002484lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002485{
Tiago Vignattibe143262012-04-16 17:31:41 +03002486 struct desktop_shell *shell =
2487 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002488 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002489 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002490
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002491 if (shell->locked) {
2492 wl_list_for_each(output, &shell->compositor->output_list, link)
2493 /* TODO: find a way to jump to other DPMS levels */
2494 if (output->set_dpms)
2495 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002496 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002497 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002498
2499 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002500
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002501 /* Hide all surfaces by removing the fullscreen, panel and
2502 * toplevel layers. This way nothing else can show or receive
2503 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002504
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002505 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002506 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002507 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002508 wl_list_insert(&shell->compositor->cursor_layer.link,
2509 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002510
Pekka Paalanen77346a62011-11-30 16:26:35 +02002511 launch_screensaver(shell);
2512
Jonas Ådahl04769742012-06-13 00:01:24 +02002513 /* stash keyboard foci in current workspace */
2514 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002515
2516 /* TODO: disable bindings that should not work while locked. */
2517
2518 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002519}
2520
2521static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002522unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002523{
Tiago Vignattibe143262012-04-16 17:31:41 +03002524 struct desktop_shell *shell =
2525 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002526
Pekka Paalanend81c2162011-11-16 13:47:34 +02002527 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002528 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002529 return;
2530 }
2531
2532 /* If desktop-shell client has gone away, unlock immediately. */
2533 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002534 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002535 return;
2536 }
2537
2538 if (shell->prepare_event_sent)
2539 return;
2540
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002541 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002542 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002543}
2544
2545static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002546show_input_panels(struct wl_listener *listener, void *data)
2547{
2548 struct desktop_shell *shell =
2549 container_of(listener, struct desktop_shell, show_input_panel_listener);
2550 struct shell_surface *priv;
2551
2552 wl_list_for_each(priv, &shell->input_panel.surfaces, link) {
2553 show_input_panel(shell, priv);
2554 }
2555}
2556
2557static void
2558hide_input_panels(struct wl_listener *listener, void *data)
2559{
2560 struct desktop_shell *shell =
2561 container_of(listener, struct desktop_shell, hide_input_panel_listener);
2562 struct shell_surface *priv;
2563
2564 wl_list_for_each(priv, &shell->input_panel.surfaces, link) {
2565 hide_input_panel(shell, priv);
2566 }
2567}
2568
2569static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002570center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002571{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002572 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002573 GLfloat x = (mode->width - surface->buffer->width) / 2;
2574 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002575
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002576 weston_surface_configure(surface, output->x + x, output->y + y,
2577 surface->buffer->width,
2578 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002579}
2580
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002581
2582static void
2583bottom_center_on_output(struct weston_surface *surface, struct weston_output *output)
2584{
2585 struct weston_mode *mode = output->current;
2586 GLfloat x = (mode->width - surface->geometry.width) / 2;
2587 GLfloat y = mode->height - surface->geometry.height;
2588
2589 weston_surface_set_position(surface, output->x + x, output->y + y);
2590}
2591
Pekka Paalanen77346a62011-11-30 16:26:35 +02002592static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002593map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002594 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002595{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002596 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002597 struct shell_surface *shsurf = get_shell_surface(surface);
2598 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002599 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002600 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002601 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002602 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002603
Pekka Paalanen60921e52012-01-25 15:55:43 +02002604 surface->geometry.width = width;
2605 surface->geometry.height = height;
2606 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002607
2608 /* initial positioning, see also configure() */
2609 switch (surface_type) {
2610 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002611 weston_surface_set_position(surface, 10 + random() % 400,
2612 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002613 break;
Alex Wu4539b082012-03-01 12:57:46 +08002614 case SHELL_SURFACE_FULLSCREEN:
2615 shell_map_fullscreen(shsurf);
2616 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002617 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002618 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002619 panel_height = get_output_panel_height(shell,surface->output);
2620 weston_surface_set_position(surface, surface->output->x,
2621 surface->output->y + panel_height);
2622 break;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002623 case SHELL_SURFACE_INPUT_PANEL:
2624 bottom_center_on_output(surface, get_default_output(compositor));
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04002625 /* Don't map the input panel here, wait for
2626 * show_input_panels signal. */
2627 return;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002628 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002629 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002630 case SHELL_SURFACE_NONE:
2631 weston_surface_set_position(surface,
2632 surface->geometry.x + sx,
2633 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002634 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002635 default:
2636 ;
2637 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002638
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002639 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002640 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002641 case SHELL_SURFACE_POPUP:
2642 case SHELL_SURFACE_TRANSIENT:
2643 parent = shsurf->parent->surface;
2644 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2645 break;
Alex Wu4539b082012-03-01 12:57:46 +08002646 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002647 case SHELL_SURFACE_NONE:
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002648 case SHELL_SURFACE_INPUT_PANEL:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002649 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002650 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002651 ws = get_current_workspace(shell);
2652 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002653 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002654 }
2655
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002656 if (surface_type != SHELL_SURFACE_NONE) {
2657 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002658 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2659 surface->output = shsurf->output;
2660 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002661
Juan Zhao7bb92f02011-12-15 11:31:51 -05002662 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002663 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002664 if (shsurf->transient.flags ==
2665 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2666 break;
2667 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002668 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002669 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002670 if (!shell->locked) {
2671 wl_list_for_each(seat, &compositor->seat_list, link)
2672 activate(shell, surface, seat);
2673 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002674 break;
2675 default:
2676 break;
2677 }
2678
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002679 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002680 {
2681 switch (shell->win_animation_type) {
2682 case ANIMATION_FADE:
2683 weston_fade_run(surface, NULL, NULL);
2684 break;
2685 case ANIMATION_ZOOM:
2686 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2687 break;
2688 default:
2689 break;
2690 }
2691 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002692}
2693
2694static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002695configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002696 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002697{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002698 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2699 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002700
Pekka Paalanen77346a62011-11-30 16:26:35 +02002701 shsurf = get_shell_surface(surface);
2702 if (shsurf)
2703 surface_type = shsurf->type;
2704
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002705 surface->geometry.x = x;
2706 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002707 surface->geometry.width = width;
2708 surface->geometry.height = height;
2709 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002710
2711 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002712 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002713 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002714 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002715 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002716 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002717 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002718 surface->geometry.x = surface->output->x;
2719 surface->geometry.y = surface->output->y +
2720 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002721 break;
Alex Wu4539b082012-03-01 12:57:46 +08002722 case SHELL_SURFACE_TOPLEVEL:
2723 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002724 default:
2725 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002726 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002727
Alex Wu4539b082012-03-01 12:57:46 +08002728 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002729 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002730 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002731
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002732 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002733 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002734 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002735}
2736
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002737static void
2738shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2739{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002740 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002741 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002742 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002743
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002744 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002745 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002746 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002747 type_changed = 1;
2748 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002749
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002750 if (!weston_surface_is_mapped(es)) {
2751 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002752 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002753 es->geometry.width != es->buffer->width ||
2754 es->geometry.height != es->buffer->height) {
2755 GLfloat from_x, from_y;
2756 GLfloat to_x, to_y;
2757
2758 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2759 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2760 configure(shell, es,
2761 es->geometry.x + to_x - from_x,
2762 es->geometry.y + to_y - from_y,
2763 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002764 }
2765}
2766
Tiago Vignattibe143262012-04-16 17:31:41 +03002767static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002768
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002769static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002770desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002771{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002772 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002773 struct desktop_shell *shell =
2774 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002775
2776 shell->child.process.pid = 0;
2777 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002778
2779 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2780 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002781 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002782 shell->child.deathstamp = time;
2783 shell->child.deathcount = 0;
2784 }
2785
2786 shell->child.deathcount++;
2787 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002788 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002789 return;
2790 }
2791
Martin Minarik6d118362012-06-07 18:01:59 +02002792 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002793 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002794}
2795
2796static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002797launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002798{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002799 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002800
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002801 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002802 &shell->child.process,
2803 shell_exe,
2804 desktop_shell_sigchld);
2805
2806 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002807 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002808 return 0;
2809}
2810
2811static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002812bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2813{
Tiago Vignattibe143262012-04-16 17:31:41 +03002814 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002815
2816 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002817 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002818}
2819
Kristian Høgsberg75840622011-09-06 13:48:16 -04002820static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002821unbind_desktop_shell(struct wl_resource *resource)
2822{
Tiago Vignattibe143262012-04-16 17:31:41 +03002823 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002824
2825 if (shell->locked)
2826 resume_desktop(shell);
2827
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002828 shell->child.desktop_shell = NULL;
2829 shell->prepare_event_sent = false;
2830 free(resource);
2831}
2832
2833static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002834bind_desktop_shell(struct wl_client *client,
2835 void *data, uint32_t version, uint32_t id)
2836{
Tiago Vignattibe143262012-04-16 17:31:41 +03002837 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002838 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002839
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002840 resource = wl_client_add_object(client, &desktop_shell_interface,
2841 &desktop_shell_implementation,
2842 id, shell);
2843
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002844 if (client == shell->child.client) {
2845 resource->destroy = unbind_desktop_shell;
2846 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002847 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002848 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002849
2850 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2851 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002852 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002853}
2854
Pekka Paalanen6e168112011-11-24 11:34:05 +02002855static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002856screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2857{
2858 struct desktop_shell *shell = surface->private;
2859
2860 if (!shell->locked)
2861 return;
2862
2863 center_on_output(surface, surface->output);
2864
2865 if (wl_list_empty(&surface->layer_link)) {
2866 wl_list_insert(shell->lock_layer.surface_list.prev,
2867 &surface->layer_link);
2868 weston_surface_assign_output(surface);
2869 shell->compositor->idle_time = shell->screensaver.duration;
2870 weston_compositor_wake(shell->compositor);
2871 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2872 }
2873}
2874
2875static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002876screensaver_set_surface(struct wl_client *client,
2877 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002878 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002879 struct wl_resource *output_resource)
2880{
Tiago Vignattibe143262012-04-16 17:31:41 +03002881 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002882 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002883 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002884
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002885 surface->configure = screensaver_configure;
2886 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002887 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002888}
2889
2890static const struct screensaver_interface screensaver_implementation = {
2891 screensaver_set_surface
2892};
2893
2894static void
2895unbind_screensaver(struct wl_resource *resource)
2896{
Tiago Vignattibe143262012-04-16 17:31:41 +03002897 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002898
Pekka Paalanen77346a62011-11-30 16:26:35 +02002899 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002900 free(resource);
2901}
2902
2903static void
2904bind_screensaver(struct wl_client *client,
2905 void *data, uint32_t version, uint32_t id)
2906{
Tiago Vignattibe143262012-04-16 17:31:41 +03002907 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002908 struct wl_resource *resource;
2909
2910 resource = wl_client_add_object(client, &screensaver_interface,
2911 &screensaver_implementation,
2912 id, shell);
2913
Pekka Paalanen77346a62011-11-30 16:26:35 +02002914 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002915 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002916 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002917 return;
2918 }
2919
2920 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2921 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002922 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002923}
2924
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002925static void
2926input_panel_set_surface(struct wl_client *client,
2927 struct wl_resource *resource,
2928 struct wl_resource *shell_surface_resource,
2929 struct wl_resource *output_resource)
2930{
2931 struct desktop_shell *shell = resource->data;
2932 struct shell_surface *surface = shell_surface_resource->data;
2933 struct weston_output *output = output_resource->data;
2934
2935 surface->next_type = SHELL_SURFACE_INPUT_PANEL;
2936
2937 surface->fullscreen_output = output;
2938 surface->output = output;
2939 wl_list_insert(shell->input_panel.surfaces.prev, &surface->link);
2940}
2941
2942static const struct input_panel_interface input_panel_implementation = {
2943 input_panel_set_surface
2944};
2945
2946static void
2947unbind_input_panel(struct wl_resource *resource)
2948{
2949 struct desktop_shell *shell = resource->data;
2950
2951 shell->input_panel.binding = NULL;
2952 free(resource);
2953}
2954
2955static void
2956bind_input_panel(struct wl_client *client,
2957 void *data, uint32_t version, uint32_t id)
2958{
2959 struct desktop_shell *shell = data;
2960 struct wl_resource *resource;
2961
2962 resource = wl_client_add_object(client, &input_panel_interface,
2963 &input_panel_implementation,
2964 id, shell);
2965
2966 if (shell->input_panel.binding == NULL) {
2967 resource->destroy = unbind_input_panel;
2968 shell->input_panel.binding = resource;
2969 return;
2970 }
2971
2972 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2973 "interface object already bound");
2974 wl_resource_destroy(resource);
2975}
2976
Kristian Høgsberg07045392012-02-19 18:52:44 -05002977struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002978 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002979 struct weston_surface *current;
2980 struct wl_listener listener;
2981 struct wl_keyboard_grab grab;
2982};
2983
2984static void
2985switcher_next(struct switcher *switcher)
2986{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002987 struct weston_surface *surface;
2988 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002989 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002990 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002991
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002992 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002993 switch (get_shell_surface_type(surface)) {
2994 case SHELL_SURFACE_TOPLEVEL:
2995 case SHELL_SURFACE_FULLSCREEN:
2996 case SHELL_SURFACE_MAXIMIZED:
2997 if (first == NULL)
2998 first = surface;
2999 if (prev == switcher->current)
3000 next = surface;
3001 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003002 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003003 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003004 weston_surface_damage(surface);
3005 break;
3006 default:
3007 break;
3008 }
Alex Wu1659daa2012-04-01 20:13:09 +08003009
3010 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003011 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003012 surface->geometry.dirty = 1;
3013 weston_surface_damage(surface);
3014 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003015 }
3016
3017 if (next == NULL)
3018 next = first;
3019
Alex Wu07b26062012-03-12 16:06:01 +08003020 if (next == NULL)
3021 return;
3022
Kristian Høgsberg07045392012-02-19 18:52:44 -05003023 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003024 wl_signal_add(&next->surface.resource.destroy_signal,
3025 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003026
3027 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003028 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003029
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003030 shsurf = get_shell_surface(switcher->current);
3031 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003032 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003033}
3034
3035static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003036switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003037{
3038 struct switcher *switcher =
3039 container_of(listener, struct switcher, listener);
3040
3041 switcher_next(switcher);
3042}
3043
3044static void
Daniel Stone351eb612012-05-31 15:27:47 -04003045switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003046{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003047 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003048 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003049 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003050
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003051 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003052 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003053 weston_surface_damage(surface);
3054 }
3055
Alex Wu07b26062012-03-12 16:06:01 +08003056 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003057 activate(switcher->shell, switcher->current,
3058 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003059 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003060 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003061 free(switcher);
3062}
3063
3064static void
3065switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003066 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003067{
3068 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003069 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003070
Daniel Stonec9785ea2012-05-30 16:31:52 +01003071 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003072 switcher_next(switcher);
3073}
3074
3075static void
3076switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3077 uint32_t mods_depressed, uint32_t mods_latched,
3078 uint32_t mods_locked, uint32_t group)
3079{
3080 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003081 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003082
Daniel Stone351eb612012-05-31 15:27:47 -04003083 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3084 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003085}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003086
3087static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003088 switcher_key,
3089 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003090};
3091
3092static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003093switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3094 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003095{
Tiago Vignattibe143262012-04-16 17:31:41 +03003096 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003097 struct switcher *switcher;
3098
3099 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003100 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003101 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003102 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003103 wl_list_init(&switcher->listener.link);
3104
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003105 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003106 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003107 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3108 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003109 switcher_next(switcher);
3110}
3111
Pekka Paalanen3c647232011-12-22 13:43:43 +02003112static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003113backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3114 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003115{
3116 struct weston_compositor *compositor = data;
3117 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003118 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003119
3120 /* TODO: we're limiting to simple use cases, where we assume just
3121 * control on the primary display. We'd have to extend later if we
3122 * ever get support for setting backlights on random desktop LCD
3123 * panels though */
3124 output = get_default_output(compositor);
3125 if (!output)
3126 return;
3127
3128 if (!output->set_backlight)
3129 return;
3130
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003131 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3132 backlight_new = output->backlight_current - 25;
3133 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3134 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003135
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003136 if (backlight_new < 5)
3137 backlight_new = 5;
3138 if (backlight_new > 255)
3139 backlight_new = 255;
3140
3141 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003142 output->set_backlight(output, output->backlight_current);
3143}
3144
3145static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003146debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3147 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003148{
Tiago Vignattibe143262012-04-16 17:31:41 +03003149 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003150 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003151 struct weston_surface *surface;
3152
3153 if (shell->debug_repaint_surface) {
3154 weston_surface_destroy(shell->debug_repaint_surface);
3155 shell->debug_repaint_surface = NULL;
3156 } else {
3157 surface = weston_surface_create(compositor);
3158 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3159 weston_surface_configure(surface, 0, 0, 8192, 8192);
3160 wl_list_insert(&compositor->fade_layer.surface_list,
3161 &surface->layer_link);
3162 weston_surface_assign_output(surface);
3163 pixman_region32_init(&surface->input);
3164
3165 /* Here's the dirty little trick that makes the
3166 * repaint debugging work: we force an
3167 * update_transform first to update dependent state
3168 * and clear the geometry.dirty bit. Then we clear
3169 * the surface damage so it only gets repainted
3170 * piecewise as we repaint other things. */
3171
3172 weston_surface_update_transform(surface);
3173 pixman_region32_fini(&surface->damage);
3174 pixman_region32_init(&surface->damage);
3175 shell->debug_repaint_surface = surface;
3176 }
3177}
3178
3179static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003180force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3181 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003182{
3183 struct wl_client *client;
3184 pid_t pid;
3185 uid_t uid;
3186 gid_t gid;
3187
Daniel Stone325fc2d2012-05-30 16:31:58 +01003188 client = seat->keyboard->focus->resource.client;
3189 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003190
Daniel Stone325fc2d2012-05-30 16:31:58 +01003191 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003192}
3193
3194static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003195workspace_up_binding(struct wl_seat *seat, uint32_t time,
3196 uint32_t key, void *data)
3197{
3198 struct desktop_shell *shell = data;
3199 unsigned int new_index = shell->workspaces.current;
3200
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003201 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003202 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003203 if (new_index != 0)
3204 new_index--;
3205
3206 change_workspace(shell, new_index);
3207}
3208
3209static void
3210workspace_down_binding(struct wl_seat *seat, uint32_t time,
3211 uint32_t key, void *data)
3212{
3213 struct desktop_shell *shell = data;
3214 unsigned int new_index = shell->workspaces.current;
3215
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003216 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003217 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003218 if (new_index < shell->workspaces.num - 1)
3219 new_index++;
3220
3221 change_workspace(shell, new_index);
3222}
3223
3224static void
3225workspace_f_binding(struct wl_seat *seat, uint32_t time,
3226 uint32_t key, void *data)
3227{
3228 struct desktop_shell *shell = data;
3229 unsigned int new_index;
3230
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003231 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003232 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003233 new_index = key - KEY_F1;
3234 if (new_index >= shell->workspaces.num)
3235 new_index = shell->workspaces.num - 1;
3236
3237 change_workspace(shell, new_index);
3238}
3239
3240
3241static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003242shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003243{
Tiago Vignattibe143262012-04-16 17:31:41 +03003244 struct desktop_shell *shell =
3245 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003246 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003247
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003248 if (shell->child.client)
3249 wl_client_destroy(shell->child.client);
3250
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003251 wl_list_remove(&shell->lock_listener.link);
3252 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003253 wl_list_remove(&shell->show_input_panel_listener.link);
3254 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003255
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003256 wl_array_for_each(ws, &shell->workspaces.array)
3257 workspace_destroy(*ws);
3258 wl_array_release(&shell->workspaces.array);
3259
Pekka Paalanen3c647232011-12-22 13:43:43 +02003260 free(shell->screensaver.path);
3261 free(shell);
3262}
3263
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003264static void
3265shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3266{
3267 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003268 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003269
3270 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003271 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3272 MODIFIER_CTRL | MODIFIER_ALT,
3273 terminate_binding, ec);
3274 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3275 click_to_activate_binding,
3276 shell);
3277 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3278 MODIFIER_SUPER | MODIFIER_ALT,
3279 surface_opacity_binding, NULL);
3280 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3281 MODIFIER_SUPER, zoom_axis_binding,
3282 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003283
3284 /* configurable bindings */
3285 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003286 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3287 zoom_key_binding, NULL);
3288 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3289 zoom_key_binding, NULL);
3290 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3291 shell);
3292 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3293 resize_binding, shell);
3294 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3295 rotate_binding, NULL);
3296 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3297 shell);
3298 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3299 ec);
3300 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3301 backlight_binding, ec);
3302 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3303 ec);
3304 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3305 backlight_binding, ec);
3306 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
3307 debug_repaint_binding, shell);
3308 weston_compositor_add_key_binding(ec, KEY_K, mod,
3309 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003310 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3311 workspace_up_binding, shell);
3312 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3313 workspace_down_binding, shell);
3314
3315 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3316 if (shell->workspaces.num > 1) {
3317 num_workspace_bindings = shell->workspaces.num;
3318 if (num_workspace_bindings > 6)
3319 num_workspace_bindings = 6;
3320 for (i = 0; i < num_workspace_bindings; i++)
3321 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3322 workspace_f_binding,
3323 shell);
3324 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003325}
3326
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003327int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003328shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003329
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003330WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003331shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003332{
Tiago Vignattibe143262012-04-16 17:31:41 +03003333 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003334 struct workspace **pws;
3335 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003336
3337 shell = malloc(sizeof *shell);
3338 if (shell == NULL)
3339 return -1;
3340
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003341 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003342 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003343
3344 shell->destroy_listener.notify = shell_destroy;
3345 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3346 shell->lock_listener.notify = lock;
3347 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3348 shell->unlock_listener.notify = unlock;
3349 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003350 shell->show_input_panel_listener.notify = show_input_panels;
3351 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3352 shell->hide_input_panel_listener.notify = hide_input_panels;
3353 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003354 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003355 ec->shell_interface.create_shell_surface = create_shell_surface;
3356 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003357 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003358 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003359 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003360
Pekka Paalanen77346a62011-11-30 16:26:35 +02003361 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003362 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003363
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003364 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3365 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003366 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3367 weston_layer_init(&shell->lock_layer, NULL);
3368
3369 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003370
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003371 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003372
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003373 for (i = 0; i < shell->workspaces.num; i++) {
3374 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3375 if (pws == NULL)
3376 return -1;
3377
3378 *pws = workspace_create();
3379 if (*pws == NULL)
3380 return -1;
3381 }
3382 activate_workspace(shell, 0);
3383
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003384 wl_list_init(&shell->workspaces.animation.link);
3385 shell->workspaces.animation.frame = animate_workspace_change_frame;
3386
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003387 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3388 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003389 return -1;
3390
Kristian Høgsberg75840622011-09-06 13:48:16 -04003391 if (wl_display_add_global(ec->wl_display,
3392 &desktop_shell_interface,
3393 shell, bind_desktop_shell) == NULL)
3394 return -1;
3395
Pekka Paalanen6e168112011-11-24 11:34:05 +02003396 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3397 shell, bind_screensaver) == NULL)
3398 return -1;
3399
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003400 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3401 shell, bind_input_panel) == NULL)
3402 return -1;
3403
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003404 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003405 if (launch_desktop_shell_process(shell) != 0)
3406 return -1;
3407
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003408 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003409 if (ec->seat->seat.pointer)
3410 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3411 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003412
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003413 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003414
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003415 return 0;
3416}