summaryrefslogtreecommitdiff
path: root/interface.c
blob: 59652244a8e6838f747ac9f2f6f2514b3d7a5309 (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 * interface.c: Abstract user interface layer
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: interface.c 1.47 2002/01/27 16:00:31 kls Exp $
 */

#include "interface.h"
#include <ctype.h>
#include <unistd.h>
#include "i18n.h"

cInterface *Interface = NULL;

cInterface::cInterface(int SVDRPport)
{
  open = 0;
  cols[0] = 0;
  width = height = 0;
  keyFromWait = kNone;
  interrupted = false;
  rcIo = NULL;
  SVDRP = NULL;
#if defined(REMOTE_RCU)
  rcIo = new cRcIoRCU("/dev/ttyS1");
#elif defined(REMOTE_LIRC)
  rcIo = new cRcIoLIRC("/dev/lircd");
#elif defined(REMOTE_KBD)
  rcIo = new cRcIoKBD;
#else
  rcIo = new cRcIoBase; // acts as a dummy device
#endif
  rcIo->SetCode(Keys.code, Keys.address);
  if (SVDRPport)
     SVDRP = new cSVDRP(SVDRPport);
}

cInterface::~cInterface()
{
  delete rcIo;
  delete SVDRP;
}

void cInterface::Open(int NumCols, int NumLines)
{
  if (!open++) {
     if (NumCols == 0)
        NumCols = Setup.OSDwidth;
     if (NumLines == 0)
        NumLines = Setup.OSDheight;
     cDvbApi::PrimaryDvbApi->Open(width = NumCols, height = NumLines);
     }
}

void cInterface::Close(void)
{
  if (open == 1)
     Clear();
  if (!--open) {
     cDvbApi::PrimaryDvbApi->Close();
     width = height = 0;
     }
}

unsigned int cInterface::GetCh(bool Wait, bool *Repeat, bool *Release)
{
  Flush();
  if (!rcIo->InputAvailable())
     cFile::AnyFileReady(-1, Wait ? 1000 : 0);
  unsigned int Command;
  return rcIo->GetCommand(&Command, Repeat, Release) ? Command : 0;
}

eKeys cInterface::GetKey(bool Wait)
{
  Flush();
  if (SVDRP) {
     SVDRP->Process();
     if (!open) {
        char *message = SVDRP->GetMessage();
        if (message) {
           Info(message);
           delete message;
           }
        }
     }
  eKeys Key = keyFromWait;
  if (Key == kNone) {
     bool Repeat = false, Release = false;
     Key = Keys.Get(GetCh(Wait, &Repeat, &Release));
     if (Repeat)
        Key = eKeys(Key | k_Repeat);
     if (Release)
        Key = eKeys(Key | k_Release);
     }
  keyFromWait = kNone;
  return Key;
}

void cInterface::PutKey(eKeys Key)
{
  keyFromWait = Key;
}

eKeys cInterface::Wait(int Seconds, bool KeepChar)
{
  if (Seconds == 0)
     Seconds = Setup.OSDMessageTime;
  Flush();
  eKeys Key = kNone;
  time_t timeout = time(NULL) + Seconds;
  for (;;) {
      Key = GetKey();
      if ((Key != kNone && (RAWKEY(Key) != kOk || RAWKEY(Key) == Key)) || time(NULL) > timeout || interrupted)
         break;
      }
  if (KeepChar && ISRAWKEY(Key))
     keyFromWait = Key;
  interrupted = false;
  return Key;
}

void cInterface::Clear(void)
{
  if (open)
     cDvbApi::PrimaryDvbApi->Clear();
}

void cInterface::ClearEol(int x, int y, eDvbColor Color)
{
  if (open)
     cDvbApi::PrimaryDvbApi->ClrEol(x, y, Color);
}

void cInterface::Fill(int x, int y, int w, int h, eDvbColor Color)
{
  if (open)
     cDvbApi::PrimaryDvbApi->Fill(x, y, w, h, Color);
}

void cInterface::SetBitmap(int x, int y, const cBitmap &Bitmap)
{
  if (open)
     cDvbApi::PrimaryDvbApi->SetBitmap(x, y, Bitmap);
}

void cInterface::Flush(void)
{
  if (open)
     cDvbApi::PrimaryDvbApi->Flush();
}

void cInterface::SetCols(int *c)
{
  for (int i = 0; i < MaxCols; i++) {
      cols[i] = *c++;
      if (cols[i] == 0)
         break;
      }
}

eDvbFont cInterface::SetFont(eDvbFont Font)
{
  return cDvbApi::PrimaryDvbApi->SetFont(Font);
}

char *cInterface::WrapText(const char *Text, int Width, int *Height)
{
  // Wraps the Text to make it fit into the area defined by the given Width
  // (which is given in character cells).
  // The actual number of lines resulting from this operation is returned in
  // Height.
  // The returned string is newly created on the heap and the caller
  // is responsible for deleting it once it is no longer used.
  // Wrapping is done by inserting the necessary number of newline
  // characters into the string.

  int Lines = 1;
  char *t = strdup(Text);
  char *Blank = NULL;
  char *Delim = NULL;
  int w = 0;

  Width *= cDvbApi::PrimaryDvbApi->CellWidth();

  while (*t && t[strlen(t) - 1] == '\n')
        t[strlen(t) - 1] = 0; // skips trailing newlines

  for (char *p = t; *p; ) {
      if (*p == '\n') {
         Lines++;
         w = 0;
         Blank = Delim = NULL;
         p++;
         continue;
         }
      else if (isspace(*p))
         Blank = p;
      int cw = cDvbApi::PrimaryDvbApi->Width(*p);
      if (w + cw > Width) {
         if (Blank) {
            *Blank = '\n';
            p = Blank;
            continue;
            }
         else {
            // Here's the ugly part, where we don't have any whitespace to
            // punch in a newline, so we need to make room for it:
            if (Delim)
               p = Delim + 1; // let's fall back to the most recent delimiter
            char *s = new char[strlen(t) + 2]; // The additional '\n' plus the terminating '\0'
            int l = p - t;
            strncpy(s, t, l);
            s[l] = '\n';
            strcpy(s + l + 1, p);
            delete t;
            t = s;
            p = t + l;
            continue;
            }
         }
      else
         w += cw;
      if (strchr("-.,:;!?_", *p)) {
         Delim = p;
         Blank = NULL;
         }
      p++;
      }

  *Height = Lines;
  return t;
}

void cInterface::Write(int x, int y, const char *s, eDvbColor FgColor, eDvbColor BgColor)
{
  if (open)
     cDvbApi::PrimaryDvbApi->Text(x, y, s, FgColor, BgColor);
}

void cInterface::WriteText(int x, int y, const char *s, eDvbColor FgColor, eDvbColor BgColor)
{
  if (open) {
     ClearEol(x, y, BgColor);
     int col = 0;
     for (;;) {
         const char *t = strchr(s, '\t');
         const char *p = s;
         char buf[1000];
         if (t && col < MaxCols && cols[col] > 0) {
            unsigned int n = t - s;
            if (n >= sizeof(buf))
               n = sizeof(buf) - 1;
            strncpy(buf, s, n);
            buf[n] = 0;
            p = buf;
            s = t + 1;
            }
         Write(x, y, p, FgColor, BgColor);
         if (p == s)
            break;
         x += cols[col++];
         }
     }
}

void cInterface::Title(const char *s)
{
  ClearEol(0, 0, clrCyan);
  const char *t = strchr(s, '\t');
  if (t) {
     char buffer[Width() + 1];
     unsigned int n = t - s;
     if (n >= sizeof(buffer))
        n = sizeof(buffer) - 1;
     strn0cpy(buffer, s, n + 1);
     Write(1, 0, buffer, clrBlack, clrCyan);
     t++;
     Write(-(cDvbApi::PrimaryDvbApi->WidthInCells(t) + 1), 0, t, clrBlack, clrCyan);
     }
  else {
     int x = (Width() - strlen(s)) / 2;
     if (x < 0)
        x = 0;
     Write(x, 0, s, clrBlack, clrCyan);
     }
}

void cInterface::Status(const char *s, eDvbColor FgColor, eDvbColor BgColor)
{
  int Line = (abs(height) == 1) ? 0 : -2;
  ClearEol(0, Line, s ? BgColor : clrBackground);
  if (s) {
     int x = (Width() - strlen(s)) / 2;
     if (x < 0)
        x = 0;
     Write(x, Line, s, FgColor, BgColor);
     }
}

void cInterface::Info(const char *s)
{
  Open(Setup.OSDwidth, -1);
  isyslog(LOG_INFO, "info: %s", s);
  Status(s, clrBlack, clrGreen);
  Wait();
  Status(NULL);
  Close();
}

void cInterface::Error(const char *s)
{
  Open(Setup.OSDwidth, -1);
  esyslog(LOG_ERR, "ERROR: %s", s);
  Status(s, clrWhite, clrRed);
  Wait();
  Status(NULL);
  Close();
}

bool cInterface::Confirm(const char *s, int Seconds, bool WaitForTimeout)
{
  Open(Setup.OSDwidth, -1);
  isyslog(LOG_INFO, "confirm: %s", s);
  Status(s, clrBlack, clrYellow);
  eKeys k = Wait(Seconds);
  bool result = WaitForTimeout ? k == kNone : k == kOk;
  Status(NULL);
  Close();
  isyslog(LOG_INFO, "%sconfirmed", result ? "" : "not ");
  return result;
}

void cInterface::HelpButton(int Index, const char *Text, eDvbColor FgColor, eDvbColor BgColor)
{
  if (open) {
     const int w = Width() / 4;
     cDvbApi::PrimaryDvbApi->Fill(Index * w, -1, w, 1, Text ? BgColor : clrBackground);
     if (Text) {
        int l = (w - int(strlen(Text))) / 2;
        if (l < 0)
           l = 0;
        cDvbApi::PrimaryDvbApi->Text(Index * w + l, -1, Text, FgColor, BgColor);
        }
     }
}

void cInterface::Help(const char *Red, const char *Green, const char *Yellow, const char *Blue)
{
  HelpButton(0, Red,    clrBlack, clrRed);
  HelpButton(1, Green,  clrBlack, clrGreen);
  HelpButton(2, Yellow, clrBlack, clrYellow);
  HelpButton(3, Blue,   clrWhite, clrBlue);
}

void cInterface::QueryKeys(void)
{
  Keys.Clear();
  Clear();
  WriteText(1, 1, tr("Learning Remote Control Keys"));
  WriteText(1, 3, tr("Phase 1: Detecting RC code type"));
  WriteText(1, 5, tr("Press any key on the RC unit"));
  Flush();
#ifndef REMOTE_KBD
  unsigned char Code = '0';
  unsigned short Address;
#endif
  for (;;) {
#ifdef REMOTE_KBD
      if (GetCh())
         break;
#else
      //TODO on screen display...
      if (rcIo->DetectCode(&Code, &Address)) {
         Keys.code = Code;
         Keys.address = Address;
         WriteText(1, 5, tr("RC code detected!"));
         WriteText(1, 6, tr("Do not press any key..."));
         Flush();
         rcIo->Flush(3000);
         ClearEol(0, 5);
         ClearEol(0, 6);
         Flush();
         break;
         }
#endif
      }
  WriteText(1, 3, tr("Phase 2: Learning specific key codes"));
  tKey *k = Keys.keys;
  while (k->type != kNone) {
        char *Prompt;
        asprintf(&Prompt, tr("Press key for '%s'"), tr(k->name));
        WriteText(1, 5, Prompt);
        delete Prompt;
        for (;;) {
            unsigned int ch = GetCh();
            if (ch != 0) {
               switch (Keys.Get(ch)) {
                 case kUp:   if (k > Keys.keys) {
                                k--;
                                break;
                                }
                 case kDown: if (k > Keys.keys + 1) {
                                WriteText(1, 5, tr("Press 'Up' to confirm"));
                                WriteText(1, 6, tr("Press 'Down' to continue"));
                                ClearEol(0, 7);
                                ClearEol(0, 8);
                                for (;;) {
                                    eKeys key = GetKey();
                                    if (key == kUp) {
                                       Clear();
                                       return;
                                       }
                                    else if (key == kDown) {
                                       ClearEol(0, 6);
                                       break;
                                       }
                                    }
                                break;
                                }
                 case kNone: k->code = ch;
                             k++;
                             break;
                 default:    break;
                 }
               break;
               }
            }
        if (k > Keys.keys)
           WriteText(1, 7, tr("(press 'Up' to go back)"));
        else
           ClearEol(0, 7);
        if (k > Keys.keys + 1)
           WriteText(1, 8, tr("(press 'Down' to end key definition)"));
        else
           ClearEol(0, 8);
        }
}

void cInterface::LearnKeys(void)
{
  isyslog(LOG_INFO, "learning keys");
  Open();
  for (;;) {
      Clear();
      QueryKeys();
      Clear();
      WriteText(1, 1, tr("Learning Remote Control Keys"));
      WriteText(1, 3, tr("Phase 3: Saving key codes"));
      WriteText(1, 5, tr("Press 'Up' to save, 'Down' to cancel"));
      for (;;) {
          eKeys key = GetKey();
          if (key == kUp) {
             Keys.Save();
             Close();
             return;
             }
          else if (key == kDown) {
             Keys.Load();
             Close();
             return;
             }
          }
      }
}

void cInterface::DisplayChannelNumber(int Number)
{
  rcIo->Number(Number);
}

void cInterface::DisplayRecording(int Index, bool On)
{
  rcIo->SetPoints(1 << Index, On);
}

bool cInterface::Recording(void)
{
  // This is located here because the Interface has to do with the "PrimaryDvbApi" anyway
  return cDvbApi::PrimaryDvbApi->Recording();
}