]> mj.ucw.cz Git - libucw.git/blob - lib/mainloop.h
Read and write functions accept void pointers instead of byte*.
[libucw.git] / lib / mainloop.h
1 /*
2  *      Sherlock Library -- Main Loop
3  *
4  *      (c) 2004 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/clists.h"
11
12 extern sh_time_t now;                           /* Current time */
13 extern uns main_shutdown;
14
15 /* User-defined fields are marked with [*], all other fields must be initialized to zero. */
16
17 /* Timers */
18
19 struct main_timer {
20   cnode n;
21   sh_time_t expires;
22   void (*handler)(struct main_timer *tm);       /* [*] Function to be called when the timer expires. Must re-add/del the timer.*/
23   void *data;                                   /* [*] Data for use by the handler */
24 };
25
26 void timer_add(struct main_timer *tm, sh_time_t expires);       /* Can modify a running timer, too */
27 void timer_del(struct main_timer *tm);
28
29 /* Files to poll */
30
31 struct main_file {
32   cnode n;
33   int fd;                                       /* [*] File descriptor */
34   int (*read_handler)(struct main_file *fi);    /* [*] To be called when ready for reading/writing; must call file_chg() afterwards */
35   int (*write_handler)(struct main_file *fi);
36   void (*error_handler)(struct main_file *fi, int cause);       /* [*] Handler to call on errors */
37   void *data;                                   /* [*] Data for use by the handlers */
38   byte *rbuf;                                   /* Read/write pointers for use by file_read/write */
39   uns rpos, rlen;
40   byte *wbuf;
41   uns wpos, wlen;
42   void (*read_done)(struct main_file *fi);      /* [*] Called when file_read is finished; rpos < rlen if EOF */
43   void (*write_done)(struct main_file *fi);     /* [*] Called when file_write is finished */
44   struct main_timer timer;
45   struct pollfd *pollfd;
46 };
47
48 enum main_file_err_cause {
49   MFERR_READ,
50   MFERR_WRITE,
51   MFERR_TIMEOUT
52 };
53
54 void file_add(struct main_file *fi);
55 void file_chg(struct main_file *fi);
56 void file_del(struct main_file *fi);
57 void file_read(struct main_file *fi, void *buf, uns len);
58 void file_write(struct main_file *fi, void *buf, uns len);
59 void file_set_timeout(struct main_file *fi, sh_time_t expires);
60
61 /* Hooks to be called in each iteration of the main loop */
62
63 struct main_hook {
64   cnode n;
65   int (*handler)(struct main_hook *ho);         /* [*] Hook function; returns 1 if should be called again */
66   void *data;                                   /* [*] For use by the handler */
67 };
68
69 void hook_add(struct main_hook *ho);
70 void hook_del(struct main_hook *ho);
71
72 /* Processes to watch */
73
74 struct main_process {
75   cnode n;
76   int pid;
77   int status;                                   /* Exit status */
78   void (*handler)(struct main_process *mp);     /* [*] Called when the process exits; process_del done automatically */
79   void *data;                                   /* [*] For use by the handler */
80 };
81
82 void process_add(struct main_process *mp);
83 void process_del(struct main_process *mp);
84 int process_fork(struct main_process *mp);
85
86 /* The main loop */
87
88 void main_init(void);
89 void main_loop(void);
90 void main_debug(void);