summaryrefslogtreecommitdiff
path: root/dvdplugin.c
blob: 950ba00e69173c6d5201b6b390f933d2acd6dca7 (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
#include <vdr/remote.h>
#include <vdr/plugin.h>
#include <vdr/player.h>
#include <unistd.h>
#include "dvdplugin.h"
#include "debug.h"

cDVDPluginThread::cDVDPluginThread(char *image)
  :cThread("DVDPluginThread")
{
  DEBUG("Neuer DVD Thread angelegt");
  Image = image ? strdup(image) : NULL;
}

cDVDPluginThread::~cDVDPluginThread(void)
{
  DEBUG("DVD Thread beendet");

  free(Image);
  Cancel();
}

void cDVDPluginThread::Action(void)
{
  DEBUG("DVD Thread gestartet");
  if(Image)
    cDVDPlugin::ChangeLink(Image);

#if VDRVERSNUM < 10332
  cPlugin *plugin = cPluginManager::GetPlugin("dvd");

  if(plugin)
  {
    plugin->MainMenuAction();
    DEBUG("DVD MainMenuAction gestartet");
  }
#else
  cRemote::CallPlugin("dvd");
  DEBUG("DVD Plugin gestaret");
#endif

  cCondWait::SleepMs(2 * 1000);
  cControl *control = cControl::Control();
  DEBUG(control ? (char*)"Control gefunden" : (char*)"Control nicht gefunden");
  while(control)
  {
    cCondWait::SleepMs(5 * 1000);
    control = cControl::Control();
  }
  DEBUG("Control wurde geschlossen");

  if(Image)
    cDVDPlugin::ChangeLink(DVDSwitchSetup.DVDLinkOrg);

  DEBUG("Beende thread");
  cDVDPlugin::Exit();
}

// --- cDVDPlugin ----------------------------------------------------------------

cDVDPluginThread *cDVDPlugin::thread = NULL;

void cDVDPlugin::DetectDevice(void)
{
  DEBUG("Scan nach DVD Device");
  char *cmd = NULL;
  char *output = NULL;
  char *dvd = NULL;

  asprintf(&cmd, "ps -p %i -o cmd --no-header", getpid());
  DEBUG("Commando: %s", cmd);

  FILE *p = popen(cmd, "r");
  if(p)
  {
#if VDRVERSNUM >= 10318
    cReadLine rl;
    output = rl.Read(p);
#else
    output = readline(p);
#endif
    pclose(p);
  }

  cTokenizer *token = new cTokenizer(output, " ");
  for(int i = 1; i <= token->Count(); i++)
  {
    if(RegIMatch(token->GetToken(i), "^--plugin=dvd") ||
       (RegIMatch(token->GetToken(i - 1), "^-P$") &&
        RegIMatch(token->GetToken(i), "^dvd$")))
    {
      if(RegIMatch(token->GetToken(i + 1), "^-C$") &&
         token->GetToken(i + 2))
      {
        dvd = strdup(token->GetToken(i + 2));
        break;
      }
      if(RegIMatch(token->GetToken(i + 1), "^--dvd="))
      {
        char *p = strchr(token->GetToken(i + 1), '=');
        p++;
        dvd = strdup(p);
        break;
      }
    }
  }
  DELETENULL(token);
  
  if(dvd)
  {
    DEBUG("Erkanntes DVD Device: %s", dvd);
    DVDSwitchSetup.SetDVDDevice(dvd);
  }
  else
  {
    DEBUG("Nehme Standard-DVD Device");
    DVDSwitchSetup.SetDVDDevice("/dev/dvd");
  }

  free(cmd);
  free(dvd);
  //free(output);
}

void cDVDPlugin::SetLink(void)
{
  int argc = 2;
  char *argv[2] = { NULL, NULL };

  asprintf(&argv[0], "%s", "dvd");
  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[0]);
  free(argv[1]);
}

void cDVDPlugin::Start(char *image)
{
  if(!thread)
  {
    thread = new cDVDPluginThread(image);
    thread->Start();
  }
}

void cDVDPlugin::Exit(void)
{
  if(thread)
    DELETENULL(thread);
}

void cDVDPlugin::ChangeLink(char *target)
{
  if(target)
  {
    char *cmd = NULL;
    int rc = 0;

    asprintf(&cmd, LINK, target, DVDSwitchSetup.DVDLink);
    DEBUG("Ändere link: %s", cmd);
    rc = system(cmd);
    DEBUG("Rückgabe Änderung: %i", rc);
    free(cmd);
  }
}