summaryrefslogtreecommitdiff
path: root/plugin.cc
blob: dbea398be1f30e13fb3a1f5ad52dca286b85c9db (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*	--*- c++ -*--
 * Copyright (C) 2012 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
 *
 * 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; version 2 and/or 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.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <getopt.h>
#include <unistd.h>

#include <vdr/plugin.h>

#include "inputdev.h"
#include "modmap.h"

static char const *DEFAULT_SOCKET_PATH = SOCKET_PATH;
static const char *VERSION        = PACKAGE_VERSION;
static const char *DESCRIPTION    = trNOOP("Linux input device plugin");

class cInputDevicePlugin : public cPlugin {
private:
	class cInputDeviceController	*controller_;
	ModifierMap			mod_map_;

	enum {
		enSOCKET,
		enSYSTEMD
	}				socket_type_;

	union {
		char const		*path;
		int			idx;
	}				socket_;

	cString				coldplug_dir;
	cString				mod_map_fname_;

private:
	cInputDevicePlugin(cInputDevicePlugin const &);
	cInputDevicePlugin &operator = (cInputDevicePlugin const &);

public:
	cInputDevicePlugin(void);
	virtual ~cInputDevicePlugin();

	virtual const char *Version(void) { return VERSION; }
	virtual const char *Description(void) { return tr(DESCRIPTION); }

	virtual bool	ProcessArgs(int argc, char *argv[]);
	virtual bool	Initialize(void);
	virtual bool	Start(void);
	virtual void	Stop(void);
};

cInputDevicePlugin::cInputDevicePlugin() :
	controller_(NULL), coldplug_dir("/dev/vdr/input")
{
}

cInputDevicePlugin::~cInputDevicePlugin(void)
{
	delete controller_;
}

bool cInputDevicePlugin::ProcessArgs(int argc, char *argv[])
{
	static struct option const	CMDLINE_OPTIONS[] = {
		{ "systemd", required_argument, NULL, 'S' },
		{ "socket",  required_argument, NULL, 's' },
		{ "modmap",  required_argument, NULL, 'M' },
		{ }
	};

	char const	*systemd_idx = NULL;
	char const	*socket_path = NULL;
	bool		is_ok = true;

	for (;;) {
		int		c;

		c = getopt_long(argc, argv, "S:s:M:", CMDLINE_OPTIONS, NULL);
		if (c == -1)
			break;

		switch (c) {
		case 'S':
#ifdef VDR_USE_SYSTEMD
			systemd_idx = optarg;
			break;
#else
			esyslog("%s: systemd support has not been compiled in\n",
				Name());
			return false;
#endif
		case 's':  socket_path = optarg; break;
		case 'M':  mod_map_fname_ = optarg; break;
		default:
			esyslog("%s: invalid option\n", Name());
			return false;
		}
	}

	if (systemd_idx != NULL && socket_path != NULL) {
		esyslog("%s: both systemd idx and socket path given\n",
			Name());
		return false;
	}

	if (systemd_idx == NULL && socket_path == NULL)
		socket_path = DEFAULT_SOCKET_PATH;

	if (systemd_idx != NULL) {
		socket_type_ = enSYSTEMD;
		socket_.idx  = atoi(systemd_idx);
	} else if (socket_path != NULL) {
		socket_type_ = enSOCKET;
		socket_.path = socket_path;
	} else
		is_ok = false;

	return is_ok;
}

bool cInputDevicePlugin::Initialize(void)
{
	bool		is_ok;

	if (mod_map_fname_ != "")
		mod_map_.read_modmap(mod_map_fname_);
	// \todo: handle errors?

	controller_ = new cInputDeviceController(*this, mod_map_);

	switch (socket_type_) {
#ifdef VDR_USE_SYSTEMD
	case enSYSTEMD:
		is_ok = controller_->open_udev_socket(socket_.idx);
		break;
#endif

	case enSOCKET:
		is_ok = controller_->open_udev_socket(socket_.path);
		break;

	default:
		esyslog("%s: bad internal socket type %d\n", Name(),
			socket_type_);
		is_ok = false;
		break;
	}

	if (is_ok)
		is_ok = controller_->initialize(coldplug_dir);

	if (!is_ok) {
		delete controller_;
		controller_ = NULL;
	}

	return is_ok;
}

bool cInputDevicePlugin::Start(void)
{
	return controller_->start();
}

void cInputDevicePlugin::Stop(void)
{
	controller_->stop();
	delete controller_;
	controller_ = NULL;
}

VDRPLUGINCREATOR(cInputDevicePlugin); // Don't touch this!