compositor: Keep track of what views were activated by clicking

Adds a weston_view_activate() that can be passed an additional active
flag WESTON_ACTIVATE_CLICKED, that the shell passes when a view was
activated by clicking.

This allows shell-independent components implement heuristics depending
on how a view was activated.

Signed-off-by: Jonas Ådahl <jadahl@gmail.com>
Acked-by: Peter Hutterer <peter.hutterer@who-t.net>
diff --git a/desktop-shell/shell.c b/desktop-shell/shell.c
index 4a44f96..9b77add 100644
--- a/desktop-shell/shell.c
+++ b/desktop-shell/shell.c
@@ -5183,7 +5183,7 @@
 	 * Leave fullscreen surfaces on unrelated outputs alone. */
 	lower_fullscreen_layer(shell, shsurf->output);
 
-	weston_seat_set_keyboard_focus(seat, es);
+	weston_view_activate(view, seat, flags);
 
 	state = ensure_focus_state(shell, seat);
 	if (state == NULL)
@@ -5258,6 +5258,7 @@
 		return;
 
 	activate_binding(pointer->seat, data, pointer->focus,
+			 WESTON_ACTIVATE_FLAG_CLICKED |
 			 WESTON_ACTIVATE_FLAG_CONFIGURE);
 }
 
diff --git a/libweston/compositor.c b/libweston/compositor.c
index 96eeb17..4a0bfef 100644
--- a/libweston/compositor.c
+++ b/libweston/compositor.c
@@ -4685,6 +4685,8 @@
 	ec->output_id_pool = 0;
 	ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
 
+	ec->activate_serial = 1;
+
 	if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
 			      ec, compositor_bind))
 		goto fail;
diff --git a/libweston/compositor.h b/libweston/compositor.h
index 0ce7909..b1f8203 100644
--- a/libweston/compositor.h
+++ b/libweston/compositor.h
@@ -802,6 +802,8 @@
 	clockid_t presentation_clock;
 	int32_t repaint_msec;
 
+	unsigned int activate_serial;
+
 	int exit_code;
 
 	void *user_data;
@@ -900,6 +902,8 @@
 	/* For weston_layer inheritance from another view */
 	struct weston_view *parent_view;
 
+	unsigned int click_to_activate_serial;
+
 	pixman_region32_t clip;          /* See weston_view_damage_below() */
 	float alpha;                     /* part of geometry, see below */
 
@@ -1127,6 +1131,7 @@
 enum weston_activate_flag {
 	WESTON_ACTIVATE_FLAG_NONE = 0,
 	WESTON_ACTIVATE_FLAG_CONFIGURE = 1 << 0,
+	WESTON_ACTIVATE_FLAG_CLICKED = 1 << 1,
 };
 
 void
@@ -1178,6 +1183,11 @@
 weston_spring_done(struct weston_spring *spring);
 
 void
+weston_view_activate(struct weston_view *view,
+		     struct weston_seat *seat,
+		     uint32_t flags);
+
+void
 notify_motion(struct weston_seat *seat, uint32_t time,
 	      struct weston_pointer_motion_event *event);
 void
diff --git a/libweston/input.c b/libweston/input.c
index b9c79d4..c34e27f 100644
--- a/libweston/input.c
+++ b/libweston/input.c
@@ -1296,6 +1296,35 @@
 	pointer->grab->interface->motion(pointer->grab, time, &event);
 }
 
+static unsigned int
+peek_next_activate_serial(struct weston_compositor *c)
+{
+	unsigned serial = c->activate_serial + 1;
+
+	return serial == 0 ? 1 : serial;
+}
+
+static void
+inc_activate_serial(struct weston_compositor *c)
+{
+	c->activate_serial = peek_next_activate_serial (c);
+}
+
+WL_EXPORT void
+weston_view_activate(struct weston_view *view,
+		     struct weston_seat *seat,
+		     uint32_t flags)
+{
+	struct weston_compositor *compositor = seat->compositor;
+
+	if (flags & WESTON_ACTIVATE_FLAG_CLICKED) {
+		view->click_to_activate_serial =
+			peek_next_activate_serial(compositor);
+	}
+
+	weston_seat_set_keyboard_focus(seat, view->surface);
+}
+
 WL_EXPORT void
 notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
 	      enum wl_pointer_button_state state)
@@ -2767,5 +2796,6 @@
 		wl_data_device_set_keyboard_focus(seat);
 	}
 
+	inc_activate_serial(compositor);
 	wl_signal_emit(&compositor->activate_signal, surface);
 }