Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GObject how to pass argument to signal

Ok, so I have a custom GObject to handle Input events, to notify connected handlers I cerated a signal in the class_init_function:

static void sample_input_manager_class_init (SampleInputManagerClass *klass)
{
  sample_input_manager_signals[KEY_EVENT] = g_signal_new(
    "key-event",
    G_TYPE_FROM_CLASS(klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL,
    NULL,
    NULL,
    G_TYPE_NONE,
    1,
    G_TYPE_INT /* Keycode */
  );
}

Might usefull (at the top of the source file):

enum SAMPLE_INPUT_MANAGER_SIGNALS
{
  KEY_EVENT = 0,

  N_SIGNALS
};

static guint ssample_input_manager_signals[N_SIGNALS] = { 0 };

Now, I want to trigger the signal with the following call:

int key = 42;
g_signal_emit(manager, sample_input_manager_signals[KEY_EVENT], 0, key);

(Where manager is a valid instance, and key is an integer to represet characters) However, in my registered callback (g_signal_connect) I get totally random values like -2073417152 (for any input integer, constant during runtime)

Am I missing anything to get this to work?


Edit #1:

Signal handler connection:

SampleInputHandler *input = sample_input_handler_new();
g_signal_connect(input, "key-event", G_CALLBACK(test_cb), NULL);

Callback:

void testcb(int key, gpointer data)
{
  printf("(%d)",key);
}
like image 436
mame98 Avatar asked Feb 01 '26 01:02

mame98


1 Answers

SOLUTION

Ok, so after a too-long time, i figured that the default callback signature in GLib is simmilar to this:

<ReturnType> callback (YourType *object[, <your arguments> ], gpointer data);

So in my issue I simply forgot the first parameter, so that instead of reading my integer, I actually read the memory pointer address of my object.

like image 127
mame98 Avatar answered Feb 03 '26 14:02

mame98



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!