summaryrefslogtreecommitdiff
path: root/src/ivyxtloop.c
blob: fc5b0ae6ddae42a2adc7735b2a0f0cf67c5a59cd (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
/*
 *	Ivy, C interface
 *
 *	Copyright (C) 1997-2000
 *	Centre d'Études de la Navigation Aérienne
 *
 * 	Main loop based on the X Toolkit
 *
 *	Authors: François-Régis Colin <fcolin@cena.dgac.fr>
 *		 Stéphane Chatty <chatty@cena.dgac.fr>
 *
 *	$Id$
 * 
 *	Please refer to file version.h for the
 *	copyright notice regarding this software
 */

#ifdef WIN32
#include <windows.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#ifdef WIN32
#else
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#endif


#include <X11/Intrinsic.h>

#include "ivydebug.h"
#include "ivychannel.h"
#include "ivyxtloop.h"

struct _channel {
	XtInputId id_read;
	XtInputId id_write;
	XtInputId id_delete;

	HANDLE fd;
	void *data;
	ChannelHandleDelete handle_delete;
	ChannelHandleRead handle_read;
	ChannelHandleWrite handle_write;
	};


static int channel_initialized = 0;


static XtAppContext    app = NULL;


void IvyChannelInit(void)
{

	if ( channel_initialized ) return;

	/* pour eviter les plantages quand les autres applis font core-dump */
#ifndef WIN32
	signal( SIGPIPE, SIG_IGN);
#endif
	/* verifie si init correct */
	if ( !app )
	{
		fprintf( stderr, "You must call IvyXtChannelAppContext before XtMainLoop. Exiting.\n");
		exit(-1);
	}
	channel_initialized = 1;
}

void IvyChannelRemove( Channel channel )
{

	if ( channel->handle_delete )
		(*channel->handle_delete)( channel->data );
	XtRemoveInput( channel->id_read );
	XtRemoveInput( channel->id_delete );
}


static void IvyXtHandleChannelRead( XtPointer closure, int* source, XtInputId* id )
{
	Channel channel = (Channel)closure;
	TRACE("Handle Channel read %d\n",*source );
	(*channel->handle_read)(channel,*source,channel->data);
}

static void IvyXtHandleChannelWrite( XtPointer closure, int* source, XtInputId* id )
{
	Channel channel = (Channel)closure;
	TRACE("Handle Channel write %d\n",*source );
	(*channel->handle_write)(channel,*source,channel->data);
}

static void IvyXtHandleChannelDelete( XtPointer closure, int* source, XtInputId* id )
{
	Channel channel = (Channel)closure;
	TRACE("Handle Channel delete %d\n",*source );
	(*channel->handle_delete)(channel->data);
}


void IvyXtChannelAppContext( XtAppContext cntx )
{
	app = cntx;
}

Channel IvyChannelAdd(HANDLE fd, void *data,
				ChannelHandleDelete handle_delete,
				ChannelHandleRead handle_read,
		                ChannelHandleWrite handle_write
				)						
{
	Channel channel;

	channel = XtNew( struct _channel );
	if ( !channel )
		{
		fprintf(stderr,"NOK Memory Alloc Error\n");
		exit(0);
		}

	channel->handle_delete = handle_delete;
	channel->handle_read = handle_read;
	channel->handle_write = handle_write;
	channel->data = data;
	channel->fd = fd;

	channel->id_read = XtAppAddInput( app, fd, (XtPointer)XtInputReadMask, IvyXtHandleChannelRead, channel);
	channel->id_delete = XtAppAddInput( app, fd, (XtPointer)XtInputExceptMask, IvyXtHandleChannelDelete, channel);

	return channel;
}


void IvyChannelAddWritableEvent(Channel channel)
{
  channel->id_write = XtAppAddInput( app,  channel->fd, (XtPointer)XtInputWriteMask, IvyXtHandleChannelWrite, channel);
}

void IvyChannelClearWritableEvent(Channel channel)
{
  XtRemoveInput( channel->id_write );
}


void
IvyChannelStop ()
{
  /* To be implemented */
}