clients: support ivi-application.xml for clients/window.c
[profile/ivi/weston-ivi-shell.git] / xwayland / window-manager.c
index 4bce02d..4e91f9d 100644 (file)
@@ -38,7 +38,6 @@
 
 #include "cairo-util.h"
 #include "compositor.h"
-#include "xserver-server-protocol.h"
 #include "hash.h"
 
 struct wm_size_hints {
@@ -122,6 +121,7 @@ struct weston_wm_window {
        xcb_window_t frame_id;
        struct frame *frame;
        cairo_surface_t *cairo_surface;
+       uint32_t surface_id;
        struct weston_surface *surface;
        struct shell_surface *shsurf;
        struct weston_view *view;
@@ -146,6 +146,7 @@ struct weston_wm_window {
        int delete_window;
        struct wm_size_hints size_hints;
        struct motif_wm_hints motif_hints;
+       struct wl_list link;
 };
 
 static struct weston_wm_window *
@@ -154,6 +155,10 @@ get_wm_window(struct weston_surface *surface);
 static void
 weston_wm_window_schedule_repaint(struct weston_wm_window *window);
 
+static void
+xserver_map_shell_surface(struct weston_wm_window *window,
+                         struct weston_surface *surface);
+
 static int __attribute__ ((format (printf, 1, 2)))
 wm_log(const char *fmt, ...)
 {
@@ -661,6 +666,28 @@ weston_wm_kill_client(struct wl_listener *listener, void *data)
 }
 
 static void
+weston_wm_create_surface(struct wl_listener *listener, void *data)
+{
+       struct weston_surface *surface = data;
+       struct weston_wm *wm =
+               container_of(listener,
+                            struct weston_wm, create_surface_listener);
+       struct weston_wm_window *window;
+
+       if (wl_resource_get_client(surface->resource) != wm->server->client)
+               return;
+
+       wl_list_for_each(window, &wm->unpaired_window_list, link)
+               if (window->surface_id ==
+                   wl_resource_get_id(surface->resource)) {
+                       xserver_map_shell_surface(window, surface);
+                       window->surface_id = 0;
+                       wl_list_remove(&window->link);
+                       break;
+               }       
+}
+
+static void
 weston_wm_window_activate(struct wl_listener *listener, void *data)
 {
        struct weston_surface *surface = data;
@@ -674,6 +701,9 @@ weston_wm_window_activate(struct wl_listener *listener, void *data)
        }
 
        if (window) {
+               if (window->override_redirect)
+                       return;
+
                client_message.response_type = XCB_CLIENT_MESSAGE;
                client_message.format = 32;
                client_message.window = window->id;
@@ -917,7 +947,8 @@ weston_wm_window_draw_decoration(void *data)
        cairo_t *cr;
        int x, y, width, height;
        int32_t input_x, input_y, input_w, input_h;
-
+       struct weston_shell_interface *shell_interface =
+               &wm->server->compositor->shell_interface;
        uint32_t flags = 0;
 
        weston_wm_window_read_properties(window);
@@ -979,6 +1010,9 @@ weston_wm_window_draw_decoration(void *data)
 
                pixman_region32_init_rect(&window->surface->pending.input,
                                          input_x, input_y, input_w, input_h);
+
+               shell_interface->set_window_geometry(window->shsurf,
+                                                    input_x, input_y, input_w, input_h);
        }
 }
 
@@ -1095,6 +1129,9 @@ weston_wm_window_destroy(struct weston_wm_window *window)
                window->frame_id = XCB_WINDOW_NONE;
        }
 
+       if (window->surface_id)
+               wl_list_remove(&window->link);
+
        hash_table_remove(window->wm->window_hash, window->id);
        free(window);
 }
@@ -1169,6 +1206,26 @@ weston_wm_pick_seat(struct weston_wm *wm)
                            struct weston_seat, link);
 }
 
+static struct weston_seat *
+weston_wm_pick_seat_for_window(struct weston_wm_window *window)
+{
+       struct weston_wm *wm = window->wm;
+       struct weston_seat *seat, *s;
+
+       seat = NULL;
+       wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
+               if (s->pointer != NULL &&
+                   s->pointer->focus == window->view &&
+                   s->pointer->button_count > 0 &&
+                   (seat == NULL ||
+                    s->pointer->grab_serial -
+                    seat->pointer->grab_serial < (1 << 30)))
+                       seat = s;
+       }
+
+       return seat;
+}
+
 static void
 weston_wm_window_handle_moveresize(struct weston_wm_window *window,
                                   xcb_client_message_event_t *client_message)
