Check in new files for xdg-activation

* xdg-activation-v1.xml:
* xdg_activation.c:
* tests/xdg_activation_test:
* tests/xdg_activation_test.c: New files.
This commit is contained in:
hujianwei 2022-11-14 05:58:00 +00:00
parent a3e87a1f34
commit 556cf6dbcc
4 changed files with 837 additions and 0 deletions

BIN
tests/xdg_activation_test Executable file

Binary file not shown.

349
tests/xdg_activation_test.c Normal file
View file

@ -0,0 +1,349 @@
/* Tests for the Wayland compositor running on the X server.
Copyright (C) 2022 to various contributors.
This file is part of 12to11.
12to11 is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
12to11 is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with 12to11. If not, see <https://www.gnu.org/licenses/>. */
#include "test_harness.h"
#include "xdg-activation-v1.h"
#include <X11/extensions/XInput2.h>
enum test_kind
{
XDG_ACTIVATION_KIND,
};
static const char *test_names[] =
{
"test_activation_kind",
};
#define LAST_TEST XDG_ACTIVATION_KIND
#define TEST_SOURCE_DEVICE 415000
/* The display. */
static struct test_display *display;
/* The activation interface. */
static struct xdg_activation_v1 *activation;
/* Test interfaces. */
static struct test_interface test_interfaces[] =
{
{ "xdg_activation_v1", &activation, &xdg_activation_v1_interface, 1, },
};
/* The test surface window. */
static Window test_surface_window;
/* The test surface and Wayland surface. */
static struct test_surface *test_surface;
static struct wl_surface *wayland_surface;
/* The the last activation times. */
static uint32_t last_activation_months;
static uint32_t last_activation_milliseconds;
/* Forward declarations. */
static void submit_surface_damage (struct wl_surface *, int, int, int, int);
static void wait_for_map (void);
/* Get a timestamp suitable for use in events dispatched to the test
seat. */
static uint32_t
test_get_time (void)
{
struct timespec timespec;
clock_gettime (CLOCK_MONOTONIC, &timespec);
return (timespec.tv_sec * 1000
+ timespec.tv_nsec / 1000000);
}
/* Get the root window. */
static Window
test_get_root (void)
{
return DefaultRootWindow (display->x_display);
}
static void
handle_xdg_activation_token_done (void *data,
struct xdg_activation_token_v1 *token,
const char *token_string)
{
char **token_pointer;
token_pointer = data;
*token_pointer = strdup (token_string);
}
static const struct xdg_activation_token_v1_listener activation_token_listener =
{
handle_xdg_activation_token_done,
};
static void
check_activation_with_serial (uint32_t serial, bool expect_success)
{
struct xdg_activation_token_v1 *token;
char *token_string;
/* Set the last user time to 0, 1001. */
test_seat_controller_set_last_user_time (display->seat->controller,
0, 1001);
/* Ask for an activation token. */
token = xdg_activation_v1_get_activation_token (activation);
xdg_activation_token_v1_set_serial (token, serial, display->seat->seat);
xdg_activation_token_v1_set_surface (token, wayland_surface);
xdg_activation_token_v1_set_app_id (token, "xdg_activation_test");
token_string = NULL;
xdg_activation_token_v1_add_listener (token, &activation_token_listener,
&token_string);
xdg_activation_token_v1_commit (token);
wl_display_roundtrip (display->display);
if (!token_string)
report_test_failure ("failed to obtain activation token");
xdg_activation_token_v1_destroy (token);
/* Now, try to activate the surface. */
last_activation_months = 0;
last_activation_milliseconds = 0;
xdg_activation_v1_activate (activation, token_string,
wayland_surface);
wl_display_roundtrip (display->display);
free (token_string);
if (expect_success)
{
if (last_activation_months != 0
|| last_activation_milliseconds != 1001)
report_test_failure ("activation failed, wrong time or event not"
" received");
}
else if (last_activation_months || last_activation_milliseconds)
report_test_failure ("activation succeeded unexpectedly");
}
static void
test_single_step (enum test_kind kind)
{
struct wl_buffer *buffer;
struct test_XIButtonState *button_state;
uint32_t serial;
test_log ("running test step: %s", test_names[kind]);
switch (kind)
{
case XDG_ACTIVATION_KIND:
buffer = load_png_image (display, "tiny.png");
if (!buffer)
report_test_failure ("failed to load tiny.png");
wl_surface_attach (wayland_surface, buffer, 0, 0);
submit_surface_damage (wayland_surface, 0, 0, 500, 500);
wl_surface_commit (wayland_surface);
wait_for_map ();
/* First, dispatch a single enter and button press event and get
a serial after that event. */
test_seat_controller_dispatch_XI_Enter (display->seat->controller,
test_get_time (),
TEST_SOURCE_DEVICE,
XINotifyAncestor,
test_get_root (),
test_surface_window,
None,
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
XINotifyNormal,
False, True, NULL, NULL,
NULL);
button_state
= test_seat_controller_get_XIButtonState (display->seat->controller);
test_seat_controller_dispatch_XI_ButtonPress (display->seat->controller,
test_get_time (),
TEST_SOURCE_DEVICE,
2,
test_get_root (),
test_surface_window,
None,
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
0,
button_state,
NULL, NULL, NULL);
test_XIButtonState_add_button (button_state, 2);
serial = test_get_serial (display);
/* Release the buttons. */
test_seat_controller_dispatch_XI_ButtonRelease (display->seat->controller,
test_get_time (),
TEST_SOURCE_DEVICE,
2,
test_get_root (),
test_surface_window,
None,
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
0,
button_state,
NULL, NULL, NULL);
test_XIButtonState_remove_button (button_state, 2);
/* Now, set the last user time and try to activate the surface
with the given serial. */
check_activation_with_serial (serial, true);
/* Next, get a serial after the button release and try to
activate with that. */
serial = test_get_serial (display);
check_activation_with_serial (serial, true);
/* Finally, click the mouse button again. Verify that using the
previously obtained serial for activation no longer
works. */
test_seat_controller_dispatch_XI_ButtonPress (display->seat->controller,
test_get_time (),
TEST_SOURCE_DEVICE,
2,
test_get_root (),
test_surface_window,
None,
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
wl_fixed_from_double (1.0),
0,
button_state,
NULL, NULL, NULL);
test_XIButtonState_add_button (button_state, 2);
check_activation_with_serial (serial, false);
break;
}
if (kind == LAST_TEST)
test_complete ();
}
static void
submit_surface_damage (struct wl_surface *surface, int x, int y, int width,
int height)
{
test_log ("damaging surface by %d, %d, %d, %d", x, y, width,
height);
wl_surface_damage (surface, x, y, width, height);
}
static void
handle_test_surface_mapped (void *data, struct test_surface *test_surface,
uint32_t xid, const char *display_string)
{
test_surface_window = xid;
}
static void
handle_test_surface_activated (void *data, struct test_surface *test_surface,
uint32_t months, uint32_t milliseconds)
{
last_activation_months = months;
last_activation_milliseconds = milliseconds;
}
static const struct test_surface_listener test_surface_listener =
{
handle_test_surface_mapped,
handle_test_surface_activated,
};
static void
wait_for_map (void)
{
while (!test_surface_window)
{
if (wl_display_dispatch (display->display) == -1)
die ("wl_display_dispatch");
}
}
static void
run_test (void)
{
if (!make_test_surface (display, &wayland_surface,
&test_surface))
report_test_failure ("failed to create test surface");
test_surface_add_listener (test_surface, &test_surface_listener,
NULL);
test_single_step (XDG_ACTIVATION_KIND);
while (true)
{
if (wl_display_dispatch (display->display) == -1)
die ("wl_display_dispatch");
}
}
int
main (void)
{
test_init ();
display = open_test_display (test_interfaces,
ARRAYELTS (test_interfaces));
if (!display)
report_test_failure ("failed to open display");
test_init_seat (display);
run_test ();
}

