File: menu.c

package info (click to toggle)
vdr-plugin-remoteosd 0.1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 184 kB
  • sloc: ansic: 1,052; makefile: 69; sh: 17
file content (400 lines) | stat: -rw-r--r-- 9,673 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <stdio.h>
#include <stdlib.h>
#include <vdr/thread.h>
#include <vdr/remote.h>
#include <vdr/skins.h>
#include <vdr/themes.h>
#include <vdr/interface.h>
#include <vdr/status.h>
#include "i18n.h"
#include "menu.h"
#include "setup.h"

#define PLUG_EXT "PLUG svdrpext "
#define PLUG_OSD "PLUG svdrposd "

#define SVDRP_SERVER_TIMEOUT_MS 10
#define RC_OK 920
#define RC_NA 930

cRemoteOsdMenu::cRemoteOsdMenu(const char *Title): cOsdMenu(Title) {
	plugin = cPluginManager::GetPlugin("svdrpservice");
	isEmpty = true;
	plugOsd = true;
	if (*RemoteOsdSetup.remoteTheme)
		cThemes::Load(Skins.Current()->Name(), RemoteOsdSetup.remoteTheme, Skins.Current()->Theme());
}

cRemoteOsdMenu::~cRemoteOsdMenu() {
	if (plugin && svdrp.handle >= 0) {
		// probably MENU key has been hit - close remote menu
		if (!isEmpty)
			CmdHITK("MENU");
		// close SVDRP connection
		plugin->Service("SvdrpConnection-v1.0", &svdrp);
	}
	if (*RemoteOsdSetup.remoteTheme)
		cThemes::Load(Skins.Current()->Name(), Setup.OSDTheme, Skins.Current()->Theme());
}

bool cRemoteOsdMenu::Open(const char *ServerIp, unsigned short ServerPort, const char *Key) {
	if (!plugin) {
		esyslog("remoteosd: svdrpservice-plugin required but not available");
		return false;
	}

	// Get the connection
	svdrp.serverIp = ServerIp;
	svdrp.serverPort = ServerPort;
	svdrp.shared = true;
	svdrp.handle = -1;
	plugin->Service("SvdrpConnection-v1.0", &svdrp);
	if (svdrp.handle < 0)
		return false;

	// Close remote OSD if necessary
	int state;
	for (int i = 1; (state = CheckState()) != RC_NA; i++) {
		if (state != RC_OK) // error occured
			return false;
		if (i == 1 && !Interface->Confirm(tr("Remote OSD already in use. Proceed anyway?")))
			return false;
		else if (i > 2) {
			esyslog("remoteosd: Cannot close remote menu");
			return false;
		}
		if (!CmdHITK("MENU"))
			return false;
	}

	if (RemoteOsdSetup.tuneServer) {
		cChannel *channel = Channels.GetByNumber(cDevice::CurrentChannel());
		CmdCHAN(channel);
	}

	// Now open the remote menu
	if (CmdHITK(Key) && CheckState() == RC_OK)
		return UpdateMenu();
	return false;
}

int cRemoteOsdMenu::CheckState() {
	SvdrpCommand_v1_0 cmd;
	// assume the OSD is empty if there is no title
	cmd.command = cString(plugOsd ? PLUG_OSD "OSDT\r\n" : PLUG_EXT "OSDT\r\n");
	cmd.handle = svdrp.handle;
	plugin->Service("SvdrpCommand-v1.0", &cmd);
	if (cmd.responseCode == 550) {
		if (plugOsd) {
			// no svdrposd-plugin - fallback to svdrpext-plugin
			plugOsd = false;
			return CheckState();
		}
		else {
			esyslog("remoteosd: svdrposd-plugin not installed on %s", *svdrp.serverIp);
		}
	}
	return cmd.responseCode;
}

bool cRemoteOsdMenu::CmdCHAN(cChannel *Channel) {
	SvdrpCommand_v1_0 cmd;
	cmd.command = cString::sprintf("CHAN %s\r\n", *Channel->GetChannelID().ToString());
	cmd.handle = svdrp.handle;
	plugin->Service("SvdrpCommand-v1.0", &cmd);
	return cmd.responseCode == 250;
}

