5230010c813b4abf1202b9e9036e24b2578fcf40
[stwbeast.git] / sfi / sficomwire.h
1 /* SFI - Synthesis Fusion Kit Interface
2  * Copyright (C) 2002 Tim Janik
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * A copy of the GNU Lesser General Public License should ship along
15  * with this library; if not, see http://www.gnu.org/copyleft/.
16  */
17 #ifndef __SFI_COM_WIRE_H__
18 #define __SFI_COM_WIRE_H__
19
20 #include <sfi/sfitypes.h>
21 #include <sfi/sfiring.h>
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif /* __cplusplus */
26
27
28 typedef struct _SfiComWire SfiComWire;
29 typedef gboolean (*SfiComDispatch)      (gpointer        data,
30                                          guint           request,
31                                          const gchar    *request_msg,
32                                          SfiComWire     *wire);
33 struct _SfiComWire
34 {
35   gchar         *ident;         /* debugging identifier for this connection */
36   gpointer       owner;         /* ScriptControl object */
37   guint          connected : 1;
38   guint          remote_input_broke : 1;
39   guint          remote_output_broke : 1;
40   guint          standard_input_broke : 1;
41   guint          standard_output_broke : 1;
42   guint          standard_error_broke : 1;
43   
44   SfiComDispatch dispatch_func;
45   gpointer       dispatch_data;
46   GDestroyNotify destroy_data;
47
48   /* message queues */
49   GList         *orequests;     /* outgoing requests */
50   GList         *iresults;      /* incoming results */
51   GList         *irequests;     /* incoming requests */
52   GList         *rrequests;     /* received requests */
53
54   /* I/O channels */
55   gint           remote_input;          /* readable */
56   gint           remote_output;         /* writable */
57
58   /* spawned child */
59   gint           standard_input;        /* writable */
60   gint           standard_output;       /* readable */
61   gint           standard_error;        /* readable */
62   gint           remote_pid;
63   GString       *gstring_stdout;
64   GString       *gstring_stderr;
65
66   /* input buffer */
67   guint8        *ibuffer;
68   guint8        *ibp;
69   guint8        *ibound;
70
71   /* output buffer */
72   guint8        *obuffer;
73   guint8        *obp;
74   guint8        *obound;
75 };
76
77 typedef enum /*< skip >*/
78 {
79   SFI_COM_MSG_INVALID           = 0,
80   SFI_COM_MSG_RESERVED1         = 1,
81   SFI_COM_MSG_RESERVED2         = 2,
82   SFI_COM_MSG_RESERVED3         = 3,
83   SFI_COM_MSG_RESERVED4         = 4,
84   SFI_COM_MSG_REQUEST           = 5,
85   SFI_COM_MSG_RESULT            = 6
86 } SfiComMsgType;
87 #define BSE_MAGIC_BSEm          (0x4253456d)    /* "BSEm" */
88 typedef struct
89 {
90   guint32       magic;          /* "BSEm" 0x4253456d */
91   guint32       mlength;        /* total length, including magic */
92   guint32       type;
93   guint32       request;
94   gchar        *message;
95 } SfiComMsg;
96
97
98 /* create wires */
99 SfiComWire*     sfi_com_wire_from_pipe          (const gchar    *ident,
100                                                  gint            remote_input,
101                                                  gint            remote_output);
102 SfiComWire*     sfi_com_wire_from_child         (const gchar    *ident,
103                                                  gint            remote_input,
104                                                  gint            remote_output,
105                                                  gint            standard_input,
106                                                  gint            standard_output,
107                                                  gint            standard_error,
108                                                  gint            remote_pid);
109
110 /* handle outgoing */
111 guint           sfi_com_wire_send_request       (SfiComWire     *wire,
112                                                  const gchar    *request_msg);
113 gchar*          sfi_com_wire_receive_result     (SfiComWire     *wire,
114                                                  guint           request);
115 void            sfi_com_wire_forget_request     (SfiComWire     *wire,
116                                                  guint           request);
117 guint           sfi_com_wire_peek_first_result  (SfiComWire     *wire);
118
119 /* handle incomming */
120 const gchar*    sfi_com_wire_receive_request    (SfiComWire     *wire,
121                                                  guint          *request);
122 void            sfi_com_wire_send_result        (SfiComWire     *wire,
123                                                  guint           request,
124                                                  const gchar    *result_msg);
125 void            sfi_com_wire_discard_request    (SfiComWire     *wire,
126                                                  guint           request);
127
128 /* dispatching */
129 void            sfi_com_wire_set_dispatcher     (SfiComWire     *wire,
130                                                  SfiComDispatch  dispatch_func,
131                                                  gpointer        dispatch_data,
132                                                  GDestroyNotify  destroy_data);
133 void            sfi_com_wire_dispatch           (SfiComWire     *wire,
134                                                  guint           request);
135 gboolean        sfi_com_wire_need_dispatch      (SfiComWire     *wire);
136
137 /* wire I/O */
138 gint*           sfi_com_wire_get_read_fds       (SfiComWire     *wire,
139                                                  guint          *n_fds);
140 gint*           sfi_com_wire_get_write_fds      (SfiComWire     *wire,
141                                                  guint          *n_fds);
142 GPollFD*        sfi_com_wire_get_poll_fds       (SfiComWire     *wire,
143                                                  guint          *n_pfds);
144 void            sfi_com_wire_process_io         (SfiComWire     *wire);
145 gchar*          sfi_com_wire_collect_stdout     (SfiComWire     *wire,
146                                                  guint          *n_chars);
147 gchar*          sfi_com_wire_collect_stderr     (SfiComWire     *wire,
148                                                  guint          *n_chars);
149
150 /* shutdown */
151 void            sfi_com_wire_close_remote       (SfiComWire     *wire,
152                                                  gboolean        terminate);
153 void            sfi_com_wire_destroy            (SfiComWire     *wire);
154
155
156 /* convenience */
157 gboolean        sfi_com_wire_receive_dispatch   (SfiComWire     *wire);
158 void            sfi_com_wire_select             (SfiComWire     *wire,
159                                                  guint           timeout);
160 gchar*          sfi_com_wire_ping_pong          (SfiComWire     *wire,
161                                                  const gchar    *ping,
162                                                  guint           timeout);
163
164
165 /* --- fork/exec --- */
166 void            sfi_com_set_spawn_dir           (const gchar    *cwd);
167 gchar*          sfi_com_spawn_async             (const gchar    *executable,
168                                                  gint           *child_pid,
169                                                  gint           *standard_input,
170                                                  gint           *standard_output,
171                                                  gint           *standard_error,
172                                                  const gchar    *command_fd_option,
173                                                  gint           *command_input,
174                                                  gint           *command_output,
175                                                  SfiRing        *args);
176
177
178 #ifdef __cplusplus
179 }
180 #endif /* __cplusplus */
181
182 #endif /* __SFI_COM_WIRE_H__ */