200
xdg-activation-v1.xml Normal file
View file

@ -0,0 +1,200 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="xdg_activation_v1">
<copyright>
Copyright © 2020 Aleix Pol Gonzalez &lt;aleixpol@kde.org&gt;
Copyright © 2020 Carlos Garnacho &lt;carlosg@gnome.org&gt;
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
</copyright>
<description summary="Protocol for requesting activation of surfaces">
The way for a client to pass focus to another toplevel is as follows.
The client that intends to activate another toplevel uses the
xdg_activation_v1.get_activation_token request to get an activation token.
This token is then forwarded to the client, which is supposed to activate
one of its surfaces, through a separate band of communication.
One established way of doing this is through the XDG_ACTIVATION_TOKEN
environment variable of a newly launched child process. The child process
should unset the environment variable again right after reading it out in
order to avoid propagating it to other child processes.
Another established way exists for Applications implementing the D-Bus
interface org.freedesktop.Application, which should get their token under
XDG_ACTIVATION_TOKEN on their platform_data.
In general activation tokens may be transferred across clients through
means not described in this protocol.
The client to be activated will then pass the token
it received to the xdg_activation_v1.activate request. The compositor can
then use this token to decide how to react to the activation request.
The token the activating client gets may be ineffective either already at
the time it receives it, for example if it was not focused, for focus
stealing prevention. The activating client will have no way to discover
the validity of the token, and may still forward it to the to be activated
client.
The created activation token may optionally get information attached to it
that can be used by the compositor to identify the application that we
intend to activate. This can for example be used to display a visual hint
about what application is being started.
Warning! The protocol described in this file is currently in the testing
phase. Backward compatible changes may be added together with the
corresponding interface version bump. Backward incompatible changes can
only be done by creating a new major version of the extension.
</description>
<interface name="xdg_activation_v1" version="1">
<description summary="interface for activating surfaces">
A global interface used for informing the compositor about applications
being activated or started, or for applications to request to be
activated.
</description>
<request name="destroy" type="destructor">
<description summary="destroy the xdg_activation object">
Notify the compositor that the xdg_activation object will no longer be
used.
The child objects created via this interface are unaffected and should
be destroyed separately.
</description>
</request>
<request name="get_activation_token">
<description summary="requests a token">
Creates an xdg_activation_token_v1 object that will provide
the initiating client with a unique token for this activation. This
token should be offered to the clients to be activated.
</description>
<arg name="id" type="new_id" interface="xdg_activation_token_v1"/>
</request>
<request name="activate">
<description summary="notify new interaction being available">
Requests surface activation. It's up to the compositor to display
this information as desired, for example by placing the surface above
the rest.
The compositor may know who requested this by checking the activation
token and might decide not to follow through with the activation if it's
considered unwanted.
Compositors can ignore unknown activation tokens when an invalid
token is passed.
</description>
<arg name="token" type="string" summary="the activation token of the initiating client"/>
<arg name="surface" type="object" interface="wl_surface"
summary="the wl_surface to activate"/>
</request>
</interface>
<interface name="xdg_activation_token_v1" version="1">
<description summary="an exported activation handle">
An object for setting up a token and receiving a token handle that can
be passed as an activation token to another client.
The object is created using the xdg_activation_v1.get_activation_token
request. This object should then be populated with the app_id, surface
and serial information and committed. The compositor shall then issue a
done event with the token. In case the request's parameters are invalid,
the compositor will provide an invalid token.
</description>
<enum name="error">
<entry name="already_used" value="0"
summary="The token has already been used previously"/>
</enum>
<request name="set_serial">
<description summary="specifies the seat and serial of the activating event">
Provides information about the seat and serial event that requested the
token.
The serial can come from an input or focus event. For instance, if a
click triggers the launch of a third-party client, the launcher client
should send a set_serial request with the serial and seat from the
wl_pointer.button event.
Some compositors might refuse to activate toplevels when the token
doesn't have a valid and recent enough event serial.
Must be sent before commit. This information is optional.
</description>
<arg name="serial" type="uint"
summary="the serial of the event that triggered the activation"/>
<arg name="seat" type="object" interface="wl_seat"
summary="the wl_seat of the event"/>
</request>
<request name="set_app_id">
<description summary="specifies the application being activated">
The requesting client can specify an app_id to associate the token
being created with it.
Must be sent before commit. This information is optional.
</description>
<arg name="app_id" type="string"
summary="the application id of the client being activated."/>
</request>
<request name="set_surface">
<description summary="specifies the surface requesting activation">
This request sets the surface requesting the activation. Note, this is
different from the surface that will be activated.
Some compositors might refuse to activate toplevels when the token
doesn't have a requesting surface.
Must be sent before commit. This information is optional.
</description>
<arg name="surface" type="object" interface="wl_surface"
summary="the requesting surface"/>
</request>
<request name="commit">
<description summary="issues the token request">
Requests an activation token based on the different parameters that
have been offered through set_serial, set_surface and set_app_id.
</description>
</request>
<event name="done">
<description summary="the exported activation token">
The 'done' event contains the unique token of this activation request
and notifies that the provider is done.
</description>
<arg name="token" type="string" summary="the exported activation token"/>
</event>
<request name="destroy" type="destructor">
<description summary="destroy the xdg_activation_token_v1 object">
Notify the compositor that the xdg_activation_token_v1 object will no
longer be used.
</description>
</request>
</interface>
</protocol>

