summaryrefslogtreecommitdiff
path: root/common/ve-signal.c
blob: 76f7ed4e7ac99102a7e796271050897ec78dc29f (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Signal routines
 *
 * (c) 2000, 2002 Queen of England
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "config.h"

#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <glib.h>
#include <glib/gi18n.h>

#include "ve-signal.h"

typedef struct _SignalSource SignalSource;
struct _SignalSource {
	GSource     source;

	int         signal;
	guint8      index;
	guint8      shift;
};

static	guint32	signals_notified[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

static gboolean
ve_signal_prepare (GSource  *source,
		   int      *timeout)
{
	SignalSource *ss = (SignalSource *)source;

	return signals_notified[ss->index] & (1 << ss->shift);
}

static gboolean
ve_signal_check (GSource *source)
{
	SignalSource *ss = (SignalSource *)source;

	return signals_notified[ss->index] & (1 << ss->shift);
}

static gboolean
ve_signal_dispatch (GSource     *source,
		    GSourceFunc  callback,
		    gpointer     user_data)
{
	SignalSource *ss = (SignalSource *)source;

	signals_notified[ss->index] &= ~(1 << ss->shift);

	return ((VeSignalFunc)callback) (ss->signal, user_data);
}

static GSourceFuncs signal_funcs = {
	ve_signal_prepare,
	ve_signal_check,
	ve_signal_dispatch
};

guint
ve_signal_add (int	      signal,
	       VeSignalFunc function,
	       gpointer      data)
{
	return ve_signal_add_full (G_PRIORITY_DEFAULT, signal, function, data, NULL);
}

guint
ve_signal_add_full (int            priority,
		    int            signal,
		    VeSignalFunc   function,
		    gpointer       data,
		    GDestroyNotify destroy)
{
	GSource *source;
	SignalSource *ss;
	guint s = 128 + signal;

	g_return_val_if_fail (function != NULL, 0);

	source = g_source_new (&signal_funcs, sizeof (SignalSource));
	ss = (SignalSource *)source;

	ss->signal = signal;
	ss->index = s / 32;
	ss->shift = s % 32;

	g_assert (ss->index < 8);

	g_source_set_priority (source, priority);
	g_source_set_callback (source, (GSourceFunc)function, data, destroy);
	g_source_set_can_recurse (source, TRUE);

	return g_source_attach (source, NULL);
}

void
ve_signal_notify (int signal)
{
	guint index, shift;
	guint s = 128 + signal;

	index = s / 32;
	shift = s % 32;

	g_assert (index < 8);

	signals_notified[index] |= 1 << shift;

	g_main_context_wakeup (NULL);
}

gboolean
ve_signal_was_notified (int signal)
{
	guint index, shift;
	guint s = 128 + signal;

	index = s / 32;
	shift = s % 32;

	g_assert (index < 8);

	return ((signals_notified[index]) & (1 << shift)) ? TRUE : FALSE;
}

void
ve_signal_unnotify (int signal)
{
	guint index, shift;
	guint s = 128 + signal;

	index = s / 32;
	shift = s % 32;

	g_assert (index < 8);

	signals_notified[index] &= ~(1 << shift);
}