gsource.c
8.17 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright 2008, Banco do Brasil S.A.
*
* This file is part of g3270
*
* This program file 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; version 3 of the License.
*
* 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 in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
*
* Perry Werneck<perry.werneck@gmail.com>
*
*/
#include "g3270.h"
#include <glib.h>
#define __USE_XOPEN
#include <poll.h>
#include "lib/hostc.h"
#define SRC_SIZE 3
// #ifdef DEBUG
// #define LOCK DBGMessage("Lock"); g_mutex_lock(mutex);
// #define UNLOCK DBGMessage("Unlock"); g_mutex_unlock(mutex);
// #else
#define LOCK g_mutex_lock(mutex);
#define UNLOCK g_mutex_unlock(mutex);
// #endif
/*---[ Structs ]--------------------------------------------------------------*/
#pragma pack(1)
typedef struct _srcdata
{
GSource sr;
} SRCDATA;
typedef struct _pooldata
{
GPollFD gfd; // MUST BE THE FIRST ONE!!!
const INPUT_3270 *ip;
int events;
} POOLDATA;
#pragma pack()
/*---[ Prototipes ]-----------------------------------------------------------*/
// http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html#GSourceFuncs
static gboolean prepare_3270(GSource *source, gint *timeout);
static gboolean check_3270(GSource *source);
static gboolean dispatch_3270(GSource *source, GSourceFunc callback, gpointer user_data);
static void finalize_3270(GSource *source); /* Can be NULL */
static gboolean closure_3270(gpointer data);
// static void DummyMarshal_3270(void); /* Really is of type GClosureMarshal */
/*---[ Constants ]------------------------------------------------------------*/
/* 3270 Event Sources */
static GSourceFuncs Source_3270 =
{
prepare_3270,
check_3270,
dispatch_3270,
finalize_3270,
closure_3270,
NULL
};
/*---[ Globals ]--------------------------------------------------------------*/
static GSource *fd3270 = 0;
static GMutex *mutex = 0;
static int szPoll = 0;
static POOLDATA *gpool = 0;
struct pollfd *fds = 0;
static Boolean inputs_changed = FALSE;
/*---[ Implement ]------------------------------------------------------------*/
int gsource_init(void)
{
/* Add 3270 as a new gtk event source */
mutex = g_mutex_new();
fd3270 = g_source_new(&Source_3270,sizeof(SRCDATA));
g_source_attach(fd3270,NULL);
return 0;
}
void gsource_addfile(const INPUT_3270 *ip)
{
// http://developer.gnome.org/doc/API/glib/glib-the-main-event-loop.html#G-MAIN-ADD-POLL
POOLDATA *fd = 0;
int sz;
int f;
LOCK
if(!gpool)
{
szPoll = SRC_SIZE;
sz = szPoll * sizeof(POOLDATA);
gpool = g_malloc(sz);
if(gpool)
{
memset(gpool,0,sz);
}
else
{
Log("Error allocating %d bytes for the GpoolFD structures",sz);
return;
}
}
for(f = 0; f < szPoll && !fd; f++)
{
if(!gpool[f].ip)
fd = gpool+f;
}
if(!fd)
{
DBGPrintf("No more space in the poll table, adding more %d entries",SRC_SIZE);
sz = (szPoll+SRC_SIZE) * sizeof(POOLDATA);
gpool = g_realloc(gpool,sz);
if(!gpool)
{
Log("Error resizing GpoolFD structures to %d bytes",sz);
return;
}
for(f=0;f < SRC_SIZE;f++)
{
memset(gpool+(szPoll+f),0,sizeof(POOLDATA));
}
fd = gpool+szPoll;
szPoll += SRC_SIZE;
}
#ifdef DEBUG // Sanity check
if(fd->ip)
{
DBGMessage("ERROR!!! The new poll structure has data!!!");
}
#endif
memset(fd,0,sizeof(POOLDATA));
fd->ip = ip;
fd->gfd.fd = ip->source;
if(ip->condition & InputReadMask)
{
fd->gfd.events |= (G_IO_IN|G_IO_PRI);
fd->events = (POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI);
}
if(ip->condition & InputExceptMask)
{
fd->gfd.events |= (G_IO_HUP|G_IO_NVAL|G_IO_ERR);
fd->events = (POLLERR|POLLHUP|POLLNVAL);
}
if(ip->condition & InputWriteMask)
{
fd->gfd.events |= G_IO_OUT;
fd->events = (POLLOUT|POLLWRBAND);
}
inputs_changed = TRUE;
g_source_add_poll(fd3270,&fd->gfd);
/* reset the poll() vector to the right size */
sz = szPoll * sizeof(struct pollfd);
if(fds)
fds = g_realloc(fds,sz);
else
fds = g_malloc(sz);
memset(fds,0,sz);
UNLOCK
DBGPrintf("Source %p added (GPool=%p, fd=%d, masc=%02x)",ip,fd,ip->source,ip->condition);
}
void gsource_removefile(const INPUT_3270 *ip)
{
POOLDATA *fd = 0;
int f;
LOCK
for(f = 0; f < szPoll && !fd; f++)
{
if(gpool[f].ip == ip)
fd = gpool+f;
}
if(fd)
{
inputs_changed = TRUE;
g_source_remove_poll(fd3270,&fd->gfd);
memset(fd,0,sizeof(POOLDATA));
}
else
{
Log("Unexpected call to gsource_removefile(%p) (fd=%d, masc=%02x)",ip,ip->source,ip->condition);
}
UNLOCK
DBGPrintf("Source %p removed (GPool=%p, fd=%d, masc=%02x)",ip,fd,ip->source,ip->condition);
}
// http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html#GSourceFuncs
static gboolean prepare_3270(GSource *source, gint *timeout)
{
/*
* Called before all the file descriptors are polled.
* If the source can determine that it is ready here
* (without waiting for the results of the poll() call)
* it should return TRUE.
*
* It can also return a timeout_ value which should be the maximum
* timeout (in milliseconds) which should be passed to the poll() call.
* The actual timeout used will be -1 if all sources returned -1,
* or it will be the minimum of all the timeout_ values
* returned which were >= 0.
*
*/
struct timeval tv;
long ln;
if(Check3270Timeouts(&tv))
{
// NOTE (perry#7#): Is there any better way?
ln = (tv.tv_sec * 1000000L);
ln += tv.tv_usec;
if(ln > 0)
*timeout = (ln/1000);
}
return 0;
}
static gboolean check_3270(GSource *source)
{
/*
* Called after all the file descriptors are polled.
* The source should return TRUE if it is ready to be dispatched.
* Note that some time may have passed since the previous prepare
* function was called, so the source should be checked again here.
*
*/
int f;
int qtd = 0;
int rc = FALSE;
LOCK
for(f = 0; f < szPoll; f++)
{
if(gpool[f].gfd.fd)
{
fds[qtd].fd = gpool[f].gfd.fd;
fds[qtd].events = gpool[f].events;
qtd++;
}
}
if(poll(fds,qtd,0) > 0)
rc = TRUE;
// DBGPrintf("Pending events: %s",rc ? "Yes" : "No");
UNLOCK
return rc;
}
static gboolean dispatch_3270(GSource *source, GSourceFunc callback, gpointer user_data)
{
/*
* Called to dispatch the event source,
* after it has returned TRUE in either its prepare or its check function.
* The dispatch function is passed in a callback function and data.
* The callback function may be NULL if the source was never connected
* to a callback using g_source_set_callback(). The dispatch function
* should call the callback function with user_data and whatever additional
* parameters are needed for this type of event source.
*
*/
struct pollfd fds;
gboolean rc = FALSE;
int f;
const INPUT_3270 *ip;
// FIXME (perry#8#): Do it right (using the callback function).
for(f = 0; f < szPoll; f++)
{
if(gpool[f].gfd.fd)
{
ip = gpool[f].ip;
fds.fd = gpool[f].gfd.fd;
fds.events = gpool[f].events;
if(poll(&fds,1,0) > 0)
{
(*ip->proc)();
rc = TRUE;
}
}
}
return rc;
}
static void finalize_3270(GSource *source)
{
LOCK
UNLOCK
}
static gboolean closure_3270(gpointer data)
{
LOCK
UNLOCK
return 0;
}