summaryrefslogtreecommitdiff
path: root/src/glibext/signal.c
blob: c7c8fe38acf951b9b1e85f9a9f23d81043563f4f (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

/* Chrysalide - Outil d'analyse de fichiers binaires
 * signal.h - prototypes pour un encadrement des signaux supplémentaire par rapport à celui de la GLib
 *
 * Copyright (C) 2009-2013 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  OpenIDA 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenIDA 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "signal.h"


#include <malloc.h>
#include <stdarg.h>



/* Prototype pour le transfert d'exécution. */
typedef void (* GSignalCallback) (gpointer, ...);


/* Informations concernant une diffusion de signal */
typedef struct _gsignal_wrapper_info
{
    GConnectFlags flags;                    /* Ordre des arguments         */

    GSignalCallback callback;               /* Fonction finale de récept°  */
    gpointer instance;                      /* Instance GLib initiatrice   */
    gpointer data;                          /* Donnée utilisateur associée */

    guint n_params;                         /* Nombre de paramètres        */
    unsigned long params[0];                /* Paramètres récupérés        */

} gsignal_wrapper_info;


/* Réceptionne un signal et redirige son exécution. */
static void carry_signal_to_main_thread(gsignal_wrapper_info *, ...);

/* Transmet un signal dans le contexte principal. */
static gboolean to_main_wrapper(gsignal_wrapper_info *);



/******************************************************************************
*                                                                             *
*  Paramètres  : info = collecteur d'informations sur la diffusion.           *
*                                                                             *
*  Description : Transmet un signal dans le contexte principal.               *
*                                                                             *
*  Retour      : FALSE / G_SOURCE_REMOVE pour arrêter la transmission.        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static gboolean to_main_wrapper(gsignal_wrapper_info *info)
{
    gpointer data1;                         /* Premier argument à envoyer  */
    gpointer data2;                         /* Dernier argument à envoyer  */

    if (info->flags & G_CONNECT_SWAPPED)
    {
        data1 = info->data;
        data2 = info->instance;
    }
    else
    {
        data1 = info->instance;
        data2 = info->data;
    }

    switch (info->n_params - 1)
    {
        case 0:
            info->callback(data1, data2);
            break;

    }

    return G_SOURCE_REMOVE;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : info = collecteur d'informations sur la diffusion.           *
*                ...  = arguments poussés par la GLib sur la pile.            *
*                                                                             *
*  Description : Réceptionne un signal et redirige son exécution.             *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void carry_signal_to_main_thread(gsignal_wrapper_info *info, ...)
{
    va_list ap;                             /* Liste d'arguments sur pile  */
    guint i;                                /* Boucle de parcours          */

    va_start(ap, info);

    for (i = 0; i < info->n_params; i++)
        info->params[i] = va_arg(ap, unsigned long);

    va_end(ap);

    g_idle_add_full(G_PRIORITY_HIGH_IDLE, (GSourceFunc)to_main_wrapper, info, free);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : instance = object GLib à l'origine de l'émission.            *
*                signal   = identification du signal à réceptionner.          *
*                handler  = fonction C servant de réceptacle.                 *
*                data     = éventuelle donnée de l'utilisateur à ajouter.     *
*                                                                             *
*  Description : Reproduit le comportement de la fonction g_signal_connect(). *
*                                                                             *
*  Retour      : Identifiant supérieur zéro en cas de succès.                 *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

gulong g_signal_connect_to_main(gpointer instance, const gchar *signal, GCallback handler, gpointer data)
{
    guint signal_id;                        /* Identifiant du signal visé  */
    GSignalQuery query;                     /* Information sur le signal   */
    gsignal_wrapper_info *info;             /* Encapsulation des données   */

    /* Collection d'informations */

    signal_id = g_signal_lookup(signal, G_TYPE_FROM_INSTANCE(instance));

    g_signal_query(signal_id, &query);

    /* Allocation adaptée */

    info = (gsignal_wrapper_info *)malloc(sizeof(gsignal_wrapper_info) + sizeof(unsigned long) * ++query.n_params);

    info->flags = 0;

    info->callback = (GSignalCallback)handler;
    info->instance = instance;
    info->data = data;

    info->n_params = query.n_params;

    return g_signal_connect_swapped(instance, signal, G_CALLBACK(carry_signal_to_main_thread), info);

}