[mutter/wip/tablet-protocol-v2: 9/13] wayland: Implement zwp_tablet_pad_v1



commit 2ea52a6b31a3abef9f459999a4d852f663272be7
Author: Carlos Garnacho <carlosg gnome org>
Date:   Tue May 10 17:14:13 2016 +0200

    wayland: Implement zwp_tablet_pad_v1
    
    This object represents the collection of buttons, strips and rings
    in a tablet pad. All the objects created (pad, strips and rings)
    share a common focus surface and have the same lifetime.

 src/Makefile.am                       |    2 +
 src/wayland/meta-wayland-tablet-pad.c |  417 +++++++++++++++++++++++++++++++++
 src/wayland/meta-wayland-tablet-pad.h |   72 ++++++
 src/wayland/meta-wayland-types.h      |    1 +
 4 files changed, 492 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 0b0bfc6..6952722 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -302,6 +302,8 @@ libmutter_la_SOURCES +=                             \
        wayland/meta-wayland-tablet-seat.h      \
        wayland/meta-wayland-tablet-tool.c      \
        wayland/meta-wayland-tablet-tool.h      \
+       wayland/meta-wayland-tablet-pad.c       \
+       wayland/meta-wayland-tablet-pad.h       \
        wayland/meta-wayland-tablet-pad-ring.c  \
        wayland/meta-wayland-tablet-pad-ring.h  \
        wayland/meta-wayland-tablet-pad-strip.c \