@@ -1185,13 +1242,13 @@ weston_wm_window_handle_moveresize(struct weston_wm_window *window,
        };
 
        struct weston_wm *wm = window->wm;
-       struct weston_seat *seat = weston_wm_pick_seat(wm);
+       struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
        int detail;
        struct weston_shell_interface *shell_interface =
                &wm->server->compositor->shell_interface;
 
-       if (seat->pointer->button_count != 1 || !window->view
-           || seat->pointer->focus != window->view)
+       if (seat == NULL || seat->pointer->button_count != 1
+           || !window->view || seat->pointer->focus != window->view)
                return;
 
        detail = client_message->data.data32[2];
@@ -1283,6 +1340,54 @@ weston_wm_window_handle_state(struct weston_wm_window *window,
 }
 
 static void
+surface_destroy(struct wl_listener *listener, void *data)
+{
+       struct weston_wm_window *window =
+               container_of(listener,
+                            struct weston_wm_window, surface_destroy_listener);
+
+       wm_log("surface for xid %d destroyed\n", window->id);
+
+       /* This should have been freed by the shell.
+        * Don't try to use it later. */
+       window->shsurf = NULL;
+       window->surface = NULL;
+       window->view = NULL;
+}
+
+static void
+weston_wm_window_handle_surface_id(struct weston_wm_window *window,
+                                  xcb_client_message_event_t *client_message)
+{
+       struct weston_wm *wm = window->wm;
+       struct wl_resource *resource;
+
+       if (window->surface_id != 0) {
+               wm_log("already have surface id for window %d\n", window->id);
+               return;
+       }
+
+       /* Xwayland will send the wayland requests to create the
+        * wl_surface before sending this client message.  Even so, we
+        * can end up handling the X event before the wayland requests
+        * and thus when we try to look up the surface ID, the surface
+        * hasn't been created yet.  In that case put the window on
+        * the unpaired window list and continue when the surface gets
+        * created. */
+       uint32_t id = client_message->data.data32[0];
+       resource = wl_client_get_object(wm->server->client, id);
+       if (resource) {
+               window->surface_id = 0;
+               xserver_map_shell_surface(window,
+                                         wl_resource_get_user_data(resource));
+       }
+       else {
+               window->surface_id = id;
+               wl_list_insert(&wm->unpaired_window_list, &window->link);
+       }
+}
+
+static void
 weston_wm_handle_client_message(struct weston_wm *wm,
                                xcb_generic_event_t *event)
 {
@@ -1301,10 +1406,18 @@ weston_wm_handle_client_message(struct weston_wm *wm,
               client_message->data.data32[4],
               client_message->window);
 
+       /* The window may get created and destroyed before we actually
+        * handle the message.  If it doesn't exist, bail.
+        */
+       if (!window)
+               return;
+
        if (client_message->type == wm->atom.net_wm_moveresize)
                weston_wm_window_handle_moveresize(window, client_message);
        else if (client_message->type == wm->atom.net_wm_state)
                weston_wm_window_handle_state(window, client_message);
+       else if (client_message->type == wm->atom.wl_surface_id)
+               weston_wm_window_handle_surface_id(window, client_message);
 }
 
 enum cursor_type {
@@ -1502,7 +1615,7 @@ weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
        xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
        struct weston_shell_interface *shell_interface =
                &wm->server->compositor->shell_interface;
-       struct weston_seat *seat = weston_wm_pick_seat(wm);
+       struct weston_seat *seat;
        struct weston_wm_window *window;
        enum theme_location location;
        enum frame_button_state button_state;
@@ -1519,22 +1632,33 @@ weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
        if (button->detail != 1 && button->detail != 2)
                return;
 
+       seat = weston_wm_pick_seat_for_window(window);
+
        button_state = button->response_type == XCB_BUTTON_PRESS ?
                FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
        button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
 
+       /* Make sure we're looking at the right location.  The frame
+        * could have received a motion event from a pointer from a
+        * different wl_seat, but under X it looks like our core
+        * pointer moved.  Move the frame pointer to the button press
+        * location before deciding what to do. */
+       location = frame_pointer_motion(window->frame, NULL,
+                                       button->event_x, button->event_y);
        location = frame_pointer_button(window->frame, NULL,
                                        button_id, button_state);
        if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
                weston_wm_window_schedule_repaint(window);
 
        if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
-               shell_interface->move(window->shsurf, seat);
+               if (seat != NULL)
+                       shell_interface->move(window->shsurf, seat);
                frame_status_clear(window->frame, FRAME_STATUS_MOVE);
        }
 
        if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
