summaryrefslogtreecommitdiff
path: root/PLUGINS/src/pictures/player.c
blob: d2dbd7b85c27e7d0743c612f275ec3b60a537cee (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
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
/*
 * player.c: A player for still pictures
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: player.c 3.1 2014/02/08 12:48:12 kls Exp $
 */

#include "player.h"
#include <vdr/remote.h>
#include <vdr/tools.h>

int SlideShowDelay = 3; // seconds

cString HandleUnderscores(const char *s)
{
  // Skip anything before and including the first '_' and replace
  // any remaining '_' with blanks:
  const char *p = strchr(s, '_');
  if (p) {
     p++;
     char buf[strlen(p) + 1];
     strcpy(buf, p);
     return strreplace(buf, '_', ' ');
     }
  return s;
}

// --- cPicturePlayer --------------------------------------------------------

class cPicturePlayer : public cPlayer {
private:
  int size;
  int length;
  uchar *buffer;
  virtual void Activate(bool On);
public:
  cPicturePlayer(void);
  ~cPicturePlayer();
  void SetPicture(const char *FileName);
  };

cPicturePlayer::cPicturePlayer(void)
{
  size = KILOBYTE(100); // will be adjusted automatically if files are larger
  length = 0;
  buffer = MALLOC(uchar, size);
}

cPicturePlayer::~cPicturePlayer()
{
  free(buffer);
}

void cPicturePlayer::Activate(bool On)
{
  if (length > 0)
     DeviceStillPicture(buffer, length);
}

void cPicturePlayer::SetPicture(const char *FileName)
{
  int f = open(FileName, O_RDONLY);
  if (f >= 0) {
     for (;;) {
         length = read(f, buffer, size);
         if (length > 0) {
            if (length >= size) {
               int NewSize = size * 3 / 2;
               if (uchar *NewBuffer = (uchar *)realloc(buffer, NewSize)) {
                  buffer = NewBuffer;
                  size = NewSize;
                  }
               else {
                  LOG_ERROR_STR("out of memory");
                  break;
                  }
               lseek(f, 0, SEEK_SET);
               continue;
               }
            DeviceStillPicture(buffer, length);
            }
         else
            LOG_ERROR_STR(FileName);
         break;
         }
     close(f);
     }
  else
     LOG_ERROR_STR(FileName);
}

// --- cPictureControl -------------------------------------------------------

int cPictureControl::active = 0;
cString cPictureControl::lastDisplayed;

cPictureControl::cPictureControl(cPictureEntry *Pictures, const cPictureEntry *PictureEntry, bool SlideShow)
:cControl(player = new cPicturePlayer)
{
  pictures = Pictures;
  pictureEntry = PictureEntry;
  osd = NULL;
  lastPath = "/";
  slideShowDelay.Set(SlideShowDelay * 1000);
  slideShow = SlideShow;
  alwaysDisplayCaption = false;
  NextPicture(slideShow && pictureEntry->IsDirectory() ? 1 : 0);
  active++;
}

cPictureControl::~cPictureControl()
{
  active--;
  delete osd;
  delete player;
  delete pictures;
}

void cPictureControl::NextPicture(int Direction)
{
  if (Direction) {
     const cPictureEntry *pe = Direction > 0 ? pictureEntry->NextPicture() : pictureEntry->PrevPicture();
     if (pe)
        pictureEntry = pe;
     }
  if (pictureEntry) {
     player->SetPicture(pictureEntry->Path());
     DisplayCaption();
     }
}

void cPictureControl::NextDirectory(int Direction)
{
  // This only works reliable if a directory contains only subdirectories or pictures, not both!
  if (Direction) {
     const cPictureEntry *pe = Direction > 0 ? pictureEntry->Parent()->Entries()->Last()->NextPicture() : pictureEntry->Parent()->Entries()->First()->PrevPicture();
     if (pe && Direction < 0)
        pe = pe->Parent()->Entries()->First();
     if (pe && pe != pictureEntry) {
        pictureEntry = pe;
        player->SetPicture(pictureEntry->Path());
        DisplayCaption();
        }
     }
}

static void DrawTextOutlined(cOsd *Osd, int x, int y, const char *s, tColor ColorFg, tColor ColorBg, const cFont *Font)
{
  for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
          if (dx || dy)
             Osd->DrawText(x + dx, y + dy, s, ColorBg, clrTransparent, Font);
          }
      }
  Osd->DrawText(x, y, s, ColorFg, clrTransparent, Font);
}

