window: Don't needlessly sync parent and geometry
[platform/upstream/weston.git] / clients / window.c
index 7b77f93..e44d65c 100644 (file)
@@ -217,6 +217,7 @@ struct window {
        struct rectangle saved_allocation;
        struct rectangle min_allocation;
        struct rectangle pending_allocation;
+       struct rectangle last_geometry;
        int x, y;
        int redraw_needed;
        int redraw_task_scheduled;
@@ -246,6 +247,7 @@ struct window {
        struct xdg_popup *xdg_popup;
 
        struct window *parent;
+       struct wl_surface *last_parent_surface;
 
        struct window_frame *frame;
 
@@ -334,6 +336,11 @@ struct input {
                xkb_mod_mask_t shift_mask;
        } xkb;
 
+       int32_t repeat_rate_sec;
+       int32_t repeat_rate_nsec;
+       int32_t repeat_delay_sec;
+       int32_t repeat_delay_nsec;
+
        struct task repeat_task;
        int repeat_timer_fd;
        uint32_t repeat_sym;
@@ -360,11 +367,14 @@ struct window_frame {
        struct widget *widget;
        struct widget *child;
        struct frame *frame;
+
+       uint32_t last_time;
+       uint32_t did_double, double_click;
 };
 
 struct menu {
+       void *user_data;
        struct window *window;
-       struct window *parent;
        struct widget *widget;
        struct input *input;
        struct frame *frame;
@@ -571,8 +581,8 @@ egl_window_surface_release(struct toysurface *base)
        if (!device)
                return;
 
-       if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
-                           surface->display->argb_ctx))
+       if (!eglMakeCurrent(surface->display->dpy,
+                           EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
                fprintf(stderr, "failed to make context current\n");
 
        cairo_device_release(device);
@@ -2229,9 +2239,9 @@ frame_get_pointer_image_for_location(struct window_frame *frame,
 }
 
 static void
-frame_menu_func(struct window *window,
-               struct input *input, int index, void *data)
+frame_menu_func(void *data, struct input *input, int index)
 {
+       struct window *window = data;
        struct display *display;
 
        switch (index) {
@@ -2377,6 +2387,7 @@ frame_handle_status(struct window_frame *frame, struct input *input,
        }
 }
 
+#define DOUBLE_CLICK_PERIOD 250
 static void
 frame_button_handler(struct widget *widget,
                     struct input *input, uint32_t time,
@@ -2387,7 +2398,27 @@ frame_button_handler(struct widget *widget,
        struct window_frame *frame = data;
        enum theme_location location;
 
-       location = frame_pointer_button(frame->frame, input, button, state);
+       frame->double_click = 0;
+       if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
+               if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
+                       frame->double_click = 1;
+                       frame->did_double = 1;
+               } else
+                       frame->did_double = 0;
+
+               frame->last_time = time;
+       } else if (frame->did_double == 1) {
+               frame->double_click = 1;
+               frame->did_double = 0;
+       }
+
+       if (frame->double_click)
+               location = frame_double_click(frame->frame, input,
+                                             button, state);
+       else
+               location = frame_pointer_button(frame->frame, input,
+                                               button, state);
+
        frame_handle_status(frame, input, time, location);
 }
 
@@ -2786,10 +2817,10 @@ keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
                return;
        }
 
-       keymap = xkb_map_new_from_string(input->display->xkb_context,
-                                        map_str,
-                                        XKB_KEYMAP_FORMAT_TEXT_V1,
-                                        0);
+       keymap = xkb_keymap_new_from_string(input->display->xkb_context,
+                                           map_str,
+                                           XKB_KEYMAP_FORMAT_TEXT_V1,
+                                           0);
        munmap(map_str, size);
        close(fd);
 
@@ -2801,7 +2832,7 @@ keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
        state = xkb_state_new(keymap);
        if (!state) {
                fprintf(stderr, "failed to create XKB state\n");
-               xkb_map_unref(keymap);
+               xkb_keymap_unref(keymap);
                return;
        }
 
@@ -2811,11 +2842,11 @@ keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
        input->xkb.state = state;
 
        input->xkb.control_mask =
-               1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
+               1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
        input->xkb.alt_mask =
-               1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
+               1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
        input->xkb.shift_mask =
-               1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
+               1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
 }
 
 static void
@@ -2871,7 +2902,7 @@ keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
        if (input->grab && input->grab_button == 0)
                return;
 
-       num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
+       num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
 
        sym = XKB_KEY_NoSymbol;
        if (num_syms == 1)
@@ -2906,10 +2937,10 @@ keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
                input->repeat_sym = sym;
                input->repeat_key = key;
                input->repeat_time = time;
-               its.it_interval.tv_sec = 0;
-               its.it_interval.tv_nsec = 25 * 1000 * 1000;
-               its.it_value.tv_sec = 0;
-               its.it_value.tv_nsec = 400 * 1000 * 1000;
+               its.it_interval.tv_sec = input->repeat_rate_sec;
+               its.it_interval.tv_nsec = input->repeat_rate_nsec;
+               its.it_value.tv_sec = input->repeat_delay_sec;
+               its.it_value.tv_nsec = input->repeat_delay_nsec;
                timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
        }
 }
