[writing a compositor] Window does not return to previous position on unmaximize

2018-09-23 Thread adlo
I am writing a compositor using libweston. When unmaximizing a window,
the window doesn't return to the position it was at before it was
maximized. How can I resolve this?

Here is my code:

static void
set_maximized_position (struct ShellSurface *shsurf)
{
pixman_rectangle32_t area;
struct weston_geometry geometry;

  area.x = shsurf->surface->output->x;
  area.y = shsurf->surface->output->y;

geometry = weston_desktop_surface_get_geometry(shsurf-
>desktop_surface);

weston_view_set_position(shsurf->view,
 area.x - geometry.x,
 area.y - geometry.y);
}

static void
map(struct TestServer *shell, struct ShellSurface *shsurf,
int32_t sx, int32_t sy)
{
  if (shsurf->maximized)
set_maximized_position (shsurf);
weston_view_update_transform(shsurf->view);

}

static void
desktop_surface_committed(struct weston_desktop_surface
*desktop_surface,
  int32_t sx, int32_t sy, void *data)
{
struct ShellSurface *shsurf =
weston_desktop_surface_get_user_data(desktop_surface);
struct weston_surface *surface =
weston_desktop_surface_get_surface(desktop_surface);
struct weston_view *view = shsurf->view;
struct TestServer *shell = data;
bool was_fullscreen;
bool was_maximized;

if (surface->width == 0)
return;

  was_maximized = shsurf->maximized;

  shsurf->maximized =
weston_desktop_surface_get_maximized (desktop_surface);

if (!weston_surface_is_mapped(surface))
map(shell, shsurf, sx, sy);

  if (was_maximized == shsurf->maximized)
return;

  if (was_maximized)
unset_maximized (shsurf);

  if (shsurf->maximized && !shsurf->saved_position_valid)
{

  shsurf->saved_x = shsurf->view->geometry.x;
  shsurf->saved_y = shsurf->view->geometry.y;
  shsurf->saved_position_valid = true;
}

  weston_view_set_position (shsurf->view,
shsurf->saved_x, shsurf->saved_y);

  if (shsurf->maximized)
set_maximized_position (shsurf);

}

An earlier version of the rest of my code can be found at:

https://github.com/adlocode/test-libweston-desktop/blob/master/shell.c

Regards

adlo

___
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/wayland-devel


Re: [PATCH v2] server: add wl_signal_emit_safe

2018-09-23 Thread Simon Ser
On Tuesday, September 18, 2018 8:43 PM, Derek Foreman 
 wrote:
> On 2018-08-08 07:00 AM, Simon Ser wrote:
>
> > This new function allows listeners to remove themselves or any
> > other listener when called. This version only works if listeners
> > are properly removed before they are free'd.
> > wl_signal_emit tries to be safe but it fails: it works fine if a
> > handler removes its own listener, but not if it removes another
> > one.
> > It's not possible to patch wl_signal_emit directly as attempted
> > in 1 because some projects using libwayland directly free
> > destroy listeners without removing them from the list. Using this
> > new strategy fails in this case, causing read-after-free errors.
> > Signed-off-by: Simon Ser cont...@emersion.fr
>
> Hi Simon,
>
> Is there intent to use this internally in libwayland (or in weston?)
> somewhere in a further patch?
>
> Since there's no way to know from the callback whether the signal is
> being emit "safely" or not, I'm a little concerned that developers might
> get confused about what style of callback they're writing. Or people
> will see "safe" and just lazily use that everywhere, even for destroy
> signals...
>
> Also, it looks at a glance like all of the struct members and such
> touched by this function are in public headers? I think the safe emit
> function doesn't strictly need to be in libwayland at all?
>
> I don't really mean to block this work, just wondering what follows next.
>
> Some tiny comments inlined further down.


Hi,

Thanks for you review.

