summaryrefslogtreecommitdiff
path: root/src/libwebvi/libwebvi.c
blob: b3d030a3e4c09a6f297f31546f90f507443dc611 (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
/*
 * libwebvi.c
 *
 * Copyright (c) 2010-2013 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/>.
 */

#include <stdarg.h>
#include <string.h>
#include <libxml/xmlversion.h>
#include "libwebvi.h"
#include "webvicontext.h"
#include "request.h"
#include "version.h"

static const char *VERSION = "libwebvi/" LIBWEBVI_VERSION;

struct WebviErrorMessage {
  WebviResult code;
  const char *message;
};

int webvi_global_init() {
  LIBXML_TEST_VERSION
  return 0;
}

void webvi_cleanup() {
  webvi_context_cleanup_all();
}

WebviCtx webvi_initialize_context(void) {
  return webvi_context_initialize();
}

void webvi_cleanup_context(WebviCtx ctxhandle) {
  webvi_context_cleanup(ctxhandle);
}

const char* webvi_version(void) {
  return VERSION;
}

const char* webvi_strerror(WebviResult err) {
  static struct WebviErrorMessage error_messages[] = 
    {{WEBVIERR_OK, "Succeeded"},
     {WEBVIERR_INVALID_HANDLE, "Invalid handle"},
     {WEBVIERR_INVALID_PARAMETER, "Invalid parameter"},
     {WEBVIERR_UNKNOWN_ERROR, "Internal error"}};

  for (int i=0; i<(sizeof(error_messages)/sizeof(error_messages[0])); i++) {
    if (err == error_messages[i].code) {
      return error_messages[i].message;
    }
  }

  return "Internal error";
}

WebviResult webvi_set_config(WebviCtx ctxhandle, WebviConfig conf, ...) {
  va_list argptr;
  const char *p;
  WebviResult res = WEBVIERR_OK;

  WebviContext *ctx = get_context_by_handle(ctxhandle);
  if (!ctx)
    return WEBVIERR_INVALID_HANDLE;

  va_start(argptr, conf);

  switch (conf) {
  case WEBVI_CONFIG_TEMPLATE_PATH:
    p = va_arg(argptr, char *);
    webvi_context_set_template_path(ctx, p);
    break;
  case WEBVI_CONFIG_DEBUG:
    p = va_arg(argptr, char *);
    webvi_context_set_debug(ctx, strcmp(p, "0") != 0);
    break;
  case WEBVI_CONFIG_TIMEOUT_CALLBACK:
    // FIXME
    // va_arg(argptr, long)
    break;
  case WEBVI_CONFIG_TIMEOUT_DATA:
    // FIXME
    // va_arg(argptr, long)
    break;
  default:
    res = WEBVIERR_INVALID_PARAMETER;
  };

  va_end(argptr);

  return res;
}

WebviHandle webvi_new_request(WebviCtx ctxhandle, const char *href) {
  WebviContext *ctx = get_context_by_handle(ctxhandle);
  if (!ctx)
          return WEBVI_INVALID_HANDLE;

  WebviRequest *req = request_create(href, ctx);
  return webvi_context_add_request(ctx, req);
}

WebviResult webvi_start_request(WebviCtx ctxhandle, WebviHandle h) {
  WebviContext *ctx = get_context_by_handle(ctxhandle);
  if (!ctx)
    return WEBVIERR_INVALID_HANDLE;

  WebviRequest *req = webvi_context_get_request(ctx, h);
  if (!req)
    return WEBVIERR_INVALID_HANDLE;

  request_start(req);

  return WEBVIERR_OK;
}

WebviResult webvi_stop_request(WebviCtx ctxhandle, WebviHandle h) {
  WebviContext *ctx = get_context_by_handle(ctxhandle);
  if (!ctx)
    return WEBVIERR_INVALID_HANDLE;

  WebviRequest *req = webvi_context_get_request(ctx, h);
  if (!req)
    return WEBVIERR_INVALID_HANDLE;

  request_stop(req);

  return WEBVIERR_OK;
}

WebviResult webvi_delete_request(WebviCtx ctxhandle, WebviHandle h) {
  WebviContext *ctx = get_context_by_handle(ctxhandle);
  if (!ctx)
    return WEBVIERR_INVALID_HANDLE;

  WebviResult res = webvi_stop_request(ctxhandle, h);
  if (res != WEBVIERR_OK)
    return res;

  webvi_context_remove_request(ctx, h);

  return WEBVIERR_OK;
}

WebviResult webvi_set_opt(WebviCtx ctxhandle, WebviHandle h, WebviOption opt, ...) {
  WebviContext *ctx = get_context_by_handle(ctxhandle);
  if (!ctx)
    return WEBVIERR_INVALID_HANDLE;

  WebviRequest *req = webvi_context_get_request(ctx, h);
  if (!req)
    return WEBVIERR_INVALID_HANDLE;

  va_list argptr;
  WebviResult res = WEBVIERR_OK;

  va_start(argptr, opt);

  switch (opt) {
  case WEBVIOPT_WRITEFUNC:
    request_set_write_callback(req, va_arg(argptr, webvi_callback));
    break;
  case WEBVIOPT_READFUNC:
    request_set_read_callback(req, va_arg(argptr, webvi_callback));
    break;
  case WEBVIOPT_WRITEDATA:
    request_set_write_data(req, va_arg(argptr, void *));
    break;
  case WEBVIOPT_READDATA:
    request_set_read_data(req, va_arg(argptr, void *));
    break;
  default:
    res = WEBVIERR_INVALID_PARAMETER;
  };

  va_end(argptr);

  return res;
}

WebviResult webvi_get_info(WebviCtx ctxhandle, WebviHandle h, WebviInfo info, ...) {
  WebviContext *ctx = get_context_by_handle(ctxhandle);
  if (!ctx)
    return WEBVIERR_INVALID_HANDLE;

  WebviRequest *req = webvi_context_get_request(ctx, h);
  if (!req)
    return WEBVIERR_INVALID_HANDLE;

  va_list argptr;
  WebviResult res = WEBVIERR_OK;

  va_start(argptr, info);

  switch (info) {
  case WEBVIINFO_URL:
  {
    char **output = va_arg(argptr, char **);
    if (output) {
      *output = NULL;

      const char *url = request_get_url(req);
      if (url) {
        *output = malloc(strlen(url)+1);
        if (*output) {
          strcpy(*output, url);
        }
      }
    }
    break;
  }

  case WEBVIINFO_CONTENT_LENGTH:
  {
    // FIXME
    long *content_length = va_arg(argptr, long *);
    if (content_length)
      *content_length = -1;
    break;
  }

  case WEBVIINFO_CONTENT_TYPE:
  {
    // FIXME
    char **output = va_arg(argptr, char **);
    if (output) {
      *output = malloc(1);
      **output = '\0';
    }
    break;
  }

  case WEBVIINFO_STREAM_TITLE:
  {
    // FIXME
    char **output = va_arg(argptr, char **);
    if (output) {
      *output = malloc(1);
      **output = '\0';
    }
    break;
  }

  default:
    res = WEBVIERR_INVALID_PARAMETER;
  };

  va_end(argptr);

  return res;
}

WebviResult webvi_fdset(WebviCtx ctxhandle, fd_set *readfd, fd_set *writefd,
                        fd_set *excfd, int *max_fd) {
  WebviContext *ctx = get_context_by_handle(ctxhandle);
  if (!ctx)
    return WEBVIERR_INVALID_HANDLE;

  return webvi_context_fdset(ctx, readfd, writefd, excfd, max_fd);
}

WebviResult webvi_perform(WebviCtx ctxhandle, int sockfd, int ev_bitmask,
                          long *running_handles) {
  WebviContext *ctx = get_context_by_handle(ctxhandle);
  if (!ctx)
    return WEBVIERR_INVALID_HANDLE;

  webvi_context_handle_socket_action(ctx, sockfd, ev_bitmask, running_handles);

  return WEBVIERR_OK;
}

WebviMsg *webvi_get_message(WebviCtx ctxhandle, int *remaining_messages) {
  WebviContext *ctx = get_context_by_handle(ctxhandle);
  if (!ctx)
    return NULL;

  return webvi_context_next_message(ctx, remaining_messages);
}

int webvi_process_some(WebviCtx ctx, int timeout_seconds) {
  fd_set readfds;
  fd_set writefds;
  fd_set excfds;
  int maxfd;
  int s;
  WebviResult res;
  struct timeval timeout;
  long running_handles;

  FD_ZERO(&readfds);
  FD_ZERO(&writefds);
  FD_ZERO(&excfds);
  res = webvi_fdset(ctx, &readfds, &writefds, &excfds, &maxfd);
  if (res != WEBVIERR_OK) {
    return -1;
  }

  timeout.tv_sec = timeout_seconds;
  timeout.tv_usec = 0;
  s = select(maxfd+1, &readfds, &writefds, NULL, &timeout);

  if (s == -1) {
    // error
    return -1;
  } else if (s == 0) {
    // timeout
    webvi_perform(ctx, WEBVI_SELECT_TIMEOUT, WEBVI_SELECT_CHECK, &running_handles);
  } else {
    // handle one fd
    for (int fd=0; fd<=maxfd; fd++) {
      if (FD_ISSET(fd, &readfds)) {
        webvi_perform(ctx, fd, WEBVI_SELECT_READ, &running_handles);
      } else if (FD_ISSET(fd, &writefds)) {
        webvi_perform(ctx, fd, WEBVI_SELECT_WRITE, &running_handles);
      } else if (FD_ISSET(fd, &excfds)) {
        webvi_perform(ctx, fd, WEBVI_SELECT_EXCEPTION, &running_handles);
      }
    }
  }

  return running_handles;
}