summaryrefslogtreecommitdiff
path: root/discmenu.c
blob: 78687722a11fdd0c93e328b145ea00949cad0c9c (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
/*
 * discmenu.c: BluRay disc library menu
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <vdr/tools.h>
#include <vdr/osdbase.h>

#include "bdplayer.h"

#include "discmenu.h"

/*
 *
 */

static bool IsBluRayFolder(const char *fname)
{
  struct stat st;

  if (stat(cString::sprintf("%s/BDMV/index.bdmv", fname), &st) == 0)
    return true;

  return false;
}

/*
 * cDiscItem
 */

class cDiscItem : public cOsdItem
{
 private:
  cString Root;
 public:
  cDiscItem(const char *title, const char *root);
  cDiscItem(const char *title);

  const char *GetRoot() { return Root; }

  virtual int Compare(const cListObject &ListObject) const {
    const cDiscItem *o = (const cDiscItem *)&ListObject;
    return strcmp(o->Root, Root);
  }
};

cDiscItem::cDiscItem(const char *title, const char *root) :
cOsdItem(title, osUser1)
{
  Root = root;
}

cDiscItem::cDiscItem(const char *title) :
cOsdItem(title, osUser2)
{
}

/*
 * cDiscMenu
 */

cDiscMenu::cDiscMenu(cDiscMgr& Mgr, cString& Root) :
    cOsdMenu("BluRay Discs"),
    mgr(Mgr)
{
  Scan(Root);

  Sort();

  if (mgr.IsMounted()) {
    Ins(new cDiscItem(cString::sprintf("BluRay disc (%s)", mgr.GetDev())));
    //SetHelp("Eject");
  } else {
    Ins(new cDiscItem("(Disc not mounted)"));
    //SetHelp("Mount");
  }

  Display();
}

void cDiscMenu::Scan(cString& Root)
{
  DIR *d = opendir(Root);
  if (d) {
    struct dirent *e;
    while ((e = readdir(d)) != NULL) {
      if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..") &&
          e->d_name[0] != '.') {
        cString buffer = cString::sprintf("%s/%s", *Root, e->d_name);
        struct stat st;
        if (stat(buffer, &st) == 0) {

          // check symlink destination
          if (S_ISLNK(st.st_mode)) {
            buffer = ReadLink(buffer);
            if (!*buffer || stat(buffer, &st))
              continue;
          }

          // folders
          if (S_ISDIR(st.st_mode)) {

            if (IsBluRayFolder(buffer)) {
              Add(new cDiscItem(e->d_name, buffer));

            } else {
              Scan(buffer);
            }
          }
        }
      }
    }
    closedir(d);
  }
}

eOSState cDiscMenu::ProcessKey(eKeys Key)
{
  eOSState state = cOsdMenu::ProcessKey(Key);
  switch (state) {
    case osUser1: {
      isyslog("disc select");
      cDiscItem *di = (cDiscItem*)Get(Current());
      if (di) {
        isyslog("- root %s", di->GetRoot());

        cControl::Shutdown();

        cControl *control = cBDControl::Create(di->GetRoot());
        if (control) {
          cControl::Launch(control);
        }

        return osEnd;
      }
      break;
    }

    case osUser2: {
      isyslog("device select");

      if (!mgr.CheckDisc()) {
        return osContinue;
      }

      cControl::Shutdown();

      cControl *control = cBDControl::Create(mgr.GetDev());
      if (control) {
        cControl::Launch(control);
        return osEnd;
      }
      break;
    }

    default:      break;
  }
  return state;
}