+/*
+ * call-seq:
+ * g.set_event_callback(cb, event_bitmask) -> event_handle
+ *
+ * Call
+ * +guestfs_set_event_callback+[http://libguestfs.org/guestfs.3.html#guestfs_set_event_callback]
+ * to register an event callback. This returns an event handle.
+ */
+static VALUE
+ruby_set_event_callback (VALUE gv, VALUE cbv, VALUE event_bitmaskv)
+{
+ guestfs_h *g;
+ uint64_t event_bitmask;
+ int eh;
+ VALUE *root;
+ char key[64];
+
+ Data_Get_Struct (gv, guestfs_h, g);
+
+ event_bitmask = NUM2ULL (event_bitmaskv);
+
+ root = guestfs_safe_malloc (g, sizeof *root);
+ *root = cbv;
+
+ eh = guestfs_set_event_callback (g, ruby_event_callback_wrapper,
+ event_bitmask, 0, root);
+ if (eh == -1) {
+ free (root);
+ rb_raise (e_Error, \"%%s\", guestfs_last_error (g));
+ }
+
+ rb_gc_register_address (root);
+
+ snprintf (key, sizeof key, \"_ruby_event_%%d\", eh);
+ guestfs_set_private (g, key, root);
+
+ return INT2NUM (eh);
+}
+
+/*
+ * call-seq:
+ * g.delete_event_callback(event_handle) -> nil
+ *
+ * Call
+ * +guestfs_delete_event_callback+[http://libguestfs.org/guestfs.3.html#guestfs_delete_event_callback]
+ * to delete an event callback.
+ */
+static VALUE
+ruby_delete_event_callback (VALUE gv, VALUE event_handlev)
+{
+ guestfs_h *g;
+ char key[64];
+ int eh = NUM2INT (event_handlev);
+ VALUE *root;
+
+ Data_Get_Struct (gv, guestfs_h, g);
+
+ snprintf (key, sizeof key, \"_ruby_event_%%d\", eh);
+
+ root = guestfs_get_private (g, key);
+ if (root) {
+ rb_gc_unregister_address (root);
+ free (root);
+ guestfs_set_private (g, key, NULL);
+ guestfs_delete_event_callback (g, eh);
+ }
+
+ return Qnil;
+}
+
+static void
+ruby_event_callback_wrapper (guestfs_h *g,
+ void *data,
+ uint64_t event,
+ int event_handle,
+ int flags,
+ const char *buf, size_t buf_len,
+ const uint64_t *array, size_t array_len)
+{
+ size_t i;
+ VALUE eventv, event_handlev, bufv, arrayv, argv;
+
+ eventv = ULL2NUM (event);
+ event_handlev = INT2NUM (event_handle);
+
+ bufv = rb_str_new (buf, buf_len);
+
+ arrayv = rb_ary_new2 (array_len);
+ for (i = 0; i < array_len; ++i)
+ rb_ary_push (arrayv, ULL2NUM (array[i]));
+
+ /* Wrap up the arguments in an array which will be unpacked
+ * and passed as multiple arguments. This is a crap limitation
+ * of rb_rescue.
+ * http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/~poffice/mail/ruby-talk/65698
+ */
+ argv = rb_ary_new2 (5);
+ rb_ary_store (argv, 0, * (VALUE *) data /* function */);
+ rb_ary_store (argv, 1, eventv);
+ rb_ary_store (argv, 2, event_handlev);
+ rb_ary_store (argv, 3, bufv);
+ rb_ary_store (argv, 4, arrayv);
+
+ rb_rescue (ruby_event_callback_wrapper_wrapper, argv,
+ ruby_event_callback_handle_exception, Qnil);
+}
+
+static VALUE
+ruby_event_callback_wrapper_wrapper (VALUE argv)
+{
+ VALUE fn, eventv, event_handlev, bufv, arrayv;
+
+ fn = rb_ary_entry (argv, 0);
+ eventv = rb_ary_entry (argv, 1);
+ event_handlev = rb_ary_entry (argv, 2);
+ bufv = rb_ary_entry (argv, 3);
+ arrayv = rb_ary_entry (argv, 4);
+
+ rb_funcall (fn, rb_intern (\"call\"), 4,
+ eventv, event_handlev, bufv, arrayv);
+
+ return Qnil;
+}
+
+static VALUE
+ruby_event_callback_handle_exception (VALUE not_used, VALUE exn)
+{
+ /* Callbacks aren't supposed to throw exceptions. The best we
+ * can do is to print the error.
+ */
+ fprintf (stderr, \"libguestfs: exception in callback: %%s\",
+ StringValueCStr (exn));
+
+ return Qnil;
+}
+
+static VALUE **
+get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
+{
+ VALUE **r;
+ size_t i;
+ const char *key;
+ VALUE *root;
+
+ /* Count the length of the array that will be needed. */
+ *len_rtn = 0;
+ root = guestfs_first_private (g, &key);
+ while (root != NULL) {
+ if (strncmp (key, \"_ruby_event_\", strlen (\"_ruby_event_\")) == 0)
+ (*len_rtn)++;
+ root = guestfs_next_private (g, &key);
+ }
+
+ /* Copy them into the return array. */
+ r = guestfs_safe_malloc (g, sizeof (VALUE *) * (*len_rtn));
+
+ i = 0;
+ root = guestfs_first_private (g, &key);
+ while (root != NULL) {
+ if (strncmp (key, \"_ruby_event_\", strlen (\"_ruby_event_\")) == 0) {
+ r[i] = root;
+ i++;
+ }
+ root = guestfs_next_private (g, &key);
+ }
+
+ return r;
+}
+