summaryrefslogtreecommitdiff
path: root/src/libwebvi/libwebvi.h
blob: e80ce43f3819682af59d7f7751f603d338d309d0 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * libwebvi.h: C bindings for webvi Python module
 *
 * Copyright (c) 2010, 2011 Antti Ajanki <antti.ajanki@iki.fi>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#ifndef __LIBWEBVI_H
#define __LIBWEBVI_H

#include <sys/select.h>
#include <stdlib.h>

typedef int WebviHandle;

typedef ssize_t (*webvi_callback)(const char *, size_t, void *);
typedef void (*webvi_timeout_callback)(long, void *);

typedef enum {
  WEBVIMSG_DONE
} WebviMsgType;

typedef struct {
  WebviMsgType msg;
  WebviHandle handle;
  int status_code;
  char *data;
} WebviMsg;

typedef enum {
  WEBVIREQ_MENU,
  WEBVIREQ_FILE,
  WEBVIREQ_STREAMURL
} WebviRequestType;

typedef enum {
  WEBVIERR_UNKNOWN_ERROR = -1,
  WEBVIERR_OK = 0,
  WEBVIERR_INVALID_HANDLE,
  WEBVIERR_INVALID_PARAMETER
} WebviResult;

typedef enum {
  WEBVIOPT_WRITEFUNC,
  WEBVIOPT_READFUNC,
  WEBVIOPT_WRITEDATA,
  WEBVIOPT_READDATA,
} WebviOption;

typedef enum {
  WEBVIINFO_URL,
  WEBVIINFO_CONTENT_LENGTH,
  WEBVIINFO_CONTENT_TYPE,
  WEBVIINFO_STREAM_TITLE
} WebviInfo;

#define WEBVI_SELECT_TIMEOUT -1

typedef enum {
  WEBVI_SELECT_CHECK = 0,
  WEBVI_SELECT_READ = 1,
  WEBVI_SELECT_WRITE = 2,
  WEBVI_SELECT_EXCEPTION = 4
} WebviSelectBitmask;

typedef enum {
  WEBVI_CONFIG_TEMPLATE_PATH,
  WEBVI_CONFIG_DEBUG,
  WEBVI_CONFIG_TIMEOUT_CALLBACK,
  WEBVI_CONFIG_TIMEOUT_DATA
} WebviConfig;

typedef long WebviCtx;

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Initialize the library. Must be called before any other functions
 * (the only exception is webvi_version() which can be called before
 * the library is initialized).
 *
 * Returns 0, if initialization succeeds.
 */
int webvi_global_init(void);

/*
 * Frees all resources currently used by libwebvi and terminates all
 * active connections. Do not call any libwebvi function after this.
 * If the cleanup_python equals 0, the Python library is deinitialized
 * by calling Py_Finalize(), otherwise the Python library is left
 * loaded to be used by other modules of the program.
 */
void webvi_cleanup(int cleanup_python);

/*
 * Create a new context. A valid context is required for calling other
 * functions in the library. The created contextes are independent of
 * each other. The context must be destroyed by a call to
 * webvi_cleanup_context when no longer needed.
 *
 * Return value 0 indicates an error.
 */
WebviCtx webvi_initialize_context(void);

/*
 * Free resources allocated by context ctx. The context can not be
 * used anymore after a call to this function.
 */
void webvi_cleanup_context(WebviCtx ctx);

/*
 * Return the version of libwebvi as a string. The returned value
 * points to a status buffer, and the caller should modify or not free() it.
 */
const char* webvi_version(void);

/*
 * Return a string describing an error code. The returned value points
 * to a status buffer, and the caller should not modify or free() it.
 */
const char* webvi_strerror(WebviCtx ctx, WebviResult err);

