summaryrefslogtreecommitdiff
path: root/skins.c
blob: 5fb85a724fe425d00b639630a23efb8be39ec97f (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * skins.c: The optical appearance of the OSD
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: skins.c 2.10 2012/06/02 11:44:14 kls Exp $
 */

#include "skins.h"
#include "interface.h"
#include "status.h"

// --- cSkinQueuedMessage ----------------------------------------------------

class cSkinQueuedMessage : public cListObject {
  friend class cSkins;
private:
  eMessageType type;
  char *message;
  int seconds;
  int timeout;
  tThreadId threadId;
  eKeys key;
  int state;
  cMutex mutex;
  cCondVar condVar;
public:
  cSkinQueuedMessage(eMessageType Type, const char *s, int Seconds, int Timeout);
  virtual ~cSkinQueuedMessage();
  };

cSkinQueuedMessage::cSkinQueuedMessage(eMessageType Type, const char *s, int Seconds, int Timeout)
{
  type = Type;
  message = s ? strdup(s) : NULL;
  seconds = Seconds;
  timeout = Timeout;
  threadId = cThread::ThreadId();
  key = kNone;
  state = 0; // waiting
}

cSkinQueuedMessage::~cSkinQueuedMessage()
{
  free(message);
}

cList<cSkinQueuedMessage> SkinQueuedMessages;

// --- cSkinDisplay ----------------------------------------------------------

cSkinDisplay *cSkinDisplay::current = NULL;

cSkinDisplay::cSkinDisplay(void)
{
  current = this;
  editableWidth = 100; //XXX
}

cSkinDisplay::~cSkinDisplay()
{
  current = NULL;
}

// --- cSkinDisplayMenu ------------------------------------------------------

cSkinDisplayMenu::cSkinDisplayMenu(void)
{
  menuCategory = mcUndefined;
  SetTabs(0);
}

void cSkinDisplayMenu::SetMenuCategory(eMenuCategory MenuCategory)
{
  menuCategory = MenuCategory;
}

void cSkinDisplayMenu::SetTabs(int Tab1, int Tab2, int Tab3, int Tab4, int Tab5)
{
  tabs[0] = 0;
  tabs[1] = Tab1 ? tabs[0] + Tab1 : 0;
  tabs[2] = Tab2 ? tabs[1] + Tab2 : 0;
  tabs[3] = Tab3 ? tabs[2] + Tab3 : 0;
  tabs[4] = Tab4 ? tabs[3] + Tab4 : 0;
  tabs[5] = Tab5 ? tabs[4] + Tab5 : 0;
  for (int i = 1; i < MaxTabs; i++)
      tabs[i] *= AvgCharWidth();
}

void cSkinDisplayMenu::Scroll(bool Up, bool Page)
{
  textScroller.Scroll(Up, Page);
}

const char *cSkinDisplayMenu::GetTabbedText(const char *s, int Tab)
{
  if (!s)
     return NULL;
  static char buffer[1000];
  const char *a = s;
  const char *b = strchrnul(a, '\t');
  while (*b && Tab-- > 0) {
        a = b + 1;
        b = strchrnul(a, '\t');
        }
  if (!*b)
     return (Tab <= 0) ? a : NULL;
  unsigned int n = b - a;
  if (n >= sizeof(buffer))
     n = sizeof(buffer) - 1;
  strncpy(buffer, a, n);
  buffer[n] = 0;
  return buffer;
}

void cSkinDisplayMenu::SetScrollbar(int Total, int Offset)
{
}

int cSkinDisplayMenu::GetTextAreaWidth(void) const
{
  return 0;
}

const cFont *cSkinDisplayMenu::GetTextAreaFont(bool) const
{
  return NULL;
}

// --- cSkinDisplayReplay::cProgressBar --------------------------------------

cSkinDisplayReplay::cProgressBar::cProgressBar(int Width, int Height, int Current, int Total, const cMarks *Marks, tColor ColorSeen, tColor ColorRest, tColor ColorSelected, tColor ColorMark, tColor ColorCurrent)
:cBitmap(Width, Height, 2)
{
  total = Total;
  if (total > 0) {
     int p = Pos(Current);
     DrawRectangle(0, 0, p, Height - 1, ColorSeen);
     DrawRectangle(p + 1, 0, Width - 1, Height - 1, ColorRest);
     if (Marks) {
        bool Start = true;
        for (const cMark *m = Marks->First(); m; m = Marks->Next(m)) {
            int p1 = Pos(m->Position());
            if (Start) {
               const cMark *m2 = Marks->Next(m);
               int p2 = Pos(m2 ? m2->Position() : total);
               int h = Height / 3;
               DrawRectangle(p1, h, p2, Height - h, ColorSelected);
               }
            Mark(p1, Start, m->Position() == Current, ColorMark, ColorCurrent);
            Start = !Start;
            }
        }
     }
}

void cSkinDisplayReplay::cProgressBar::Mark(int x, bool Start, bool Current, tColor ColorMark, tColor ColorCurrent)
{
  DrawRectangle(x, 0, x, Height() - 1, ColorMark);
  const int d = Height() / (Current ? 3 : 9);
  for (int i = 0; i < d; i++) {
      int h = Start ? i : Height() - 1 - i;
      DrawRectangle(x - d + i, h, x + d - i, h, Current ? ColorCurrent : ColorMark);
      }
}

// --- cSkinDisplayReplay ----------------------------------------------------

cSkinDisplayReplay::cSkinDisplayReplay(void)
{
  marks = NULL;
}

void cSkinDisplayReplay::SetRecording(const cRecording *Recording)
{
  SetTitle(Recording->Title());
}

void cSkinDisplayReplay::SetMarks(const cMarks *Marks)
{
  marks = Marks;
}

// --- cSkin -----------------------------------------------------------------

cSkin::cSkin(const char *Name, cTheme *Theme)
{
  name = strdup(Name);
  theme = Theme;
  if (theme)
     cThemes::Save(name, theme);
  Skins.Add(this);
}

cSkin::~cSkin()
{
  free(name);
}

// --- cSkins ----------------------------------------------------------------

cSkins Skins;

cSkins::cSkins(void)
{
  displayMessage = NULL;
}

cSkins::~cSkins()
{
  delete displayMessage;
}

bool cSkins::SetCurrent(const char *Name)
{
  if (Name) {
     for (cSkin *Skin = First(); Skin; Skin = Next(Skin)) {
         if (strcmp(Skin->Name(), Name) == 0) {
            isyslog("setting current skin to \"%s\"", Name);
            current = Skin;
            return true;
            }
         }
     }
  current = First();
  if (current)
     isyslog("skin \"%s\" not available - using \"%s\" instead", Name, current->Name());
  else
     esyslog("ERROR: no skin available");
  return current != NULL;
}

eKeys cSkins::Message(eMessageType Type, const char *s, int Seconds)
{
  if (!cThread::IsMainThread()) {
     dsyslog("cSkins::Message() called from background thread - ignored! (Use cSkins::QueueMessage() instead)");
     return kNone;
     }
  switch (Type) {
    case mtInfo:    isyslog("info: %s", s); break;
    case mtWarning: isyslog("warning: %s", s); break;
    case mtError:   esyslog("ERROR: %s", s); break;
    default: ;
    }
  if (!Current())
     return kNone;
  if (!cSkinDisplay::Current()) {
     if (displayMessage)
        delete displayMessage;
     displayMessage = Current()->DisplayMessage();
     }
  cSkinDisplay::Current()->SetMessage(Type, s);
  cSkinDisplay::Current()->Flush();
  cStatus::MsgOsdStatusMessage(s);
  eKeys k = kNone;
  if (Type != mtStatus) {
     k = Interface->Wait(Seconds);
     if (displayMessage) {
        delete displayMessage;
        displayMessage = NULL;
        cStatus::MsgOsdClear();
        }
     else {
        cSkinDisplay::Current()->SetMessage(Type, NULL);
        cStatus::MsgOsdStatusMessage(NULL);
        }
     }
  else if (!s && displayMessage) {
     delete displayMessage;
     displayMessage = NULL;
     cStatus::MsgOsdClear();
     }
  return k;
}

int cSkins::QueueMessage(eMessageType Type, const char *s, int Seconds, int Timeout)
{
  if (Type == mtStatus) {
     dsyslog("cSkins::QueueMessage() called with mtStatus - ignored!");
     return kNone;
     }
  if (isempty(s)) {
     if (!cThread::IsMainThread()) {
        queueMessageMutex.Lock();
        for (cSkinQueuedMessage *m = SkinQueuedMessages.Last(); m; m = SkinQueuedMessages.Prev(m)) {
            if (m->threadId == cThread::ThreadId() && m->state == 0)
               m->state = 2; // done
            }
        queueMessageMutex.Unlock();
        }
     else
        dsyslog("cSkins::QueueMessage() called with empty message from main thread - ignored!");
     return kNone;
     }
  int k = kNone;
  if (Timeout > 0) {
     if (cThread::IsMainThread()) {
        dsyslog("cSkins::QueueMessage() called from main thread with Timeout = %d - ignored!", Timeout);
        return k;
        }
     cSkinQueuedMessage *m = new cSkinQueuedMessage(Type, s, Seconds, Timeout);
     queueMessageMutex.Lock();
     SkinQueuedMessages.Add(m);
     m->mutex.Lock();
     queueMessageMutex.Unlock();
     if (m->condVar.TimedWait(m->mutex, Timeout * 1000))
        k = m->key;
     else
        k = -1; // timeout, nothing has been displayed
     m->state = 2; // done
     m->mutex.Unlock();
     }
  else {
     queueMessageMutex.Lock();
     // Check if there is a waiting message w/o timeout for this thread:
     if (Timeout == -1) {
        for (cSkinQueuedMessage *m = SkinQueuedMessages.Last(); m; m = SkinQueuedMessages.Prev(m)) {
            if (m->threadId == cThread::ThreadId()) {
               if (m->state == 0 && m->timeout == -1)
                  m->state = 2; // done
               break;
               }
            }
         }
     // Add the new message:
     SkinQueuedMessages.Add(new cSkinQueuedMessage(Type, s, Seconds, Timeout));
     queueMessageMutex.Unlock();
     }
  return k;
}

void cSkins::ProcessQueuedMessages(void)
{
  if (!cThread::IsMainThread()) {
     dsyslog("cSkins::ProcessQueuedMessages() called from background thread - ignored!");
     return;
     }
  cSkinQueuedMessage *msg = NULL;
  // Get the first waiting message:
  queueMessageMutex.Lock();
  for (cSkinQueuedMessage *m = SkinQueuedMessages.First(); m; m = SkinQueuedMessages.Next(m)) {
      if (m->state == 0) { // waiting
         m->state = 1; // active
         msg = m;
         break;
         }
      }
  queueMessageMutex.Unlock();
  // Display the message:
  if (msg) {
     msg->mutex.Lock();
     if (msg->state == 1) { // might have changed since we got it
        msg->key = Skins.Message(msg->type, msg->message, msg->seconds);
        if (msg->timeout == 0)
           msg->state = 2; // done
        else
           msg->condVar.Broadcast();
        }
     msg->mutex.Unlock();
     }
  // Remove done messages from the queue:
  queueMessageMutex.Lock();
  for (;;) {
      cSkinQueuedMessage *m = SkinQueuedMessages.First();
      if (m && m->state == 2) { // done
         SkinQueuedMessages.Del(m);
         }
      else
         break;
      }
  queueMessageMutex.Unlock();
}

void cSkins::Flush(void)
{
  if (cSkinDisplay::Current())
     cSkinDisplay::Current()->Flush();
}

void cSkins::Clear(void)
{
  if (displayMessage) {
     delete displayMessage;
     displayMessage = NULL;
     }
  cList<cSkin>::Clear();
}