Class: CFrida::GObject
- Inherits:
-
Object
- Object
- CFrida::GObject
- Defined in:
- ext/c_frida/GObject.c
Instance Method Summary collapse
- #off(*args) ⇒ Object
-
#on(*args) ⇒ Object
#on(signal_name, callback) -> nil.
Instance Method Details
#off(*args) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'ext/c_frida/GObject.c', line 83
static VALUE GObject_off(int argc, VALUE *argv, VALUE self)
{
GET_DATA(GObject);
REQUIRE_GOBJECT_HANDLE();
VALUE signal_name, callback;
GType self_type = G_OBJECT_TYPE(d->handle);
guint signal_id;
GSList *callback_entry;
int is_block = rb_block_given_p();
// remember you cannot remove Proc objects that you do not hold reference to!
rb_scan_args(argc, argv, "2", &signal_name, &callback);
if (!RB_TYPE_P(signal_name, T_STRING)) {
raise_argerror("signal name must be a string.");
return (Qnil);
}
signal_id = g_signal_lookup(StringValueCStr(signal_name), self_type);
if (!signal_id) {
raise_argerror("invalid signal name.");
return (Qnil);
}
if (rb_respond_to(callback, rb_intern("call"))) {
callback_entry = g_slist_find_custom(d->signal_closures, (gpointer)callback, (GCompareFunc)compare_callback);
if (!callback_entry) {
raise_argerror("callback not found.");
return (Qnil);
}
} else {
raise_argerror("second argument should be callable.");
return (Qnil);
}
GClosure *closure = callback_entry->data;
d->signal_closures = g_slist_delete_link(d->signal_closures, callback_entry);
g_signal_handlers_disconnect_matched(d->handle, G_SIGNAL_MATCH_CLOSURE, signal_id, 0, closure, NULL, NULL);
// this will delete a duplicate callback for a signal, but that is fine ?
rb_ary_delete(rb_hash_aref(rb_iv_get(self, "callbacks"), signal_name), callback);
return (Qnil);
}
|
#on(*args) ⇒ Object
#on(signal_name, callback) -> nil
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'ext/c_frida/GObject.c', line 17
static VALUE GObject_on(int argc, VALUE *argv, VALUE self)
{
GET_DATA(GObject);
REQUIRE_GOBJECT_HANDLE();
VALUE signal_name, callback;
GType self_type = G_OBJECT_TYPE(d->handle);
guint signal_id;
bool is_lambda;
GSignalQuery squery;
if (rb_block_given_p()) {
// blocks are handy however we cannot compare them because they are unique
// so you cannot remove them later!
rb_scan_args(argc, argv, "1&", &signal_name, &callback);
} else {
rb_scan_args(argc, argv, "2", &signal_name, &callback);
// either a Method or Proc object.
if (!rb_respond_to(callback, rb_intern("call"))) {
raise_argerror("callback should be callable.");
return (Qnil);
}
}
if (!RB_TYPE_P(signal_name, T_STRING)) {
raise_argerror("signal name must be a string.");
return (Qnil);
}
signal_id = g_signal_lookup(StringValueCStr(signal_name), self_type);
if (!signal_id) {
raise_argerror("invalid signal name.");
return (Qnil);
}
is_lambda = (rb_obj_is_kind_of(callback, rb_const_get(rb_cObject, rb_intern("Method"))) == Qtrue) ? true : (rb_funcall(callback, rb_intern("lambda?"), 0, NULL) == Qtrue);
int arity = NUM2INT(rb_funcall(callback, rb_intern("arity"), 0, NULL));
g_signal_query(signal_id, &squery);
if (is_lambda && arity > (int)(squery.n_params + 1)) {
gchar *err = g_strdup_printf("callback expects too many arguments, signal expects %d.", squery.n_params);
raise_argerror(err);
g_free(err);
return (Qnil);
}
GClosure *closure = g_closure_new_simple(sizeof(RBClosure), (void*)callback);
g_closure_set_marshal(closure, (GClosureMarshal)gvl_bridge_forward_GC);
TO_RBCLOSURE(closure)->is_lambda = is_lambda;
TO_RBCLOSURE(closure)->signal_id = signal_id;
TO_RBCLOSURE(closure)->arity = arity;
g_signal_connect_closure_by_id(d->handle, signal_id, 0, closure, TRUE);
// objects generated on the fly (thus not referenced), e.g blocks `#on("signal", :id) {block}` / `#on("signal", &callable)`
// or Methods `#on("signal", method(:defined_fn))` or Procs `#on("signal", ->{})`
// will be picked up by the GC, since in Ruby's view there is no more usage/reference to them
// so we keep them in this hidden Hash.
if (rb_hash_aref(rb_iv_get(self, "callbacks"), signal_name) == Qnil)
rb_hash_aset(rb_iv_get(self, "callbacks"), signal_name, rb_ary_new());
rb_ary_push(rb_hash_aref(rb_iv_get(self, "callbacks"), signal_name), callback);
d->signal_closures = g_slist_prepend(d->signal_closures, closure);
return (Qnil);
}
|