From 6ea9457286279140428f4ad59e2a83c1f4252bc6 Mon Sep 17 00:00:00 2001 From: Kristian Høgsberg Date: Mon, 11 Apr 2011 14:15:50 -0400 Subject: MeeGo Tablet Shell --- compositor/Makefile.am | 8 +- compositor/compositor.c | 333 ++++++++++++++++++++++--- compositor/compositor.h | 76 +++++- compositor/meego-tablet-shell.c | 520 ++++++++++++++++++++++++++++++++++++++++ protocol/meego-tablet.xml | 31 +++ 5 files changed, 927 insertions(+), 41 deletions(-) create mode 100644 compositor/meego-tablet-shell.c create mode 100644 protocol/meego-tablet.xml diff --git a/compositor/Makefile.am b/compositor/Makefile.am index 9c62355c..b002dd12 100644 --- a/compositor/Makefile.am +++ b/compositor/Makefile.am @@ -38,6 +38,10 @@ compositor_SOURCES = \ screenshooter-protocol.c \ screenshooter-server-protocol.h \ shm.c \ + meego-tablet-shell.c \ + meego-tablet-shell.h \ + meego-tablet-protocol.c \ + meego-tablet-server-protocol.h \ $(drm_compositor_sources) \ $(openwfd_compositor_sources) \ $(x11_compositor_sources) \ @@ -50,7 +54,9 @@ dist_udevrulesd_DATA = \ BUILT_SOURCES = \ screenshooter-server-protocol.h \ - screenshooter-protocol.c + screenshooter-protocol.c \ + meego-tablet-protocol.c \ + meego-tablet-server-protocol.h CLEANFILES = $(BUILT_SOURCES) diff --git a/compositor/compositor.c b/compositor/compositor.c index 1d2fed33..9df01418 100644 --- a/compositor/compositor.c +++ b/compositor/compositor.c @@ -42,7 +42,9 @@ static const char *option_socket_name = NULL; static const char *option_background = "background.jpg"; static const char *option_geometry = "1024x640"; +static int option_idle_time = 5; static int option_connector = 0; +static int option_meego = 0; static const GOptionEntry option_entries[] = { { "background", 'b', 0, G_OPTION_ARG_STRING, @@ -53,6 +55,10 @@ static const GOptionEntry option_entries[] = { &option_geometry, "Geometry" }, { "socket", 's', 0, G_OPTION_ARG_STRING, &option_socket_name, "Socket Name" }, + { "idle-time", 'i', 0, G_OPTION_ARG_INT, + &option_idle_time, "Screensaver idle time" }, + { "meego", 0, 0, G_OPTION_ARG_NONE, + &option_meego, "Launch the meego tablet shell" }, { NULL } }; @@ -120,6 +126,47 @@ wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v) *v = t; } +void +wlsc_tweener_init(struct wlsc_tweener *tweener, + double k, double current, double target) +{ + tweener->k = k; + tweener->current = current; + tweener->previous = current; + tweener->target = target; +} + +void +wlsc_tweener_update(struct wlsc_tweener *tweener, uint32_t msec) +{ + double force, current; + + current = tweener->current; + force = tweener->k * (tweener->target - current) / 10.0 + + (tweener->previous - current); + + tweener->current = + current + (current - tweener->previous) + force; + tweener->previous = current; + + if (tweener->current >= 1.0) { + tweener->current = 1.0; + tweener->previous = 1.0; + } + + if (tweener->current <= 0.0) { + tweener->current = 0.0; + tweener->previous = 0.0; + } +} + +int +wlsc_tweener_done(struct wlsc_tweener *tweener) +{ + return fabs(tweener->previous - tweener->target) < 0.0002 && + fabs(tweener->current - tweener->target) < 0.0002; +} + struct wlsc_surface * wlsc_surface_create(struct wlsc_compositor *compositor, int32_t x, int32_t y, int32_t width, int32_t height) @@ -532,10 +579,20 @@ wlsc_surface_update_matrix(struct wlsc_surface *es) } void -wlsc_output_finish_frame(struct wlsc_output *output, int msecs) +wlsc_compositor_damage_all(struct wlsc_compositor *compositor) +{ + struct wlsc_output *output; + + wl_list_for_each(output, &compositor->output_list, link) + wlsc_output_damage(output); +} + +void +wlsc_output_finish_frame(struct wlsc_output *output, uint32_t msecs) { struct wlsc_compositor *compositor = output->compositor; struct wlsc_surface *es; + struct wlsc_animation *animation, *next; wl_list_for_each(es, &compositor->surface_list, link) { if (es->output == output) { @@ -548,6 +605,63 @@ wlsc_output_finish_frame(struct wlsc_output *output, int msecs) wl_event_source_timer_update(compositor->timer_source, 5); compositor->repaint_on_timeout = 1; + + wl_list_for_each_safe(animation, next, + &compositor->animation_list, link) + animation->frame(animation, output, msecs); +} + +void +wlsc_output_damage(struct wlsc_output *output) +{ + struct wlsc_compositor *compositor = output->compositor; + + pixman_region32_union_rect(&compositor->damage_region, + &compositor->damage_region, + output->x, output->y, + output->width, output->height); + wlsc_compositor_schedule_repaint(compositor); +} + +static void +tint_frame(struct wlsc_animation *animation, + struct wlsc_output *output, uint32_t msecs) +{ + struct wlsc_compositor *compositor = + container_of(animation, + struct wlsc_compositor, tint_animation); + + wlsc_tweener_update(&compositor->tint, msecs); + if (wlsc_tweener_done(&compositor->tint)) { + if (compositor->tint.current > 0.999 && + compositor->tablet_shell) + compositor->tablet_shell->lock(compositor); + wl_list_remove(&animation->link); + wl_list_init(&animation->link); + } else + wlsc_output_damage(output); +} + +static void +tint_output(struct wlsc_output *output, + GLfloat tint, pixman_region32_t *region) +{ + struct wlsc_compositor *compositor = output->compositor; + struct wlsc_surface surface; + GLfloat color[4] = { 0.0, 0.0, 0.0, tint }; + + surface.compositor = compositor; + surface.x = output->x; + surface.y = output->y; + surface.width = output->width; + surface.height = output->height; + surface.visual = &compositor->compositor.premultiplied_argb_visual; + surface.texture = GL_NONE; + glUseProgram(compositor->solid_shader.program); + glUniformMatrix4fv(compositor->solid_shader.proj_uniform, + 1, GL_FALSE, output->matrix.d); + glUniform4fv(compositor->solid_shader.color_uniform, 1, color); + wlsc_surface_draw(&surface, output, region); } static void @@ -563,8 +677,10 @@ wlsc_output_repaint(struct wlsc_output *output) glViewport(0, 0, output->width, output->height); - glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, output->matrix.d); - glUniform1i(ec->tex_uniform, 0); + glUseProgram(ec->texture_shader.program); + glUniformMatrix4fv(ec->texture_shader.proj_uniform, + 1, GL_FALSE, output->matrix.d); + glUniform1i(ec->texture_shader.tex_uniform, 0); pixman_region32_init(&new_damage); pixman_region32_init(&total_damage); @@ -581,10 +697,13 @@ wlsc_output_repaint(struct wlsc_output *output) if (ec->focus) if (output->set_hardware_cursor(output, ec->input_device) < 0) using_hardware_cursor = 0; + if (ec->tint.current > 0.001) + using_hardware_cursor = 0; es = container_of(ec->surface_list.next, struct wlsc_surface, link); if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN && es->fullscreen_output == output) { + fprintf(stderr, "fullscreen\n"); if (es->visual == &ec->compositor.rgb_visual && using_hardware_cursor) { if (output->prepare_scanout_surface(output, es) == 0) { @@ -602,6 +721,7 @@ wlsc_output_repaint(struct wlsc_output *output) glClear(GL_COLOR_BUFFER_BIT); wlsc_surface_draw(es, output, &total_damage); } else { +#if 0 wl_list_for_each(es, &ec->surface_list, link) { if (es->visual != &ec->compositor.rgb_visual) continue; @@ -613,17 +733,18 @@ wlsc_output_repaint(struct wlsc_output *output) pixman_region32_subtract(&total_damage, &total_damage, &repaint); } - if (output->background) wlsc_surface_draw(output->background, output, &total_damage); else glClear(GL_COLOR_BUFFER_BIT); +#endif wl_list_for_each_reverse(es, &ec->surface_list, link) { if (ec->overlay == es) continue; +#if 0 if (es->visual == &ec->compositor.rgb_visual) { pixman_region32_union_rect(&total_damage, &total_damage, @@ -631,11 +752,13 @@ wlsc_output_repaint(struct wlsc_output *output) es->width, es->height); continue; } +#endif wlsc_surface_draw(es, output, &total_damage); } } + glUseProgram(ec->texture_shader.program); if (ec->overlay) wlsc_surface_draw(ec->overlay, output, &total_damage); @@ -646,6 +769,9 @@ wlsc_output_repaint(struct wlsc_output *output) wlsc_surface_draw(eid->sprite, output, &total_damage); } + + if (ec->tint.current > 0.001) + tint_output(output, ec->tint.current, &total_damage); } static void @@ -691,6 +817,16 @@ wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor) compositor->repaint_on_timeout = 1; } +void +wlsc_compositor_set_tint(struct wlsc_compositor *compositor, float tint) +{ + compositor->tint.target = tint; + wlsc_compositor_damage_all(compositor); + if (wl_list_empty(&compositor->tint_animation.link)) + wl_list_insert(compositor->animation_list.prev, + &compositor->tint_animation.link); +} + static void surface_destroy(struct wl_client *client, struct wl_surface *surface) @@ -747,6 +883,9 @@ surface_attach(struct wl_client *client, if (x != 0 || y != 0) wlsc_surface_assign_output(es); wlsc_surface_update_matrix(es); + + if (es->compositor->tablet_shell) + es->compositor->tablet_shell->attach(es); } static void @@ -1014,6 +1153,46 @@ static const struct wl_grab_interface motion_grab_interface = { motion_grab_end }; +void +wlsc_compositor_wake(struct wlsc_compositor *compositor) +{ + if (compositor->idle_inhibit) + return; + + if (compositor->state == WLSC_COMPOSITOR_SLEEPING) + wlsc_compositor_set_tint(compositor, 0.0); + + compositor->state = WLSC_COMPOSITOR_ACTIVE; + wl_event_source_timer_update(compositor->idle_source, + option_idle_time * 1000); +} + +static void +wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor) +{ + wlsc_compositor_wake(compositor); + compositor->idle_inhibit++; +} + +static void +wlsc_compositor_idle_release(struct wlsc_compositor *compositor) +{ + compositor->idle_inhibit--; + wlsc_compositor_wake(compositor); +} + +static void +idle_handler(void *data) +{ + struct wlsc_compositor *compositor = data; + + if (compositor->idle_inhibit) + return; + + compositor->state = WLSC_COMPOSITOR_SLEEPING; + wlsc_compositor_set_tint(compositor, 1); +} + void notify_motion(struct wl_input_device *device, uint32_t time, int x, int y) { @@ -1027,6 +1206,8 @@ notify_motion(struct wl_input_device *device, uint32_t time, int x, int y) int x_valid = 0, y_valid = 0; int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN; + wlsc_compositor_wake(ec); + wl_list_for_each(output, &ec->output_list, link) { if (output->x <= x && x <= output->x + output->width) x_valid = 1; @@ -1120,6 +1301,11 @@ notify_button(struct wl_input_device *device, struct wlsc_surface *surface = (struct wlsc_surface *) device->pointer_focus; + if (state) + wlsc_compositor_idle_inhibit(compositor); + else + wlsc_compositor_idle_release(compositor); + if (state && surface && device->grab == NULL) { wlsc_surface_activate(surface, wd, time); wl_input_device_start_grab(device, @@ -1150,7 +1336,8 @@ terminate_binding(struct wl_input_device *device, uint32_t time, { struct wlsc_compositor *compositor = data; - wl_display_terminate(compositor->wl_display); + if (state) + wl_display_terminate(compositor->wl_display); } struct wlsc_binding * @@ -1224,9 +1411,14 @@ notify_key(struct wl_input_device *device, uint32_t *k, *end; struct wlsc_binding *b; + if (state) + wlsc_compositor_idle_inhibit(compositor); + else + wlsc_compositor_idle_release(compositor); + wl_list_for_each(b, &compositor->binding_list, link) { if (b->key == key && - b->modifier == wd->modifier_state && state) { + b->modifier == wd->modifier_state) { b->handler(&wd->input_device, time, key, 0, state, b->data); break; @@ -1307,12 +1499,17 @@ notify_keyboard_focus(struct wl_input_device *device, wd->modifier_state = 0; end = device->keys.data + device->keys.size; for (k = device->keys.data; k < end; k++) { + wlsc_compositor_idle_inhibit(compositor); update_modifier_state(wd, *k, 1); } wl_input_device_set_keyboard_focus(&wd->input_device, &es->surface, time); } else { + end = device->keys.data + device->keys.size; + for (k = device->keys.data; k < end; k++) + wlsc_compositor_idle_release(compositor); + wd->modifier_state = 0; wl_input_device_set_keyboard_focus(&wd->input_device, NULL, time); @@ -1399,7 +1596,7 @@ static const char vertex_shader[] = " v_texcoord = texcoord;\n" "}\n"; -static const char fragment_shader[] = +static const char texture_fragment_shader[] = "precision mediump float;\n" "varying vec2 v_texcoord;\n" "uniform sampler2D tex;\n" @@ -1408,53 +1605,103 @@ static const char fragment_shader[] = " gl_FragColor = texture2D(tex, v_texcoord)\n;" "}\n"; +static const char solid_fragment_shader[] = + "precision mediump float;\n" + "varying vec2 v_texcoord;\n" + "uniform vec4 color;\n" + "void main()\n" + "{\n" + " gl_FragColor = color\n;" + "}\n"; + static int -init_shaders(struct wlsc_compositor *ec) +compile_shader(GLenum type, const char *source) { - GLuint v, f, program; - const char *p; + GLuint s; char msg[512]; GLint status; - p = vertex_shader; - v = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(v, 1, &p, NULL); - glCompileShader(v); - glGetShaderiv(v, GL_COMPILE_STATUS, &status); + s = glCreateShader(type); + glShaderSource(s, 1, &source, NULL); + glCompileShader(s); + glGetShaderiv(s, GL_COMPILE_STATUS, &status); if (!status) { - glGetShaderInfoLog(v, sizeof msg, NULL, msg); - fprintf(stderr, "vertex shader info: %s\n", msg); - return -1; + glGetShaderInfoLog(s, sizeof msg, NULL, msg); + fprintf(stderr, "shader info: %s\n", msg); + return GL_NONE; } - p = fragment_shader; - f = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(f, 1, &p, NULL); - glCompileShader(f); - glGetShaderiv(f, GL_COMPILE_STATUS, &status); + return s; +} + +static int +wlsc_shader_init(struct wlsc_shader *shader, + const char *vertex_source, const char *fragment_source) +{ + char msg[512]; + GLint status; + + shader->vertex_shader = + compile_shader(GL_VERTEX_SHADER, vertex_source); + shader->fragment_shader = + compile_shader(GL_FRAGMENT_SHADER, fragment_source); + + shader->program = glCreateProgram(); + glAttachShader(shader->program, shader->vertex_shader); + glAttachShader(shader->program, shader->fragment_shader); + glBindAttribLocation(shader->program, 0, "position"); + glBindAttribLocation(shader->program, 1, "texcoord"); + + glLinkProgram(shader->program); + glGetProgramiv(shader->program, GL_LINK_STATUS, &status); if (!status) { - glGetShaderInfoLog(f, sizeof msg, NULL, msg); - fprintf(stderr, "fragment shader info: %s\n", msg); + glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg); + fprintf(stderr, "link info: %s\n", msg); return -1; } - program = glCreateProgram(); - glAttachShader(program, v); - glAttachShader(program, f); - glBindAttribLocation(program, 0, "position"); - glBindAttribLocation(program, 1, "texcoord"); + shader->proj_uniform = glGetUniformLocation(shader->program, "proj"); + shader->tex_uniform = glGetUniformLocation(shader->program, "tex"); - glLinkProgram(program); - glGetProgramiv(program, GL_LINK_STATUS, &status); + return 0; +} + +static void +init_solid_shader(struct wlsc_shader *shader, + GLuint vertex_shader, const char *fragment_source) +{ + GLint status; + char msg[512]; + + shader->vertex_shader = vertex_shader; + shader->fragment_shader = + compile_shader(GL_FRAGMENT_SHADER, fragment_source); + + shader->program = glCreateProgram(); + glAttachShader(shader->program, shader->vertex_shader); + glAttachShader(shader->program, shader->fragment_shader); + glBindAttribLocation(shader->program, 0, "position"); + glBindAttribLocation(shader->program, 1, "texcoord"); + + glLinkProgram(shader->program); + glGetProgramiv(shader->program, GL_LINK_STATUS, &status); if (!status) { - glGetProgramInfoLog(program, sizeof msg, NULL, msg); + glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg); fprintf(stderr, "link info: %s\n", msg); - return -1; } - glUseProgram(program); - ec->proj_uniform = glGetUniformLocation(program, "proj"); - ec->tex_uniform = glGetUniformLocation(program, "tex"); + shader->proj_uniform = glGetUniformLocation(shader->program, "proj"); + shader->color_uniform = glGetUniformLocation(shader->program, "color"); +} + +static int +init_shaders(struct wlsc_compositor *ec) +{ + wlsc_shader_init(&ec->texture_shader, + vertex_shader, texture_fragment_shader); + init_solid_shader(&ec->solid_shader, + ec->texture_shader.vertex_shader, + solid_fragment_shader); return 0; } @@ -1537,6 +1784,10 @@ wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display) wl_list_init(&ec->input_device_list); wl_list_init(&ec->output_list); wl_list_init(&ec->binding_list); + wl_list_init(&ec->animation_list); + wlsc_tweener_init(&ec->tint, 0.8, 0.0, 1.0); + ec->tint_animation.frame = tint_frame; + wl_list_init(&ec->tint_animation.link); wlsc_shell_init(ec); wlsc_switcher_init(ec); @@ -1561,6 +1812,9 @@ wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display) return -1; loop = wl_display_get_event_loop(ec->wl_display); + ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec); + wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000); + ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec); pixman_region32_init(&ec->damage_region); wlsc_compositor_schedule_repaint(ec); @@ -1636,6 +1890,11 @@ int main(int argc, char *argv[]) wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec); wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec); + if (option_meego) { + printf("Launching the meego tablet shell\n"); + meego_tablet_shell_init(ec); + } + wl_display_run(display); eglUnbindWaylandDisplayWL(ec->display, display); diff --git a/compositor/compositor.h b/compositor/compositor.h index 832ad084..fc387c8b 100644 --- a/compositor/compositor.h +++ b/compositor/compositor.h @@ -94,6 +94,38 @@ struct wlsc_sprite { int height; }; +struct wlsc_tablet_shell { + struct wl_object object; + void (*lock)(struct wlsc_compositor *compositor); + void (*attach)(struct wlsc_surface *surface); +}; + +struct wlsc_shader { + GLuint program; + GLuint vertex_shader, fragment_shader; + GLuint proj_uniform; + GLuint tex_uniform; + GLuint color_uniform; +}; + +struct wlsc_animation { + void (*frame)(struct wlsc_animation *animation, + struct wlsc_output *output, uint32_t msecs); + struct wl_list link; +}; + +struct wlsc_tweener { + double k; + double current; + double target; + double previous; +}; + +enum { + WLSC_COMPOSITOR_ACTIVE, + WLSC_COMPOSITOR_SLEEPING +}; + struct wlsc_compositor { struct wl_compositor compositor; @@ -104,8 +136,10 @@ struct wlsc_compositor { GLuint fbo; GLuint proj_uniform, tex_uniform; struct wlsc_sprite **pointer_sprites; - struct wl_display *wl_display; + struct wlsc_shader texture_shader; + struct wlsc_shader solid_shader; + struct wl_display *wl_display; /* We implement the shell interface. */ struct wl_shell shell; @@ -116,6 +150,13 @@ struct wlsc_compositor { struct wl_list input_device_list; struct wl_list surface_list; struct wl_list binding_list; + struct wl_list animation_list; + struct wlsc_tweener tint; + struct wlsc_animation tint_animation; + + uint32_t state; + struct wl_event_source *idle_source; + uint32_t idle_inhibit; /* Repaint state. */ struct wl_event_source *timer_source; @@ -128,6 +169,8 @@ struct wlsc_compositor { struct wlsc_switcher *switcher; uint32_t focus; + struct wlsc_tablet_shell *tablet_shell; + void (*destroy)(struct wlsc_compositor *ec); int (*authenticate)(struct wlsc_compositor *c, uint32_t id); EGLImageKHR (*create_cursor_image)(struct wlsc_compositor *c, @@ -150,7 +193,9 @@ enum wlsc_surface_map_type { WLSC_SURFACE_MAP_UNMAPPED, WLSC_SURFACE_MAP_TOPLEVEL, WLSC_SURFACE_MAP_TRANSIENT, - WLSC_SURFACE_MAP_FULLSCREEN + WLSC_SURFACE_MAP_FULLSCREEN, + + WLSC_SURFACE_MAP_SWITCHER }; struct wlsc_surface { @@ -171,6 +216,14 @@ struct wlsc_surface { EGLImageKHR image; }; +void +wlsc_tweener_init(struct wlsc_tweener *tweener, + double k, double current, double target); +void +wlsc_tweener_update(struct wlsc_tweener *tweener, uint32_t msec); +int +wlsc_tweener_done(struct wlsc_tweener *tweener); + void wlsc_surface_update_matrix(struct wlsc_surface *es); @@ -200,9 +253,23 @@ notify_keyboard_focus(struct wl_input_device *device, struct wl_array *keys); void -wlsc_output_finish_frame(struct wlsc_output *output, int msecs); +wlsc_output_finish_frame(struct wlsc_output *output, uint32_t msecs); +void +wlsc_output_damage(struct wlsc_output *output); void wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor); +void +wlsc_compositor_set_tint(struct wlsc_compositor *compositor, float tint); +void +wlsc_compositor_damage_all(struct wlsc_compositor *compositor); +void +wlsc_compositor_unlock(struct wlsc_compositor *compositor); +void +wlsc_compositor_wake(struct wlsc_compositor *compositor); + +void +wlsc_surface_activate(struct wlsc_surface *surface, + struct wlsc_input_device *device, uint32_t time); struct wlsc_binding; typedef void (*wlsc_binding_handler_t)(struct wl_input_device *device, @@ -270,6 +337,9 @@ wlsc_shm_init(struct wlsc_compositor *ec); int wlsc_shell_init(struct wlsc_compositor *ec); +void +meego_tablet_shell_init(struct wlsc_compositor *compositor); + void wlsc_switcher_init(struct wlsc_compositor *compositor); diff --git a/compositor/meego-tablet-shell.c b/compositor/meego-tablet-shell.c new file mode 100644 index 00000000..36505d3c --- /dev/null +++ b/compositor/meego-tablet-shell.c @@ -0,0 +1,520 @@ +/* + * Copyright © 2011 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "compositor.h" +#include "meego-tablet-server-protocol.h" + +/* + * TODO: Don't fade back from black until we've received a lockscreen + * attachment. + */ + +struct meego_tablet_shell { + struct wlsc_tablet_shell shell; + + struct wlsc_compositor *compositor; + struct wl_event_source *sigchld_source; + pid_t pid; + struct wlsc_input_device *device; + struct wl_client *client; + struct wlsc_animation animation; + struct wlsc_tweener scale; + int width, height; + + struct wlsc_surface *surface; + + struct wlsc_surface *lockscreen_surface; + struct wl_listener lockscreen_listener; + + struct wlsc_surface *home_surface; + + struct wlsc_surface *switcher_surface; + struct wl_listener switcher_listener; + + /* FIXME: Need a state machine here. */ + int starting, locked; + int home_active; + int task_switcher_active; + int long_press_active; + struct wl_event_source *long_press_source; +}; + +static void +sigchld_handler(int signal_number, void *data) +{ + struct meego_tablet_shell *shell = data; + int status; + + wait(&status); + shell->pid = 0; + + fprintf(stderr, "meego-ux-daemon crashed, exit code %d\n", status); +} + +static void +tablet_shell_frame(struct wlsc_animation *animation, + struct wlsc_output *output, uint32_t msecs) +{ + struct meego_tablet_shell *shell = + container_of(animation, + struct meego_tablet_shell, animation); + struct wlsc_surface *es = shell->surface; + float scale; + + scale = shell->scale.current; + wlsc_tweener_update(&shell->scale, msecs); + + if (wlsc_tweener_done(&shell->scale)) { + wl_list_remove(&animation->link); + shell->surface = NULL; + fprintf(stderr, "animation done\n"); + } + + es->width = scale * shell->width; + es->height = scale * shell->height; + es->x = (output->width - es->width) / 2; + es->y = (output->height - es->height) / 2; + wlsc_surface_update_matrix(es); + wlsc_surface_damage(es); +} + +static void +meego_tablet_shell_attach(struct wlsc_surface *surface) +{ + struct meego_tablet_shell *shell = + (struct meego_tablet_shell *) + surface->compositor->tablet_shell; + struct wlsc_compositor *ec = shell->compositor; + const char *visual; + + if (surface == shell->lockscreen_surface) { + if (shell->starting) { + wlsc_compositor_set_tint(shell->compositor, 0.0); + wlsc_compositor_wake(shell->compositor); + wlsc_surface_damage(surface); + shell->starting = 0; + } + } else if (surface == shell->switcher_surface) { + + if (surface->visual == &ec->compositor.argb_visual) { + visual = "argb"; + } else if (surface->visual == &ec->compositor.premultiplied_argb_visual) { + visual = "premultiplied argb"; + } else { + visual = "rgb"; + } + + fprintf(stderr, "switcher attach, size %dx%d, visual %s\n", + shell->switcher_surface->width, + shell->switcher_surface->height, visual); + } else if (surface == shell->home_surface) { + surface->x = 0; + surface->y = 0; + wlsc_surface_update_matrix(surface); + } else { + /* FIXME: Fullscreen everything else. */ + fprintf(stderr, "attach from surface %p, client %p\n", + surface, surface->surface.client); + if (shell->surface == NULL) { + fprintf(stderr, "starting animation for surface %p\n", + surface); + + shell->width = surface->width; + shell->height = surface->height; + wlsc_tweener_init(&shell->scale, 3.0, 0.3, 1.0); + shell->animation.frame = tablet_shell_frame; + wl_list_insert(ec->animation_list.prev, + &shell->animation.link); + shell->surface = surface; + } + } +} + +static void +handle_lockscreen_surface_destroy(struct wl_listener *listener, + struct wl_surface *surface, uint32_t time) +{ + struct meego_tablet_shell *shell = + container_of(listener, + struct meego_tablet_shell, lockscreen_listener); + + shell->lockscreen_surface = NULL; + shell->locked = 0; +} + +static void +tablet_shell_set_lockscreen(struct wl_client *client, + struct meego_tablet_shell *shell, + struct wl_surface *surface) +{ + struct wlsc_surface *es = (struct wlsc_surface *) surface; + struct wlsc_input_device *device = + (struct wlsc_input_device *) shell->compositor->input_device; + + fprintf(stderr, "set lock screen\n"); + es->x = 0; + es->y = 0; + wlsc_surface_update_matrix(es); + wlsc_surface_activate(es, device, get_time()); + shell->lockscreen_surface = es; + + shell->lockscreen_listener.func = handle_lockscreen_surface_destroy; + wl_list_insert(es->surface.destroy_listener_list.prev, + &shell->lockscreen_listener.link); +} + +static void +handle_switcher_surface_destroy(struct wl_listener *listener, + struct wl_surface *surface, uint32_t time) +{ + struct meego_tablet_shell *shell = + container_of(listener, + struct meego_tablet_shell, switcher_listener); + + shell->switcher_surface = NULL; + shell->task_switcher_active = 0; +} + +static void +tablet_shell_set_switcher(struct wl_client *client, + struct meego_tablet_shell *shell, + struct wl_surface *surface) +{ + struct wlsc_input_device *device = + (struct wlsc_input_device *) shell->compositor->input_device; + struct wlsc_surface *es = (struct wlsc_surface *) surface; + + /* FIXME: Switcher should be centered and the compositor + * should do the tinting of the background. With the cache + * layer idea, we should be able to hit the framerate on the + * fade/zoom in. */ + shell->switcher_surface = es; + shell->switcher_surface->x = 0; + shell->switcher_surface->y = 0; + wlsc_surface_update_matrix(es); + + wlsc_surface_activate(es, device, get_time()); + + fprintf(stderr, "set switcher %p, size %dx%d\n", + es, es->width, es->height); + + shell->switcher_listener.func = handle_switcher_surface_destroy; + wl_list_insert(es->surface.destroy_listener_list.prev, + &shell->switcher_listener.link); +} + +static void +tablet_shell_set_homescreen(struct wl_client *client, + struct meego_tablet_shell *shell, + struct wl_surface *surface) +{ + struct wlsc_input_device *device = + (struct wlsc_input_device *) shell->compositor->input_device; + + shell->home_surface = (struct wlsc_surface *) surface; + shell->home_surface->x = 0; + shell->home_surface->y = 0; + wlsc_surface_update_matrix(shell->home_surface); + + wlsc_surface_activate(shell->home_surface, device, get_time()); + fprintf(stderr, "set home screen\n"); +} + +struct meego_tablet_client { + struct wl_resource resource; + struct meego_tablet_shell *shell; + struct wl_client *client; + char *name; +}; + +static void +destroy_tablet_client(struct wl_resource *resource, struct wl_client *client) +{ + struct meego_tablet_client *tablet_client = + container_of(resource, struct meego_tablet_client, resource); + + free(tablet_client->name); + free(tablet_client); +} + +static void +tablet_client_poke(struct wl_client *client, + struct meego_tablet_client *tablet_client) +{ + struct wlsc_compositor *compositor = tablet_client->shell->compositor; + struct wlsc_surface *surface; + struct wlsc_input_device *device = + (struct wlsc_input_device *) compositor->input_device; + + wl_list_for_each(surface, &compositor->surface_list, link) { + if (surface->surface.client == tablet_client->client) { + wlsc_surface_activate(surface, device, get_time()); + break; + } + } + + fprintf(stderr, "poke client %s\n", tablet_client->name); +} + +static const struct meego_tablet_client_interface tablet_client_interface = { + tablet_client_poke +}; + +static void +tablet_shell_create_client(struct wl_client *client, + struct meego_tablet_shell *shell, + uint32_t id, const char *name, int fd) +{ + struct wlsc_compositor *compositor = shell->compositor; + struct meego_tablet_client *tablet_client; + + tablet_client = malloc(sizeof *tablet_client); + if (tablet_client == NULL) { + wl_client_post_no_memory(client); + return; + } + + tablet_client->client = wl_client_create(compositor->wl_display, fd); + tablet_client->shell = shell; + tablet_client->name = strdup(name); + + tablet_client->resource.destroy = destroy_tablet_client; + tablet_client->resource.object.id = id; + tablet_client->resource.object.interface = + &meego_tablet_client_interface; + tablet_client->resource.object.implementation = + (void (**)(void)) &tablet_client_interface; + + wl_client_add_resource(client, &tablet_client->resource); + + fprintf(stderr, "created client %p, id %d, name %s, fd %d\n", + tablet_client->client, id, name, fd); +} + +static const struct meego_tablet_shell_interface tablet_shell_interface = { + tablet_shell_set_lockscreen, + tablet_shell_set_switcher, + tablet_shell_set_homescreen, + tablet_shell_create_client +}; + +static void +launch_switcher(struct meego_tablet_shell *shell) +{ + struct wlsc_compositor *compositor = shell->compositor; + char s[32]; + int sv[2], flags; + + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) { + fprintf(stderr, "socketpair failed\n"); + return; + } + + /* SOCK_CLOEXEC closes both ends, so we need to unset the flag + * on the client fd. */ + flags = fcntl(sv[1], F_GETFD); + if (flags != -1) + fcntl(sv[1], F_SETFD, flags & ~FD_CLOEXEC); + + shell->pid = fork(); + switch (shell->pid) { + case 0: + snprintf(s, sizeof s, "%d", sv[1]); + setenv("WAYLAND_SOCKET", s, 1); + setenv("EGL_PLATFORM", "wayland", 1); + setenv("QT_QPA_PLATFORM", "waylandgl", 1); + if (execl("/usr/libexec/meego-ux-daemon", + "/usr/libexec/meego-ux-daemon", NULL) < 0) + fprintf(stderr, "exec failed: %m\n"); + exit(-1); + + default: + close(sv[1]); + shell->client = + wl_client_create(compositor->wl_display, sv[0]); + break; + + case -1: + fprintf(stderr, "failed to fork\n"); + break; + } +} + +static void +toggle_switcher(struct meego_tablet_shell *shell) +{ + struct wlsc_surface *surface = shell->switcher_surface; + + if (surface) { + wl_client_post_event(shell->client, &shell->shell.object, + MEEGO_TABLET_SHELL_HIDE_SWITCHER); + printf("hide switcher\n"); + /* cleanupTaskSwitcher(); */ + } else { + shell->task_switcher_active = 1; + wl_client_post_event(shell->client, &shell->shell.object, + MEEGO_TABLET_SHELL_SHOW_SWITCHER); + } +} + +static void +meego_tablet_shell_lock(struct wlsc_compositor *compositor) +{ + struct meego_tablet_shell *shell = + (struct meego_tablet_shell *) compositor->tablet_shell; + + if (!shell->locked) + wl_client_post_event(shell->client, &shell->shell.object, + MEEGO_TABLET_SHELL_SHOW_LOCKSCREEN); + shell->locked = 1; +} + +static void +go_home(struct meego_tablet_shell *shell) +{ + struct wlsc_input_device *device = + (struct wlsc_input_device *) shell->compositor->input_device; + + if (shell->task_switcher_active) + wl_client_post_event(shell->client, &shell->shell.object, + MEEGO_TABLET_SHELL_HIDE_SWITCHER); + + wlsc_surface_activate(shell->home_surface, device, get_time()); + printf("go home\n"); +} + +static void +long_press_handler(void *data) +{ + struct meego_tablet_shell *shell = data; + + shell->long_press_active = 0; + toggle_switcher(shell); +} + +static void +menu_key_binding(struct wl_input_device *device, uint32_t time, + uint32_t key, uint32_t button, uint32_t state, void *data) +{ + struct meego_tablet_shell *shell = data; + + if (shell->locked) + return; + + if (state) + toggle_switcher(shell); +} + +static void +home_key_binding(struct wl_input_device *device, uint32_t time, + uint32_t key, uint32_t button, uint32_t state, void *data) +{ + struct meego_tablet_shell *shell = data; + + if (shell->locked) + return; + + shell->device = (struct wlsc_input_device *) device; + + if (state) { + wl_event_source_timer_update(shell->long_press_source, 500); + shell->long_press_active = 1; + } else if (shell->long_press_active) { + wl_event_source_timer_update(shell->long_press_source, 0); + shell->long_press_active = 0; + + if (shell->home_active || shell->task_switcher_active) + toggle_switcher(shell); + else + go_home(shell); + } +} + +static void +tablet_shell_bind(struct wl_client *client, + struct wl_object *object, uint32_t version) +{ + struct meego_tablet_shell *shell = + (struct meego_tablet_shell *) object; + + wl_client_post_event(shell->client, &shell->shell.object, + MEEGO_TABLET_SHELL_SHOW_LOCKSCREEN); +} + +void +meego_tablet_shell_init(struct wlsc_compositor *compositor) +{ + struct meego_tablet_shell *shell; + struct wl_event_loop *loop; + + shell = malloc(sizeof *shell); + if (shell == NULL) + return; + + memset(shell, 0, sizeof *shell); + shell->compositor = compositor; + shell->locked = 1; + + shell->shell.object.interface = &meego_tablet_shell_interface; + shell->shell.object.implementation = + (void (**)(void)) &tablet_shell_interface; + wl_display_add_object(compositor->wl_display, &shell->shell.object); + + /* FIXME: This will make the object available to all clients. */ + wl_display_add_global(compositor->wl_display, + &shell->shell.object, tablet_shell_bind); + + loop = wl_display_get_event_loop(compositor->wl_display); + shell->sigchld_source = + wl_event_loop_add_signal(loop, SIGCHLD, + sigchld_handler, shell); + + shell->long_press_source = + wl_event_loop_add_timer(loop, long_press_handler, shell); + + wlsc_compositor_add_binding(compositor, KEY_SPACE, 0, MODIFIER_CTRL, + home_key_binding, shell); + wlsc_compositor_add_binding(compositor, KEY_ENTER, 0, MODIFIER_CTRL, + menu_key_binding, shell); + + compositor->tablet_shell = &shell->shell; + + shell->shell.lock = meego_tablet_shell_lock; + shell->shell.attach = meego_tablet_shell_attach; + + launch_switcher(shell); + + compositor->tint.current = 1; + compositor->tint.target = 1; + compositor->tint.previous = 1; + shell->starting = 1; +} diff --git a/protocol/meego-tablet.xml b/protocol/meego-tablet.xml new file mode 100644 index 00000000..9815b889 --- /dev/null +++ b/protocol/meego-tablet.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3