summaryrefslogtreecommitdiff
path: root/src/irbox.c
blob: 355a932216d2a88ed293fe5fd4b23cc53dfdc17a (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
/*
 *
 *	IRBOX, an Ivy driver for infra-red remote controls
 *
 *	Copyright 1998-1999 
 *	Centre d'Etudes de la Navigation Aerienne
 *
 *	Main file
 *
 *	Authors: Francois-Regis Colin <fcolin@cena.dgac.fr>
 *		Stephane Chatty <chatty@cena.dgac.fr>
 *
 *	$Id$
 *
 *	Please refer to file version.h for the
 *	copyright notice regarding this software
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

#include "timer.h"
#include "ivy.h"
#include "ivychannel.h"

#include "irdev.h"
#include "irtable.h"

#ifndef DEFAULT_DEVICE
#define DEFAULT_DEVICE "/dev/ttyS1"
#endif

#ifndef DEFAULT_IRBOX_DIR
#define DEFAULT_IRBOX_DIR "."
#endif

static const char* data_dir_name = 0;
static const char* app_name;
static IrState *ir;


static void
HandleChannel (Channel channel, int fd, void *data)
{
	IrState *ir = (IrState*) data;
	IrIntr (ir);
}

static void*
IrTimeout (IrTimerCallback cb, long value, void *data )
{
	return TimerRepeatAfter (1, value, (TimerCb) cb, ir);
}

static void
IrUntimeout (void *id)
{
	TimerRemove ((TimerId) id);
}

static void
IrHandleHup (int sig)
{
	IrResetTables ();
	IrReadTables (data_dir_name);
}

static void
IrEventCb (IrState *ir, const unsigned char* code)
{
	IrTable* t;
	const char* key;
	if (IrTableTranslateCode (code, &t, &key)) {
		IvySendMsg ("%s Down control=%s button=%s", app_name, t->name, key);
	} else {
		IvySendMsg ("%s unknown code %.3d %.3d %.3d %.3d %.3d %.3d", app_name, code[0], code[1],code[2],code[3],code[4],code[5]);
		fprintf (stderr, "unknown code %.3d %.3d %.3d %.3d %.3d %.3d\n", code[0], code[1],code[2],code[3],code[4],code[5]);
	}
}

static void
IrFailCb (IrState* ir)
{
	fprintf (stderr,"IR box not responding.\n");
	IvySendMsg ("%s Failed", app_name);
	IvyStop ();
}

int
main (int argc, char *argv[])
{
	int c;
	const char* dev = DEFAULT_DEVICE;
	const char* bus = 0;
	static char app_name_buf [1024];
	struct sigaction act;

	while ((c = getopt(argc, argv, "s:b:n:p:d:")) != EOF)
		switch (c)	{
		case 's':
			dev = optarg;
			break;
		case 'b':
			bus = optarg;
			break;
		case 'n':
			app_name = optarg;
			break;
		case 'p':
			data_dir_name = optarg;
			break;
		}

	if (!app_name) {
		gethostname (app_name_buf, 1024);
		strcat (app_name_buf, ".IRBOX");
		app_name = app_name_buf;
	}

	/* data directory */
	if (!data_dir_name)
		data_dir_name = getenv ("IRBOX_DIR");
	if (!data_dir_name)
		data_dir_name = DEFAULT_IRBOX_DIR;

	ir = IrOpen (dev);
	if (!ir) {
		fprintf (stderr, "Can't open %s\n", dev);
		return -1;
	}

	if (!IrInit (ir, IrEventCb, IrFailCb, IrTimeout, IrUntimeout))
		return -1;

	/* Let's read tables and provide for their re-reading on SIGHUP */
	IrReadTables (data_dir_name);
	act.sa_handler = IrHandleHup;
	sigemptyset (&act.sa_mask);
	act.sa_flags = 0;
	sigaction (1, &act, 0);


	IvyInit (app_name, 0, 0, 0, 0, 0);
	IvyChannelSetUp (IrGetFd (ir), ir, 0, HandleChannel);

	IvyStart (bus);
	IvyMainLoop (0);

	return 0 ; 
}