summaryrefslogtreecommitdiff
path: root/plugin/status.cpp
blob: 8cf4966999f0959480c19e836e2fd3ab70f04dea (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
/*
 * status.cpp: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "status.h"

void cStatusMarkAd::Recording(const cDevice *Device, const char *UNUSED(Name), const char *FileName, bool On)
{
    if (!Device) return; // just to be safe
    if (!FileName) return; // we cannot operate without a filename
    if (!bindir) return; // we cannot operate without bindir
    if (!logodir) return; // we dont want to operate without logodir

    if (On)
    {
        // Start markad with (before) recording
        cString cmd = cString::sprintf("\"%s\"/markad --online=2 -l \"%s\" before \"%s\"",bindir,
                                       logodir,FileName);
        if (SystemExec(cmd)!=-1)
        {
            Add(FileName);
        }
    }
    else
    {
#if 0
        // Start markad after recording
        cString cmd = cString::sprintf("\"%s\"/markad -l \"%s\" after \"%s\"",bindir,
                                       logodir,FileName);
        if (SystemExec(cmd)!=-1)
        {
            Add(FileName);
        }
#endif
        // TODO: Start second pass?
    }

}

cStatusMarkAd::cStatusMarkAd(const char *BinDir, const char *LogoDir)
{
    bindir=BinDir;
    logodir=LogoDir;
    memset(&recs,0,sizeof(recs));
}

cStatusMarkAd::~cStatusMarkAd()
{
    for (int i=0; i<(MAXDEVICES*MAXRECEIVERS); i++)
    {
        if (recs[i]) free(recs[i]);
    }
}

bool cStatusMarkAd::MarkAdRunning()
{
    bool running=false;
    for (int i=0; i<(MAXDEVICES*MAXRECEIVERS); i++)
    {
        if (recs[i])
        {
            char *buf;
            if (asprintf(&buf,"%s/markad.pid",recs[i])==-1)
            {
                // this is crude, but if we fail to allocate memory something
                // is going really wrong!
                return false;
            }
            // check for running markad process
            FILE *fpid=fopen(buf,"r");
            free(buf);
            if (fpid)
            {
                int pid,ret;
                ret=fscanf(fpid,"%i\n",&pid);
                fclose(fpid);
                if (ret==1)
                {
                    char procname[256]="";
                    snprintf(procname,sizeof(procname),"/proc/%i",pid);
                    struct stat statbuf;
                    if (stat(procname,&statbuf)==0)
                    {
                        // found a running markad
                        running=true;
                    }
                    else
                    {
                        if (errno==ENOENT)
                        {
                            // no such file or directory -> markad crashed?
                            // remove filename from list
                            free(recs[i]);
                            recs[i]=NULL;
                        }
                        else
                        {
                            // serious error? -> let vdr close
                            return false;
                        }
                    }
                }
            }
            else
            {
                if (errno==ENOENT)
                {
                    // no such file or directory -> markad already finished
                    // remove filename from list
                    free(recs[i]);
                    recs[i]=NULL;
                }
                else
                {
                    // serious error? -> let vdr close
                    return false;
                }
            }
        }
    }
    return running;
}

void cStatusMarkAd::Add(const char *FileName)
{
    for (int i=0; i<(MAXDEVICES*MAXRECEIVERS); i++)
    {
        if (!recs[i])
        {
            recs[i]=strdup(FileName);
        }
    }
}