[retro-gtk/wip/aplazas/master: 9/10] Port InputDevice to C



commit fa2b5160498c3e5e615f9cd185c9d5bb36341d90
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Tue Sep 5 15:55:10 2017 +0200

    Port InputDevice to C

 retro-gtk/Makefile.am                    |    3 +-
 retro-gtk/input/input-device.vala        |   14 ----
 retro-gtk/retro-core-view-input-device.c |    6 +-
 retro-gtk/retro-core-view-input-device.h |    1 +
 retro-gtk/retro-gtk.h                    |    1 +
 retro-gtk/retro-input-device.c           |  108 ++++++++++++++++++++++++++++++
 retro-gtk/retro-input-device.h           |   42 ++++++++++++
 retro-gtk/vapi/retro-gtk-c.vapi          |    8 ++
 8 files changed, 166 insertions(+), 17 deletions(-)
---
diff --git a/retro-gtk/Makefile.am b/retro-gtk/Makefile.am
index 2a50b6f..4a6abbd 100644
--- a/retro-gtk/Makefile.am
+++ b/retro-gtk/Makefile.am
@@ -35,6 +35,7 @@ retro_gtk_public_h_sources = \
        retro-device-type.h \
        retro-gtk.h \
        retro-gtk-vala.h \
+       retro-input-device.h \
        retro-joypad-id.h \
        retro-lightgun-id.h \
        retro-mouse-id.h \
@@ -63,7 +64,6 @@ libretro_gtk_la_SOURCES = \
        \
        input/controller.vala \
        input/input.vala \
-       input/input-device.vala \
        input/input-device-manager.vala \
        input/retro-keyboard-key.c \
        \
@@ -83,6 +83,7 @@ libretro_gtk_la_SOURCES = \
        retro-device-type.c \
        retro-game-info.c \
        retro-input-descriptor.c \
+       retro-input-device.c \
        retro-joypad-id.c \
        retro-lightgun-id.c \
        retro-log.c \
diff --git a/retro-gtk/retro-core-view-input-device.c b/retro-gtk/retro-core-view-input-device.c
index 9017048..4f55f98 100644
--- a/retro-gtk/retro-core-view-input-device.c
+++ b/retro-gtk/retro-core-view-input-device.c
@@ -2,6 +2,8 @@
 
 #include "retro-core-view-input-device.h"
 
+#include "retro-input-device.h"
+
 struct _RetroCoreViewInputDevice
 {
   GObject parent_instance;
@@ -9,7 +11,7 @@ struct _RetroCoreViewInputDevice
   RetroDeviceType device_type;
 };
 
