[Spice-devel] [PATCH 06/12] spicevmc: Introduce RedCharDeviceSpiceVmc GObject

Frediano Ziglio fziglio at redhat.com
Mon Mar 21 11:24:22 UTC 2016


From: Christophe Fergeau <cfergeau at redhat.com>

---
 server/spicevmc.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 88 insertions(+), 14 deletions(-)

diff --git a/server/spicevmc.c b/server/spicevmc.c
index f745fdb..0cf2cce 100644
--- a/server/spicevmc.c
+++ b/server/spicevmc.c
@@ -61,6 +61,41 @@ typedef struct SpiceVmcState {
     uint8_t port_opened;
 } SpiceVmcState;
 
+#define RED_TYPE_CHAR_DEVICE_SPICEVMC red_char_device_spicevmc_get_type()
+
+#define RED_CHAR_DEVICE_SPICEVMC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), RED_TYPE_CHAR_DEVICE_SPICEVMC, RedCharDeviceSpiceVmc))
+#define RED_CHAR_DEVICE_SPICEVMC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), RED_TYPE_CHAR_DEVICE_SPICEVMC, RedCharDeviceSpiceVmcClass))
+#define RED_IS_CHAR_DEVICE_SPICEVMC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), RED_TYPE_CHAR_DEVICE_SPICEVMC))
+#define RED_IS_CHAR_DEVICE_SPICEVMC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), RED_TYPE_CHAR_DEVICE_SPICEVMC))
+#define RED_CHAR_DEVICE_SPICEVMC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), RED_TYPE_CHAR_DEVICE_SPICEVMC, RedCharDeviceSpiceVmcClass))
+
+typedef struct RedCharDeviceSpiceVmc RedCharDeviceSpiceVmc;
+typedef struct RedCharDeviceSpiceVmcClass RedCharDeviceSpiceVmcClass;
+typedef struct RedCharDeviceSpiceVmcPrivate RedCharDeviceSpiceVmcPrivate;
+
+struct RedCharDeviceSpiceVmc {
+    RedCharDevice parent;
+    RedCharDeviceSpiceVmcPrivate *priv;
+};
+
+struct RedCharDeviceSpiceVmcClass
+{
+    RedCharDeviceClass parent_class;
+};
+
+struct RedCharDeviceSpiceVmcPrivate {
+    SpiceVmcPipeItem *pipe_item;
+};
+
+static GType red_char_device_spicevmc_get_type(void) G_GNUC_CONST;
+static RedCharDevice *red_char_device_spicevmc_new(SpiceCharDeviceInstance *sin,
+                                                   RedsState *reds,
+                                                   void *opaque);
+
+G_DEFINE_TYPE(RedCharDeviceSpiceVmc, red_char_device_spicevmc, RED_TYPE_CHAR_DEVICE)
+
+#define RED_CHAR_DEVICE_SPICEVMC_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RED_TYPE_CHAR_DEVICE_SPICEVMC, RedCharDeviceSpiceVmcPrivate))
+
 typedef struct PortInitPipeItem {
     PipeItem base;
     char* name;
@@ -507,7 +542,6 @@ SpiceCharDeviceState *spicevmc_device_connect(RedsState *reds,
     SpiceVmcState *state;
     ChannelCbs channel_cbs = { NULL, };
     ClientCbs client_cbs = { NULL, };
-    SpiceCharDeviceCallbacks char_dev_cbs = {NULL, };
 
     channel_cbs.config_socket = spicevmc_red_channel_client_config_socket;
     channel_cbs.on_disconnect = spicevmc_red_channel_client_on_disconnect;
@@ -530,19 +564,7 @@ SpiceCharDeviceState *spicevmc_device_connect(RedsState *reds,
     client_cbs.connect = spicevmc_connect;
     red_channel_register_client_cbs(&state->channel, &client_cbs, NULL);
 
-    char_dev_cbs.read_one_msg_from_device = spicevmc_chardev_read_msg_from_dev;
-    char_dev_cbs.ref_msg_to_client = spicevmc_chardev_ref_msg_to_client;
-    char_dev_cbs.unref_msg_to_client = spicevmc_chardev_unref_msg_to_client;
-    char_dev_cbs.send_msg_to_client = spicevmc_chardev_send_msg_to_client;
-    char_dev_cbs.send_tokens_to_client = spicevmc_char_dev_send_tokens_to_client;
-    char_dev_cbs.remove_client = spicevmc_char_dev_remove_client;
-
-    state->chardev_st = spice_char_device_state_create(sin,
-                                                       reds,
-                                                       0, /* tokens interval */
-                                                       ~0, /* self tokens */
-                                                       &char_dev_cbs,
-                                                       state);
+    state->chardev_st = red_char_device_spicevmc_new(sin, reds, state);
     state->chardev_sin = sin;
 
     reds_register_channel(reds, &state->channel);
@@ -590,3 +612,55 @@ SPICE_GNUC_VISIBLE void spice_server_port_event(SpiceCharDeviceInstance *sin, ui
 
     spicevmc_port_send_event(state->rcc, event);
 }
+
+static void
+red_char_device_spicevmc_finalize(GObject *object)
+{
+    RedCharDeviceSpiceVmc *self = RED_CHAR_DEVICE_SPICEVMC(object);
+
+    free(self->priv->pipe_item);
+}
+
+static void
+red_char_device_spicevmc_class_init(RedCharDeviceSpiceVmcClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    g_type_class_add_private(klass, sizeof (RedCharDeviceSpiceVmcPrivate));
+
+    object_class->finalize = red_char_device_spicevmc_finalize;
+}
+
+static void
+red_char_device_spicevmc_init(RedCharDeviceSpiceVmc *self)
+{
+    self->priv = RED_CHAR_DEVICE_SPICEVMC_PRIVATE(self);
+}
+
+static RedCharDevice *
+red_char_device_spicevmc_new(SpiceCharDeviceInstance *sin,
+                             RedsState *reds,
+                             void *opaque)
+{
+    RedCharDevice *char_dev;
+    SpiceCharDeviceCallbacks char_dev_cbs = {
+        .read_one_msg_from_device = spicevmc_chardev_read_msg_from_dev,
+        .ref_msg_to_client = spicevmc_chardev_ref_msg_to_client,
+        .unref_msg_to_client = spicevmc_chardev_unref_msg_to_client,
+        .send_msg_to_client = spicevmc_chardev_send_msg_to_client,
+        .send_tokens_to_client = spicevmc_char_dev_send_tokens_to_client,
+        .remove_client = spicevmc_char_dev_remove_client,
+    };
+
+    char_dev = g_object_new(RED_TYPE_CHAR_DEVICE_SPICEVMC,
+                            "sin", sin,
+                            "spice-server", reds,
+                            "client-tokens-interval", 0ULL,
+                            "self-tokens", ~0ULL,
+                            "opaque", opaque,
+                            NULL);
+
+    red_char_device_set_callbacks(RED_CHAR_DEVICE(char_dev),
+                                  &char_dev_cbs, opaque);
+    return char_dev;
+}
-- 
2.5.5



More information about the Spice-devel mailing list