<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Wed, Sep 27, 2017 at 1:02 PM Frediano Ziglio <<a href="mailto:fziglio@redhat.com">fziglio@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">><br>
> Related to ongoing work to use GMainLoop and GTK integration<br>
><br>
> Signed-off-by: Victor Toso <<a href="mailto:victortoso@redhat.com" target="_blank">victortoso@redhat.com</a>><br>
> ---<br>
>  Makefile.am           |   3 +<br>
>  <a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a>          |   1 +<br>
>  src/vdagent/vdagent.c | 149<br>
>  +++++++++++++++++++++++++++++++-------------------<br>
>  src/vdagent/vdagent.h |  54 ++++++++++++++++++<br>
>  4 files changed, 150 insertions(+), 57 deletions(-)<br>
>  create mode 100644 src/vdagent/vdagent.h<br>
><br>
> diff --git a/Makefile.am b/Makefile.am<br>
> index 45f7177..efd058a 100644<br>
> --- a/Makefile.am<br>
> +++ b/Makefile.am<br>
> @@ -14,6 +14,7 @@ common_sources =                            \<br>
>  src_spice_vdagent_CFLAGS =                   \<br>
>       $(X_CFLAGS)                             \<br>
>       $(SPICE_CFLAGS)                         \<br>
> +     $(GOBJECT2_CFLAGS)                      \<br>
>       $(GLIB2_CFLAGS)                         \<br>
>       $(ALSA_CFLAGS)                          \<br>
>       -I$(srcdir)/src                         \<br>
> @@ -23,6 +24,7 @@ src_spice_vdagent_CFLAGS =                  \<br>
>  src_spice_vdagent_LDADD =                    \<br>
>       $(X_LIBS)                               \<br>
>       $(SPICE_LIBS)                           \<br>
> +     $(GOBJECT2_LIBS)                        \<br>
>       $(GLIB2_LIBS)                           \<br>
>       $(ALSA_LIBS)                            \<br>
>       $(NULL)<br>
> @@ -38,6 +40,7 @@ src_spice_vdagent_SOURCES =                 \<br>
>       src/vdagent/x11.c                       \<br>
>       src/vdagent/x11.h                       \<br>
>       src/vdagent/vdagent.c                   \<br>
> +     src/vdagent/vdagent.h                   \<br>
>       $(NULL)<br>
><br>
>  src_spice_vdagentd_CFLAGS =                  \<br>
> diff --git a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a> b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
> index d92b527..12b7d23 100644<br>
> --- a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
> +++ b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
> @@ -86,6 +86,7 @@ AC_ARG_ENABLE([static-uinput],<br>
>                [enable_static_uinput="no"])<br>
><br>
>  PKG_CHECK_MODULES([GLIB2], [glib-2.0 >= 2.34])<br>
> +PKG_CHECK_MODULES(GOBJECT2, [gobject-2.0])<br>
>  PKG_CHECK_MODULES(X, [xfixes xrandr >= 1.3 xinerama x11])<br>
>  PKG_CHECK_MODULES(SPICE, [spice-protocol >= 0.12.13])<br>
>  PKG_CHECK_MODULES(ALSA, [alsa >= 1.0.22])<br>
> diff --git a/src/vdagent/vdagent.c b/src/vdagent/vdagent.c<br>
> index 0b6df3f..cdbda8b 100644<br>
> --- a/src/vdagent/vdagent.c<br>
> +++ b/src/vdagent/vdagent.c<br>
> @@ -37,16 +37,13 @@<br>
>  #include <glib.h><br>
>  #include <poll.h><br>
><br>
> -#include "udscs.h"<br>
>  #include "vdagentd-proto.h"<br>
>  #include "vdagentd-proto-strings.h"<br>
>  #include "audio.h"<br>
> -#include "x11.h"<br>
> -#include "file-xfers.h"<br>
> +#include "vdagent.h"<br>
> +<br>
> +G_DEFINE_TYPE (SpiceVDAgent, spice_vdagent, G_TYPE_OBJECT);<br>
><br>
<br>
Lot of functions use already the vdagent_ prefix.<br>
Would not easier to call the class just VDAgent ?<br>
<br></blockquote><div>Victor used "SessionAgent" in the earlier version, what about that?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> -static struct vdagent_x11 *x11 = NULL;<br>
> -static struct vdagent_file_xfers *vdagent_file_xfers = NULL;<br>
> -static struct udscs_connection *client = NULL;<br>
>  static int quit = 0;<br>
>  static int version_mismatch = 0;<br>
><br>
> @@ -90,12 +87,12 @@ static GOptionEntry entries[] = {<br>
>   * Return path where transferred files should be stored.<br>
>   * Returned path should not be freed or modified.<br>
>   **/<br>
> -static const gchar *xfer_get_download_directory(void)<br>
> +static const gchar *xfer_get_download_directory(SpiceVDAgent *agent)<br>
>  {<br>
>      if (fx_dir != NULL)<br>
>          return fx_dir;<br>
><br>
> -    if (vdagent_x11_has_icons_on_desktop(x11))<br>
> +    if (vdagent_x11_has_icons_on_desktop(agent->x11))<br>
>          return g_get_user_special_dir(G_USER_DIRECTORY_DESKTOP);<br>
><br>
>      return g_get_user_special_dir(G_USER_DIRECTORY_DOWNLOAD);<br>
> @@ -105,61 +102,63 @@ static const gchar *xfer_get_download_directory(void)<br>
>   * vdagent_init_file_xfer<br>
>   *<br>
>   * Initialize handler for file xfer,<br>
> - * return TRUE on success (vdagent_file_xfers is not NULL).<br>
> + * return TRUE on success (agent->xfers is not NULL).<br>
>   **/<br>
> -static gboolean vdagent_init_file_xfer(void)<br>
> +static gboolean vdagent_init_file_xfer(SpiceVDAgent *agent)<br>
>  {<br>
>      const gchar *xfer_dir;<br>
><br>
> -    if (vdagent_file_xfers != NULL) {<br>
> +    if (agent->xfers != NULL) {<br>
>          syslog(LOG_DEBUG, "File-xfer already initialized");<br>
>          return TRUE;<br>
>      }<br>
><br>
> -    xfer_dir = xfer_get_download_directory();<br>
> +    xfer_dir = xfer_get_download_directory(agent);<br>
>      if (xfer_dir == NULL) {<br>
>          syslog(LOG_WARNING,<br>
>                 "warning could not get file xfer save dir, "<br>
>                 "file transfers will be disabled");<br>
> -        vdagent_file_xfers = NULL;<br>
>          return FALSE;<br>
>      }<br>
><br>
> -    vdagent_file_xfers = vdagent_file_xfers_create(client, xfer_dir,<br>
> -                                                   fx_open_dir, debug);<br>
> -    return (vdagent_file_xfers != NULL);<br>
> +    agent->xfers = vdagent_file_xfers_create(agent->conn, xfer_dir,<br>
> +                                             fx_open_dir, debug);<br>
> +    return (agent->xfers != NULL);<br>
>  }<br>
><br>
> -static gboolean vdagent_finalize_file_xfer(void)<br>
> +static gboolean vdagent_finalize_file_xfer(SpiceVDAgent *agent)<br>
>  {<br>
> -    if (vdagent_file_xfers == NULL)<br>
> +    if (agent->xfers == NULL)<br>
>          return FALSE;<br>
><br>
> -    g_clear_pointer(&vdagent_file_xfers, vdagent_file_xfers_destroy);<br>
> +    g_clear_pointer(&agent->xfers, vdagent_file_xfers_destroy);<br>
>      return TRUE;<br>
>  }<br>
><br>
>  static void daemon_read_complete(struct udscs_connection **connp,<br>
>      struct udscs_message_header *header, uint8_t *data,<br>
> -    G_GNUC_UNUSED void *user_data)<br>
> +    void *user_data)<br>
>  {<br>
> +    g_return_if_fail(SPICE_IS_VDAGENT(user_data));<br>
> +    SpiceVDAgent *agent = user_data;<br>
> +<br>
>      switch (header->type) {<br>
>      case VDAGENTD_MONITORS_CONFIG:<br>
> -        vdagent_x11_set_monitor_config(x11, (VDAgentMonitorsConfig *)data,<br>
> 0);<br>
> +        vdagent_x11_set_monitor_config(agent->x11, (VDAgentMonitorsConfig<br>
> *)data, 0);<br>
>          break;<br>
>      case VDAGENTD_CLIPBOARD_REQUEST:<br>
> -        vdagent_x11_clipboard_request(x11, header->arg1, header->arg2);<br>
> +        vdagent_x11_clipboard_request(agent->x11, header->arg1,<br>
> header->arg2);<br>
>          break;<br>
>      case VDAGENTD_CLIPBOARD_GRAB:<br>
> -        vdagent_x11_clipboard_grab(x11, header->arg1, (uint32_t *)data,<br>
> +        vdagent_x11_clipboard_grab(agent->x11, header->arg1, (uint32_t<br>
> *)data,<br>
>                                     header->size / sizeof(uint32_t));<br>
>          break;<br>
>      case VDAGENTD_CLIPBOARD_DATA:<br>
> -        vdagent_x11_clipboard_data(x11, header->arg1, header->arg2,<br>
> +        vdagent_x11_clipboard_data(agent->x11, header->arg1, header->arg2,<br>
>                                     data, header->size);<br>
>          break;<br>
>      case VDAGENTD_CLIPBOARD_RELEASE:<br>
> -        vdagent_x11_clipboard_release(x11, header->arg1);<br>
> +        vdagent_x11_clipboard_release(agent->x11, header->arg1);<br>
>          break;<br>
>      case VDAGENTD_VERSION:<br>
>          if (strcmp((char *)data, VERSION) != 0) {<br>
> @@ -170,8 +169,8 @@ static void daemon_read_complete(struct udscs_connection<br>
> **connp,<br>
>          }<br>
>          break;<br>
>      case VDAGENTD_FILE_XFER_START:<br>
> -        if (vdagent_file_xfers != NULL) {<br>
> -            vdagent_file_xfers_start(vdagent_file_xfers,<br>
> +        if (agent->xfers != NULL) {<br>
> +            vdagent_file_xfers_start(agent->xfers,<br>
>                                       (VDAgentFileXferStartMessage *)data);<br>
>          } else {<br>
>              vdagent_file_xfers_error_disabled(*connp,<br>
> @@ -179,8 +178,8 @@ static void daemon_read_complete(struct udscs_connection<br>
> **connp,<br>
>          }<br>
>          break;<br>
>      case VDAGENTD_FILE_XFER_STATUS:<br>
> -        if (vdagent_file_xfers != NULL) {<br>
> -            vdagent_file_xfers_status(vdagent_file_xfers,<br>
> +        if (agent->xfers != NULL) {<br>
> +            vdagent_file_xfers_status(agent->xfers,<br>
>                                        (VDAgentFileXferStatusMessage *)data);<br>
>          } else {<br>
>              vdagent_file_xfers_error_disabled(*connp,<br>
> @@ -191,7 +190,7 @@ static void daemon_read_complete(struct udscs_connection<br>
> **connp,<br>
>          if (debug)<br>
>              syslog(LOG_DEBUG, "Disabling file-xfers");<br>
><br>
> -        vdagent_finalize_file_xfer();<br>
> +        vdagent_finalize_file_xfer(agent);<br>
>          break;<br>
>      case VDAGENTD_AUDIO_VOLUME_SYNC: {<br>
>          VDAgentAudioVolumeSync *avs = (VDAgentAudioVolumeSync *)data;<br>
> @@ -203,8 +202,8 @@ static void daemon_read_complete(struct udscs_connection<br>
> **connp,<br>
>          break;<br>
>      }<br>
>      case VDAGENTD_FILE_XFER_DATA:<br>
> -        if (vdagent_file_xfers != NULL) {<br>
> -            vdagent_file_xfers_data(vdagent_file_xfers,<br>
> +        if (agent->xfers != NULL) {<br>
> +            vdagent_file_xfers_data(agent->xfers,<br>
>                                      (VDAgentFileXferDataMessage *)data);<br>
>          } else {<br>
>              vdagent_file_xfers_error_disabled(*connp,<br>
> @@ -212,9 +211,9 @@ static void daemon_read_complete(struct udscs_connection<br>
> **connp,<br>
>          }<br>
>          break;<br>
>      case VDAGENTD_CLIENT_DISCONNECTED:<br>
> -        vdagent_x11_client_disconnected(x11);<br>
> -        if (vdagent_finalize_file_xfer()) {<br>
> -            vdagent_init_file_xfer();<br>
> +        vdagent_x11_client_disconnected(agent->x11);<br>
> +        if (vdagent_finalize_file_xfer(agent)) {<br>
> +            vdagent_init_file_xfer(agent);<br>
>          }<br>
>          break;<br>
>      default:<br>
> @@ -223,12 +222,12 @@ static void daemon_read_complete(struct<br>
> udscs_connection **connp,<br>
>      }<br>
>  }<br>
><br>
> -static struct udscs_connection *client_setup_sync(void)<br>
> +static struct udscs_connection *client_setup_sync(SpiceVDAgent *agent)<br>
>  {<br>
>      struct udscs_connection *conn = NULL;<br>
><br>
>      while (!quit) {<br>
> -        conn = udscs_connect(vdagentd_socket, daemon_read_complete, NULL,<br>
> +        conn = udscs_connect(vdagentd_socket, daemon_read_complete, agent,<br>
>                               NULL, vdagentd_messages, VDAGENTD_NO_MESSAGES,<br>
>                               debug);<br>
>          if (conn || !do_daemonize || quit) {<br>
> @@ -299,6 +298,49 @@ static int file_test(const char *path)<br>
>      return stat(path, &buffer);<br>
>  }<br>
><br>
> +static void spice_vdagent_init(SpiceVDAgent *self)<br>
> +{<br>
> +}<br>
> +<br>
> +static void spice_vdagent_finalize(GObject *gobject)<br>
> +{<br>
> +    SpiceVDAgent *self = SPICE_VDAGENT(gobject);<br>
> +    vdagent_finalize_file_xfer(self);<br>
> +    vdagent_x11_destroy(self->x11, self->conn == NULL);<br>
> +<br>
> +    if (self->conn != NULL) {<br>
> +        udscs_destroy_connection(&self->conn);<br>
> +        self->conn = NULL;<br>
> +    }<br>
> +<br>
<br>
g_clear_pointer here?<br></blockquote><div><br></div><div>Sure. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> +    if (G_OBJECT_CLASS(spice_vdagent_parent_class)->finalize)<br>
<br>
this if is not necessary, see <a href="https://developer.gnome.org/gobject/stable/howto-gobject-destruction.html" rel="noreferrer" target="_blank">https://developer.gnome.org/gobject/stable/howto-gobject-destruction.html</a><br>
<br></blockquote><div>OK.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> +        G_OBJECT_CLASS(spice_vdagent_parent_class)->finalize(gobject);<br>
> +}<br>
> +<br>
> +static void spice_vdagent_class_init(SpiceVDAgentClass  *klass)<br>
> +{<br>
> +    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);<br>
> +    gobject_class->finalize = spice_vdagent_finalize;<br>
> +}<br>
> +<br>
> +static gboolean spice_vdagent_init_sync(SpiceVDAgent *agent)<br>
> +{<br>
> +    g_return_val_if_fail(SPICE_IS_VDAGENT(agent), FALSE);<br>
> +<br>
<br>
I don't think this check is necessary.<br>
<br>
> +    agent->conn = client_setup_sync(agent);<br>
> +    if (agent->conn == NULL)<br>
> +        return FALSE;<br>
> +<br>
> +    agent->x11 = vdagent_x11_create(agent->conn, debug, x11_sync);<br>
> +    if (agent->x11 == NULL)<br>
> +        return FALSE;<br>
> +<br>
> +    if (!vdagent_init_file_xfer(agent))<br>
> +        syslog(LOG_WARNING, "File transfer is disabled");<br>
> +<br>
> +    return TRUE;<br>
> +}<br>
> +<br>
>  int main(int argc, char *argv[])<br>
>  {<br>
>      fd_set readfds, writefds;<br>
> @@ -307,6 +349,7 @@ int main(int argc, char *argv[])<br>
>      struct sigaction act;<br>
>      GOptionContext *context;<br>
>      GError *error = NULL;<br>
> +    SpiceVDAgent *agent;<br>
><br>
>      context = g_option_context_new(NULL);<br>
>      g_option_context_add_main_entries(context, entries, NULL);<br>
> @@ -357,20 +400,13 @@ reconnect:<br>
>          execvp(argv[0], argv);<br>
>      }<br>
><br>
> -    client = client_setup_sync();<br>
> -    if (client == NULL) {<br>
> -        return 1;<br>
> +    agent = g_object_new(SPICE_TYPE_VDAGENT, NULL);<br>
> +    agent->parent_socket = parent_socket;<br>
> +    if (!spice_vdagent_init_sync(agent)) {<br>
> +        quit = 1;<br>
> +        goto end_main;<br>
>      }<br>
><br>
> -    x11 = vdagent_x11_create(client, debug, x11_sync);<br>
> -    if (!x11) {<br>
> -        udscs_destroy_connection(&client);<br>
> -        return 1;<br>
> -    }<br>
> -<br>
> -    if (!vdagent_init_file_xfer())<br>
> -        syslog(LOG_WARNING, "File transfer is disabled");<br>
> -<br>
>      if (parent_socket) {<br>
>          if (write(parent_socket, "OK", 2) != 2)<br>
>              syslog(LOG_WARNING, "Parent already gone.");<br>
> @@ -378,12 +414,12 @@ reconnect:<br>
>          parent_socket = 0;<br>
>      }<br>
><br>
> -    while (client && !quit) {<br>
> +    while (agent->conn && !quit) {<br>
>          FD_ZERO(&readfds);<br>
>          FD_ZERO(&writefds);<br>
><br>
> -        nfds = udscs_client_fill_fds(client, &readfds, &writefds);<br>
> -        x11_fd = vdagent_x11_get_fd(x11);<br>
> +        nfds = udscs_client_fill_fds(agent->conn, &readfds, &writefds);<br>
> +        x11_fd = vdagent_x11_get_fd(agent->x11);<br>
>          FD_SET(x11_fd, &readfds);<br>
>          if (x11_fd >= nfds)<br>
>              nfds = x11_fd + 1;<br>
> @@ -397,13 +433,12 @@ reconnect:<br>
>          }<br>
><br>
>          if (FD_ISSET(x11_fd, &readfds))<br>
> -            vdagent_x11_do_read(x11);<br>
> -        udscs_client_handle_fds(&client, &readfds, &writefds);<br>
> +            vdagent_x11_do_read(agent->x11);<br>
> +        udscs_client_handle_fds(&agent->conn, &readfds, &writefds);<br>
>      }<br>
><br>
> -    vdagent_finalize_file_xfer();<br>
> -    vdagent_x11_destroy(x11, client == NULL);<br>
> -    udscs_destroy_connection(&client);<br>
> +end_main:<br>
> +    g_clear_object(&agent);<br>
>      if (!quit && do_daemonize)<br>
>          goto reconnect;<br>
><br>
> diff --git a/src/vdagent/vdagent.h b/src/vdagent/vdagent.h<br>
> new file mode 100644<br>
> index 0000000..f98c9b2<br>
> --- /dev/null<br>
> +++ b/src/vdagent/vdagent.h<br>
> @@ -0,0 +1,54 @@<br>
> +/*  vdagent.h<br>
> +<br>
> +    Copyright 2017 Red Hat, Inc.<br>
> +<br>
> +    This program is free software: you can redistribute it and/or modify<br>
> +    it under the terms of the GNU General Public License as published by<br>
> +    the Free Software Foundation, either version 3 of the License, or<br>
> +    (at your option) any later version.<br>
> +<br>
> +    This program is distributed in the hope that it will be useful,<br>
> +    but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
> +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br>
> +    GNU General Public License for more details.<br>
> +<br>
> +    You should have received a copy of the GNU General Public License<br>
> +    along with this program.  If not, see <<a href="http://www.gnu.org/licenses/" rel="noreferrer" target="_blank">http://www.gnu.org/licenses/</a>>.<br>
> +*/<br>
> +<br>
> +#ifndef __VDAGENT_H_<br>
> +#define __VDAGENT_H_<br>
> +<br>
> +#include <glib-object.h><br>
> +#include "udscs.h"<br>
> +#include "x11.h"<br>
> +#include "file-xfers.h"<br>
> +<br>
> +G_BEGIN_DECLS<br>
> +<br>
> +#define SPICE_TYPE_VDAGENT            (spice_vdagent_get_type ())<br>
> +#define SPICE_VDAGENT(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj),<br>
> SPICE_TYPE_VDAGENT, SpiceVDAgent))<br>
> +#define SPICE_VDAGENT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),<br>
> SPICE_TYPE_VDAGENT, SpiceVDAgentClass))<br>
> +#define SPICE_IS_VDAGENT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj),<br>
> SPICE_TYPE_VDAGENT))<br>
> +#define SPICE_IS_VDAGENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),<br>
> SPICE_TYPE_VDAGENT))<br>
> +#define SPICE_VDAGENT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),<br>
> SPICE_TYPE_VDAGENT, SpiceVDAgentClass))<br>
> +<br>
> +typedef struct _SpiceVDAgent {<br>
<br>
Ids that starts with underscore and capital are not C99 complaint.<br></blockquote><div><br></div><div>I'm not sure I understand that, why do we use these in spice-gtk then?</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> +    GObject parent;<br>
> +<br>
> +    struct vdagent_x11             *x11;<br>
> +    struct vdagent_file_xfers      *xfers;<br>
> +    struct udscs_connection        *conn;<br>
> +<br>
> +    int                             parent_socket;<br>
> +} SpiceVDAgent;<br>
> +<br>
> +typedef struct _SpiceVDAgentClass {<br>
<br>
ditto<br>
<br>
> +    GObjectClass parent;<br>
> +} SpiceVDAgentClass;<br>
> +<br>
> +GType spice_vdagent_get_type(void);<br>
> +<br>
> +G_END_DECLS<br>
> +<br>
> +#endif<br>
> \ No newline at end of file<br>
<br>
Frediano<br>
</blockquote></div></div>