-               shell_interface->resize(window->shsurf, seat, location);
+               if (seat != NULL)
+                       shell_interface->resize(window->shsurf, seat, location);
                frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
        }
 
@@ -1777,7 +1901,8 @@ weston_wm_get_resources(struct weston_wm *wm)
                { "XdndStatus",         F(atom.xdnd_status) },
                { "XdndFinished",       F(atom.xdnd_finished) },
                { "XdndTypeList",       F(atom.xdnd_type_list) },
-               { "XdndActionCopy",     F(atom.xdnd_action_copy) }
+               { "XdndActionCopy",     F(atom.xdnd_action_copy) },
+               { "WL_SURFACE_ID",      F(atom.wl_surface_id) }
        };
 #undef F
 
@@ -1899,13 +2024,12 @@ weston_wm_create_wm_window(struct weston_wm *wm)
 }
 
 struct weston_wm *
-weston_wm_create(struct weston_xserver *wxs)
+weston_wm_create(struct weston_xserver *wxs, int fd)
 {
        struct weston_wm *wm;
        struct wl_event_loop *loop;
        xcb_screen_iterator_t s;
        uint32_t values[1];
-       int sv[2];
        xcb_atom_t supported[3];
 
        wm = zalloc(sizeof *wm);
@@ -1919,22 +2043,11 @@ weston_wm_create(struct weston_xserver *wxs)
                return NULL;
        }
 
-       if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
-               weston_log("socketpair failed\n");
-               hash_table_destroy(wm->window_hash);
-               free(wm);
-               return NULL;
-       }
-
-       xserver_send_client(wxs->resource, sv[1]);
-       wl_client_flush(wl_resource_get_client(wxs->resource));
-       close(sv[1]);
-       
        /* xcb_connect_to_fd takes ownership of the fd. */
-       wm->conn = xcb_connect_to_fd(sv[0], NULL);
+       wm->conn = xcb_connect_to_fd(fd, NULL);
        if (xcb_connection_has_error(wm->conn)) {
                weston_log("xcb_connect_to_fd failed\n");
-               close(sv[0]);
+               close(fd);
                hash_table_destroy(wm->window_hash);
                free(wm);
                return NULL;
@@ -1945,7 +2058,7 @@ weston_wm_create(struct weston_xserver *wxs)
 
        loop = wl_display_get_event_loop(wxs->wl_display);
        wm->source =
-               wl_event_loop_add_fd(loop, sv[0],
+               wl_event_loop_add_fd(loop, fd,
                                     WL_EVENT_READABLE,
                                     weston_wm_handle_event, wm);
        wl_event_source_check(wm->source);
@@ -1965,8 +2078,6 @@ weston_wm_create(struct weston_xserver *wxs)
 
        wm->theme = theme_create();
 
-       weston_wm_create_wm_window(wm);
-
        supported[0] = wm->atom.net_wm_moveresize;
        supported[1] = wm->atom.net_wm_state;
        supported[2] = wm->atom.net_wm_state_fullscreen;
@@ -1984,6 +2095,9 @@ weston_wm_create(struct weston_xserver *wxs)
 
        xcb_flush(wm->conn);
 
+       wm->create_surface_listener.notify = weston_wm_create_surface;
+       wl_signal_add(&wxs->compositor->create_surface_signal,
+                     &wm->create_surface_listener);
        wm->activate_listener.notify = weston_wm_window_activate;
        wl_signal_add(&wxs->compositor->activate_signal,
                      &wm->activate_listener);
@@ -1993,11 +2107,16 @@ weston_wm_create(struct weston_xserver *wxs)
        wm->kill_listener.notify = weston_wm_kill_client;
        wl_signal_add(&wxs->compositor->kill_signal,
                      &wm->kill_listener);
+       wl_list_init(&wm->unpaired_window_list);
 
        weston_wm_create_cursors(wm);
        weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
 
-       weston_log("created wm\n");
+       /* Create wm window and take WM_S0 selection last, which
+        * signals to Xwayland that we're done with setup. */
+       weston_wm_create_wm_window(wm);
+
+       weston_log("created wm, root %d\n", wm->screen->root);
 
        return wm;
 }
@@ -2018,22 +2137,6 @@ weston_wm_destroy(struct weston_wm *wm)
        free(wm);
 }
 
-static void
-surface_destroy(struct wl_listener *listener, void *data)
-{
-       struct weston_wm_window *window =
-               container_of(listener,
-                            struct weston_wm_window, surface_destroy_listener);
-
-       wm_log("surface for xid %d destroyed\n", window->id);
-
-       /* This should have been freed by the shell.
-       Don't try to use it later. */
-       window->shsurf = NULL;
-       window->surface = NULL;
-       window->view = NULL;
-}
-
 static struct weston_wm_window *
 get_wm_window(struct weston_surface *surface)
 {
@@ -2083,23 +2186,19 @@ weston_wm_window_configure(void *data)
 }
 
 static void
