3

Is there a way to use C blocks in Gtk+ callbacks? I was looking into something like that:

gboolean (^calledBack)(gpointer) = ^gboolean (gpointer data) {
    printf("Callback fired!\n");    
    return FALSE;
};

g_timeout_add(300, calledBack, NULL);

2 Answers 2

2

Even though you can't pass the block to g_timeout_add directly, it is easy to set up a trampoline to do it for you. Here is a small test program that creates two closures using blocks and passes them off to g_timeout_add:

#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Block.h>

typedef gboolean (^callback_type)();

static gboolean trampoline(gpointer data) {
    callback_type callback = data;
    gboolean ret = callback();
    Block_release(callback);
    return ret;
}

void some_gtk_handler(int param)
{
  gboolean (^callback)() = ^gboolean () {
      printf("Callback fired: %d!\n", param);    
      return FALSE;
  };
  g_timeout_add(300, trampoline, Block_copy(callback));
}

int main()
{
  GMainLoop *ml = g_main_loop_new(NULL, FALSE);
  some_gtk_handler(0);
  some_gtk_handler(42);
  g_main_loop_run(ml);
  return 0;
}

The above code specifies a one-shot handler, so it can release the block in the trampoline. If you need blocks that are run multiple times, remove the call to Block_release from the trampoline and schedule them using g_timeout_add_full with a destroy notify callback:

g_timeout_add_full(G_PRIORITY_DEFAULT, 300, trampoline, Block_copy(callback),
                   release_callback);

...where release_callback is a utility function defined as:

static void release_callback(gpointer data) {
    Block_release(data);
}
6
  • The lengths to which this blog post goes, suggest that it's not possible to pass the block to g_timeout_add() directly.
    – ptomato
    Oct 5, 2013 at 5:39
  • @ptomato The blog post does start off with And while you can adapt those APIs to use blocks by building small adapter functions, it's annoying to have to do that every time you want to use one, especially since each one works a little bit differently. The non-generic trampoline in my answer is an example of a "small adapter function". Oct 5, 2013 at 5:44
  • I didn't mean to imply that your original answer wasn't correct, trampoline functions are definitely the way to go! I was just commenting on your first sentence, "If you can't pass the block to g_timeout_add directly..." to suggest that indeed you can't.
    – ptomato
    Oct 5, 2013 at 17:53
  • Thank you for an extensive answer. This seems like a solution.
    – egorFiNE
    Oct 6, 2013 at 22:35
  • @ptomato Ah, now I see how I misread your comment. Thanks for the clarification. Oct 7, 2013 at 12:20
0

ANSI C does not have any block facility. Do you mean Objective-C? In the latter case the answer is no, as Gtk+ is a pure C framework.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.