bool cRemoteOsdMenu::CmdHITK(const char* Key) {
	SvdrpCommand_v1_0 cmd;
	cmd.command = cString::sprintf("HITK %s\r\n", Key);
	cmd.handle = svdrp.handle;
	plugin->Service("SvdrpCommand-v1.0", &cmd);

	// A timeout in cFile::AnyFileReady defines how long the
	// server will now wait for more SVDRP commands before
	// it actually processes our HITK command
	cCondWait::SleepMs(SVDRP_SERVER_TIMEOUT_MS);

	return cmd.responseCode == 250;
}

bool cRemoteOsdMenu::CmdLSTO() {
	int maxItems = RemoteOsdSetup.maxItems;
	// special value 0: use local maxItems
	if (maxItems == 0)
		maxItems = DisplayMenu()->MaxItems();

	SvdrpCommand_v1_0 cmd;
	cmd.command = cString::sprintf(PLUG_OSD "LSTO %d\r\n", maxItems);
	cmd.handle = svdrp.handle;
	plugin->Service("SvdrpCommand-v1.0", &cmd);

	int cols[cSkinDisplayMenu::MaxTabs];
	int currentCol = 0;
	memset(&cols, 0, sizeof(cols));

	switch (cmd.responseCode) {
		case RC_OK:
			for (cLine *line = cmd.reply.First(); line; line = cmd.reply.Next(line)) {
				const char *s = line->Text();
				if (strlen(s) > 2) {
					switch (*s) {
						case 'T':
							title = cString::sprintf("%s: %s", *svdrp.serverIp, s + 2);
							isEmpty = false;
							break;
						case 'C':
							if (currentCol < cSkinDisplayMenu::MaxTabs)
								cols[currentCol++] = strtol(s + 2, NULL, 10);
							break;
						case 'I':
							Add(new cOsdItem(s + 2));
							isEmpty = false;
							break;
						case 'S':
							Add(new cOsdItem(s + 2), true);
							isEmpty = false;
							break;
						case 'R': red    = s + 2; break;
						case 'G': green  = s + 2; break;
						case 'Y': yellow = s + 2; break;
						case 'B': blue   = s + 2; break;
						case 'M':
							message = cString::sprintf("%s: %s", *svdrp.serverIp, s + 2);
							break;
						case 'X': {
							char* t = strdup(s + 2);
							strreplace(t, '|', '\n');
							text = cString(t, true);
							isEmpty = false;
							}
							break;
						default:
							esyslog("remoteosd: unexpected reply type '%c'", *s);
							break;
					}
				}
			}
			SetTitle(*title ? title : svdrp.serverIp);
			SetCols(cols[0], cols[1], cols[2], cols[3], cols[4]);
			SetHelp(red, green, yellow, blue);
			SetStatus(message);
			break;
		case RC_NA:
			break;
		default:
			return false;
	}
	return true;
}

bool cRemoteOsdMenu::CmdOSDT() {
	SvdrpCommand_v1_0 cmd;
	cLine *line;

	cmd.command = cString(plugOsd ? PLUG_OSD "OSDT\r\n" : PLUG_EXT "OSDT\r\n");
	cmd.handle = svdrp.handle;
	plugin->Service("SvdrpCommand-v1.0", &cmd);

	switch (cmd.responseCode) {
		case RC_OK:
			line = cmd.reply.First();
			title = cString::sprintf("%s: %s", *svdrp.serverIp, line->Text());
			isEmpty = false;
			break;
		case RC_NA:
			title = svdrp.serverIp;
			break;
		default:
			return false;
	}
	SetTitle(title);
	return true;
}

bool cRemoteOsdMenu::CmdOSDI() {
	int maxItems = RemoteOsdSetup.maxItems;
	// special value 0: use local maxItems
	if (maxItems == 0)
		maxItems = DisplayMenu()->MaxItems();

	SvdrpCommand_v1_0 cmd;
	cmd.command = cString::sprintf(PLUG_EXT "OSDI %d\r\n", maxItems);
	cmd.handle = svdrp.handle;
	plugin->Service("SvdrpCommand-v1.0", &cmd);

	int cols[cSkinDisplayMenu::MaxTabs];
	int currentCol = 0;
	memset(&cols, 0, sizeof(cols));

	switch (cmd.responseCode) {
		case RC_OK:
			for (cLine *line = cmd.reply.First(); line; line = cmd.reply.Next(line)) {
				const char *s = line->Text();
				if (strlen(s) > 2) {
					switch (*s) {
						case 'C': if (currentCol < cSkinDisplayMenu::MaxTabs)
								  cols[currentCol++] = strtol(s + 2, NULL, 10);
							  break;
						case 'I': Add(new cOsdItem(s + 2)); break;
						case 'S': Add(new cOsdItem(s + 2), true); break;
					}
				}
			}
			SetCols(cols[0], cols[1], cols[2], cols[3], cols[4]);
			isEmpty = false;
			break;
		case RC_NA:
			break;
		default:
			return false;
	}
	return true;
}

