vdr  2.2.0
PLUGINS/src/pictures/player.c
Go to the documentation of this file.
1 /*
2  * player.c: A player for still pictures
3  *
4  * See the README file for copyright information and how to reach the author.
5  *
6  * $Id: player.c 3.1 2014/02/08 12:48:12 kls Exp $
7  */
8 
9 #include "player.h"
10 #include <vdr/remote.h>
11 #include <vdr/tools.h>
12 
13 int SlideShowDelay = 3; // seconds
14 
15 cString HandleUnderscores(const char *s)
16 {
17  // Skip anything before and including the first '_' and replace
18  // any remaining '_' with blanks:
19  const char *p = strchr(s, '_');
20  if (p) {
21  p++;
22  char buf[strlen(p) + 1];
23  strcpy(buf, p);
24  return strreplace(buf, '_', ' ');
25  }
26  return s;
27 }
28 
29 // --- cPicturePlayer --------------------------------------------------------
30 
31 class cPicturePlayer : public cPlayer {
32 private:
33  int size;
34  int length;
36  virtual void Activate(bool On);
37 public:
38  cPicturePlayer(void);
40  void SetPicture(const char *FileName);
41  };
42 
44 {
45  size = KILOBYTE(100); // will be adjusted automatically if files are larger
46  length = 0;
47  buffer = MALLOC(uchar, size);
48 }
49 
51 {
52  free(buffer);
53 }
54 
56 {
57  if (length > 0)
59 }
60 
61 void cPicturePlayer::SetPicture(const char *FileName)
62 {
63  int f = open(FileName, O_RDONLY);
64  if (f >= 0) {
65  for (;;) {
66  length = read(f, buffer, size);
67  if (length > 0) {
68  if (length >= size) {
69  int NewSize = size * 3 / 2;
70  if (uchar *NewBuffer = (uchar *)realloc(buffer, NewSize)) {
71  buffer = NewBuffer;
72  size = NewSize;
73  }
74  else {
75  LOG_ERROR_STR("out of memory");
76  break;
77  }
78  lseek(f, 0, SEEK_SET);
79  continue;
80  }
82  }
83  else
84  LOG_ERROR_STR(FileName);
85  break;
86  }
87  close(f);
88  }
89  else
90  LOG_ERROR_STR(FileName);
91 }
92 
93 // --- cPictureControl -------------------------------------------------------
94 
97 
98 cPictureControl::cPictureControl(cPictureEntry *Pictures, const cPictureEntry *PictureEntry, bool SlideShow)
99 :cControl(player = new cPicturePlayer)
100 {
101  pictures = Pictures;
102  pictureEntry = PictureEntry;
103  osd = NULL;
104  lastPath = "/";
106  slideShow = SlideShow;
107  alwaysDisplayCaption = false;
109  active++;
110 }
111 
113 {
114  active--;
115  delete osd;
116  delete player;
117  delete pictures;
118 }
119 
120 void cPictureControl::NextPicture(int Direction)
121 {
122  if (Direction) {
123  const cPictureEntry *pe = Direction > 0 ? pictureEntry->NextPicture() : pictureEntry->PrevPicture();
124  if (pe)
125  pictureEntry = pe;
126  }
127  if (pictureEntry) {
129  DisplayCaption();
130  }
131 }
132 
134 {
135  // This only works reliable if a directory contains only subdirectories or pictures, not both!
136  if (Direction) {
137  const cPictureEntry *pe = Direction > 0 ? pictureEntry->Parent()->Entries()->Last()->NextPicture() : pictureEntry->Parent()->Entries()->First()->PrevPicture();
138  if (pe && Direction < 0)
139  pe = pe->Parent()->Entries()->First();
140  if (pe && pe != pictureEntry) {
141  pictureEntry = pe;
143  DisplayCaption();
144  }
145  }
146 }
147 
148 static void DrawTextOutlined(cOsd *Osd, int x, int y, const char *s, tColor ColorFg, tColor ColorBg, const cFont *Font)
149 {
150  for (int dx = -1; dx <= 1; dx++) {
151  for (int dy = -1; dy <= 1; dy++) {
152  if (dx || dy)
153  Osd->DrawText(x + dx, y + dy, s, ColorBg, clrTransparent, Font);
154  }
155  }
156  Osd->DrawText(x, y, s, ColorFg, clrTransparent, Font);
157 }
158 
160 {
161  bool Force = false;
162  cString Path = pictureEntry->Path();
163  lastDisplayed = Path + strlen(pictures->Name()) + 1;
164  const char *p = strrchr(Path, '/');
165  const char *q = strrchr(lastPath, '/');
166  if (p && q) {
167  int lp = p - Path;
168  int lq = q - lastPath;
169  if (lp != lq || strncmp(lastPath, Path, lp)) {
170  lastPath = Path;
171  Force = true;
172  }
173  }
174  if (!alwaysDisplayCaption && !Force) {
175  DELETENULL(osd);
176  return;
177  }
178  const cFont *Font = cFont::GetFont(fontOsd);
179  int w = cOsd::OsdWidth();
180  int h = 2 * Font->Height();
181  if (!osd) {
183  tArea Areas[] = { { 0, 0, w - 1, h - 1, 8 } };
184  if (Setup.AntiAlias && osd->CanHandleAreas(Areas, sizeof(Areas) / sizeof(tArea)) == oeOk)
185  osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
186  else {
187  tArea Areas[] = { { 0, 0, w - 1, h - 1, 4 } };
188  osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
189  }
190  }
191  const char *Year = pictureEntry->Parent()->Parent() ? pictureEntry->Parent()->Parent()->Name() : "";
192  cString Description = HandleUnderscores(pictureEntry->Parent()->Name());
193  osd->DrawRectangle(0, 0, w - 1, h - 1, clrTransparent);
194  DrawTextOutlined(osd, 0, 0, Description, clrWhite, clrBlack, Font);
195  DrawTextOutlined(osd, 0, h / 2, Year, clrWhite, clrBlack, Font);
196  struct stat sb;
197  if (stat(Path, &sb) == 0) {
198  cString Time = DayDateTime(sb.st_mtime);
199  DrawTextOutlined(osd, w - Font->Width(Time), h / 2, Time, clrWhite, clrBlack, Font);
200  }
201  p++;
202  Path.Truncate(-4); // don't display the ".mpg" extension
203  DrawTextOutlined(osd, w - Font->Width(p), 0, p, clrWhite, clrBlack, Font);
204  osd->Flush();
205 }
206 
208 {
209  return tr("Pictures");
210 }
211 
213 {
214  switch (int(Key)) {
215  case kUp:
216  case kPlay: slideShowDelay.Set();
217  slideShow = true;
218  break;
219  case kDown:
220  case kPause: slideShow = false;
221  break;
222  case kLeft|k_Repeat:
223  case kLeft: NextPicture(-1);
224  slideShow = false;
225  break;
226  case kRight|k_Repeat:
227  case kRight: NextPicture(+1);
228  slideShow = false;
229  break;
230  case kOk: if (osd && !alwaysDisplayCaption)
231  DELETENULL(osd);
232  else {
234  DisplayCaption();
235  }
236  break;
237  case kGreen:
238  case kPrev: NextDirectory(-1);
239  slideShow = false;
240  break;
241  case kYellow:
242  case kNext: NextDirectory(+1);
243  slideShow = false;
244  break;
245  case kBlue:
246  case kStop: return osEnd;
247  case kBack: slideShow = false;
248  cRemote::CallPlugin(PLUGIN_NAME_I18N);
249  break;
250  default: break;
251  }
252  if (slideShow && slideShowDelay.TimedOut()) {
253  NextPicture(+1);
255  }
256  return osContinue;
257 }
258 
260 {
261  return lastDisplayed;
262 }
int AntiAlias
Definition: config.h:323
unsigned char uchar
Definition: tools.h:30
static int OsdHeight(void)
Definition: osd.h:789
Definition: keys.h:37
void Set(int Ms=0)
Definition: tools.c:738
virtual eOSState ProcessKey(eKeys Key)
Definition: keys.h:23
virtual eOsdError SetAreas(const tArea *Areas, int NumAreas)
Sets the sub-areas to the given areas.
Definition: osd.c:1823
const char * Name(void) const
Definition: entry.h:25
const cPictureEntry * NextPicture(const cPictureEntry *This=NULL) const
Definition: entry.c:125
cString Path(void) const
Definition: entry.c:38
Definition: keys.h:17
const cPictureEntry * PrevPicture(const cPictureEntry *This=NULL) const
Definition: entry.c:106
Definition: keys.h:61
cString & Truncate(int Index)
Truncate the string at the given Index (if Index is < 0 it is counted from the end of the string)...
Definition: tools.c:1064
void NextPicture(int Direction)
#define LOG_ERROR_STR(s)
Definition: tools.h:39
Definition: keys.h:33
virtual void DrawRectangle(int x1, int y1, int x2, int y2, tColor Color)
Draws a filled rectangle defined by the upper left (x1, y1) and lower right (x2, y2) corners with the...
Definition: osd.c:1952
Definition: keys.h:27
Definition: keys.h:25
Definition: keys.h:38
virtual void Flush(void)
Actually commits all data to the OSD hardware.
Definition: osd.c:1982
#define MALLOC(type, size)
Definition: tools.h:46
Definition: osd.h:34
bool IsDirectory(void) const
Definition: entry.h:27
T * Last(void) const
Definition: tools.h:493
eOSState
Definition: osdbase.h:18
const cList< cPictureEntry > * Entries(void) const
Definition: entry.c:66
Definition: player.h:16
static const char * LastDisplayed(void)
Definition: font.h:22
static int OsdWidth(void)
Definition: osd.h:788
Definition: osdbase.h:35
virtual eOsdError CanHandleAreas(const tArea *Areas, int NumAreas)
Checks whether the OSD can display the given set of sub-areas.
Definition: osd.c:1801
virtual cString GetHeader(void)
This can be used by players that don&#39;t play a cRecording, but rather do something completely differen...
void DeviceStillPicture(const uchar *Data, int Length)
Definition: player.h:36
bool TimedOut(void) const
Definition: tools.c:743
Definition: keys.h:18
static const cCursesFont Font
Definition: skincurses.c:30
The cOsd class is the interface to the "On Screen Display".
Definition: osd.h:720
const cPictureEntry * Parent(void) const
Definition: entry.h:26
cSetup Setup
Definition: config.c:373
Definition: keys.h:20
cString DayDateTime(time_t t)
Converts the given time to a string of the form "www dd.mm. hh:mm".
Definition: tools.c:1145
virtual void DrawText(int x, int y, const char *s, tColor ColorFg, tColor ColorBg, const cFont *Font, int Width=0, int Height=0, int Alignment=taDefault)
Draws the given string at coordinates (x, y) with the given foreground and background color and font...
Definition: osd.c:1942
cString HandleUnderscores(const char *s)
Definition: keys.h:26
static int OsdTop(void)
Definition: osd.h:787
const cPictureEntry * pictureEntry
static int OsdLeft(void)
Definition: osd.h:786
Definition: osd.h:41
Definition: keys.h:21
#define OSD_LEVEL_SUBTITLES
Definition: osd.h:22
T * First(void) const
Definition: tools.h:492
Definition: osd.h:298
virtual int Width(uint c) const =0
Returns the width of the given character in pixel.
#define KILOBYTE(n)
Definition: tools.h:43
#define tr(s)
Definition: i18n.h:85
static void DrawTextOutlined(cOsd *Osd, int x, int y, const char *s, tColor ColorFg, tColor ColorBg, const cFont *Font)
void DELETENULL(T *&p)
Definition: tools.h:48
Definition: keys.h:32
Definition: osd.h:44
cPictureControl(cPictureEntry *Pictures, const cPictureEntry *PictureEntry, bool SlideShow=false)
Definition: keys.h:31
virtual int Height(void) const =0
Returns the height of this font in pixel (all characters have the same height).
virtual void Activate(bool On)
void NextDirectory(int Direction)
char * strreplace(char *s, char c1, char c2)
Definition: tools.c:139
eKeys
Definition: keys.h:16
Definition: font.h:37
Definition: tools.h:168
static const cFont * GetFont(eDvbFont Font)
Gets the given Font, which was previously set by a call to SetFont().
Definition: font.c:406
static bool CallPlugin(const char *Plugin)
Initiates calling the given plugin&#39;s main menu function.
Definition: remote.c:151
uint32_t tColor
Definition: font.h:29
Definition: keys.h:22
void SetPicture(const char *FileName)
static cOsd * NewOsd(int Left, int Top, uint Level=OSD_LEVEL_DEFAULT)
Returns a pointer to a newly created cOsd object, which will be located at the given coordinates...
Definition: osd.c:2006