-static void retro_input_device_interface_init (RetroInputDeviceIface   *iface);
+static void retro_input_device_interface_init (RetroInputDeviceInterface *iface);
 
 G_DEFINE_TYPE_WITH_CODE (RetroCoreViewInputDevice, retro_core_view_input_device, G_TYPE_OBJECT,
                          G_IMPLEMENT_INTERFACE (RETRO_TYPE_INPUT_DEVICE,
@@ -106,7 +108,7 @@ retro_core_view_input_device_init (RetroCoreViewInputDevice *self)
 }
 
 static void
-retro_input_device_interface_init (RetroInputDeviceIface *iface)
+retro_input_device_interface_init (RetroInputDeviceInterface *iface)
 {
   iface->poll = retro_core_view_input_device_poll;
   iface->get_input_state =  retro_core_view_input_device_get_input_state;
diff --git a/retro-gtk/retro-core-view-input-device.h b/retro-gtk/retro-core-view-input-device.h
index faa3036..d772f2e 100644
--- a/retro-gtk/retro-core-view-input-device.h
+++ b/retro-gtk/retro-core-view-input-device.h
@@ -8,6 +8,7 @@
 #endif
 
 #include <glib-object.h>
+#include "retro-device-type.h"
 #include "retro-gtk-internal.h"
 
 G_BEGIN_DECLS
diff --git a/retro-gtk/retro-gtk.h b/retro-gtk/retro-gtk.h
index 1e4c59b..f4c552d 100644
--- a/retro-gtk/retro-gtk.h
+++ b/retro-gtk/retro-gtk.h
@@ -14,6 +14,7 @@
 #include "retro-device-type.h"
 #include "retro-gtk-vala.h"
 #include "retro-input-descriptor.h"
+#include "retro-input-device.h"
 #include "retro-joypad-id.h"
 #include "retro-lightgun-id.h"
 #include "retro-mouse-id.h"
diff --git a/retro-gtk/retro-input-device.c b/retro-gtk/retro-input-device.c
new file mode 100644
index 0000000..c274e57
--- /dev/null
+++ b/retro-gtk/retro-input-device.c
@@ -0,0 +1,108 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#include "retro-input-device.h"
+
+G_DEFINE_INTERFACE (RetroInputDevice, retro_input_device, G_TYPE_OBJECT);
+
+static void
+retro_input_device_default_init (RetroInputDeviceInterface *iface)
+{
+}
+
+/**
+ * retro_input_device_poll:
+ * @self: a #RetroInputDevice
+ *
+ * Polls the pending input events for @self.
+ */
+void
+retro_input_device_poll (RetroInputDevice *self)
+{
+  RetroInputDeviceInterface *iface;
+
+  g_return_if_fail (RETRO_IS_INPUT_DEVICE (self));
+
+  iface = RETRO_INPUT_DEVICE_GET_IFACE (self);
+
+  g_return_if_fail (iface->poll != NULL);
+
+  iface->poll (self);
+}
+
+/**
+ * retro_input_device_get_input_state:
+ * @self: a #RetroInputDevice
+ * @device: an #RetroDeviceType to query @self
+ * @index: an input index to interpret depending on @device
+ * @id: an input id to interpret depending on @device
+ *
+ * Gets the state of an input of @self.
+ *
+ * Returns: the input's state
+ */
+gint16
+retro_input_device_get_input_state (RetroInputDevice *self,
+                                    RetroDeviceType   device,
+                                    guint             index,
+                                    guint             id)
+{
+  RetroInputDeviceInterface *iface;
+
+  g_return_val_if_fail (RETRO_IS_INPUT_DEVICE (self), 0);
+
+  iface = RETRO_INPUT_DEVICE_GET_IFACE (self);
+
+  g_return_val_if_fail (iface->get_input_state != NULL, 0);
+
+  return iface->get_input_state (self, device, index, id);
+}
+
+/**
+ * retro_input_device_get_device_type:
+ * @self: a #RetroInputDevice
+ *
+ * Gets the main type of the controller.
+ *
+ * Returns: the type of @self
+ */
+RetroDeviceType
+retro_input_device_get_device_type (RetroInputDevice *self)
+{
+
+  RetroInputDeviceInterface *iface;
+
+  g_return_val_if_fail (RETRO_IS_INPUT_DEVICE (self), RETRO_DEVICE_TYPE_NONE);
+
+  iface = RETRO_INPUT_DEVICE_GET_IFACE (self);
+
+  g_return_val_if_fail (iface->get_device_type != NULL, RETRO_DEVICE_TYPE_NONE);
+
+  return iface->get_device_type (self);
+}
+
+/**
+ * retro_input_device_get_device_capabilities:
+ * @self: a #RetroInputDevice
+ *
+ * Gets a flag representing the capabilities of @self. Each bit index matches
+ * the #RetroDeviceType of same number.
+ *
+ * For example, if @self is an analog gamepad, the value would be: (1 <<
+ * RETRO_DEVICE_TYPE_JOYPAD) | (1 << RETRO_DEVICE_TYPE_ANALOG).
+ *
+ * Returns: the capabilities flag of @self
+ */
+guint64
+retro_input_device_get_device_capabilities (RetroInputDevice *self)
+{
+
+  RetroInputDeviceInterface *iface;
+
+  g_return_val_if_fail (RETRO_IS_INPUT_DEVICE (self), 0);
+
+  iface = RETRO_INPUT_DEVICE_GET_IFACE (self);
+
+  g_return_val_if_fail (iface->get_device_capabilities != NULL, 0);
+
+  return iface->get_device_capabilities (self);
+}
diff --git a/retro-gtk/retro-input-device.h b/retro-gtk/retro-input-device.h
new file mode 100644
index 0000000..3b3e6e2
--- /dev/null
+++ b/retro-gtk/retro-input-device.h
@@ -0,0 +1,42 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#ifndef RETRO_INPUT_DEVICE_H
+#define RETRO_INPUT_DEVICE_H
+
+#if !defined(__RETRO_GTK_INSIDE__) && !defined(RETRO_GTK_COMPILATION)
+# error "Only <retro-gtk.h> can be included directly."
+#endif
+
+#include <glib-object.h>
+#include "retro-device-type.h"
+
+G_BEGIN_DECLS
+
+#define RETRO_TYPE_INPUT_DEVICE (retro_input_device_get_type())
+
+G_DECLARE_INTERFACE (RetroInputDevice, retro_input_device, RETRO, INPUT_DEVICE, GObject)
+
+struct _RetroInputDeviceInterface
+{
+  GTypeInterface parent_iface;
+
+  void (*poll) (RetroInputDevice *self);
+  gint16 (*get_input_state) (RetroInputDevice *self,
+                             RetroDeviceType   device,
+                             guint             index,
+                             guint             id);
+  RetroDeviceType (*get_device_type) (RetroInputDevice *self);
+  guint64 (*get_device_capabilities) (RetroInputDevice *self);
+};
+
+void retro_input_device_poll (RetroInputDevice *self);
+gint16 retro_input_device_get_input_state (RetroInputDevice *self,
+                                           RetroDeviceType   device,
+                                           guint             index,
+                                           guint             id);
+RetroDeviceType retro_input_device_get_device_type (RetroInputDevice *self);
+guint64 retro_input_device_get_device_capabilities (RetroInputDevice *self);
+
+G_END_DECLS
+
+#endif /* RETRO_INPUT_DEVICE_H */
diff --git a/retro-gtk/vapi/retro-gtk-c.vapi b/retro-gtk/vapi/retro-gtk-c.vapi
index ff5a3d5..c22ec5c 100644
--- a/retro-gtk/vapi/retro-gtk-c.vapi
+++ b/retro-gtk/vapi/retro-gtk-c.vapi
@@ -12,6 +12,14 @@ public enum Retro.DeviceType {
        POINTER = 6,
 }
 
+[CCode (cheader_filename = "retro-input-device.h")]
+public interface Retro.InputDevice : GLib.Object {
+       public abstract void  poll ();
+       public abstract int16 get_input_state (DeviceType device, uint index, uint id);
+       public abstract DeviceType get_device_type ();
+       public abstract uint64 get_device_capabilities ();
+}
+
 [CCode (cheader_filename = "retro-input-descriptor.h")]
 public struct Retro.InputDescriptor {
        uint port;


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