bool cRemoteOsdMenu::CmdOSDH() {
	SvdrpCommand_v1_0 cmd;

	cmd.command = cString(PLUG_EXT "OSDH\r\n");
	cmd.handle = svdrp.handle;
	plugin->Service("SvdrpCommand-v1.0", &cmd);

	switch (cmd.responseCode) {
		case RC_OK:
			for (cLine *line = cmd.reply.First(); line; line = cmd.reply.Next(line)) {
				const char *s = line->Text();
				if (strlen(s) > 2) {
					switch (*s) {
						case 'R': red = s + 2; break;
						case 'G': green = s + 2; break;
						case 'Y': yellow = s + 2; break;
						case 'B': blue = s + 2; break;
					}
				}
			}
			isEmpty = false;
			break;
		case RC_NA:
			break;
		default:
			return false;
	}
	SetHelp(red, green, yellow, blue);
	return true;
}

bool cRemoteOsdMenu::CmdOSDM() {
	SvdrpCommand_v1_0 cmd;
	cLine *line;

	cmd.command = cString(PLUG_EXT "OSDM\r\n");
	cmd.handle = svdrp.handle;
	plugin->Service("SvdrpCommand-v1.0", &cmd);

	switch (cmd.responseCode) {
		case RC_OK:
			line = cmd.reply.First();
			message = cString::sprintf("%s: %s", *svdrp.serverIp, line->Text());
			break;
		case RC_NA:
			break;
		default:
			return false;
	}
	SetStatus(message);
	return true;
}

bool cRemoteOsdMenu::CmdOSDX() {
	SvdrpCommand_v1_0 cmd;
	cLine *line;

	cmd.command = cString(PLUG_EXT "OSDX\r\n");
	cmd.handle = svdrp.handle;
	plugin->Service("SvdrpCommand-v1.0", &cmd);

	switch (cmd.responseCode) {
		case RC_OK: {
			line = cmd.reply.First();
			char* t = strdup(line->Text());
			strreplace(t, '|', '\n');
			text = cString(t, true);
			isEmpty = false;
			}
			break;
		case RC_NA:
			break;
		default:
			return false;
	}
	return true;
}

bool cRemoteOsdMenu::UpdateMenu() {
	Clear();

	if (plugOsd) {
		if (!CmdLSTO())
			return false;
	}
	else {
		if (!CmdOSDT() || !CmdOSDM() || !CmdOSDH() || !CmdOSDI())
			return false;
		// Check for text only if no item is shown
		if (Count() == 0 && !CmdOSDX())
			return false;
	}

	Display();
	return true;
}

void cRemoteOsdMenu::Clear() {
	cOsdMenu::Clear();
	title = NULL;
	message = NULL;
	text = NULL;
	red = NULL;
	green = NULL;
	yellow = NULL;
	blue = NULL;
	isEmpty = true;
}

void cRemoteOsdMenu::Display(void)
{
	cOsdMenu::Display();
	if (*text) {
		DisplayMenu()->SetText(text, true);
		cStatus::MsgOsdTextItem(text);
	}
}

eOSState cRemoteOsdMenu::ProcessKey(eKeys Key) {
	if (Key == kNone)
		return osContinue;	//TODO: Update menu e.g. once per second

	if (Key & k_Release)
		return osContinue;

	if (*text) {
		switch (Key) {
			case kUp|k_Repeat:
			case kUp:
			case kDown|k_Repeat:
			case kDown:
			case kLeft|k_Repeat:
			case kLeft:
			case kRight|k_Repeat:
			case kRight:
				DisplayMenu()->Scroll(NORMALKEY(Key) == kUp || NORMALKEY(Key) == kLeft, NORMALKEY(Key) == kLeft || NORMALKEY(Key) == kRight);
				cStatus::MsgOsdTextItem(NULL, NORMALKEY(Key) == kUp);
				return osContinue;
			default: break;
		}
	}
	if (CmdHITK(cKey::ToString(RAWKEY(Key))) && UpdateMenu())
		return isEmpty ? osBack : osContinue;
	return osBack;
}