/*
 * Set a new value for a global configuration option conf.
 *
 * Possible values and their meanings:
 *
 * WEBVI_CONFIG_TEMPLATE_PATH
 *   Set the base directory for the XSLT templates (char *)
 *
 * WEBVI_CONFIG_DEBUG
 *   If value is not "0", print debug output to stdin (char *)
 *
 * WEBVI_CONFIG_TIMEOUT_CALLBACK
 *   Set timeout callback function (webvi_timeout_callback)
 *
 * WEBVI_CONFIG_TIMEOUT_DATA
 *   Set user data which will passed as second argument of the timeout
 *   callback (void *)
 *
 * The strings (char * arguments) are copied to the library (the user
 * can free their original copy).
 */
WebviResult webvi_set_config(WebviCtx ctx, WebviConfig conf, ...);

/*
 * Creates a new download request.
 *
 * webvireference is a wvt:// URI of the resource that should be
 * downloaded. type should be WEBVIREQ_MENU, if the resource should be
 * transformed into a XML menu (that is if webvireferece comes from
 * <ref> tag), WEBVIREQ_FILE, if the resource points to a media stream
 * (from <stream> tag) whose contents should be downloaded, or
 * WEBVIREQ_STREAMURL, if the resource is points to a media stream
 * whose real URL should be resolved.
 *
 * Typically, the reference has been acquired from a previously
 * downloaded menu. A special constant "wvt:///?srcurl=mainmenu" with
 * type WEBVIREQ_MENU can be used to download mainmenu.
 *
 * The return value is a handle to the newly created request. Value -1
 * indicates an error.
 *
 * The request is initialized but the actual network transfer is not
 * started. You can set up additional configuration options on the
 * handle using webvi_set_opt() before starting the handle with
 * webvi_start_handle().
 */
WebviHandle webvi_new_request(WebviCtx ctx, const char *wvtreference, WebviRequestType type);

/*
 * Starts the transfer on handle h. The transfer one or more sockets
 * whose file descriptors are returned by webvi_fdset(). The actual
 * transfer is done during webvi_perform() calls.
 */
WebviResult webvi_start_handle(WebviCtx ctx, WebviHandle h);

/*
 * Requests that the transfer on handle h shoud be aborted. After the
 * library has actually finished aborting the transfer, the handle h
 * is returned by webvi_get_message() with non-zero status code.
 */
WebviResult webvi_stop_handle(WebviCtx ctx, WebviHandle h);

/*
 * Frees resources associated with handle h. The handle can not be
 * used after this call. If the handle is still in the middle of a
 * transfer, the transfer is forcefully aborted.
 */
WebviResult webvi_delete_handle(WebviCtx ctx, WebviHandle h);

/*
 * Sets configuration options that changes behaviour of the handle.
 * opt is one of the values of WebviOption enum as indicated below.
 * The fourth parameter sets the value of the specified option. Its
 * type depends on opt as discussed below.
 *
 * Possible values for opt:
 *
 * WEBVIOPT_WRITEFUNC
 *
 * Set the callback function that shall be called when data is read
 * from the network. The fourth parameter is a pointer to the callback
 * funtion
 *
 * ssize_t (*webvi_callback)(const char *, size_t, void *).
 *
 * When the function is called, the first parameter is a pointer to
 * the incoming data, the second parameters is the size of the
 * incoming data block in bytes, and the third parameter is a pointer
 * to user's data structure can be set by WEBVIOPT_WRITEDATA option.
 *
 * The callback funtion should return the number of bytes is
 * processed. If this differs from the size of the incoming data
 * block, it indicates that an error occurred and the transfer will be
 * aborted.
 *
 * If write callback has not been set (or if it is set to NULL) the
 * incoming data is printed to stdout.
 *
 * WEBVIOPT_WRITEDATA
 *
 * Sets the value that will be passed to the write callback. The
 * fourth parameter is of type void *.
 *
 * WEBVIOPT_READFUNC
 *
 * Set the callback function that shall be called when data is to be
 * send to network. The fourth parameter is a pointer to the callback
 * funtion
 *
 * ssize_t (*webvi_callback)(const char *, size_t, void *)
 *
 * The first parameter is a pointer to a buffer where the data that is
 * to be sent should be written. The second parameter is the maximum
 * size of the buffer. The thirs parameter is a pointer to user data
 * set with WEBVIOPT_READDATA.
 *
 * The return value should be the number of bytes actually written to
 * the buffer. If the return value is -1, the transfer is aborted.
 *
 * WEBVIOPT_READDATA
 *
 * Sets the value that will be passed to the read callback. The
 * fourth parameter is of type void *.
 *
 */