@@ -2930,8 +2961,8 @@ keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
        xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
                              mods_locked, 0, 0, group);
        mask = xkb_state_serialize_mods(input->xkb.state,
-                                       XKB_STATE_DEPRESSED |
-                                       XKB_STATE_LATCHED);
+                                       XKB_STATE_MODS_DEPRESSED |
+                                       XKB_STATE_MODS_LATCHED);
        input->modifiers = 0;
        if (mask & input->xkb.control_mask)
                input->modifiers |= MOD_CONTROL_MASK;
@@ -2941,12 +2972,44 @@ keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
                input->modifiers |= MOD_SHIFT_MASK;
 }
 
+static void
+set_repeat_info(struct input *input, int32_t rate, int32_t delay)
+{
+       input->repeat_rate_sec = input->repeat_rate_nsec = 0;
+       input->repeat_delay_sec = input->repeat_delay_nsec = 0;
+
+       /* a rate of zero disables any repeating, regardless of the delay's
+        * value */
+       if (rate == 0)
+               return;
+
+       if (rate == 1)
+               input->repeat_rate_sec = 1;
+       else
+               input->repeat_rate_nsec = 1000000000 / rate;
+
+       input->repeat_delay_sec = delay / 1000;
+       delay -= (input->repeat_delay_sec * 1000);
+       input->repeat_delay_nsec = delay * 1000 * 1000;
+}
+
+static void
+keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
+                           int32_t rate, int32_t delay)
+{
+       struct input *input = data;
+
+       set_repeat_info(input, rate, delay);
+}
+
 static const struct wl_keyboard_listener keyboard_listener = {
        keyboard_handle_keymap,
        keyboard_handle_enter,
        keyboard_handle_leave,
        keyboard_handle_key,
        keyboard_handle_modifiers,
+       keyboard_handle_repeat_info
+
 };
 
 static void
@@ -3829,6 +3892,15 @@ widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
        window_schedule_resize(widget->window, width, height);
 }
 
+static int
+window_get_shadow_margin(struct window *window)
+{
+       if (window->frame && !window->fullscreen)
+               return frame_get_shadow_margin(window->frame->frame);
+       else
+               return 0;
+}
+
 static void
 handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
                         int32_t width, int32_t height,