-send_configure(struct weston_surface *surface,
-              uint32_t edges, int32_t width, int32_t height)
+send_configure(struct weston_surface *surface, int32_t width, int32_t height)
 {
        struct weston_wm_window *window = get_wm_window(surface);
        struct weston_wm *wm = window->wm;
        struct theme *t = window->wm->theme;
        int vborder, hborder;
 
-       if (window->fullscreen) {
+       if (window->decorate) {
+               hborder = 2 * t->width;
+               vborder = t->titlebar_height + t->width;
+       } else {
                hborder = 0;
                vborder = 0;
-       } else if (window->decorate) {
-               hborder = 2 * (t->margin + t->width);
-               vborder = 2 * t->margin + t->titlebar_height + t->width;
-       } else {
-               hborder = 2 * t->margin;
-               vborder = 2 * t->margin;
        }
 
        if (width > hborder)
@@ -2172,13 +2271,31 @@ legacy_fullscreen(struct weston_wm *wm,
 
        return 0;
 }
+
 static void
-xserver_map_shell_surface(struct weston_wm *wm,
-                         struct weston_wm_window *window)
+xserver_map_shell_surface(struct weston_wm_window *window,
+                         struct weston_surface *surface)
 {
+       struct weston_wm *wm = window->wm;
        struct weston_shell_interface *shell_interface =
                &wm->server->compositor->shell_interface;
        struct weston_output *output;
+       struct weston_wm_window *parent;
+
+       weston_wm_window_read_properties(window);
+
+       /* A weston_wm_window may have many different surfaces assigned
+        * throughout its life, so we must make sure to remove the listener
+        * from the old surface signal list. */
+       if (window->surface)
+               wl_list_remove(&window->surface_destroy_listener.link);
+
+       window->surface = surface;
+       window->surface_destroy_listener.notify = surface_destroy;
+       wl_signal_add(&window->surface->destroy_signal,
+                     &window->surface_destroy_listener);
+
+       weston_wm_window_schedule_repaint(window);
 
        if (!shell_interface->create_shell_surface)
                return;
@@ -2208,55 +2325,18 @@ xserver_map_shell_surface(struct weston_wm *wm,
                shell_interface->set_fullscreen(window->shsurf,
                                                WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
                                                0, output);
-       } else if (!window->override_redirect && !window->transient_for) {
-               shell_interface->set_toplevel(window->shsurf);
-               return;
-       } else {
+       } else if (window->override_redirect) {
                shell_interface->set_xwayland(window->shsurf,
                                              window->x,
                                              window->y,
                                              WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
+       } else if (window->transient_for && window->transient_for->surface) {
+               parent = window->transient_for;
+               shell_interface->set_transient(window->shsurf,
+                                              parent->surface,
+                                              window->x - parent->x,
+                                              window->y - parent->y, 0);
+       } else {
+               shell_interface->set_toplevel(window->shsurf);
        }
 }
-
-static void
-xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
-                     struct wl_resource *surface_resource, uint32_t id)
-{
-       struct weston_xserver *wxs = wl_resource_get_user_data(resource);
-       struct weston_wm *wm = wxs->wm;
-       struct weston_surface *surface =
-               wl_resource_get_user_data(surface_resource);
-       struct weston_wm_window *window;
-
-       if (client != wxs->client)
-               return;
-
-       window = hash_table_lookup(wm->window_hash, id);
-       if (window == NULL) {
-               weston_log("set_window_id for unknown window %d\n", id);
-               return;
-       }
-
-       wm_log("set_window_id %d for surface %p\n", id, surface);
-
-       weston_wm_window_read_properties(window);
-
-       /* A weston_wm_window may have many different surfaces assigned
-        * throughout its life, so we must make sure to remove the listener
-        * from the old surface signal list. */
-       if (window->surface)
-               wl_list_remove(&window->surface_destroy_listener.link);
-
-       window->surface = (struct weston_surface *) surface;
-       window->surface_destroy_listener.notify = surface_destroy;
-       wl_signal_add(&surface->destroy_signal,
-                     &window->surface_destroy_listener);
-
-       weston_wm_window_schedule_repaint(window);
-       xserver_map_shell_surface(wm, window);
-}
-
-const struct xserver_interface xserver_implementation = {
-       xserver_set_window_id
-};