198int main(
int argc,
char *argv[])
202 struct termios savedTm;
203 bool HasStdin = (tcgetpgrp(STDIN_FILENO) == getpid() || getppid() != (pid_t)1) && tcgetattr(STDIN_FILENO, &savedTm) == 0;
207 setlocale(LC_ALL,
"");
211#define dd(a, b) (*a ? a : b)
212#define DEFAULTSVDRPPORT 6419
213#define DEFAULTWATCHDOG 0
214#define DEFAULTVIDEODIR VIDEODIR
215#define DEFAULTCONFDIR dd(CONFDIR, VideoDirectory)
216#define DEFAULTARGSDIR dd(ARGSDIR, "/etc/vdr/conf.d")
217#define DEFAULTCACHEDIR dd(CACHEDIR, VideoDirectory)
218#define DEFAULTRESDIR dd(RESDIR, ConfigDirectory)
219#define DEFAULTPLUGINDIR PLUGINDIR
220#define DEFAULTLOCDIR LOCDIR
221#define DEFAULTEPGDATAFILENAME "epg.data"
223 bool StartedAsRoot =
false;
224 const char *VdrUser = NULL;
225 bool UserDump =
false;
227 const char *AudioCommand = NULL;
229 const char *ConfigDirectory = NULL;
230 const char *CacheDirectory = NULL;
231 const char *ResourceDirectory = NULL;
234 bool DisplayHelp =
false;
235 bool DisplayVersion =
false;
236 bool DaemonMode =
false;
237 int SysLogTarget = LOG_USER;
238 bool MuteAudio =
false;
240 const char *Terminal = NULL;
241 const char *OverrideCharacterTable = NULL;
244 const char *LircDevice = NULL;
245#if !defined(REMOTE_KBD)
248#if defined(REMOTE_LIRC)
249 LircDevice = LIRC_DEVICE;
255 time_t SdWatchdog = 0;
256 int SdWatchdogTimeout = 0;
261 Args =
new cArgs(argv[0]);
271 static struct option long_options[] = {
272 {
"audio", required_argument, NULL,
'a' },
273 {
"cachedir", required_argument, NULL,
'c' | 0x100 },
274 {
"chartab", required_argument, NULL,
'c' | 0x200 },
275 {
"config", required_argument, NULL,
'c' },
276 {
"daemon", no_argument, NULL,
'd' },
277 {
"device", required_argument, NULL,
'D' },
278 {
"dirnames", required_argument, NULL,
'd' | 0x100 },
279 {
"edit", required_argument, NULL,
'e' | 0x100 },
280 {
"epgfile", required_argument, NULL,
'E' },
281 {
"filesize", required_argument, NULL,
'f' | 0x100 },
282 {
"genindex", required_argument, NULL,
'g' | 0x100 },
283 {
"grab", required_argument, NULL,
'g' },
284 {
"help", no_argument, NULL,
'h' },
285 {
"instance", required_argument, NULL,
'i' },
286 {
"lib", required_argument, NULL,
'L' },
287 {
"lirc", optional_argument, NULL,
'l' | 0x100 },
288 {
"localedir",required_argument, NULL,
'l' | 0x200 },
289 {
"log", required_argument, NULL,
'l' },
290 {
"mute", no_argument, NULL,
'm' },
291 {
"no-kbd", no_argument, NULL,
'n' | 0x100 },
292 {
"plugin", required_argument, NULL,
'P' },
293 {
"port", required_argument, NULL,
'p' },
294 {
"record", required_argument, NULL,
'r' },
295 {
"resdir", required_argument, NULL,
'r' | 0x100 },
296 {
"showargs", optional_argument, NULL,
's' | 0x200 },
297 {
"shutdown", required_argument, NULL,
's' },
298 {
"split", no_argument, NULL,
's' | 0x100 },
299 {
"terminal", required_argument, NULL,
't' },
300 {
"updindex", required_argument, NULL,
'u' | 0x200 },
301 {
"user", required_argument, NULL,
'u' },
302 {
"userdump", no_argument, NULL,
'u' | 0x100 },
303 {
"version", no_argument, NULL,
'V' },
304 {
"vfat", no_argument, NULL,
'v' | 0x100 },
305 {
"video", required_argument, NULL,
'v' },
306 {
"watchdog", required_argument, NULL,
'w' },
307 { NULL, no_argument, NULL, 0 }
311 while ((c = getopt_long(argc, argv,
"a:c:dD:e:E:g:hi:l:L:mp:P:r:s:t:u:v:Vw:", long_options, NULL)) != -1) {
313 case 'a': AudioCommand = optarg;
316 CacheDirectory = optarg;
319 OverrideCharacterTable = optarg;
321 case 'c': ConfigDirectory = optarg;
323 case 'd': DaemonMode =
true;
325 case 'D':
if (*optarg ==
'-') {
330 int n = atoi(optarg);
336 fprintf(stderr,
"vdr: invalid DVB device number: %s\n", optarg);
341 int n = strtol(s, &s, 10);
342 if (n <= 0 || n >= PATH_MAX) {
343 fprintf(stderr,
"vdr: invalid directory path length: %s\n", optarg);
350 fprintf(stderr,
"vdr: invalid delimiter: %s\n", optarg);
358 int n = strtol(s, &s, 10);
359 if (n <= 0 || n > NAME_MAX) {
360 fprintf(stderr,
"vdr: invalid directory name length: %s\n", optarg);
367 fprintf(stderr,
"vdr: invalid delimiter: %s\n", optarg);
374 int n = strtol(s, &s, 10);
375 if (n != 0 && n != 1) {
376 fprintf(stderr,
"vdr: invalid directory encoding: %s\n", optarg);
381 fprintf(stderr,
"vdr: unexpected data: %s\n", optarg);
388 case 'E': EpgDataFileName = (*optarg !=
'-' ? optarg : NULL);
401 case 'h': DisplayHelp =
true;
408 fprintf(stderr,
"vdr: invalid instance id: %s\n", optarg);
411 char *p = strchr(optarg,
'.');
415 int l = atoi(optarg);
416 if (0 <= l && l <= 3) {
423 if (0 <= l && l <= 7) {
424 int targets[] = { LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
425 SysLogTarget = targets[l];
433 fprintf(stderr,
"vdr: invalid log level: %s\n", optarg);
436 case 'L':
if (access(optarg, R_OK | X_OK) == 0)
439 fprintf(stderr,
"vdr: can't access plugin directory: %s\n", optarg);
444 LircDevice = optarg ? optarg : LIRC_DEVICE;
447 if (access(optarg, R_OK | X_OK) == 0)
448 LocaleDirectory = optarg;
450 fprintf(stderr,
"vdr: can't access locale directory: %s\n", optarg);
454 case 'm': MuteAudio =
true;
460 SVDRPport = atoi(optarg);
462 fprintf(stderr,
"vdr: invalid port number: %s\n", optarg);
466 case 'P': PluginManager.
AddPlugin(optarg);
471 ResourceDirectory = optarg;
476 Setup.SplitEditedFiles = 1;
482 fprintf(stderr,
"vdr: can't read arguments from directory: %s\n", ArgsDir);
487 for (
int i = 1; i < c; i++)
488 printf(
"%s\n", v[i]);
491 case 't': Terminal = optarg;
492 if (access(Terminal, R_OK | W_OK) < 0) {
493 fprintf(stderr,
"vdr: can't access terminal: %s\n", Terminal);
497 case 'u':
if (*optarg)
505 case 'V': DisplayVersion =
true;
512 case 'v': VideoDirectory = optarg;
513 while (optarg && *optarg && optarg[strlen(optarg) - 1] ==
'/')
514 optarg[strlen(optarg) - 1] = 0;
518 int t = atoi(optarg);
524 fprintf(stderr,
"vdr: invalid watchdog timeout: %s\n", optarg);
532 if (DisplayHelp || DisplayVersion) {
537 printf(
"Usage: vdr [OPTIONS]\n\n"
538 " -a CMD, --audio=CMD send Dolby Digital audio to stdin of command CMD\n"
539 " --cachedir=DIR save cache files in DIR (default: %s)\n"
540 " --chartab=CHARACTER_TABLE\n"
541 " set the character table to use for strings in the\n"
542 " DVB data stream that don't begin with a character\n"
543 " table indicator, but don't use the standard default\n"
544 " character table (for instance ISO-8859-9)\n"
545 " -c DIR, --config=DIR read config files from DIR (default: %s)\n"
546 " -d, --daemon run in daemon mode\n"
547 " -D NUM, --device=NUM use only the given DVB device (NUM = 0, 1, 2...)\n"
548 " there may be several -D options (default: all DVB\n"
549 " devices will be used); if -D- is given, no DVB\n"
550 " devices will be used at all, independent of any\n"
551 " other -D options\n"
552 " --dirnames=PATH[,NAME[,ENC]]\n"
553 " set the maximum directory path length to PATH\n"
554 " (default: %d); if NAME is also given, it defines\n"
555 " the maximum directory name length (default: %d);\n"
556 " the optional ENC can be 0 or 1, and controls whether\n"
557 " special characters in directory names are encoded as\n"
558 " hex values (default: 0); if PATH or NAME are left\n"
559 " empty (as in \",,1\" to only set ENC), the defaults\n"
561 " --edit=REC cut recording REC and exit\n"
562 " -E FILE, --epgfile=FILE write the EPG data into the given FILE (default is\n"
563 " '%s' in the cache directory)\n"
564 " '-E-' disables this\n"
565 " if FILE is a directory, the default EPG file will be\n"
566 " created in that directory\n"
567 " --filesize=SIZE limit video files to SIZE bytes (default is %dM)\n"
568 " only useful in conjunction with --edit\n"
569 " --genindex=REC generate index for recording REC and exit\n"
570 " -g DIR, --grab=DIR write images from the SVDRP command GRAB into the\n"
571 " given DIR; DIR must be the full path name of an\n"
572 " existing directory, without any \"..\", double '/'\n"
573 " or symlinks (default: none, same as -g-)\n"
574 " -h, --help print this help and exit\n"
575 " -i ID, --instance=ID use ID as the id of this VDR instance (default: 0)\n"
576 " -l LEVEL, --log=LEVEL set log level (default: 3)\n"
577 " 0 = no logging, 1 = errors only,\n"
578 " 2 = errors and info, 3 = errors, info and debug\n"
579 " if logging should be done to LOG_LOCALn instead of\n"
580 " LOG_USER, add '.n' to LEVEL, as in 3.7 (n=0..7)\n"
581 " -L DIR, --lib=DIR search for plugins in DIR (default is %s)\n"
582 " --lirc[=PATH] use a LIRC remote control device, attached to PATH\n"
584 " --localedir=DIR search for locale files in DIR (default is\n"
586 " -m, --mute mute audio of the primary DVB device at startup\n"
587 " --no-kbd don't use the keyboard as an input device\n"
588 " -p PORT, --port=PORT use PORT for SVDRP (default: %d)\n"
589 " 0 turns off SVDRP\n"
590 " -P OPT, --plugin=OPT load a plugin defined by the given options\n"
591 " -r CMD, --record=CMD call CMD before and after a recording, and after\n"
592 " a recording has been edited or deleted\n"
593 " --resdir=DIR read resource files from DIR (default: %s)\n"
594 " -s CMD, --shutdown=CMD call CMD to shutdown the computer\n"
595 " --split split edited files at the editing marks (only\n"
596 " useful in conjunction with --edit)\n"
597 " --showargs[=DIR] print the arguments read from DIR and exit\n"
599 " -t TTY, --terminal=TTY controlling tty\n"
600 " -u USER, --user=USER run as user USER; only applicable if started as\n"
601 " root; USER can be a user name or a numerical id\n"
602 " --updindex=REC update index for recording REC and exit\n"
603 " --userdump allow coredumps if -u is given (debugging)\n"
604 " -v DIR, --video=DIR use DIR as video directory (default: %s)\n"
605 " -V, --version print version information and exit\n"
606 " --vfat for backwards compatibility (same as\n"
607 " --dirnames=250,40,1)\n"
608 " -w SEC, --watchdog=SEC activate the watchdog timer with a timeout of SEC\n"
609 " seconds (default: %d); '0' disables the watchdog\n"
631 printf(
"Plugins: vdr -P\"name [OPTIONS]\"\n\n");
632 for (
int i = 0; ; i++) {
637 if (DisplayHelp && help) {
652 openlog(
"vdr", LOG_CONS, SysLogTarget);
657 if (daemon(1, 0) == -1) {
658 fprintf(stderr,
"vdr: %m\n");
665 stdin = freopen(Terminal,
"r", stdin);
666 stdout = freopen(Terminal,
"w", stdout);
667 stderr = freopen(Terminal,
"w", stderr);
669 tcgetattr(STDIN_FILENO, &savedTm);
674 if (VdrUser && geteuid() == 0) {
675 StartedAsRoot =
true;
676 if (strcmp(VdrUser,
"root") && strcmp(VdrUser,
"0")) {
679 if (!
SetUser(VdrUser, UserDump))
691 fprintf(stderr,
"vdr: can't access video directory %s\n", VideoDirectory);
696 if (StartedAsRoot && VdrUser)
697 isyslog(
"switched to user '%s'", VdrUser);
704 char *CodeSet = NULL;
705 if (setlocale(LC_CTYPE,
""))
706 CodeSet = nl_langinfo(CODESET);
708 char *LangEnv = getenv(
"LANG");
710 CodeSet = strchr(LangEnv,
'.');
717 isyslog(
"codeset is '%s' - %s", CodeSet, known ?
"known" :
"unknown");
720 if (OverrideCharacterTable) {
722 isyslog(
"override character table is '%s' - %s", OverrideCharacterTable, known ?
"known" :
"unknown");
734 int LastTimerChannel = -1;
735 int PreviousChannel[2] = { 1, 1 };
736 int PreviousChannelIndex = 0;
737 time_t LastChannelChanged = time(NULL);
738 time_t LastInteract = 0;
739 int MaxLatencyTime = 0;
740 bool IsInfoMenu =
false;
741 cSkin *CurrentSkin = NULL;
742 int OldPrimaryDVB = 0;
751 if (!ConfigDirectory)
757 if (!ResourceDirectory)
780 const char *msg =
"no fonts available - OSD will not show any text!";
781 fprintf(stderr,
"vdr: %s\n", msg);
791 if (EpgDataFileName) {
792 const char *EpgDirectory = NULL;
794 EpgDirectory = EpgDataFileName;
797 else if (*EpgDataFileName !=
'/' && *EpgDataFileName !=
'.')
798 EpgDirectory = CacheDirectory;
803 EpgDataReader.
Start();
825 isyslog(
"trying device number %d instead", i + 1);
827 Setup.PrimaryDVB = i + 1;
833 const char *msg =
"no primary device found - using first device!";
834 fprintf(stderr,
"vdr: %s\n", msg);
839 const char *msg =
"no primary device found - giving up!";
840 fprintf(stderr,
"vdr: %s\n", msg);
846 OldPrimaryDVB =
Setup.PrimaryDVB;
863 CurrentSkin =
Skins.Current();
872 if (!CurrentSkin || CurrentSkin ==
Skins.Current() && strcmp(
Skins.Current()->Name(),
Setup.OSDSkin) != 0) {
880 if (!DaemonMode && HasStdin && UseKbd)
904 if (*
Setup.InitialChannel) {
907 if (
const cChannel *Channel = Channels->GetByNumber(atoi(
Setup.InitialChannel)))
908 Setup.InitialChannel = Channel->GetChannelID().ToString();
911 Setup.CurrentChannel = Channel->Number();
913 if (
Setup.InitialVolume >= 0)
927 if (signal(SIGHUP,
SignalHandler) == SIG_IGN) signal(SIGHUP, SIG_IGN);
928 if (signal(SIGINT,
SignalHandler) == SIG_IGN) signal(SIGINT, SIG_IGN);
929 if (signal(SIGTERM,
SignalHandler) == SIG_IGN) signal(SIGTERM, SIG_IGN);
930 if (signal(SIGPIPE,
SignalHandler) == SIG_IGN) signal(SIGPIPE, SIG_IGN);
931 if (WatchdogTimeout > 0)
932 if (signal(SIGALRM,
Watchdog) == SIG_IGN) signal(SIGALRM, SIG_IGN);
936 if (WatchdogTimeout > 0) {
937 dsyslog(
"setting watchdog timer to %d seconds", WatchdogTimeout);
938 alarm(WatchdogTimeout);
942 if (sd_watchdog_enabled(0, NULL) > 0) {
944 SdWatchdog = time(NULL);
945 sd_watchdog_enabled(0, &timeout);
946 SdWatchdogTimeout = (int)timeout/1000000;
947 dsyslog(
"SD_WATCHDOG enabled with timeout set to %d seconds", SdWatchdogTimeout);
952 sd_notify(0,
"READY=1\nSTATUS=Ready");
962#define DELETE_MENU ((IsInfoMenu &= (Menu == NULL)), delete Menu, Menu = NULL)
965#ifdef DEBUGRINGBUFFERS
966 cRingBufferLinear::PrintDebugRBL();
971 time_t Now = time(NULL);
975 static time_t lastTime = 0;
980 if (Channel && (Channel->
Vpid() || Channel->
Apid(0) || Channel->
Dpid(0))) {
983 else if (LastTimerChannel > 0) {
984 Channel = Channels->GetByNumber(LastTimerChannel);
990 LastTimerChannel = -1;
998 static time_t lastOsdSizeUpdate = 0;
999 if (Now != lastOsdSizeUpdate) {
1001 static int OsdState = 0;
1006 lastOsdSizeUpdate = Now;
1010 if (WatchdogTimeout > 0) {
1011 int LatencyTime = WatchdogTimeout - alarm(WatchdogTimeout);
1012 if (LatencyTime > MaxLatencyTime) {
1013 MaxLatencyTime = LatencyTime;
1014 dsyslog(
"max. latency time %d seconds", MaxLatencyTime);
1019 if (SdWatchdogTimeout && (Now - SdWatchdog) * 2 > SdWatchdogTimeout) {
1020 sd_notify(0,
"WATCHDOG=1");
1026 static bool ChannelsRenumber =
false;
1030 static time_t ChannelSaveTimeout = 0;
1032 static cStateKey ChannelsStateKey(
true);
1033 static int ChannelsModifiedByUser = 0;
1036 if (ChannelSaveTimeout != 1) {
1039 ChannelSaveTimeout = 1;
1040 else if (!ChannelSaveTimeout)
1044 ChannelSaveTimeout = 1;
1047 ChannelSaveTimeout = 1;
1048 if (Timers && Channels) {
1051 ChannelSaveTimeout = 0;
1054 for (
const cChannel *Channel = Channels->
First(); Channel; Channel = Channels->
Next(Channel)) {
1057 ChannelsRenumber =
true;
1063 isyslog(
"retuning due to modification of channel %d (%s)", Channel->Number(), Channel->Name());
1064 Channels->
SwitchTo(Channel->Number());
1074 ChannelsStateKey.
Remove();
1077 if (ChannelSaveTimeout == 1) {
1079 ChannelsStateKey.
Reset();
1080 TimersStateKey.
Reset();
1088 LastChannelChanged = Now;
1090 if (Now - LastChannelChanged >=
Setup.ZapTimeout && LastChannel != PreviousChannel[PreviousChannelIndex])
1091 PreviousChannel[PreviousChannelIndex ^= 1] = LastChannel;
1101 SchedulesStateKey.
Reset();
1102 bool TimersModified =
false;
1107 TimersModified =
true;
1110 TimersModified =
true;
1113 TimersModified =
true;
1115 SchedulesStateKey.
Remove();
1117 TimersStateKey.
Remove(TimersModified);
1122 bool TimersModified =
false;
1124 TimersModified =
true;
1128 Timer->SetPending(
true);
1130 LastTimerChannel = Timer->Channel()->Number();
1131 TimersModified =
true;
1134 static time_t LastTimerCheck = 0;
1136 for (
cTimer *Timer = Timers->
First(); Timer; Timer = Timers->
Next(Timer)) {
1137 if (Timer->Remote() || Timer->IsPatternTimer())
1139 bool InVpsMargin =
false;
1140 bool NeedsTransponder =
false;
1141 if (Timer->HasFlags(
tfActive) && !Timer->Recording()) {
1142 if (Timer->HasFlags(
tfVps)) {
1143 if (Timer->Matches(Now,
true,
Setup.VpsMargin))
1145 else if (Timer->Event()) {
1147 InVpsMargin = Timer->Event()->StartTime() <= Now && Now < Timer->Event()->EndTime();
1152 const cSchedule *Schedule = Schedules->GetSchedule(Timer->Channel());
1153 InVpsMargin = !Schedule;
1160 if (!Timer->Recording())
1161 Timer->SetInVpsMargin(InVpsMargin);
1162 if (NeedsTransponder || InVpsMargin) {
1177 dsyslog(
"switching device %d to channel %d %s (%s)", Device->
DeviceNumber() + 1, Timer->Channel()->Number(), *Timer->Channel()->GetChannelID().ToString(), Timer->Channel()->Name());
1182 LastTimerChannel = Timer->Channel()->Number();
1188 LastTimerCheck = Now;
1192 TimersModified =
true;
1195 if (MaxPriority >= 0)
1197 TimersStateKey.
Remove(TimersModified);
1200 if (ChannelsRenumber) {
1202 Channels->ReNumber();
1203 ChannelsRenumber =
false;
1214 Skins.ProcessQueuedMessages();
1217 if (!NeedsFastResponse) {
1242 bool WasOpen = Interact != NULL;
1243 bool WasMenu = Interact && Interact->
IsMenu();
1252 if (!WasOpen || !WasMenu && !
Setup.MenuKeyCloses)
1281 #define DirectMainFunction(function)\
1285 Menu = new cMenuMain(function);\
1307 esyslog(
"ERROR: unknown plugin '%s'", PluginName);
1376 if (
Setup.PauseKeyHandling) {
1377 if (
Setup.PauseKeyHandling > 1 ||
Interface->Confirm(
tr(
"Pause live video?"))) {
1379 Skins.QueueMessage(
mtError,
tr(
"No free DVB device to record!"));
1388 if (
Setup.RecordKeyHandling) {
1389 if (
Setup.RecordKeyHandling > 1 ||
Interface->Confirm(
tr(
"Start recording?"))) {
1399 isyslog(
"Power button pressed");
1426 Interact = Menu ? Menu : Control;
1430 if (state ==
osUnknown && Interact != Control) {
1433 if (state ==
osEnd) {
1446 Skins.QueueMessage(
mtError,
tr(
"No free DVB device to record!"));
1474 case osEnd:
if (Interact == Menu)
1494 if (PreviousChannel[PreviousChannelIndex ^ 1] == LastChannel || LastChannel != PreviousChannel[0] && LastChannel != PreviousChannel[1])
1495 PreviousChannelIndex ^= 1;
1497 Channels->SwitchTo(PreviousChannel[PreviousChannelIndex ^= 1]);
1520 case kOk: LastChannel = -1;
break;
1546 int NewPrimaryDVB =
Setup.PrimaryDVB;
1547 if (NewPrimaryDVB != OldPrimaryDVB) {
1554 OldPrimaryDVB = NewPrimaryDVB;
1598 static time_t LastMemoryCleanup = 0;
1601 LastMemoryCleanup = Now;
1613 esyslog(
"emergency exit requested - shutting down");
1618 signal(SIGHUP, SIG_DFL);
1619 signal(SIGINT, SIG_DFL);
1620 signal(SIGTERM, SIG_DFL);
1621 signal(SIGPIPE, SIG_DFL);
1622 signal(SIGALRM, SIG_DFL);
1651 if (WatchdogTimeout > 0)
1652 dsyslog(
"max. latency time %d seconds", MaxLatencyTime);
1661 tcsetattr(STDIN_FILENO, TCSANOW, &savedTm);
1664 sd_notify(0,
"STOPPING=1\nSTATUS=Startup failed, exiting");
1666 sd_notify(0,
"STOPPING=1\nSTATUS=Exiting");