File: lircin.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (217 lines) | stat: -rw-r--r-- 5,607 bytes parent folder | download
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
/** \file server/drivers/lircin.c
 * LCDd \c lirc input driver for LIRC infrared devices (http://www.lirc.org),
 */

/*
    Copyright (C) 2000, Harald Klein
                  2002, Rene Wagner
    [Merged some stuff from a different lircin driver, so:]
                  1999, David Glaude

    This program 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; either version 2 of the License, or
    any later version.

    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; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
*/


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <lirc/lirc_client.h>

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "lcd.h"
#include "lircin.h"

#include "shared/report.h"


/** private data for the \c lirc driver */
typedef struct lircin_private_data {
	char *lircrc;			/**< path/name of the LIRC config file */
	char *prog;			/**< program identifier in LIRC config file */
	int lircin_fd;			/**< LIRC socket file handle */
	struct lirc_config *lircin_irconfig;	/**< LIRC config */
} PrivateData;


/* vars for the server core */
MODULE_EXPORT char *api_version = API_VERSION;
MODULE_EXPORT int stay_in_foreground = 0;
MODULE_EXPORT int supports_multiple = 0;
MODULE_EXPORT char *symbol_prefix = "lircin_";


/**
 * Initialize the driver.
 * \param drvthis  Pointer to driver structure.
 * \retval 0       Success.
 * \retval <0      Error.
 */
MODULE_EXPORT int
lircin_init (Driver *drvthis)
{
	char s[256] = "";

	PrivateData *p;

	/* Alocate and store private data */
        p = (PrivateData *) malloc(sizeof(PrivateData));
	if (p == NULL) {
		report(RPT_ERR, "%s: Could not allocate private data", drvthis->name);
	        return -1;
	}
	if (drvthis->store_private_ptr(drvthis, p)) {
		report(RPT_ERR, "%s: Could not store private data", drvthis->name);
	        return -1;
	}

	/* Initialise the PrivateData structure */
	p->lircrc = NULL;
	p->prog = NULL;
	p->lircin_irconfig = NULL;
	p->lircin_fd = -1;

	/* READ CONFIG FILE:*/

	/* Get location of lircrc to be used */
	if (drvthis->config_get_string(drvthis->name, "lircrc", 0 , NULL) != NULL) {
		strncpy(s, drvthis->config_get_string(drvthis->name, "lircrc", 0, ""), sizeof(s));
		s[sizeof(s)-1] = '\0';
	}

	if (*s != '\0') {
		p->lircrc = malloc(strlen(s) + 1);
		if (p->lircrc == NULL) {
			report(RPT_ERR, "%s: Could not allocate new memory", drvthis->name);
			return -1;
		}
		strcpy(p->lircrc, s);
		report(RPT_INFO,"%s: Using lircrc: %s", drvthis->name, p->lircrc);
	}
	else {
		report(RPT_INFO,"%s: Using default lircrc: ~/.lircrc", drvthis->name);
	}

	/* Get program identifier "prog=..." to be used */
	strncpy(s, drvthis->config_get_string(drvthis->name, "Prog", 0, LIRCIN_DEF_PROG), sizeof(s));

	p->prog = malloc(strlen(s) + 1);
	if (p->prog == NULL) {
		report(RPT_ERR, "%s: Could not allocate new memory", drvthis->name);
		return -1;
	}
	strcpy(p->prog, s);
	report(RPT_INFO, "%s: Using prog: %s", drvthis->name, p->prog);

	/* End of config file parsing */

	/* open socket to lirc */
	if (-1 == (p->lircin_fd = lirc_init(p->prog, LIRCIN_VERBOSELY))) {
		report(RPT_ERR, "%s: Could not connect to lircd", drvthis->name);

		lircin_close(drvthis);
		return -1;
	}

	if (0 != lirc_readconfig(p->lircrc, &p->lircin_irconfig, NULL)) {
		report(RPT_ERR, "%s: lirc_readconfig() failed", drvthis->name);

		lircin_close(drvthis);
		return -1;
	}

	/* socket shouldn't block lcdd */
	if (fcntl(p->lircin_fd, F_SETFL, O_NONBLOCK) < 0){
                report(RPT_ERR, "%s: Unable to change lircin_fd(%d) to O_NONBLOCK: %s",
			drvthis->name, p->lircin_fd, strerror(errno));

		lircin_close(drvthis);
		return -1;
		}
	fcntl (p->lircin_fd, F_SETFD, FD_CLOEXEC);

	report(RPT_DEBUG, "%s: init() done", drvthis->name);

	return 0;
}


/**
 * Close the driver (do necessary clean-up).
 * \param drvthis  Pointer to driver structure.
 */
MODULE_EXPORT void
lircin_close (Driver *drvthis)
{
        PrivateData * p = drvthis->private_data;

	if (p != NULL) {
		if (p->lircrc != NULL)
			free(p->lircrc);
		p->lircrc = NULL;

		if (p->prog != NULL)
			free(p->prog);
		p->prog = NULL;

		if (p->lircin_irconfig != NULL)
			lirc_freeconfig (p->lircin_irconfig);
		p->lircin_irconfig = NULL;

		if (p->lircin_fd >= 0) {
			lirc_deinit ();
			close (p->lircin_fd);
		}
		p->lircin_fd = -1;

		free(p);
	}
	drvthis->store_private_ptr(drvthis, NULL);
}


/**
 * Get key from the device.
 * \param drvthis  Pointer to driver structure.
 * \return         String representation of the key;
 *                 \c NULL if nothing available / unmapped key.
 */
MODULE_EXPORT const char *
lircin_get_key (Driver *drvthis)
{
        PrivateData * p = drvthis->private_data;

	char *code = NULL, *cmd = NULL;

	if ((lirc_nextcode(&code) == 0) && (code != NULL)) {
		if ((lirc_code2char(p->lircin_irconfig,code,&cmd)==0) && (cmd!=NULL)) {
			report(RPT_DEBUG, "%s: \"%s\"", drvthis->name, cmd);
		}
		free(code);
	}

	return cmd;
}