summaryrefslogtreecommitdiff
path: root/libnul.h
blob: b3e9955fb6dfd8f592e5e4ad140c5c16cfa00c0d (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*  libnul 
 *  Copyright (C) 2002, 2008  Søren Sandmann (sandmann@daimi.au.dk)
 *
 *  This library 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 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU 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
 */

#ifndef __LIBNUL_H
#define __LIBNUL_H

#include <stdint.h>
#include <glib.h>
#include <stdarg.h>

/* These macros come from glib */

/* Provide convenience macros for handling structure
 * fields through their offsets.
 */

#if defined(__GNUC__)  && __GNUC__ >= 4
#  define NUL_STRUCT_OFFSET(struct_type, member)			\
    ((gssize) __builtin_offsetof (struct_type, member))
#else
#  define NUL_STRUCT_OFFSET(struct_type, member)			\
    ((gssize) ((guint8*) &((struct_type*) 0)->member))
#endif

#if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
#define NUL_GNUC_WARN_UNUSED_RESULT					\
    __attribute__((warn_unused_result))
#else
#define G_GNUC_WARN_UNUSED_RESULT
#endif /* __GNUC__ */

#define NUL_UR G_GNUC_WARN_UNUSED_RESULT

typedef void * nul_ptr_t;
typedef void * const nul_const_ptr_t;

/*
 * Pointer arrays
 */
nul_ptr_t *nul_ptr_array_new    (void);
nul_ptr_t *nul_ptr_array_append (nul_ptr_t *arr,
				 nul_ptr_t data) NUL_UR;
gsize      nul_ptr_array_len    (nul_const_ptr_t *arr);
void       nul_ptr_array_free   (nul_ptr_t *arr);


/*
 * Strings
 */
typedef char nul_string_t;

nul_string_t *nul_string_new              (void);
void          nul_string_free             (nul_string_t        *str);
gsize         nul_string_len              (const nul_string_t  *str);
gboolean      nul_string_empty            (const nul_string_t  *str);
nul_string_t *nul_string_append_undefined (nul_string_t        *string,
					   gsize                n_bytes,
					   nul_string_t       **tail) NUL_UR;

nul_string_t *nul_string_append           (nul_string_t        *string,
					   const char          *bytes,
					   gssize               n_bytes) NUL_UR;
nul_string_t *nul_string_append_printf    (nul_string_t        *string,
					   const char          *fmt,
					   ...) NUL_UR;
nul_string_t *nul_string_append_vprintf   (nul_string_t        *string,
					   const char          *fmt,
					   va_list              args) NUL_UR;
nul_string_t *nul_string_delete_head      (nul_string_t        *str,
					   gsize                n_bytes) NUL_UR;
nul_string_t *nul_string_delete_tail      (nul_string_t        *str,
					   gsize                n_bytes) NUL_UR;

/*
 * Buffers
 */
typedef struct nul_buffer_t nul_buffer_t;

nul_buffer_t *      nul_buffer_new           (void);
char *              nul_buffer_free          (nul_buffer_t *queue,
					      gboolean      free_data);
gsize               nul_buffer_get_length    (nul_buffer_t *queue);
gboolean            nul_buffer_is_empty      (nul_buffer_t *queue);
const nul_string_t *nul_buffer_peek          (nul_buffer_t *queue,
					      gsize        *n_bytes);
nul_string_t *      nul_buffer_steal         (nul_buffer_t *queue,
					      gsize        *n_bytes);
nul_string_t *      nul_buffer_alloc_tail    (nul_buffer_t *queue,
					      gsize         size);
void                nul_buffer_append        (nul_buffer_t *queue,
					      const char   *bytes,
					      gsize         n_bytes);
void                nul_buffer_append_printf (nul_buffer_t *queue,
					      const char   *fmt,
					      ...);
void                nul_buffer_transfer_data (nul_buffer_t *dest,
					      nul_buffer_t *src,
					      gssize        n_bytes);
void                nul_buffer_delete_head   (nul_buffer_t *queue,
					      gsize         size);
void                nul_buffer_delete_tail   (nul_buffer_t *queue,
					      gsize         size);

typedef union
{
    uint32_t		v_uint32;
    int32_t		v_int32;
    uint16_t		v_uint16;
    int16_t		v_int16;
    uint8_t		v_uint8;
    int8_t		v_int8;
    
    unsigned int	v_uint;
    int			v_int;
    unsigned short	v_ushort;
    short		v_short;
    unsigned char	v_uchar;
    signed char		v_schar;
    char		v_char;
    
    void *		v_pointer;
    
    float		v_float;
    double		v_double;
} nul_arg_t;

typedef enum
{
    NUL_TYPE_UINT32,
    NUL_TYPE_INT32,
    NUL_TYPE_UINT16,
    NUL_TYPE_INT16,
    NUL_TYPE_UINT8,
    NUL_TYPE_INT8,
    
    NUL_TYPE_UINT,
    NUL_TYPE_INT,
    NUL_TYPE_USHORT,
    NUL_TYPE_SHORT,
    
    NUL_TYPE_UCHAR,
    NUL_TYPE_SCHAR,
    NUL_TYPE_CHAR,
    
    NUL_TYPE_POINTER,
    NUL_TYPE_STRING,
    
    NUL_TYPE_DOUBLE,
    NUL_TYPE_FLOAT,
    
    NUL_TYPE_VOID
} nul_type_t;

typedef void (* nul_function_t)();

typedef struct nul_fun_def_t nul_fun_def_t;

nul_fun_def_t *nul_fun_def_new    (nul_type_t      ret_type,
				   int             n_args,
				   nul_type_t     *arg_types);
nul_arg_t      nul_fun_def_invoke (nul_fun_def_t  *fun,
				   nul_function_t  f,
				   nul_arg_t      *args);
void	       nul_fun_def_free   (nul_fun_def_t  *fun);


/*
 * Polling filedescriptors
 */
typedef struct nul_poll_t nul_poll_t;

typedef enum
{
    NUL_POLL_READ	= 1 << 0,
    NUL_POLL_WRITE	= 1 << 1,
    NUL_POLL_HANGUP	= 1 << 2,
    NUL_POLL_ERROR	= 1 << 3,
    NUL_POLL_PRIORITY	= 1 << 4,
    
    NUL_POLL_RESERVED	= 1 << 5	/* This and higher bits are reserved */
} nul_poll_event_type_t;

typedef struct
{
    nul_poll_event_type_t	events;
    int				fd;
} nul_poll_event_t;

nul_poll_t        *nul_poll_new         (void);
void               nul_poll_add_fd      (nul_poll_t            *epoll,
					 int                    fd,
					 nul_poll_event_type_t  mask,
					 gpointer               data);
void               nul_poll_remove_fd   (nul_poll_t            *epoll,
					 int                    fd);
gpointer           nul_poll_get_fd_data (nul_poll_t            *epoll,
					 int                    fd);
gboolean           nul_poll_has_fd      (nul_poll_t            *epoll,
					 int                    fd);
void               nul_poll_reenable_fd (nul_poll_t            *epoll,
					 int                    fd);
gint               nul_poll_get_n_fds   (nul_poll_t            *epoll);
nul_poll_event_t * nul_poll_wait        (nul_poll_t            *epoll,
					 int                   *n_events,
					 int                    timeout);

/*
 * Watching file descriptors
 */
typedef void (* WatchCallback) (gpointer data);

void     nul_fd_add_watch             (int           fd,
				       gpointer      data);
gpointer nul_fd_get_data	      (int           fd);
void     nul_fd_set_data	      (int	     fd,
				       gpointer      data);
void     nul_fd_set_read_callback     (int           fd,
				       WatchCallback read_cb);
void     nul_fd_set_write_callback    (int           fd,
				       WatchCallback write_cb);
void     nul_fd_set_hangup_callback   (int           fd,
				       WatchCallback hangup_cb);
void     nul_fd_set_error_callback    (int           fd,
				       WatchCallback error_cb);
void     nul_fd_set_priority_callback (int           fd,
				       WatchCallback priority_cb);
void	 nul_fd_set_poll_handler      (int	     fd,
				       WatchCallback poll_handler);
void     nul_fd_remove_watch          (int           fd);
gboolean nul_fd_is_watched            (int           fd);

/*
 * File utilities
 */
char *nul_canonicalize_filename (const char *filename);

/*
 * Unix signal handlers
 */
typedef void (* signal_func_t) (int signo, gpointer data);

/* FIXME: error out is required */
gboolean nul_signal_set_handler (int signo,
				 signal_func_t handler,
				 gpointer data,
				 GError **err);

/* Implementing a service */

typedef struct nul_dbus_service_t nul_dbus_service_t;
typedef struct nul_dbus_object_t nul_dbus_object_t;
typedef struct nul_dbus_member_t nul_dbus_member_t;
typedef struct nul_dbus_interface_t nul_dbus_interface_t;
typedef struct nul_dbus_type_t nul_dbus_type_t;
typedef struct nul_dbus_parameter_t nul_dbus_parameter_t;
typedef struct nul_dbus_arg_t nul_dbus_arg_t;

/* Return TRUE for handled, FALSE for not handled */
typedef gboolean (* nul_dbus_function_t) ();

nul_dbus_service_t *  nul_dbus_session_service (const char    *name,
						nul_dbus_object_t     *object1,
						...);
nul_dbus_object_t *   nul_dbus_object          (const char    *name,
						gpointer       data,
						nul_dbus_interface_t  *interface1,
						...);
nul_dbus_interface_t *nul_dbus_interface       (const char    *name,
						nul_dbus_member_t     *member1,
						...);
nul_dbus_member_t *   nul_dbus_method          (const char    *name,
						nul_dbus_function_t    function,
						nul_dbus_parameter_t  *parameter1,
						...);
nul_dbus_parameter_t *nul_dbus_parameter_in    (const char    *name,
						const nul_dbus_type_t *type);
nul_dbus_parameter_t *nul_dbus_parameter_out   (const char    *name,
						const nul_dbus_type_t *type);
gboolean              nul_dbus_service_start   (nul_dbus_service_t *service);
void                  nul_dbus_service_stop    (nul_dbus_service_t *service);
void                  nul_dbus_service_add_object (nul_dbus_service_t *service,
						   nul_dbus_object_t *object);

/* The returned values here are automatically freed at idle. You
 * must copy them if you want to keep them around
 */
const nul_dbus_type_t * nul_dbus_type_int32  (void);
const nul_dbus_type_t * nul_dbus_type_uint32 (void);
const nul_dbus_type_t * nul_dbus_type_string (void);

/* Using a service */
void nul_dbus_invoke (nul_dbus_service_t *service,
		      const char *method_desc,
		      nul_dbus_function_t callback,
		      gpointer data,
		      ...);

#endif