core: output_hook defaults sending to 'socket' for now...
[lumina.git] / core / src / output_hook.c
blobd362fb51301744d34d97a99ebd7d8d55c3cc5dbe
1 #include "output_hook.h"
2 #include "output_item.h"
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <memory.h>
7 #include <malloc.h>
9 #include <sys/queue.h>
11 SIMPLEQ_HEAD(output_list, output_item);
13 struct output_hook {
14 struct output_list queue;
17 output_hook *new_output_hook() {
18 output_hook *ret = calloc(1, sizeof(output_hook));
19 if(!ret) return NULL;
20 SIMPLEQ_INIT(&ret->queue);
21 return ret;
24 void release_output_hook(output_hook *hook) {
25 if(hook) free(hook);
28 void output_hook_queue(output_hook *hook, output_item *item) {
29 SIMPLEQ_INSERT_TAIL(&hook->queue, item, queue_entries);
32 static output_item *get_next(output_hook *hook) {
33 output_item *next = SIMPLEQ_FIRST(&hook->queue);
34 while(next && !output_remaining(next)) {
35 SIMPLEQ_REMOVE_HEAD(&hook->queue, queue_entries);
36 output_on_complete(next);
37 /* PERFORM RELEASE... user callback should be doing this */
38 output_release(next);
39 next = SIMPLEQ_FIRST(&hook->queue);
41 return next;
44 void output_hook_callback(EV_P_ struct ev_io *w, int revents) {
45 output_hook *hook = w->data;
46 int fd = w->fd;
47 ssize_t ret;
48 output_item *next = get_next(hook);
49 fprintf(stderr, "In output_hook\n");
50 while(next) {
51 /* ASSUME OUTPUT IS PIPE FOR NOW */
52 ssize_t ret = output_send(next, fd, FDT_SOCKET);
53 if(ret < 0) break; /* Error... output_item should have triggered */
54 next = get_next(hook);
56 /* Completed normally */
57 if(next == NULL) {
58 ev_io_stop(EV_A_ w);
59 return;
61 if(ret < 0 && errno != EAGAIN) {
62 perror("Something went wrong while writing out to the fd");
63 ev_io_stop(EV_A_ w);