void cPictureControl::DisplayCaption(void)
{
  bool Force = false;
  cString Path = pictureEntry->Path();
  lastDisplayed = Path + strlen(pictures->Name()) + 1;
  const char *p = strrchr(Path, '/');
  const char *q = strrchr(lastPath, '/');
  if (p && q) {
     int lp = p - Path;
     int lq = q - lastPath;
     if (lp != lq || strncmp(lastPath, Path, lp)) {
        lastPath = Path;
        Force = true;
        }
     }
  if (!alwaysDisplayCaption && !Force) {
     DELETENULL(osd);
     return;
     }
  const cFont *Font = cFont::GetFont(fontOsd);
  int w = cOsd::OsdWidth();
  int h = 2 * Font->Height();
  if (!osd) {
     osd = cOsdProvider::NewOsd(cOsd::OsdLeft(), cOsd::OsdTop() + cOsd::OsdHeight() - h, OSD_LEVEL_SUBTITLES);
     tArea Areas[] = { { 0, 0, w - 1, h - 1, 8 } };
     if (Setup.AntiAlias && osd->CanHandleAreas(Areas, sizeof(Areas) / sizeof(tArea)) == oeOk)
        osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
     else {
        tArea Areas[] = { { 0, 0, w - 1, h - 1, 4 } };
        osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
        }
     }
  const char *Year = pictureEntry->Parent()->Parent() ? pictureEntry->Parent()->Parent()->Name() : "";
  cString Description = HandleUnderscores(pictureEntry->Parent()->Name());
  osd->DrawRectangle(0, 0, w - 1, h - 1, clrTransparent);
  DrawTextOutlined(osd, 0, 0, Description, clrWhite, clrBlack, Font);
  DrawTextOutlined(osd, 0, h / 2, Year, clrWhite, clrBlack, Font);
  struct stat sb;
  if (stat(Path, &sb) == 0) {
     cString Time = DayDateTime(sb.st_mtime);
     DrawTextOutlined(osd, w - Font->Width(Time), h / 2, Time, clrWhite, clrBlack, Font);
     }
  p++;
  Path.Truncate(-4); // don't display the ".mpg" extension
  DrawTextOutlined(osd, w - Font->Width(p), 0, p, clrWhite, clrBlack, Font);
  osd->Flush();
}

cString cPictureControl::GetHeader(void)
{
  return tr("Pictures");
}

eOSState cPictureControl::ProcessKey(eKeys Key)
{
  switch (int(Key)) {
    case kUp:
    case kPlay:   slideShowDelay.Set();
                  slideShow = true;
                  break;
    case kDown:
    case kPause:  slideShow = false;
                  break;
    case kLeft|k_Repeat:
    case kLeft:   NextPicture(-1);
                  slideShow = false;
                  break;
    case kRight|k_Repeat:
    case kRight:  NextPicture(+1);
                  slideShow = false;
                  break;
    case kOk:     if (osd && !alwaysDisplayCaption)
                     DELETENULL(osd);
                  else {
                     alwaysDisplayCaption = !alwaysDisplayCaption;
                     DisplayCaption();
                     }
                  break;
    case kGreen:
    case kPrev:   NextDirectory(-1);
                  slideShow = false;
                  break;
    case kYellow:
    case kNext:   NextDirectory(+1);
                  slideShow = false;
                  break;
    case kBlue:
    case kStop:   return osEnd;
    case kBack:   slideShow = false;
                  cRemote::CallPlugin(PLUGIN_NAME_I18N);
                  break;
    default: break;
    }
  if (slideShow && slideShowDelay.TimedOut()) {
     NextPicture(+1);
     slideShowDelay.Set(SlideShowDelay * 1000);
     }
  return osContinue;
}

const char *cPictureControl::LastDisplayed(void)
{
  return lastDisplayed;
}