I have no plans to make libwayland or weston use this function in a future
patch. It should be used by everybody who's sure users correctly remove
destroy listeners (or doesn't care to break its ABI).

Yeah, this function doesn't need to be in libwayland per se, in fact we already
have it in wlroots and use it everywhere. I just thought users would benefit
from having a safe version of wl_signal_emit in libwayland, because it's easy
to remove multiple listeners from a destroy handler once you have multiple
destroy signals and inter-dependant structs. It doesn't _need_ to be in
libwayland, but putting it in libwayland would allow people to make sure they
don't run into issues without needing to copy-paste the function from wlroots.

People could use it without understanding why it's safe, but I prefer to have
users running into issues because they forget to remove the destroy listener
than having users running into issues because they use multiple wl_signals.

What do you think? In the end it's not that a big deal, I just think it would
be nice to have.

Simon
___
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/wayland-devel


Re: [PATCH] cursor: use memfd_create or shm_open for anonymous in-memory files

2018-09-23 Thread Simon Ser
Hi all,

Any news about this?

Thanks,

---
Simon Ser
https://emersion.fr

On Wednesday, August 15, 2018 4:14 PM, Simon Ser  wrote:
> On Linux, try using memfd_create and file sealing. Fallback to
> shm_open on old kernels.
>
> On FreeBSD, use shm_open with SHM_ANON.
>
> Otherwise, use shm_open with a random name, making sure the name
> isn't already taken.
>
> Signed-off-by: Simon Ser cont...@emersion.fr
>
> Makefile.am | 2 +-
> configure.ac | 4 +-
> cursor/os-compatibility.c | 172 ++
> 3 files changed, 86 insertions(+), 92 deletions(-)
>
> diff --git a/Makefile.am b/Makefile.am
> index 697c517..c612672 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -136,7 +136,7 @@ libwayland_cursor_la_SOURCES = \
> cursor/cursor-data.h \
> cursor/xcursor.c \
> cursor/xcursor.h
> -libwayland_cursor_la_LIBADD = libwayland-client.la
> +libwayland_cursor_la_LIBADD = libwayland-client.la -lrt
>
> pkgconfig_DATA += cursor/wayland-cursor.pc
>
> diff --git a/configure.ac b/configure.ac
> index 9419ae3..d59c61d 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -62,8 +62,8 @@ if test "x$GCC" = "xyes"; then
> fi
> AC_SUBST(GCC_CFLAGS)
>
> -AC_CHECK_HEADERS([sys/prctl.h])
> -AC_CHECK_FUNCS([accept4 mkostemp posix_fallocate prctl])
> +AC_CHECK_HEADERS([sys/prctl.h linux/memfd.h])
> +AC_CHECK_FUNCS([accept4 prctl])
>
> AC_ARG_ENABLE([libraries],
> [AC_HELP_STRING([--disable-libraries],
> diff --git a/cursor/os-compatibility.c b/cursor/os-compatibility.c
> index e972d21..96649e3 100644
> --- a/cursor/os-compatibility.c
> +++ b/cursor/os-compatibility.c
> @@ -25,124 +25,118 @@
>
> #define _GNU_SOURCE
>
> -#include 
> -#include 
> -#include 
> #include 
> -#include 
> +#include 
> #include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
>
> -
>
> +#ifdef HAVE_LINUX_MEMFD_H
> +#include 
> +#endif
>
> #include "config.h"
> #include "os-compatibility.h"
>
> -#ifndef HAVE_MKOSTEMP
> +static void
> +randname(char *buf) {
>
> -   struct timespec ts;
>
> -   long r;
>
> -   int i;
>
> -
> -   clock_gettime(CLOCK_REALTIME, &ts);
>
> -   r = ts.tv_nsec;
>
> -   for (i = 0; i < 6; ++i) {
>
> - buf[i] = 'A'+(r&15)+(r&16)*2;
>
>
> - r >>= 5;
>
>
> -   }
> +}
>
> -
>
> static int
> -set_cloexec_or_close(int fd)
> +anonymous_shm_open(off_t size)
> {
>
> -   long flags;
> -
> -   if (fd == -1)
> - return -1;
>
>
>
> -   char name[] = "/wayland-cursor-XX";
> -   int fd, retries = 100;
>
> -   flags = fcntl(fd, F_GETFD);
> -   if (flags == -1)
> - goto err;
>
>
>
> -   do {
> - randname(name + strlen(name) - 6);
>
>
>
> -   if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
> - goto err;
>
>
>
> - --retries;
>
>
> - fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
>
>
> - if (fd >= 0) {
>
>
> - shm_unlink(name);
>
>
> - return fd;
>
>
> - }
>
>
> -   } while (retries > 0 && errno == EEXIST);
>
>
> -   return fd;
> -
>
> -err:
>
> -   close(fd);
> return -1;
> }
>
>
> -
>
> +static int
> +seal_or_close(int fd) {
> +#if defined(F_ADD_SEALS) && defined(F_SEAL_SHRINK)
>
> -   if (fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK) == -1) {
> - close(fd);
>
>
> - return -1;
>
>
> -   }
> #endif
>
> -   return 0;
> +}
>
> -
>
> static int
> -create_tmpfile_cloexec(char *tmpname)
> +create_anonymous_file(off_t size)
> {
>
> -   int fd;
>
> -   int fd, flags;
>
> -#ifdef HAVE_MKOSTEMP
>
>
> -   fd = mkostemp(tmpname, O_CLOEXEC);
>
> -   if (fd >= 0)
>
> - unlink(tmpname);
>
>
>
> -#else
>
> -   fd = mkstemp(tmpname);
> +#if defined(__NR_memfd_create) && defined(MFD_CLOEXEC)
>
>
> -   flags = MFD_CLOEXEC;
> +#if defined(MFD_ALLOW_SEALING)
>
> -   flags |= MFD_ALLOW_SEALING;
> +#endif
>
> -   fd = syscall(__NR_memfd_create, "wayland-cursor", flags);
> if (fd >= 0) {
>
>
> - fd = set_cloexec_or_close(fd);
>
>
> - unlink(tmpname);
>
>
> -   }
>
> - if (ftruncate(fd, size) < 0) {
>
>
> - close(fd);
>
>
> - return -1;
>
>
> - }
>
>
> -
>
> +#if defined(MFD_ALLOW_SEALING)
>
> - if (seal_or_close(fd) != 0)
>
>
> - return -1;
>
>
>
> #endif
>
> - return fd;
>
>
> -   } else if (errno != ENOSYS)
> - return fd;
>
>
>
> +#endif
> +
> +#if defined(FreeBSD)
>
> -   fd = shm_open(SHM_ANON, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0600);
> +#else
>
> -   fd = anonymous_shm_open(size);
> +#endif
>
> -
> -   if (ftruncate(fd, size) < 0) {
> - close(fd);
>
>
> - return -1;
>
>
> -   }
> -   return fd;
> }
>
> -/*
>
>
> -   -   Create a new, unique, anonymous file of the given size, and
> -   -   return the file descriptor for it. The file descriptor is set
> -   -   CLOEXEC. The file is immediately suitable for mmap()'ing
> -   -   the given size at offset zero.
> -   -
> -   -   The file should not have a permanen

[PATCH wayland-protocols v2] unstable: add primary-selection protocol

2018-09-23 Thread Simon Ser
From: emersion 

This primary selection is similar in spirit to the eponimous
in X11, allowing a quick "select text + middle click" shortcut
to copying and pasting.

It's otherwise very similar to its Wayland counterpart, and
explicitly made consistent with it.

Signed-off-by: Simon Ser 
---

Changes from v1 to v2: renamed with the wp_ prefix

 Makefile.am   |   3 +-
 unstable/primary-selection/README |   4 +
 .../primary-selection-unstable-v1.xml | 226 ++
 3 files changed, 232 insertions(+), 1 deletion(-)
 create mode 100644 unstable/primary-selection/README
 create mode 100644 unstable/primary-selection/primary-selection-unstable-v1.xml

diff --git a/Makefile.am b/Makefile.am
index 6394e26..aca32c2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -20,7 +20,8 @@ unstable_protocols =  
\

unstable/keyboard-shortcuts-inhibit/keyboard-shortcuts-inhibit-unstable-v1.xml \
unstable/xdg-output/xdg-output-unstable-v1.xml  
\
unstable/input-timestamps/input-timestamps-unstable-v1.xml  \
-   unstable/xdg-decoration/xdg-decoration-unstable-v1.xml  \
+  unstable/xdg-decoration/xdg-decoration-unstable-v1.xml   \
+   unstable/xdg-primary-selection/xdg-primary-selection-unstable-v1.xml
\
$(NULL)
 
 stable_protocols = 
\
diff --git a/unstable/primary-selection/README 
b/unstable/primary-selection/README
new file mode 100644
index 000..ae0a402
--- /dev/null
+++ b/unstable/primary-selection/README
@@ -0,0 +1,4 @@
+Primary selection protocol
+
+Maintainers:
+Simon Ser 
diff --git a/unstable/primary-selection/primary-selection-unstable-v1.xml 
b/unstable/primary-selection/primary-selection-unstable-v1.xml
new file mode 100644
index 000..749dd86
--- /dev/null
+++ b/unstable/primary-selection/primary-selection-unstable-v1.xml
@@ -0,0 +1,226 @@
+
+
+  
+Copyright © 2015, 2016 Red Hat
+
+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.
+  
+
+  
+This protocol provides the ability to have a primary selection device to
+match that of the X server. This primary selection is a shortcut to the
+common clipboard selection, where text just needs to be selected in order
+to allow copying it elsewhere. The de facto way to perform this action
+is the middle mouse button, although it is not limited to this one.
+
+Clients wishing to honor primary selection should create a primary
+selection source and set it as the selection through
+wp_primary_selection_device.set_selection whenever the text selection
+changes. In order to minimize calls in pointer-driven text selection,
+it should happen only once after the operation finished. Similarly,
+a NULL source should be set when text is unselected.
+
+wp_primary_selection_offer objects are first announced through the
+wp_primary_selection_device.data_offer event. Immediately after this event,
+the primary data offer will emit wp_primary_selection_offer.offer events
+to let know of the mime types being offered.
+
+When the primary selection changes, the client with the keyboard focus
+will receive wp_primary_selection_device.selection events. Only the client
+with the keyboard focus will receive such events with a non-NULL
+wp_primary_selection_offer. Across keyboard focus changes, previously
+focused clients will receive wp_primary_selection_device.events with a
+NULL wp_primary_selection_offer.
+
+In order to request the primary selection data, the client must pass
+a recent serial pertaining to the press event that is triggering the
+operation, if the compositor deems the serial valid and recent, the
+wp_primary_selection_source.send event will happen in the other