-
Notifications
You must be signed in to change notification settings - Fork 0
/
dvdplugin.c
196 lines (172 loc) · 4.82 KB
/
dvdplugin.c
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
#include <vdr/plugin.h>
#include <vdr/player.h>
#include <unistd.h>
#include "imagelist.h"
#include "dvdplugin.h"
cDVDPluginThread::cDVDPluginThread(const char *image)
:cThread("DVDPluginThread")
{
dsyslog("dvdswitch: Create new DVD Thread");
Image = image ? strdup(image) : NULL;
}
cDVDPluginThread::~cDVDPluginThread(void)
{
dsyslog("dvdswitch: DVD Thread stopped");
free(Image);
Cancel();
}
void cDVDPluginThread::Action(void)
{
dsyslog("dvdswitch: DVD Thread started");
if(Image)
cDVDPlugin::ChangeLink(Image, DVDSwitchSetup.DVDLink);
#if VDRVERSNUM < 10332
cPlugin *plugin = cPluginManager::GetPlugin("dvd");
if(plugin)
{
plugin->MainMenuAction();
dsyslog("dvdswitch: DVD MainMenuAction called");
}
#else
cRemote::CallPlugin("dvd");
dsyslog("dvdswitch: DVD Plugin called");
#endif
cCondWait::SleepMs(2 * 1000);
#if APIVERSNUM >= 20402
cControl *control;
{ // need scope to release the mutex
cMutexLock mutexLock;
control = cControl::Control(mutexLock);
}
#else
cControl *control = cControl::Control();
#endif
dsyslog("dvdswitch: DVD control %s",control ? "found" : "not found");
while(control)
{
cCondWait::SleepMs(5 * 1000);
#if APIVERSNUM >= 20402
cMutexLock mutexLock;
control = cControl::Control(mutexLock);
#else
control = cControl::Control();
#endif
}
dsyslog("dvdswitch: DVD control closed");
if(Image)
cDVDPlugin::ChangeLink(DVDSwitchSetup.DVDLinkOrg, DVDSwitchSetup.DVDLink);
dsyslog("dvdswitch: DVD Thread closed");
cDVDPlugin::Exit();
}
// --- cDVDPlugin ----------------------------------------------------------------
cDVDPluginThread *cDVDPlugin::thread = NULL;
void cDVDPlugin::DetectDevice(void)
{
dsyslog("dvdswitch: parse cmdline of dvd-plugin for DVD Device");
char *file = NULL;
char *dvd = NULL;
if(0 < asprintf(&file, "/proc/%i/cmdline", getpid())) {
/* read /proc/`pidof vdr`/cmdline */
char buffer[1024];
int len = -1;
int fd = open(file,O_RDONLY);
if (fd >= 0) {
do {
len = read(fd,buffer,sizeof(buffer)); }
while (len == -1 && errno == EINTR);
close(fd);
} else {
char* szErr = get_strerror(errno);
esyslog("dvdswitch: could not open %s:%s", file, szErr ? szErr : "");
if(szErr) free(szErr);
}
free(file);
if (len < 0) {
char* szErr = get_strerror(errno);
esyslog("dvdswitch: could not read %s:%s", file, szErr ? szErr : "");
if(szErr) free(szErr);
} else {
char *output = NULL;
for(int n = 0; n < len && !dvd;n += (strlen(output) + 1)) {
output = buffer + n;
//dsyslog("dvdswitch: parse '%s'", output);
if(0 == strncmp(output, "--plugin=dvd ",13)
|| 0 == strncmp(output, "-Pdvd ",6)
|| 0 == strncmp(output, "-P dvd ",7)
|| 0 == strncmp(output, "-P'dvd ",7)
|| 0 == strncmp(output, "-P\"dvd ",7)) {
cTokenizer *token = new cTokenizer(output, " ");
for(int i = 0; i <= token->Count(); i++) {
const char* t = token->GetToken(i);
if(t && 0 == strcmp(t, "-C")
&& token->GetToken(i + 1)) {
dvd = strdup(token->GetToken(i + 1));
break;
}
else if(t && 0 == strncmp(t, "--dvd=", 6)) {
const char *p = strchr(t, '=');
dvd = strdup(p + 1);
break;
}
}
DELETENULL(token);
}
}
}
if(!dvd) {
dvd = strdup("/dev/dvd");
}
if(dvd) {
isyslog("dvdswitch: use DVD Device: %s", dvd);
DVDSwitchSetup.SetDVDDevice(dvd);
free(dvd);
}
}
}
void cDVDPlugin::SetLink(void)
{
int argc = 2;
char *argv[2] = { NULL, NULL };
if(0 < asprintf(&argv[0], "%s", "dvd")) {
if(0 < asprintf(&argv[1], "--dvd=%s", DVDSwitchSetup.DVDLink)) {
cPlugin *plugin = cPluginManager::GetPlugin("dvd");
if(plugin)
{
optind = 0; //reset for getopt
plugin->ProcessArgs(argc, argv);
}
free(argv[1]);
}
free(argv[0]);
}
}
void cDVDPlugin::Start(const char *image)
{
if(!thread)
{
thread = new cDVDPluginThread(image);
thread->Start();
}
}
void cDVDPlugin::Exit(void)
{
if(thread)
DELETENULL(thread);
}
void cDVDPlugin::ChangeLink(const char *target,const char *link)
{
if(target && link)
{
isyslog("dvdswitch: create link %s to %s", link, target);
if(-1 == unlink(link) && errno != ENOENT) {
char* szErr = get_strerror(errno);
esyslog("dvdswitch: could not remove symbolic link %s : %s (%d)", link, szErr ? szErr : "", errno);
if(szErr) free(szErr);
}
if(-1 == symlink(target, link)) {
char* szErr = get_strerror(errno);
esyslog("dvdswitch: could not create link %s to %s :%s (%d)", link, target, szErr ? szErr : "", errno);
if(szErr) free(szErr);
}
}
}