summaryrefslogtreecommitdiff
path: root/lib/dynamic_obj.c
blob: 1673eb76f9525e702fec8dccb0849a4245bd79f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson
 *
 * List of "dynamic" (animated) objects, and their refresh rates
 * Copyright (C) 2002 Cyrille Chépélov
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <glib.h>
#include <dynamic_obj.h>

typedef struct {
    DiaObject* obj;
    guint timeout;
} DynobjRec;

static GList* dyn_obj_list = NULL;

void dynobj_list_add_object(DiaObject* obj, guint timeout) {
    DynobjRec *dor = g_new(DynobjRec,1);
    dor->obj = obj;
    dor->timeout = timeout;

    dyn_obj_list = g_list_append(dyn_obj_list,dor);
}

static gint dor_found(gconstpointer data, gconstpointer user_data) {
    const DynobjRec* dor = (const DynobjRec*)data;
    const DiaObject* obj = (const DiaObject*)user_data;
    
    if ((!dor) || (!obj)) return 1;
    if (dor->obj != obj) return 1;
    return 0;
}
                          
void dynobj_list_remove_object(DiaObject* obj) {
    GList* item = g_list_find_custom(dyn_obj_list,obj,dor_found);

    if (item) {
        DynobjRec* dor = item->data;
        dyn_obj_list = g_list_remove(dyn_obj_list,dor);
        g_free(dor);
    }
}

typedef struct {
    ObjectDynobjFunc odf;
    gpointer data;
} TrampolineData;

static void foreach_trampoline(gpointer data, gpointer user_data) {
    TrampolineData* td = (TrampolineData*)user_data;
    DynobjRec* dor = (DynobjRec*)data;

    td->odf(dor->obj,td->data);
}

void dynobj_list_foreach(ObjectDynobjFunc odf, gpointer data) {
    TrampolineData td;
    td.odf = odf;
    td.data = data;
    g_list_foreach(dyn_obj_list,foreach_trampoline,&td);
}

static void accum_timeout(gpointer data, gpointer user_data) {
    guint* accum = (guint*)user_data;
    DynobjRec* dor = (DynobjRec*)data;

    if (!dor) return;
    
    *accum = MAX(*accum,dor->timeout);
}

guint dynobj_list_get_dynobj_rate(void) {
    guint timeout = 250;

    if (dyn_obj_list)
        g_list_foreach(dyn_obj_list,accum_timeout,&timeout);
    else
        timeout = 0; /* no objet to refresh, no timeout */

    return timeout;
}