1
0
mirror of https://github.com/VDR4Arch/vdr.git synced 2023-10-10 13:36:52 +02:00
vdr/interface.c

480 lines
12 KiB
C
Raw Normal View History

2000-02-19 13:36:48 +01:00
/*
* interface.c: Abstract user interface layer
*
2000-04-24 09:46:05 +02:00
* See the main source file 'vdr.c' for copyright information and
2000-02-19 13:36:48 +01:00
* how to reach the author.
*
* $Id: interface.c 1.46 2002/01/26 11:09:58 kls Exp $
2000-02-19 13:36:48 +01:00
*/
#include "interface.h"
2000-11-01 11:45:05 +01:00
#include <ctype.h>
2000-02-19 13:36:48 +01:00
#include <unistd.h>
2000-11-11 10:39:27 +01:00
#include "i18n.h"
cInterface *Interface = NULL;
2000-02-19 13:36:48 +01:00
cInterface::cInterface(int SVDRPport)
2000-02-19 13:36:48 +01:00
{
open = 0;
cols[0] = 0;
2000-11-01 11:45:05 +01:00
width = height = 0;
2000-04-22 13:51:48 +02:00
keyFromWait = kNone;
2001-09-01 09:04:37 +02:00
interrupted = false;
rcIo = NULL;
2000-09-19 17:48:42 +02:00
SVDRP = NULL;
#if defined(REMOTE_RCU)
rcIo = new cRcIoRCU("/dev/ttyS1");
#elif defined(REMOTE_LIRC)
rcIo = new cRcIoLIRC("/dev/lircd");
2001-02-02 14:56:45 +01:00
#elif defined(REMOTE_KBD)
rcIo = new cRcIoKBD;
2001-02-02 14:56:45 +01:00
#else
rcIo = new cRcIoBase; // acts as a dummy device
#endif
rcIo->SetCode(Keys.code, Keys.address);
2000-09-19 17:48:42 +02:00
if (SVDRPport)
SVDRP = new cSVDRP(SVDRPport);
}
cInterface::~cInterface()
2000-09-19 17:48:42 +02:00
{
delete rcIo;
2000-09-19 17:48:42 +02:00
delete SVDRP;
2000-02-19 13:36:48 +01:00
}
2000-04-23 15:38:16 +02:00
void cInterface::Open(int NumCols, int NumLines)
2000-02-19 13:36:48 +01:00
{
if (!open++) {
if (NumCols == 0)
NumCols = Setup.OSDwidth;
if (NumLines == 0)
NumLines = Setup.OSDheight;
2000-11-01 11:45:05 +01:00
cDvbApi::PrimaryDvbApi->Open(width = NumCols, height = NumLines);
}
2000-02-19 13:36:48 +01:00
}
void cInterface::Close(void)
{
if (open == 1)
Clear();
2000-11-01 11:45:05 +01:00
if (!--open) {
2000-05-01 16:29:46 +02:00
cDvbApi::PrimaryDvbApi->Close();
2000-11-01 11:45:05 +01:00
width = height = 0;
}
2000-02-19 13:36:48 +01:00
}
unsigned int cInterface::GetCh(bool Wait, bool *Repeat, bool *Release)
2000-02-19 13:36:48 +01:00
{
Flush();
if (!rcIo->InputAvailable())
2000-10-08 09:25:20 +02:00
cFile::AnyFileReady(-1, Wait ? 1000 : 0);
unsigned int Command;
return rcIo->GetCommand(&Command, Repeat, Release) ? Command : 0;
2000-02-19 13:36:48 +01:00
}
2000-04-23 15:38:16 +02:00
eKeys cInterface::GetKey(bool Wait)
2000-02-19 13:36:48 +01:00
{
Flush();
if (SVDRP) {
2000-09-19 17:48:42 +02:00
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);
}
2000-04-22 13:51:48 +02:00
keyFromWait = kNone;
return Key;
2000-02-19 13:36:48 +01:00
}
2000-09-17 09:36:50 +02:00
void cInterface::PutKey(eKeys Key)
{
keyFromWait = Key;
}
2000-04-22 13:51:48 +02:00
eKeys cInterface::Wait(int Seconds, bool KeepChar)
{
2001-09-01 15:23:27 +02:00
if (Seconds == 0)
Seconds = Setup.OSDMessageTime;
Flush();
2000-10-08 09:25:20 +02:00
eKeys Key = kNone;
time_t timeout = time(NULL) + Seconds;
for (;;) {
Key = GetKey();
2001-09-01 09:04:37 +02:00
if ((Key != kNone && (RAWKEY(Key) != kOk || RAWKEY(Key) == Key)) || time(NULL) > timeout || interrupted)
2000-10-08 09:25:20 +02:00
break;
}
2000-10-08 16:34:17 +02:00
if (KeepChar && ISRAWKEY(Key))
2000-04-22 13:51:48 +02:00
keyFromWait = Key;
2001-09-01 09:04:37 +02:00
interrupted = false;
2000-04-22 13:51:48 +02:00
return Key;
}
2000-02-19 13:36:48 +01:00
void cInterface::Clear(void)
{
if (open)
2000-05-01 16:29:46 +02:00
cDvbApi::PrimaryDvbApi->Clear();
}
void cInterface::ClearEol(int x, int y, eDvbColor Color)
{
if (open)
2000-05-01 16:29:46 +02:00
cDvbApi::PrimaryDvbApi->ClrEol(x, y, Color);
2000-02-19 13:36:48 +01:00
}
void cInterface::Fill(int x, int y, int w, int h, eDvbColor Color)
{
if (open)
cDvbApi::PrimaryDvbApi->Fill(x, y, w, h, Color);
}
2000-12-09 11:13:00 +01:00
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();
}
2000-02-19 13:36:48 +01:00
void cInterface::SetCols(int *c)
{
for (int i = 0; i < MaxCols; i++) {
cols[i] = *c++;
if (cols[i] == 0)
break;
}
}
2000-11-18 15:46:00 +01:00
eDvbFont cInterface::SetFont(eDvbFont Font)
{
return cDvbApi::PrimaryDvbApi->SetFont(Font);
}
2000-11-01 11:45:05 +01:00
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)
2000-02-19 13:36:48 +01:00
{
if (open)
2000-05-01 16:29:46 +02:00
cDvbApi::PrimaryDvbApi->Text(x, y, s, FgColor, BgColor);
2000-02-19 13:36:48 +01:00
}
2000-09-09 14:57:43 +02:00
void cInterface::WriteText(int x, int y, const char *s, eDvbColor FgColor, eDvbColor BgColor)
2000-02-19 13:36:48 +01:00
{
if (open) {
ClearEol(x, y, BgColor);
2000-02-19 13:36:48 +01:00
int col = 0;
for (;;) {
const char *t = strchr(s, '\t');
const char *p = s;
2000-02-19 13:36:48 +01:00
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);
2000-02-19 13:36:48 +01:00
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)
Write(0, Line, s, FgColor, BgColor);
}
void cInterface::Info(const char *s)
2000-02-19 13:36:48 +01:00
{
Open(Setup.OSDwidth, -1);
isyslog(LOG_INFO, "info: %s", s);
2001-07-28 15:03:22 +02:00
Status(s, clrBlack, clrGreen);
Wait();
Status(NULL);
2000-02-19 13:36:48 +01:00
Close();
}
void cInterface::Error(const char *s)
2000-02-19 13:36:48 +01:00
{
Open(Setup.OSDwidth, -1);
esyslog(LOG_ERR, "ERROR: %s", s);
Status(s, clrWhite, clrRed);
Wait();
Status(NULL);
2000-02-19 13:36:48 +01:00
Close();
}
2001-09-01 09:04:37 +02:00
bool cInterface::Confirm(const char *s, int Seconds, bool WaitForTimeout)
{
Open(Setup.OSDwidth, -1);
isyslog(LOG_INFO, "confirm: %s", s);
2001-07-28 15:03:22 +02:00
Status(s, clrBlack, clrYellow);
2001-09-01 09:04:37 +02:00
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) {
2000-11-01 11:45:05 +01:00
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);
}
2000-02-19 13:36:48 +01:00
void cInterface::QueryKeys(void)
{
Keys.Clear();
2000-10-08 09:25:20 +02:00
Clear();
2000-11-11 10:39:27 +01:00
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();
2000-07-15 12:39:20 +02:00
#ifndef REMOTE_KBD
2001-09-01 11:44:08 +02:00
unsigned char Code = '0';
2000-02-19 13:36:48 +01:00
unsigned short Address;
#endif
for (;;) {
2000-07-15 12:39:20 +02:00
#ifdef REMOTE_KBD
2000-02-19 13:36:48 +01:00
if (GetCh())
break;
#else
//TODO on screen display...
if (rcIo->DetectCode(&Code, &Address)) {
2000-02-19 13:36:48 +01:00
Keys.code = Code;
Keys.address = Address;
2000-11-11 10:39:27 +01:00
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();
2000-02-19 13:36:48 +01:00
break;
}
#endif
}
2000-11-11 10:39:27 +01:00
WriteText(1, 3, tr("Phase 2: Learning specific key codes"));
2000-02-19 13:36:48 +01:00
tKey *k = Keys.keys;
while (k->type != kNone) {
char *Prompt;
2000-11-11 10:39:27 +01:00
asprintf(&Prompt, tr("Press key for '%s'"), tr(k->name));
2000-02-19 13:36:48 +01:00
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) {
2000-11-11 10:39:27 +01:00
WriteText(1, 5, tr("Press 'Up' to confirm"));
WriteText(1, 6, tr("Press 'Down' to continue"));
ClearEol(0, 7);
ClearEol(0, 8);
2000-02-19 13:36:48 +01:00
for (;;) {
eKeys key = GetKey();
if (key == kUp) {
Clear();
return;
}
else if (key == kDown) {
ClearEol(0, 6);
2000-02-19 13:36:48 +01:00
break;
}
}
break;
}
case kNone: k->code = ch;
k++;
break;
default: break;
}
break;
}
}
if (k > Keys.keys)
2000-11-11 10:39:27 +01:00
WriteText(1, 7, tr("(press 'Up' to go back)"));
2000-02-19 13:36:48 +01:00
else
ClearEol(0, 7);
2000-02-19 13:36:48 +01:00
if (k > Keys.keys + 1)
2000-11-11 10:39:27 +01:00
WriteText(1, 8, tr("(press 'Down' to end key definition)"));
2000-02-19 13:36:48 +01:00
else
ClearEol(0, 8);
2000-02-19 13:36:48 +01:00
}
}
void cInterface::LearnKeys(void)
{
isyslog(LOG_INFO, "learning keys");
Open();
2000-02-19 13:36:48 +01:00
for (;;) {
Clear();
QueryKeys();
Clear();
2000-11-11 10:39:27 +01:00
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"));
2000-02-19 13:36:48 +01:00
for (;;) {
eKeys key = GetKey();
if (key == kUp) {
Keys.Save();
Close();
2000-02-19 13:36:48 +01:00
return;
}
else if (key == kDown) {
Keys.Load();
Close();
2000-02-19 13:36:48 +01:00
return;
}
}
}
}
void cInterface::DisplayChannelNumber(int Number)
2000-10-29 13:17:22 +01:00
{
rcIo->Number(Number);
2000-10-29 13:17:22 +01:00
}
void cInterface::DisplayRecording(int Index, bool On)
{
rcIo->SetPoints(1 << Index, On);
}
2000-05-01 16:29:46 +02:00
bool cInterface::Recording(void)
{
// This is located here because the Interface has to do with the "PrimaryDvbApi" anyway
return cDvbApi::PrimaryDvbApi->Recording();
}