@@ -3878,7 +3950,14 @@ handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
        }
 
        if (width > 0 && height > 0) {
-               window_schedule_resize(window, width, height);
+               /* The width / height params are for window geometry,
+                * but window_schedule_resize takes allocation. Add
+                * on the shadow margin to get the difference. */
+               int margin = window_get_shadow_margin(window);
+
+               window_schedule_resize(window,
+                                      width + margin * 2,
+                                      height + margin * 2);
        } else {
                window_schedule_resize(window,
                                       window->saved_allocation.width,
@@ -3916,28 +3995,47 @@ window_sync_parent(struct window *window)
        else
                parent_surface = NULL;
 
+       if (parent_surface == window->last_parent_surface)
+               return;
+
        xdg_surface_set_parent(window->xdg_surface, parent_surface);
+       window->last_parent_surface = parent_surface;
 }
 
 static void
-window_sync_margin(struct window *window)
+window_get_geometry(struct window *window, struct rectangle *geometry)
+{
+       if (window->frame && !window->fullscreen)
+               frame_input_rect(window->frame->frame,
+                                &geometry->x,
+                                &geometry->y,
+                                &geometry->width,
+                                &geometry->height);
+       else
+               window_get_allocation(window, geometry);
+}
+
+static void
+window_sync_geometry(struct window *window)
 {
-       int margin;
+       struct rectangle geometry;
 
        if (!window->xdg_surface)
                return;
 
-       if (!window->frame)
+       window_get_geometry(window, &geometry);
+       if (geometry.x == window->last_geometry.x &&
+           geometry.y == window->last_geometry.y &&
+           geometry.width == window->last_geometry.width &&
+           geometry.height == window->last_geometry.height)
                return;
 
-       margin = frame_get_shadow_margin(window->frame->frame);
-
-       /* Shadow size is the same on every side. */
-       xdg_surface_set_margin(window->xdg_surface,
-                                    margin,
-                                    margin,
-                                    margin,
-                                    margin);
+       xdg_surface_set_window_geometry(window->xdg_surface,
+                                       geometry.x,
+                                       geometry.y,
+                                       geometry.width,
+                                       geometry.height);
+       window->last_geometry = geometry;
 }
 
 static void
@@ -3948,7 +4046,7 @@ window_flush(struct window *window)
        if (!window->custom) {
                if (window->xdg_surface) {
                        window_sync_parent(window);
-                       window_sync_margin(window);
+                       window_sync_geometry(window);
                }
        }
 
@@ -4507,8 +4605,7 @@ menu_button_handler(struct widget *widget,
            (menu->release_count > 0 || time - menu->time > 500)) {
                /* Either relase after press-drag-release or
                 * click-motion-click. */
-               menu->func(menu->parent, input,
-                          menu->current, menu->parent->user_data);
+               menu->func(menu->user_data, input, menu->current);
                input_ungrab(input);
                menu_destroy(menu);
        } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
@@ -4584,31 +4681,28 @@ static const struct xdg_popup_listener xdg_popup_listener = {
        handle_popup_popup_done,
 };
 
-void
-window_show_menu(struct display *display,
-                struct input *input, uint32_t time, struct window *parent,
-                int32_t x, int32_t y,
-                menu_func_t func, const char **entries, int count)
+static struct menu *
+create_menu(struct display *display,
+           struct input *input, uint32_t time,
+           menu_func_t func, const char **entries, int count,
+           void *user_data)
 {
        struct window *window;
        struct menu *menu;
-       int32_t ix, iy;
 
        menu = malloc(sizeof *menu);
        if (!menu)
-               return;
+               return NULL;
 
-       window = window_create_internal(parent->display, 0);
+       window = window_create_internal(display, 0);
        if (!window) {
                free(menu);
-               return;
+               return NULL;
        }
 
        menu->window = window;
-       menu->parent = parent;
+       menu->user_data = user_data;
        menu->widget = window_add_widget(menu->window, menu);
-       window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
-       window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
        menu->frame = frame_create(window->display->theme, 0, 0,
                                   FRAME_BUTTON_NONE, NULL);
        fail_on_null(menu->frame);
@@ -4619,8 +4713,6 @@ window_show_menu(struct display *display,
        menu->time = time;
        menu->func = func;
        menu->input = input;
-       window->x = x;
-       window->y = y;
 
        input_ungrab(input);
 
@@ -4637,6 +4729,47 @@ window_show_menu(struct display *display,
        window_schedule_resize(window, frame_width(menu->frame),
                               frame_height(menu->frame));
 
+       return menu;
+}
+
+struct window *
+window_create_menu(struct display *display,
+                  struct input *input, uint32_t time,
+                  menu_func_t func, const char **entries, int count,
+                  void *user_data)
+{
+       struct menu *menu;
+       menu = create_menu(display, input, time, func, entries, count, user_data);
+
+       if (menu == NULL)
+               return NULL;
+
+       return menu->window;
+}
+
+void
+window_show_menu(struct display *display,
+                struct input *input, uint32_t time, struct window *parent,
+                int32_t x, int32_t y,
+                menu_func_t func, const char **entries, int count)
+{
+       struct menu *menu;
+       struct window *window;
+       int32_t ix, iy;
+
+       menu = create_menu(display, input, time, func, entries, count, parent);
+
+       if (menu == NULL)
+               return;
+
+       window = menu->window;
+
+       window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
+       window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
+
+       window->x = x;
+       window->y = y;
+
        frame_interior(menu->frame, &ix, &iy, NULL, NULL);
 
        window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
@@ -4938,7 +5071,7 @@ static void
 fini_xkb(struct input *input)
 {
        xkb_state_unref(input->xkb.state);
-       xkb_map_unref(input->xkb.keymap);
+       xkb_keymap_unref(input->xkb.keymap);
 }
 
 #define MIN(a,b) ((a) < (b) ? a : b)
@@ -4951,7 +5084,7 @@ display_add_input(struct display *d, uint32_t id)
        input = xzalloc(sizeof *input);
        input->display = d;
        input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
-                                      MIN(d->seat_version, 3));
+                                      MIN(d->seat_version, 4));
        input->touch_focus = NULL;
        input->pointer_focus = NULL;
        input->keyboard_focus = NULL;
@@ -4972,6 +5105,8 @@ display_add_input(struct display *d, uint32_t id)
 
        input->pointer_surface = wl_compositor_create_surface(d->compositor);
 
+       set_repeat_info(input, 40, 400);
+
        input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
                                                TFD_CLOEXEC | TFD_NONBLOCK);
        input->repeat_task.run = keyboard_repeat_func;
@@ -5046,7 +5181,7 @@ static const struct xdg_shell_listener xdg_shell_listener = {
        xdg_shell_ping,
 };
 
-#define XDG_VERSION 3 /* The version of xdg-shell that we implement */
+#define XDG_VERSION 4 /* The version of xdg-shell that we implement */
 #ifdef static_assert
 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
              "Interface version doesn't match implementation version");