Compare commits

...

2 Commits

Author SHA1 Message Date
Carlos Garnacho
2cf9249d2b x11: Fix race conditions in XSetSelection
Some clients like X11 LibreOffice clear the selection prior to copying
some content there. This selection clear is correctly seen by our clipboard
manager as a hint to take ownership and preserve the last copied content,
all while LO is issuing other XSetSelection with the new clipboard content.

Our use of META_CURRENT_TIME turns this into a race condition, as there's
both LO and our clipboard manager trying to do XSetSelection(), from our
side it's all up to the order in which the requests arrive to the X server.

In order to break the tie, keep the selection timestamp from the XFixes
event (i.e. the timestamp set by the XSetSelection external call that is
unsetting the clipboard) and ensure it is used for our own XSetSelection
call replacing the selection. In this same situation, it will make the
X server deem the request too old, and let LO win.

If the compositor-side XSetSelection event does not happen in result to
a XFixes selection notify event, the current event time will be used, and
META_CURRENT_TIME as a last resort.

Closes: https://gitlab.gnome.org/GNOME/mutter/-/issues/1113

https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1205
2020-04-17 14:37:33 +02:00
Carlos Garnacho
f988a9caaa x11: Clear X11 selection source after unsetting owner
The X11 selection source was being preserved after unsetting its
ownership. This is no leak as it would be eventually replaced by
another source, or destroyed on finalize. But it's pointless to
keep it.

https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1205
2020-04-17 14:37:30 +02:00
2 changed files with 11 additions and 1 deletions

View File

@ -127,6 +127,7 @@ struct _MetaX11Display
struct {
Window xwindow;
uint32_t ownership_timestamp;
MetaSelectionSource *owners[META_N_SELECTION_TYPES];
GCancellable *cancellables[META_N_SELECTION_TYPES];

View File

@ -322,6 +322,7 @@ meta_x11_selection_handle_xfixes_selection_notify (MetaX11Display *x11_display,
if (!atom_to_selection_type (xdisplay, event->selection, &selection_type))
return FALSE;
x11_display->selection.ownership_timestamp = event->selection_timestamp;
selection = meta_display_get_selection (meta_get_display ());
if (x11_display->selection.cancellables[selection_type])
@ -339,6 +340,7 @@ meta_x11_selection_handle_xfixes_selection_notify (MetaX11Display *x11_display,
/* An X client went away, clear the selection */
meta_selection_unset_owner (selection, selection_type,
x11_display->selection.owners[selection_type]);
g_clear_object (&x11_display->selection.owners[selection_type]);
}
}
else if (event->owner != x11_display->selection.xwindow)
@ -359,6 +361,8 @@ meta_x11_selection_handle_xfixes_selection_notify (MetaX11Display *x11_display,
data);
}
x11_display->selection.ownership_timestamp = 0;
return TRUE;
}
@ -380,6 +384,7 @@ notify_selection_owner (MetaX11Display *x11_display,
MetaSelectionSource *new_owner)
{
Display *xdisplay = x11_display->xdisplay;
uint32_t timestamp;
if (new_owner && !META_IS_SELECTION_SOURCE_X11 (new_owner))
{
@ -389,13 +394,17 @@ notify_selection_owner (MetaX11Display *x11_display,
g_clear_object (&x11_display->selection.cancellables[selection_type]);
}
timestamp = x11_display->selection.ownership_timestamp ?
x11_display->selection.ownership_timestamp :
meta_display_get_current_time (x11_display->display);
/* If the owner is non-X11, claim the selection on our selection
* window, so X11 apps can interface with it.
*/
XSetSelectionOwner (xdisplay,
selection_to_atom (selection_type, xdisplay),
x11_display->selection.xwindow,
META_CURRENT_TIME);
timestamp);
}
}