WebviResult webvi_set_opt(WebviCtx ctx, WebviHandle h, WebviOption opt, ...);

/*
 * Get information specific to a WebviHandle. The value will be
 * written to the memory location pointed by the third argument. The
 * type of the pointer depends in the second parameter as discused
 * below. 
 *
 * Available information:
 * 
 * WEBVIINFO_URL
 *
 * Receive URL. The third parameter must be a pointer to char *. The
 * caller must free() the memory.
 *
 * WEBVIINFO_CONTENT_LENGTH
 *
 * Receive the value of Content-length field, or -1 if the size is
 * unknown. The third parameter must be a pointer to long.
 *
 * WEBVIINFO_CONTENT_TYPE
 *
 * Receive the Content-type string. The returned value is NULL, if the
 * Content-type is unknown. The third parameter must be a pointer to
 * char *. The caller must free() the memory.
 * 
 * WEBVIINFO_STREAM_TITLE
 *
 * Receive stream title. The returned value is NULL, if title is
 * unknown. The third parameter must be a pointer to char *. The
 * caller must free() the memory.
 *
 */
WebviResult webvi_get_info(WebviCtx ctx, WebviHandle h, WebviInfo info, ...);

/*
 * Get active file descriptors in use by the library. The file
 * descriptors that should be waited for reading, writing or
 * exceptions are returned in read_fd_set, write_fd_set and
 * exc_fd_set, respectively. The fd_sets are not cleared, but the new
 * file descriptors are added to them. max_fd will contain the highest
 * numbered file descriptor that was returned in one of the fd_sets.
 *
 * One should wait for action in one of the file descriptors returned
 * by this function using select(), poll() or similar system call,
 * and, after seeing action on a file descriptor, call webvi_perform
 * on that descriptor.
 */
WebviResult webvi_fdset(WebviCtx ctx, fd_set *readfd, fd_set *writefd, fd_set *excfd, int *max_fd);

/*
 * Perform input or output action on a file descriptor.
 * 
 * activefd is a file descriptor that was returned by an earlier call
 * to webvi_fdset and has been signalled to be ready by select() or
 * similar funtion. ev_bitmask should be OR'ed combination of
 * WEBVI_SELECT_READ, WEBVI_SELECT_WRITE, WEBVI_SELECT_EXCEPTION to
 * indicate that activefd has been signalled to be ready for reading,
 * writing or being in exception state, respectively. ev_bitmask can
 * also set to WEBVI_SELECT_CHECK which means that the state is
 * checked internally. On return, running_handles will contain the
 * number of still active file descriptors.
 *
 * If a timeout occurs before any file descriptor becomes ready, this
 * function should be called with sockfd set to WEBVI_SELECT_TIMEOUT
 * and ev_bitmask set to WEBVI_SELECT_CHECK.
 */
WebviResult webvi_perform(WebviCtx ctx, int sockfd, int ev_bitmask, long *running_handles);

/*
 * Return the next message from the message queue. Currently the only
 * message, WEBVIMSG_DONE, indicates that a transfer on a handle has
 * finished. The number of messages remaining in the queue after this
 * call is written to remaining_messages. The pointers in the returned
 * WebviMsg point to handle's internal buffers and is valid until the
 * next call to webvi_get_message(). The caller should free the
 * returned WebviMsg. The return value is NULL if there is no messages
 * in the queue.
 */
WebviMsg *webvi_get_message(WebviCtx ctx, int *remaining_messages);

#ifdef __cplusplus
}
#endif


#endif