Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
eventqueue.c
Go to the documentation of this file.
00001 /*
00002  * Audacious
00003  * Copyright (c) 2006-2007 Audacious development team.
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; under version 3 of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses>.
00016  *
00017  * The Audacious team does not consider modular code linking to
00018  * Audacious or using our public API to be a derived work.
00019  */
00020 
00021 #include <stdio.h>
00022 
00023 #include "eventqueue.h"
00024 
00025 static gboolean eventqueue_handle(gpointer udata)
00026 {
00027     HookCallQueue *hq = (HookCallQueue *) udata;
00028 
00029     hook_call(hq->name, hq->user_data);
00030 
00031     g_free(hq->name);
00032     if (hq->free_data && hq->user_data)
00033         g_free(hq->user_data);
00034 
00035     g_slice_free(HookCallQueue, hq);
00036 
00037     return FALSE;
00038 }
00039 
00040 void event_queue(const gchar *name, gpointer user_data)
00041 {
00042     HookCallQueue *hq;
00043 
00044     hq = g_slice_new0(HookCallQueue);
00045     hq->name = g_strdup(name);
00046     hq->user_data = user_data;
00047     hq->free_data = FALSE;
00048 
00049     g_idle_add_full(G_PRIORITY_HIGH_IDLE, eventqueue_handle, hq, NULL);
00050 }
00051 
00052 void event_queue_timed(gint time, const gchar *name, gpointer user_data)
00053 {
00054     HookCallQueue *hq;
00055 
00056     /* event_queue() with a pointer may be unsafe: the data might
00057      * get freed or moved before the event is processed. -jlindgren
00058      */
00059     if (user_data)
00060     {
00061         g_warning("Unsafe event_queue of \"%s\" with "
00062             "pointer. (Use event_queue_with_data_free instead.)\n", name);
00063         return;
00064     }
00065 
00066     hq = g_slice_new0(HookCallQueue);
00067     hq->name = g_strdup(name);
00068     hq->user_data = user_data;
00069     hq->free_data = FALSE;
00070 
00071     g_timeout_add(time, eventqueue_handle, hq);
00072 }
00073 
00074 void event_queue_with_data_free(const gchar *name, gpointer user_data)
00075 {
00076     HookCallQueue *hq;
00077 
00078     g_return_if_fail(name != NULL);
00079     g_return_if_fail(user_data != NULL);
00080 
00081     hq = g_slice_new0(HookCallQueue);
00082     hq->name = g_strdup(name);
00083     hq->user_data = user_data;
00084     hq->free_data = TRUE;
00085 
00086     g_idle_add_full(G_PRIORITY_HIGH_IDLE, eventqueue_handle, hq, NULL);
00087 }