288
xdg_activation.c Normal file
View file

@ -0,0 +1,288 @@
/* Wayland compositor running on top of an X server.
Copyright (C) 2022 to various contributors.
This file is part of 12to11.
12to11 is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
12to11 is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with 12to11. If not, see <https://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <string.h>
#include "compositor.h"
#include "xdg-activation-v1.h"
typedef struct _XdgActivationToken XdgActivationToken;
struct _XdgActivationToken
{
/* The wl_resource associated with this activation token. */
struct wl_resource *resource;
/* The seat associated with this activation token. */
Seat *seat;
/* The seat destroy callback. */
void *seat_destroy_callback;
/* The serial associated with this activation token. */
uint32_t serial;
};
/* The xdg_activation_v1 global. */
static struct wl_global *xdg_activation_global;
static void
HandleSeatDestroyed (void *data)
{
XdgActivationToken *token;
token = data;
token->seat = NULL;
token->serial = 0;
}
/* Compositor activation policy. The protocol translator,
notwithstanding the judgement of the window manager, allows any
client whose token was created with (IOW, had at the time of
Commit) at least the latest key or pointer serial on its seat to
activate its toplevels. The timestamp used to focus the toplevels
is the activation token. */
static void
SetSerial (struct wl_client *client, struct wl_resource *resource,
uint32_t serial, struct wl_resource *seat_resource)
{
XdgActivationToken *token;
Seat *seat;
token = wl_resource_get_user_data (resource);
/* If token is NULL, then the token has already been used. Silently
ignore this request. */
if (!token)
return;
/* First, clear the current seat. */
if (token->seat_destroy_callback)
XLSeatCancelDestroyListener (token->seat_destroy_callback);
token->seat = NULL;
token->seat_destroy_callback = NULL;
seat = wl_resource_get_user_data (seat_resource);
if (XLSeatIsInert (seat))
/* Just return if the seat is inert. */
return;
/* Otherwise, set the seat and serial. */
token->seat = seat;
token->serial = serial;
token->seat_destroy_callback
= XLSeatRunOnDestroy (seat, HandleSeatDestroyed, token);
}
static void
SetAppId (struct wl_client *client, struct wl_resource *resource,
const char *app_id)
{
/* This information is not useful. */
}
static void
SetSurface (struct wl_client *client, struct wl_resource *resource,
struct wl_resource *surface_resource)
{
/* This information is not useful. */
}
static void
Commit (struct wl_client *client, struct wl_resource *resource)
{
XdgActivationToken *token;
Timestamp last_user_time;
char buffer[80];
token = wl_resource_get_user_data (resource);
if (!token)
{
wl_resource_post_error (resource,
XDG_ACTIVATION_TOKEN_V1_ERROR_ALREADY_USED,
"the specified activation token has been passed"
" to a previous commit request and is no longer"
" valid");
return;
}
wl_resource_set_user_data (resource, NULL);
if (!token->seat || !XLSeatCheckActivationSerial (token->seat,
token->serial))
{
/* Send an invalid serial. */
xdg_activation_token_v1_send_done (token->resource,
"activation_rejected");
goto finish;
}
/* Send the last user time as the activation token. */
last_user_time = XLSeatGetLastUserTime (token->seat);
sprintf (buffer, "%"PRIu32".%"PRIu32".%d",
last_user_time.months,
last_user_time.milliseconds,
XLSeatGetPointerDevice (token->seat));
xdg_activation_token_v1_send_done (token->resource, buffer);
/* Free the token. */
finish:
if (token->seat_destroy_callback)
XLSeatCancelDestroyListener (token->seat_destroy_callback);
XLFree (token);
}
static void
DestroyActivationToken (struct wl_client *client,
struct wl_resource *resource)
{
wl_resource_destroy (resource);
}
static struct xdg_activation_token_v1_interface xdg_activation_token_impl =
{
.set_serial = SetSerial,
.set_app_id = SetAppId,
.set_surface = SetSurface,
.commit = Commit,
.destroy = DestroyActivationToken,
};
static void
HandleResourceDestroy (struct wl_resource *resource)
{
XdgActivationToken *token;
token = wl_resource_get_user_data (resource);
if (!token)
return;
if (token->seat_destroy_callback)
XLSeatCancelDestroyListener (token->seat_destroy_callback);
XLFree (token);
}
static void
Destroy (struct wl_client *client, struct wl_resource *resource)
{
wl_resource_destroy (resource);
}
static void
GetActivationToken (struct wl_client *client, struct wl_resource *resource,
uint32_t id)
{
XdgActivationToken *token;
token = XLSafeMalloc (sizeof *token);
if (!token)
{
wl_resource_post_no_memory (resource);
return;
}
memset (token, 0, sizeof *token);
token->resource
= wl_resource_create (client, &xdg_activation_token_v1_interface,
wl_resource_get_version (resource), id);
if (!token->resource)
{
XLFree (token);
wl_resource_post_no_memory (resource);
return;
}
wl_resource_set_implementation (token->resource,
&xdg_activation_token_impl,
token, HandleResourceDestroy);
}
static void
Activate (struct wl_client *client, struct wl_resource *resource,
const char *token, struct wl_resource *surface_resource)
{
Timestamp timestamp;
Surface *surface;
int deviceid;
if (sscanf (token, "%"SCNu32".%"SCNu32".%d", &timestamp.months,
&timestamp.milliseconds, &deviceid) != 3)
/* The activation token is invalid. */
return;
/* Activate the surface with the given token. */
surface = wl_resource_get_user_data (surface_resource);
if (surface->role->funcs.activate)
surface->role->funcs.activate (surface, surface->role,
deviceid, timestamp);
}
static const struct xdg_activation_v1_interface xdg_activation_impl =
{
.destroy = Destroy,
.get_activation_token = GetActivationToken,
.activate = Activate,
};
static void
HandleBind (struct wl_client *client, void *data, uint32_t version,
uint32_t id)
{
struct wl_resource *resource;
/* Create an xdg_activation_v1 resource. This resource is then used
to create activation tokens and activate surfaces. */
resource = wl_resource_create (client, &xdg_activation_v1_interface,
version, id);
if (!resource)
{
wl_client_post_no_memory (client);
return;
}
wl_resource_set_implementation (resource, &xdg_activation_impl,
NULL, NULL);
}
void
XLInitXdgActivation (void)
{
xdg_activation_global
= wl_global_create (compositor.wl_display,
&xdg_activation_v1_interface,
1, NULL, HandleBind);
}