[gvfs/mtp-backend: 30/64] MTP: Event handling.



commit 50a35ca5e3a2207cd552a9fb932c16b57ca8c56e
Author: Philip Langdale <philipl overt org>
Date:   Sat Sep 1 16:51:42 2012 -0700

    MTP: Event handling.
    
    This change adds an event listener to receive libmtp events. The
    primary use for this is detecting when stores appear after a phone
    is unlocked. That way you can plug it in, then unlock it, and then
    you'll see the stores appear automatically.

 configure.ac            |    5 +++++
 daemon/gvfsbackendmtp.c |   32 ++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 2a0504a..edd665f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -514,6 +514,11 @@ if test "x$enable_libmtp" != "xno" -a "x$msg_gudev" = "xyes"; then
     if test "x$have_libmtp_get_thumbnail" = "xyes"; then
       AC_DEFINE(HAVE_LIBMTP_GET_THUMBNAIL, 1, [Define to 1 if LIBMTP_Get_Thumbnail is available])
     fi
+
+    AC_CHECK_LIB(mtp, LIBMTP_Read_Event, have_libmtp_read_event=yes)
+    if test "x$have_libmtp_read_event" = "xyes"; then
+      AC_DEFINE(HAVE_LIBMTP_READ_EVENT, 1, [Define to 1 if LIBMTP_Read_Event is available])
+    fi
     LIBS="$save_libs"
   fi
 fi
diff --git a/daemon/gvfsbackendmtp.c b/daemon/gvfsbackendmtp.c
index a593199..10e70bc 100644
--- a/daemon/gvfsbackendmtp.c
+++ b/daemon/gvfsbackendmtp.c
@@ -253,6 +253,34 @@ try_mount (GVfsBackend *backend,
   return FALSE;
 }
 
+#if HAVE_LIBMTP_READ_EVENT
+static gpointer
+check_event(gpointer user_data)
+{
+  GVfsBackendMtp *backend = user_data;
+
+  LIBMTP_event_t event;
+  int ret = 0;
+  while (ret == 0) {
+    uint32_t param1;
+    char *path;
+    ret = LIBMTP_Read_Event(backend->device, &event, &param1);
+    switch (event) {
+    case LIBMTP_EVENT_STORE_ADDED:
+      path = g_strdup_printf ("/%u", param1);
+      g_mutex_lock (&backend->mutex);
+      g_hash_table_foreach (backend->monitors, emit_create_event, path);
+      g_mutex_unlock (&backend->mutex);
+      g_free (path);
+      break;
+    default:
+      break;
+    }
+  }
+  return NULL;
+}
+#endif
+
 static void
 do_mount (GVfsBackend *backend,
            GVfsJobMount *job,
@@ -319,6 +347,10 @@ do_mount (GVfsBackend *backend,
     g_mount_spec_unref (mtp_mount_spec);
 
     g_vfs_job_succeeded (G_VFS_JOB (job));
+
+#if HAVE_LIBMTP_READ_EVENT
+    g_thread_new("events", check_event, backend);
+#endif
   }
 }
 



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