diff --git a/src/wayland/meta-wayland-tablet-pad.c b/src/wayland/meta-wayland-tablet-pad.c
new file mode 100644
index 0000000..e030d24
--- /dev/null
+++ b/src/wayland/meta-wayland-tablet-pad.c
@@ -0,0 +1,417 @@
+/*
+ * Wayland Support
+ *
+ * Copyright (C) 2016 Red Hat
+ *
+ * This program 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 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Author: Carlos Garnacho <carlosg gnome org>
+ */
+
+#define _GNU_SOURCE
+
+#include "config.h"
+
+#include <glib.h>
+
+#include <wayland-server.h>
+#include "tablet-unstable-v1-server-protocol.h"
+
+#include "meta-surface-actor-wayland.h"
+#include "meta-wayland-private.h"
+#include "meta-wayland-tablet-pad.h"
+#include "meta-wayland-tablet-pad-ring.h"
+#include "meta-wayland-tablet-pad-strip.h"
+
+#ifdef HAVE_NATIVE_BACKEND
+#include <clutter/evdev/clutter-evdev.h>
+#include "backends/native/meta-backend-native.h"
+#endif
+
+static void
+unbind_resource (struct wl_resource *resource)
+{
+  wl_list_remove (wl_resource_get_link (resource));
+}
+
+static void
+pad_handle_focus_surface_destroy (struct wl_listener *listener,
+                                  void               *data)
+{
+  MetaWaylandTabletPad *pad = wl_container_of (listener, pad, focus_surface_listener);
+
+  meta_wayland_tablet_pad_set_focus (pad, NULL);
+}
+
+MetaWaylandTabletPad *
+meta_wayland_tablet_pad_new (ClutterInputDevice    *device,
+                             MetaWaylandTabletSeat *tablet_seat)
+{
+  MetaWaylandTabletPad *pad;
+  gint n_elems, i;
+
+  pad = g_slice_new0 (MetaWaylandTabletPad);
+  wl_list_init (&pad->resource_list);
+  wl_list_init (&pad->focus_resource_list);
+  pad->focus_surface_listener.notify = pad_handle_focus_surface_destroy;
+  pad->device = device;
+  pad->tablet_seat = tablet_seat;
+
+  n_elems = clutter_input_device_get_n_rings (device);
+
+  for (i = 0; i < n_elems; i++)
+    {
+      pad->rings = g_list_prepend (pad->rings,
+                                   meta_wayland_tablet_pad_ring_new (pad));
+    }
+
+  n_elems = clutter_input_device_get_n_strips (device);
+
+  for (i = 0; i < n_elems; i++)
+    {
+      pad->strips = g_list_prepend (pad->strips,
+                                    meta_wayland_tablet_pad_strip_new (pad));
+    }
+
+  return pad;
+}
+
+void
+meta_wayland_tablet_pad_free (MetaWaylandTabletPad *pad)
+{
+  struct wl_resource *resource, *next;
+  GList *l;
+
+  meta_wayland_tablet_pad_set_focus (pad, NULL);
+
+  wl_resource_for_each_safe (resource, next, &pad->resource_list)
+    {
+      zwp_tablet_pad_v1_send_removed (resource);
+      wl_list_remove (wl_resource_get_link (resource));
+      wl_list_init (wl_resource_get_link (resource));
+    }
+
+  for (l = pad->rings; l; l = l->next)
+    meta_wayland_tablet_pad_ring_free (l->data);
+  for (l = pad->strips; l; l = l->next)
+    meta_wayland_tablet_pad_strip_free (l->data);
+
+  g_slice_free (MetaWaylandTabletPad, pad);
+}
+
+static void
+tablet_pad_set_feedback (struct wl_client   *client,
+                         struct wl_resource *resource,
+                         uint32_t            button,
+                         const char         *str)
+{
+}
+
+static void
+tablet_pad_destroy (struct wl_client   *client,
+                    struct wl_resource *resource)
+{
+  wl_resource_destroy (resource);
+}
+
+static const struct zwp_tablet_pad_v1_interface pad_interface = {
+  tablet_pad_set_feedback,
+  tablet_pad_destroy,
+};
+
+void
+meta_wayland_tablet_pad_notify (MetaWaylandTabletPad  *pad,
+                                struct wl_resource    *resource)
+{
+  ClutterInputDevice *device = pad->device;
+  MetaBackend *backend = meta_get_backend ();
+  struct wl_client *client = wl_resource_get_client (resource);
+  GList *l;
+
+#ifdef HAVE_NATIVE_BACKEND
+  /* Buttons, only honor this with the native backend */
+  if (META_IS_BACKEND_NATIVE (backend))
+    {
+      struct libinput_device *libinput_device;
+      struct wl_array buttons;
+      gint n_buttons, i;
+
+      libinput_device = clutter_evdev_input_device_get_libinput_device (device);
+      n_buttons = libinput_device_tablet_pad_get_num_buttons (libinput_device);
+      wl_array_init (&buttons);
+
+      for (i = 0; i < n_buttons; i++)
+        {
+          gint *button;
+
+          button = wl_array_add (&buttons, sizeof (uint32_t));
+          *button = i;
+        }
+
+      zwp_tablet_pad_v1_send_buttons (resource, &buttons);
+    }
+#endif
+
+  /* Rings */
+  for (l = pad->rings; l; l = l->next)
+    {
+      MetaWaylandTabletPadRing *ring = l->data;
+      struct wl_resource *ring_resource;
+
+      g_print ("Notifying ring???????????????????????\n");
+      ring_resource = meta_wayland_tablet_pad_ring_create_new_resource (ring,
+                                                                        client,
+                                                                        resource,
+                                                                        0);
+      zwp_tablet_pad_v1_send_ring (resource, ring_resource);
+    }
+
+  /* Strips */
+  for (l = pad->strips; l; l = l->next)
+    {
+      MetaWaylandTabletPadStrip *strip = l->data;
+      struct wl_resource *strip_resource;
+
+      strip_resource = meta_wayland_tablet_pad_strip_create_new_resource (strip,
+                                                                          client,
+                                                                          resource,
+                                                                          0);
+      zwp_tablet_pad_v1_send_strip (resource, strip_resource);
+    }
+
+  /* FIXME: zwp_tablet_pad_v1.modes */
+
+  zwp_tablet_pad_v1_send_done (resource);
+}
+
+struct wl_resource *
+meta_wayland_tablet_pad_create_new_resource (MetaWaylandTabletPad *pad,
+                                             struct wl_client     *client,
+                                             struct wl_resource   *seat_resource,
+                                             uint32_t              id)
+{
+  struct wl_resource *resource;
+
+  resource = wl_resource_create (client, &zwp_tablet_pad_v1_interface,
+                                 wl_resource_get_version (seat_resource), id);
+  wl_resource_set_implementation (resource, &pad_interface,
+                                  pad, unbind_resource);
+  wl_resource_set_user_data (resource, pad);
+  wl_list_insert (&pad->resource_list, wl_resource_get_link (resource));
+
+  return resource;
+}
+
+struct wl_resource *
+meta_wayland_tablet_pad_lookup_resource (MetaWaylandTabletPad *pad,
+                                         struct wl_client     *client)
+{
+  struct wl_resource *resource;
+
+  resource = wl_resource_find_for_client (&pad->resource_list, client);
+
+  if (!resource)
+    resource = wl_resource_find_for_client (&pad->focus_resource_list, client);
+
+  return resource;
+}
+
+static gboolean
+handle_pad_button_event (MetaWaylandTabletPad *pad,
+                         const ClutterEvent   *event)
+{
+  enum zwp_tablet_pad_v1_button_state button_state;
+  struct wl_list *l = &pad->focus_resource_list;
+  struct wl_resource *resource;
+
+  if (wl_list_empty (l))
+    return FALSE;
+
+  if (event->type == CLUTTER_PAD_BUTTON_PRESS)
+    button_state = ZWP_TABLET_TOOL_V1_BUTTON_STATE_PRESSED;
+  else if (event->type == CLUTTER_PAD_BUTTON_RELEASE)
+    button_state = ZWP_TABLET_TOOL_V1_BUTTON_STATE_RELEASED;
+  else
+    return FALSE;
+
+  wl_resource_for_each (resource, l)
+    {
+      zwp_tablet_pad_v1_send_button (resource,
+                                     clutter_event_get_time (event),
+                                     event->pad_button.button, button_state);
+    }
+
+  return TRUE;
+}
+
+static gboolean
+handle_pad_ring_event (MetaWaylandTabletPad *pad,
+                       const ClutterEvent   *event)
+{
+  MetaWaylandTabletPadRing *ring;
+
+  if (event->type != CLUTTER_PAD_RING)
+    return FALSE;
+
+  g_print (">>> %d\n", event->pad_ring.ring_number);
+
+  ring = g_list_nth_data (pad->rings, event->pad_ring.ring_number);
+
+  if (!ring)
+    return FALSE;
+
+  return meta_wayland_tablet_pad_ring_handle_event (ring, event);
+}
+
+static gboolean
+handle_pad_strip_event (MetaWaylandTabletPad *pad,
+                        const ClutterEvent   *event)
+{
+  MetaWaylandTabletPadStrip *strip;
+
+  if (event->type != CLUTTER_PAD_STRIP)
+    return FALSE;
+
+  strip = g_list_nth_data (pad->strips, event->pad_strip.strip_number);
+
+  if (!strip)
+    return FALSE;
+
+  return meta_wayland_tablet_pad_strip_handle_event (strip, event);
+}
+
+gboolean
+meta_wayland_tablet_pad_handle_event (MetaWaylandTabletPad *pad,
+                                      const ClutterEvent   *event)
+{
+  switch (clutter_event_type (event))
+    {
+    case CLUTTER_PAD_BUTTON_PRESS:
+    case CLUTTER_PAD_BUTTON_RELEASE:
+      return handle_pad_button_event (pad, event);
+    case CLUTTER_PAD_RING:
+      return handle_pad_ring_event (pad, event);
+    case CLUTTER_PAD_STRIP:
+      return handle_pad_strip_event (pad, event);
+    default:
+      return FALSE;
+    }
+}
+
+static void
+move_resources (struct wl_list *destination, struct wl_list *source)
+{
+  wl_list_insert_list (destination, source);
+  wl_list_init (source);
+}
+
+static void
+move_resources_for_client (struct wl_list *destination,
+                          struct wl_list *source,
+                          struct wl_client *client)
+{
+  struct wl_resource *resource, *tmp;
+
+  wl_resource_for_each_safe (resource, tmp, source)
+    {
+      if (wl_resource_get_client (resource) == client)
+        {
+          wl_list_remove (wl_resource_get_link (resource));
+          wl_list_insert (destination, wl_resource_get_link (resource));
+        }
+    }
+}
+
+static void
+meta_wayland_tablet_pad_update_rings_focus (MetaWaylandTabletPad *pad)
+{
+  GList *l;
+
+  g_print ("update rings focus %p: %d\n", pad, g_list_length (pad->rings));
+
+  for (l = pad->rings; l; l = l->next)
+    meta_wayland_tablet_pad_ring_sync_focus (l->data);
+}
+
+static void
+meta_wayland_tablet_pad_update_strips_focus (MetaWaylandTabletPad *pad)
+{
+  GList *l;
+
+  for (l = pad->strips; l; l = l->next)
+    meta_wayland_tablet_pad_strip_sync_focus (l->data);
+}
+
+void
+meta_wayland_tablet_pad_set_focus (MetaWaylandTabletPad *pad,
+                                   MetaWaylandSurface   *surface)
+{
+  if (pad->focus_surface == surface)
+    return;
+
+  if (pad->focus_surface != NULL)
+    {
+      struct wl_resource *resource;
+      struct wl_list *l = &pad->focus_resource_list;
+
+      if (!wl_list_empty (l))
+        {
+          struct wl_client *client = wl_resource_get_client (pad->focus_surface->resource);
+          struct wl_display *display = wl_client_get_display (client);
+          uint32_t serial = wl_display_next_serial (display);
+
+          wl_resource_for_each (resource, l)
+            {
+              zwp_tablet_pad_v1_send_leave (resource, serial, pad->focus_surface->resource);
+            }
+
+          move_resources (&pad->resource_list, &pad->focus_resource_list);
+        }
+
+      wl_list_remove (&pad->focus_surface_listener.link);
+      pad->focus_surface = NULL;
+    }
+
+  if (surface != NULL)
+    {
+      struct wl_resource *resource;
+      struct wl_list *l;
+
+      pad->focus_surface = surface;
+      wl_resource_add_destroy_listener (pad->focus_surface->resource, &pad->focus_surface_listener);
+
+      move_resources_for_client (&pad->focus_resource_list,
+                                 &pad->resource_list,
+                                 wl_resource_get_client (pad->focus_surface->resource));
+
+      l = &pad->focus_resource_list;
+      if (!wl_list_empty (l))
+        {
+          struct wl_client *client = wl_resource_get_client (pad->focus_surface->resource);
+          struct wl_display *display = wl_client_get_display (client);
+          pad->focus_serial = wl_display_next_serial (display);
+
+          wl_resource_for_each (resource, l)
+            {
+              zwp_tablet_pad_v1_send_enter (resource, pad->focus_serial,
+                                            pad->focus_surface->resource);
+            }
+        }
+    }
+
+  meta_wayland_tablet_pad_update_rings_focus (pad);
+  meta_wayland_tablet_pad_update_strips_focus (pad);
+}
diff --git a/src/wayland/meta-wayland-tablet-pad.h b/src/wayland/meta-wayland-tablet-pad.h
new file mode 100644
index 0000000..b416139
--- /dev/null
+++ b/src/wayland/meta-wayland-tablet-pad.h
@@ -0,0 +1,72 @@
+/*
+ * Wayland Support
+ *
+ * Copyright (C) 2016 Red Hat
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Carlos Garnacho <carlosg gnome org>
+ */
+
+#ifndef META_WAYLAND_TABLET_PAD_H
+#define META_WAYLAND_TABLET_PAD_H
+
+#include <wayland-server.h>
+
+#include <glib.h>
+
+#include "meta-wayland-types.h"
+#include "meta-cursor-renderer.h"
+
+struct _MetaWaylandTabletPad
+{
+  MetaWaylandTabletSeat *tablet_seat;
+  ClutterInputDevice *device;
+
+  struct wl_list resource_list;
+  struct wl_list focus_resource_list;
+
+  MetaWaylandSurface *focus_surface;
+  struct wl_listener focus_surface_listener;
+  uint32_t focus_serial;
+
+  GList *strips;
+  GList *rings;
+
+  MetaWaylandSurface *focus;
+};
+
+MetaWaylandTabletPad * meta_wayland_tablet_pad_new       (ClutterInputDevice    *device,
+                                                          MetaWaylandTabletSeat *tablet_seat);
+void                   meta_wayland_tablet_pad_free      (MetaWaylandTabletPad  *pad);
+
+struct wl_resource *
+             meta_wayland_tablet_pad_create_new_resource (MetaWaylandTabletPad *pad,
+                                                          struct wl_client     *client,
+                                                          struct wl_resource   *seat_resource,
+                                                          uint32_t              id);
+struct wl_resource *
+             meta_wayland_tablet_pad_lookup_resource     (MetaWaylandTabletPad *pad,
+                                                          struct wl_client     *client);
+
+void         meta_wayland_tablet_pad_notify              (MetaWaylandTabletPad *pad,
+                                                          struct wl_resource   *resource);
+
+gboolean     meta_wayland_tablet_pad_handle_event        (MetaWaylandTabletPad *pad,
+                                                          const ClutterEvent   *event);
+
+void         meta_wayland_tablet_pad_set_focus           (MetaWaylandTabletPad *pad,
+                                                          MetaWaylandSurface   *surface);
+
+#endif /* META_WAYLAND_TABLET_PAD_H */
diff --git a/src/wayland/meta-wayland-types.h b/src/wayland/meta-wayland-types.h
index b927768..0ff46f5 100644
--- a/src/wayland/meta-wayland-types.h
+++ b/src/wayland/meta-wayland-types.h
@@ -41,6 +41,7 @@ typedef struct _MetaWaylandTabletManager MetaWaylandTabletManager;
 typedef struct _MetaWaylandTabletSeat MetaWaylandTabletSeat;
 typedef struct _MetaWaylandTabletTool MetaWaylandTabletTool;
 typedef struct _MetaWaylandTablet MetaWaylandTablet;
+typedef struct _MetaWaylandTabletPad MetaWaylandTabletPad;
 typedef struct _MetaWaylandTabletPadStrip MetaWaylandTabletPadStrip;
 typedef struct _MetaWaylandTabletPadRing MetaWaylandTabletPadRing;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]