source: titan/titan/powerofftimer.h @ 14271

Last change on this file since 14271 was 14271, checked in by nit, 12 years ago

fix

File size: 4.8 KB
Line 
1#ifndef POWEROFFTIMER_H
2#define POWEROFFTIMER_H
3
4void calcfixpowerofftime()
5{
6        char* tmpstr = NULL, *fixtime = NULL;
7        time_t akttime = time(NULL);
8        struct tm* loctime = NULL;
9
10        if(akttime < 1072224000) // 01.01.2004
11                return;
12
13        fixtime = getconfig("fixshutdowntime", NULL);
14        if(fixtime != NULL)
15        {
16                loctime = olocaltime(&akttime);
17                tmpstr = strptime(fixtime, "%H:%M", loctime);
18                if(tmpstr != NULL)
19                {
20                        status.fixpowerofftime = mktime(loctime);
21                        if(status.fixpowerofftime <= akttime)
22                                status.fixpowerofftime += 86400;
23                }
24        }
25        else
26                status.fixpowerofftime = 1;
27        free(loctime); loctime = NULL;
28}
29
30void checkshutdowntimer()
31{
32        if(status.standby != 0) return;
33
34        if(status.fixpowerofftime == 0)
35                calcfixpowerofftime();
36
37        time_t akttime = time(NULL);
38
39        if((status.sd_timer != NULL && status.sd_timer->active && status.sd_timer->shutdown_time < akttime) || (status.fixpowerofftime > 1 && status.fixpowerofftime > akttime - 180 && status.fixpowerofftime < akttime))
40        {
41                status.fixpowerofftime += 86400;
42                status.sd_timer->active = 0;
43                if(getconfigint("shutdowntimetype", NULL) == 0)
44                        oshutdown(1, 3);
45                else
46                {
47                        status.standby = 2;
48                        screenstandby();
49                }
50        }
51}
52
53void free_shutdowntimervar()
54{
55        if(status.sd_timer)
56        {
57                free(status.sd_timer);
58                status.sd_timer = NULL;
59        }
60}
61
62void screenpowerofftimer(void)
63{
64        if(status.sd_timer == NULL) //first call
65        {
66                status.sd_timer = (struct shutdowntimer*) malloc(sizeof(struct shutdowntimer));
67                if(!status.sd_timer)
68                {
69                        err("can't allocate memory for variable");
70                        return;
71                }
72                status.sd_timer->active = 0;
73                status.sd_timer->shutdown_time = time(NULL);
74        }
75
76        int rcret = 0;
77        struct skin* screen = getscreen("powerofftimerscreen");
78        struct skin* listbox = getscreennode(screen, "listbox");
79        struct skin* minutes = getscreennode(screen, "minutes");
80        struct skin* fixshutdowntime = getscreennode(screen, "fixshutdowntime");
81        struct skin* shutdowntimetype = getscreennode(screen, "mode");
82        long long secs = 0;
83
84        if(status.sd_timer->active) //calculate minutes until previously scheduled shutdown
85        {
86                secs = status.sd_timer->shutdown_time - (long long)time(NULL);
87                if(secs <= 0) secs = 1800; //oops, we should already be shut down or at least almost;)
88        }
89        int min = ((float)secs / 60 + 0.5);
90        if(min > 1000) min = 999;
91        char *tmpstr = (char*)malloc(4 * sizeof(char)) ;
92        snprintf(tmpstr,4,"%03d", min);
93        changemask(minutes, "000");
94        changeinput(minutes, tmpstr);
95        free(tmpstr);
96
97        addchoicebox(shutdowntimetype, "0", _("DeepStandby"));
98        addchoicebox(shutdowntimetype, "1", _("Standby"));
99        setchoiceboxselection(shutdowntimetype, getconfig("shutdowntimetype", NULL));
100
101        changeinput(fixshutdowntime, "deaktiv\n22:00\n22:15\n22:30\n22:45\n23:00\n23:15\n23:30\n23:45\n00:00\n00:15\n00:30\n00:45\n01:00\n01:15\n01:30\n01:45\n02:00\n02:15\n02:30\n02:45\n03:00\n03:15\n03:30\n03:45\n04:00");
102        changechoiceboxvalue(fixshutdowntime, "0\n22:00\n22:15\n22:30\n22:45\n23:00\n23:15\n23:30\n23:45\n00:00\n00:15\n00:30\n00:45\n01:00\n01:15\n01:30\n01:45\n02:00\n02:15\n02:30\n02:45\n03:00\n03:15\n03:30\n03:45\n04:00");
103        setchoiceboxselection(fixshutdowntime, getconfig("fixshutdowntime", NULL));
104
105        listbox->aktline = 1;
106        listbox->aktpage = 1;
107
108        drawscreen(screen, 0);
109        addscreenrc(screen, listbox);
110
111        struct skin* tmp = NULL;
112        tmp = listbox->select;
113
114        while(1)
115        {
116                addscreenrc(screen, tmp);
117                rcret = waitrc(screen, 0, 0);
118                tmp = listbox->select;
119                if(rcret == getrcconfigint("rcexit", NULL))  //user pressed exit so no change
120                {
121                        //fprintf(stderr,"shutdowntimerscreen cancelled\n");
122                        break;
123                }
124                if(rcret == getrcconfigint("rcred", NULL)) //red means "cancel shutdowntimer"
125                {
126                        //fprintf(stderr,"shutdowntimer cancelled\n");
127                        status.sd_timer->active = 0;
128                        status.sd_timer->shutdown_time = time(NULL);
129                        break;
130                }
131                if(rcret == getrcconfigint("rcok", NULL)) //ok means "activate shutdowntimer"
132                {
133                        if(minutes->ret != NULL)
134                        {
135                                addconfigscreencheck("fixshutdowntime", fixshutdowntime, "0");
136                                status.fixpowerofftime = 0;
137                                addconfigscreencheck("shutdowntimetype", shutdowntimetype, "0");
138                                if(atoi(minutes->ret) == 0) break;
139                                status.sd_timer->shutdown_time = atoll(minutes->ret) * 60 + (long long)time(NULL);
140                                status.sd_timer->active = 1;
141                                debug(100, "found: time=%s, type=%d, secs=%lld, now=%ld", minutes->ret, getconfigint("shutdowntimetype", NULL), status.sd_timer->shutdown_time, time(NULL));
142                                break;
143                        }
144                }
145                if(rcret == getrcconfigint("rcgreen",NULL)) //blue means "use epg end time of current program"
146                {
147                        struct epg* epgnode = getepgakt(status.aktservice->channel);
148                        if(epgnode != NULL)
149                        {
150                                min = (epgnode->endtime - time(NULL)) / 60 + 1;
151                                char *tmpstr = (char*)malloc(4 * sizeof(char));
152                                snprintf(tmpstr, 4, "%03d", min);
153                                debug(100, "blue: %s %d", tmpstr, min);
154                                changeinput(minutes, tmpstr);
155                                free(tmpstr);
156                                drawscreen(screen, 0);
157                        }
158                }
159        }
160
161        delownerrc(screen);
162        clearscreen(screen);
163}
164
165#endif
Note: See TracBrowser for help on using the repository browser.