summaryrefslogtreecommitdiff
path: root/src/ivyglutloop.c
blob: 95430e5ac14f62094ec532b533cd0ab4b2818929 (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
/*
 *	Ivy, C interface
 *
 *	Copyright (C) 1997-1999
 *	Centre d'Études de la Navigation Aérienne
 *
 * 	Main loop based on GLUT ( OpenGL ) Toolkit
 *
 *	Authors: François-Régis Colin <colin@cena.fr>
 *		 Stéphane Chatty <chatty@cena.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 <GL/glut.h>

#include "ivychannel.h"
#include "ivyglutloop.h"

struct _channel {
	GLUTInputId id_read;
	GLUTInputId id_delete;
	void *data;
	ChannelHandleDelete handle_delete;
	ChannelHandleRead handle_read;
	};


static int channel_initialized = 0;



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
	channel_initialized = 1;
}
void IvyChannelStop(void)
{
	channel_initialized = 0;
}
void IvyChannelClose( Channel channel )
{

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


static void IvyGlutHandleChannelRead( int source, GLUTInputId id, void *data )
{
	Channel channel = (Channel)data;
#ifdef DEBUG
	printf("Handle Channel read %d\n",source );
#endif
	(*channel->handle_read)(channel,source,channel->data);
}

static void IvyGlutHandleChannelDelete( int source, GLUTInputId id, void *data )
{
	Channel channel = (Channel)data;
#ifdef DEBUG
	printf("Handle Channel delete %d\n",source );
#endif
	(*channel->handle_delete)(channel->data);
}

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

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

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

	channel->id_read = glutAddInput( fd,  IvyGlutHandleChannelRead, channel);
	channel->id_delete = glutAddInput( fd, IvyGlutHandleChannelDelete, channel);

	return channel;
}

extern void IvyMainLoop(void(*hook)(void))
{
	glutMainLoop();
}