]> git.vomp.tv Git - vompclient.git/blob - vsleeptimer.cc
Replace TCP. Use new IP API only, getMAC works with if!=eth0
[vompclient.git] / vsleeptimer.cc
1 /*
2     Copyright 2008 Thomas Steger
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "vsleeptimer.h"
21
22 #include "input.h"
23 #include "wsymbol.h"
24 #include "colour.h"
25 #include "video.h"
26 #include "boxstack.h"
27 #include "command.h"
28 #include "messagequeue.h"
29
30 Sleeptimer* Sleeptimer::instance = NULL;
31
32 Sleeptimer::Sleeptimer()
33 {
34    if (instance) return;
35    instance = this;
36 }
37
38 Sleeptimer::~Sleeptimer()
39 {
40    instance = NULL;
41 }
42
43 const char* Sleeptimer::SetTime()
44 {
45   if (sec < 890)
46   {
47     sec = 900;
48     if (active == false)
49     {
50       active = true;
51       threadStart();
52     }
53
54     return "0:15";
55   }
56   else if (sec <1790)
57   {
58     sec = 1800;
59     if (active == false)
60     {
61       active = true;
62       threadStart();
63     }
64
65     return "0:30";
66   }
67   else if (sec < 2690)
68   {
69     sec = 2700;
70     if (active==false)
71     {
72       active = true;
73       threadStart();
74     }
75
76     return "0:45";
77   }
78   else if (sec < 3590)
79   {
80     sec = 3600;
81     if (active==false)
82     {
83       active = true;
84       threadStart();
85     }
86
87     return "1:00";
88   }
89   else if (sec < 4490)
90   {
91     sec = 4500;
92     if (active==false)
93     {
94       active = true;
95       threadStart();
96     }
97
98     return "1:15";
99   }
100   else if (sec < 5390)
101   {
102     sec = 5400;
103     if (active==false)
104     {
105         active = true;
106         threadStart();
107     }
108
109     return "1:30";
110   }
111   else if (sec < 6290)
112   {
113     sec = 6300;
114     if (active==false)
115     {
116       active = true;
117       threadStart();
118     }
119
120     return "1:45";
121   }
122   else if (sec < 7190)
123   {
124     sec = 7200;
125     if (active==false)
126     {
127       active = true;
128       threadStart();
129     }
130
131     return "2:00";
132
133   }
134   else
135   {
136     sec = -1;
137     if (active==true)
138     shutdown();
139     return "AUS";
140   }
141 }
142
143 Sleeptimer* Sleeptimer::getInstance()
144 {
145   return instance;
146 }
147
148 void Sleeptimer::threadMethod()
149 {
150   while (sec>-1 && active==true)
151   {
152     sec--;
153     if (sec<31 && sec>-1)
154     {
155       VCountdown* count = new VCountdown();
156       char temp[20];
157       sprintf(temp, "0:%02d", sec);
158       count->drawClock(temp);
159
160       Message* m1 = new Message();
161       m1->message = Message::ADD_VIEW;
162       m1->to = BoxStack::getInstance();
163       m1->data = reinterpret_cast<void*>(count);
164       MessageQueue::getInstance()->postMessage(m1);
165     }
166     MILLISLEEP(1000);
167
168     if (sec == -1)
169     {
170       Message* m2 = new Message();
171       m2->message = Message::INPUT_EVENT;
172       m2->to = Command::getInstance();
173       m2->from = this;
174       m2->parameter = Input::POWER;
175       MessageQueue::getInstance()->postMessage(m2);
176       shutdown();
177     }
178   }
179 }
180
181 void Sleeptimer::shutdown()
182 {
183   if (active)
184   {
185     sec = -1;
186     threadCancel();
187     active = false;
188   }
189 }
190
191
192 VSleeptimer::VSleeptimer()
193 {
194   setSize(100, 28);
195   createBuffer();
196   if (Video::getInstance()->getFormat() == Video::PAL)
197   {
198     setPosition(100, 499);
199   }
200   else
201   {
202     setPosition(90, 400);
203   }
204 }
205
206 VSleeptimer::~VSleeptimer()
207 {
208   // Make sure the timer is deleted
209   Timers::getInstance()->cancelTimer(this, 1);
210 }
211
212 void VSleeptimer::draw()
213 {
214   fillColour(DrawStyle::VIEWBACKGROUND);
215   WSymbol w;
216   TEMPADD(&w);
217   w.nextSymbol = WSymbol::CLOCK;
218   w.setPosition(3, 0);
219   w.draw();
220  
221   Boxx::draw();
222   drawText(displaySleeptimer,50,2,DrawStyle::LIGHTTEXT);
223   Timers::getInstance()->setTimerD(this, 1, 2);
224 }
225
226 void VSleeptimer::timercall(int /* clientReference */)
227 {
228   // delete me!
229   Message* m = new Message(); // Delete self
230   m->message = Message::CLOSE_ME;
231   m->to = BoxStack::getInstance();
232   m->from = this;
233   MessageQueue::getInstance()->postMessage(m);
234 }
235
236 int VSleeptimer::handleCommand(int command)
237 {
238   switch(command)
239   {
240     case Input::GO:
241     {
242       displaySleeptimer = Sleeptimer::getInstance()->SetTime();
243       draw();
244       BoxStack::getInstance()->update(this);
245       // handled
246       return 2;
247     }
248   }
249
250   // allow command to drop through to other views
251   return 0;
252 }
253
254
255
256
257 VCountdown::VCountdown()
258 {
259
260   setSize(100, 28);
261   createBuffer();
262   if (Video::getInstance()->getFormat() == Video::PAL)
263   {
264     setPosition(100, 499);
265   }
266   else
267   {
268     setPosition(90, 400);
269   }
270 }
271
272 VCountdown::~VCountdown()
273 {
274   // Make sure the timer is deleted
275   Timers::getInstance()->cancelTimer(this, 1);
276 }
277
278 void VCountdown::drawClock(const char* sec)
279 {
280   fillColour(DrawStyle::VIEWBACKGROUND);
281   WSymbol w;
282   TEMPADD(&w);
283   w.nextSymbol = WSymbol::CLOCK;
284   w.nextColour = DrawStyle::RED;
285   w.setPosition(3, 0);
286   w.draw();
287
288   Boxx::draw();
289   drawText(sec, 50, 2, DrawStyle::RED);
290   Timers::getInstance()->setTimerD(this, 1, 1);
291 }
292
293 void VCountdown::timercall(int /* clientReference */)
294 {
295   // delete me!
296   Message* m = new Message(); // Delete self
297   m->message = Message::CLOSE_ME;
298   m->to = BoxStack::getInstance();
299   m->from = this;
300   MessageQueue::getInstance()->postMessage(m);
301 }