Skip to content

Instantly share code, notes, and snippets.

@hissinger
Last active August 1, 2019 09:39
Show Gist options
  • Save hissinger/18713bb935afabd6f4c6763ae1c1ba69 to your computer and use it in GitHub Desktop.
Save hissinger/18713bb935afabd6f4c6763ae1c1ba69 to your computer and use it in GitHub Desktop.
#include <glib.h>
#include <stdio.h>
#include <gnu/libc-version.h>
typedef struct {
GSource parent;
} custom_t;
static gboolean custom_prepare(GSource *source, gint *timeout)
{
printf("call custom_prepare() source:%s timeout:%d\n", g_source_get_name(source), *timeout);
return FALSE;
}
static gboolean custom_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
{
return G_SOURCE_CONTINUE;
}
static void custom_finalize(GSource *source)
{
}
static GSourceFuncs custom_funcs = {
custom_prepare,
NULL, /* We don't need check */
custom_dispatch,
custom_finalize,
NULL, NULL
};
gboolean timeout_callback(gpointer data)
{
printf("timeout_callback called\n");
return TRUE;
}
void create_timer_source(GMainContext* context, GMainLoop* loop)
{
GSource *source = NULL;
int id;
source = g_timeout_source_new_seconds(1);
g_source_set_name (source, "timer");
g_source_set_priority(source, G_PRIORITY_DEFAULT);
id = g_source_attach(source, context);
g_source_set_callback(source, timeout_callback, loop, NULL);
}
void create_source(GMainContext* context, GMainLoop* loop)
{
GSource *source = g_source_new(&custom_funcs, sizeof(custom_t));
g_source_set_name (source, "custom");
g_source_set_priority(source, G_PRIORITY_DEFAULT);
int id = g_source_attach(source, context);
}
int main(void)
{
printf("glibc version: %s\n", gnu_get_libc_version());
GMainLoop *loop = NULL;
GMainContext *context = NULL;
context = g_main_context_new();
loop = g_main_loop_new(context, FALSE);
create_timer_source(context, loop);
create_source(context, loop);
g_main_loop_run(loop);
g_main_loop_unref(loop);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment