[Spice-devel] [PATCH spice-gtk 4/5] main: simplify usage of VD_AGENT_HAS_CAPABILITY

Marc-André Lureau marcandre.lureau at gmail.com
Wed Nov 6 13:25:11 PST 2013


From: Marc-André Lureau <marcandre.lureau at redhat.com>

Introduce a helper function test_agent_cap() to clear the code a
little bit.
---
 gtk/channel-main.c | 60 ++++++++++++++++++++++++------------------------------
 1 file changed, 27 insertions(+), 33 deletions(-)

diff --git a/gtk/channel-main.c b/gtk/channel-main.c
index cc64274..2887208 100644
--- a/gtk/channel-main.c
+++ b/gtk/channel-main.c
@@ -201,6 +201,16 @@ static const char *agent_caps[] = {
 
 /* ------------------------------------------------------------------ */
 
+static gboolean test_agent_cap(SpiceMainChannel *channel, guint32 cap)
+{
+    SpiceMainChannelPrivate *c = channel->priv;
+
+    if (!c->agent_caps_received)
+        return FALSE;
+
+    return VD_AGENT_HAS_CAPABILITY(c->agent_caps, G_N_ELEMENTS(c->agent_caps), cap);
+}
+
 static void spice_main_channel_reset_capabilties(SpiceChannel *channel)
 {
     spice_channel_set_capability(SPICE_CHANNEL(channel), SPICE_MAIN_CAP_SEMI_SEAMLESS_MIGRATE);
@@ -1224,9 +1234,6 @@ static void agent_announce_caps(SpiceMainChannel *channel)
     free(caps);
 }
 
-#define HAS_CLIPBOARD_SELECTION(c) \
-    VD_AGENT_HAS_CAPABILITY((c)->agent_caps, G_N_ELEMENTS((c)->agent_caps), VD_AGENT_CAP_CLIPBOARD_SELECTION)
-
 /* any context: the message is not flushed immediately,
    you can wakeup() the channel coroutine or send_msg_queue() */
 static void agent_clipboard_grab(SpiceMainChannel *channel, guint selection,
@@ -1241,13 +1248,12 @@ static void agent_clipboard_grab(SpiceMainChannel *channel, guint selection,
     if (!c->agent_connected)
         return;
 
-    g_return_if_fail(VD_AGENT_HAS_CAPABILITY(c->agent_caps,
-        G_N_ELEMENTS(c->agent_caps), VD_AGENT_CAP_CLIPBOARD_BY_DEMAND));
+    g_return_if_fail(test_agent_cap(channel, VD_AGENT_CAP_CLIPBOARD_BY_DEMAND));
 
     size = sizeof(VDAgentClipboardGrab) + sizeof(uint32_t) * ntypes;
-    if (HAS_CLIPBOARD_SELECTION(c))
+    if (test_agent_cap(channel, VD_AGENT_CAP_CLIPBOARD_SELECTION)) {
         size += 4;
-    else if (selection != VD_AGENT_CLIPBOARD_SELECTION_CLIPBOARD) {
+    } else if (selection != VD_AGENT_CLIPBOARD_SELECTION_CLIPBOARD) {
         CHANNEL_DEBUG(channel, "Ignoring clipboard grab");
         return;
     }
@@ -1257,7 +1263,7 @@ static void agent_clipboard_grab(SpiceMainChannel *channel, guint selection,
 
     grab = (VDAgentClipboardGrab *)msg;
 
-    if (HAS_CLIPBOARD_SELECTION(c)) {
+    if (test_agent_cap(channel, VD_AGENT_CAP_CLIPBOARD_SELECTION)) {
         msg[0] = selection;
         grab = (VDAgentClipboardGrab *)(msg + 4);
     }
@@ -1280,16 +1286,13 @@ static void agent_clipboard_notify(SpiceMainChannel *channel, guint selection,
     size_t msgsize;
 
     g_return_if_fail(c->agent_connected);
-
-    g_return_if_fail(VD_AGENT_HAS_CAPABILITY(c->agent_caps,
-        G_N_ELEMENTS(c->agent_caps), VD_AGENT_CAP_CLIPBOARD_BY_DEMAND));
-
+    g_return_if_fail(test_agent_cap(channel, VD_AGENT_CAP_CLIPBOARD_BY_DEMAND));
     g_return_if_fail(c->max_clipboard == -1 || size < c->max_clipboard);
 
     msgsize = sizeof(VDAgentClipboard);
-    if (HAS_CLIPBOARD_SELECTION(c))
+    if (test_agent_cap(channel, VD_AGENT_CAP_CLIPBOARD_SELECTION)) {
         msgsize += 4;
-    else if (selection != VD_AGENT_CLIPBOARD_SELECTION_CLIPBOARD) {
+    } else if (selection != VD_AGENT_CLIPBOARD_SELECTION_CLIPBOARD) {
         CHANNEL_DEBUG(channel, "Ignoring clipboard notify");
         return;
     }
@@ -1299,7 +1302,7 @@ static void agent_clipboard_notify(SpiceMainChannel *channel, guint selection,
 
     cb = (VDAgentClipboard *)msg;
 
-    if (HAS_CLIPBOARD_SELECTION(c)) {
+    if (test_agent_cap(channel, VD_AGENT_CAP_CLIPBOARD_SELECTION)) {
         msg[0] = selection;
         cb = (VDAgentClipboard *)(msg + 4);
     }
@@ -1318,14 +1321,12 @@ static void agent_clipboard_request(SpiceMainChannel *channel, guint selection,
     size_t msgsize;
 
     g_return_if_fail(c->agent_connected);
-
-    g_return_if_fail(VD_AGENT_HAS_CAPABILITY(c->agent_caps,
-        G_N_ELEMENTS(c->agent_caps), VD_AGENT_CAP_CLIPBOARD_BY_DEMAND));
+    g_return_if_fail(test_agent_cap(channel, VD_AGENT_CAP_CLIPBOARD_BY_DEMAND));
 
     msgsize = sizeof(VDAgentClipboardRequest);
-    if (HAS_CLIPBOARD_SELECTION(c))
+    if (test_agent_cap(channel, VD_AGENT_CAP_CLIPBOARD_SELECTION)) {
         msgsize += 4;
-    else if (selection != VD_AGENT_CLIPBOARD_SELECTION_CLIPBOARD) {
+    } else if (selection != VD_AGENT_CLIPBOARD_SELECTION_CLIPBOARD) {
         SPICE_DEBUG("Ignoring clipboard request");
         return;
     }
@@ -1335,7 +1336,7 @@ static void agent_clipboard_request(SpiceMainChannel *channel, guint selection,
 
     request = (VDAgentClipboardRequest *)msg;
 
-    if (HAS_CLIPBOARD_SELECTION(c)) {
+    if (test_agent_cap(channel, VD_AGENT_CAP_CLIPBOARD_SELECTION)) {
         msg[0] = selection;
         request = (VDAgentClipboardRequest *)(msg + 4);
     }
@@ -1354,11 +1355,9 @@ static void agent_clipboard_release(SpiceMainChannel *channel, guint selection)
     guint8 msgsize = 0;
 
     g_return_if_fail(c->agent_connected);
+    g_return_if_fail(test_agent_cap(channel, VD_AGENT_CAP_CLIPBOARD_BY_DEMAND));
 
-    g_return_if_fail(VD_AGENT_HAS_CAPABILITY(c->agent_caps,
-        G_N_ELEMENTS(c->agent_caps), VD_AGENT_CAP_CLIPBOARD_BY_DEMAND));
-
-    if (HAS_CLIPBOARD_SELECTION(c)) {
+    if (test_agent_cap(channel, VD_AGENT_CAP_CLIPBOARD_SELECTION)) {
         msg[0] = selection;
         msgsize += 4;
     } else if (selection != VD_AGENT_CLIPBOARD_SELECTION_CLIPBOARD) {
@@ -1814,7 +1813,7 @@ static void main_agent_handle_msg(SpiceChannel *channel,
     case VD_AGENT_CLIPBOARD_REQUEST:
     case VD_AGENT_CLIPBOARD_GRAB:
     case VD_AGENT_CLIPBOARD:
-        if (VD_AGENT_HAS_CAPABILITY(c->agent_caps, G_N_ELEMENTS(c->agent_caps), VD_AGENT_CAP_CLIPBOARD_SELECTION)) {
+        if (test_agent_cap(self, VD_AGENT_CAP_CLIPBOARD_SELECTION)) {
             selection = *((guint8*)payload);
             payload = ((guint8*)payload) + 4;
             msg->size -= 4;
@@ -1847,7 +1846,7 @@ static void main_agent_handle_msg(SpiceChannel *channel,
         if (caps->request)
             agent_announce_caps(self);
 
-        if (VD_AGENT_HAS_CAPABILITY(caps->caps, G_N_ELEMENTS(c->agent_caps), VD_AGENT_CAP_DISPLAY_CONFIG) &&
+        if (test_agent_cap(self, VD_AGENT_CAP_DISPLAY_CONFIG) &&
             !c->agent_display_config_sent) {
             agent_display_config(self);
             agent_send_msg_queue(self);
@@ -2427,12 +2426,7 @@ gboolean spice_main_agent_test_capability(SpiceMainChannel *channel, guint32 cap
 {
     g_return_val_if_fail(SPICE_IS_MAIN_CHANNEL(channel), FALSE);
 
-    SpiceMainChannelPrivate *c = channel->priv;
-
-    if (!c->agent_caps_received)
-        return FALSE;
-
-    return VD_AGENT_HAS_CAPABILITY(c->agent_caps, G_N_ELEMENTS(c->agent_caps), cap);
+    return test_agent_cap(channel, cap);
 }
 
 /**
-- 
1.8.3.1



More information about the Spice-devel mailing list