]> git.vomp.tv Git - vompclient.git/blob - vsleeptimer.cc
20 CWFs, fix some snprintf calls
[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 "timers.h"
27 #include "boxstack.h"
28 #include "command.h"
29 #include "messagequeue.h"
30
31 Sleeptimer* Sleeptimer::instance = NULL;
32
33 Sleeptimer::Sleeptimer()
34 {
35    if (instance) return;
36    instance = this;
37 }
38
39 Sleeptimer::~Sleeptimer()
40 {
41    instance = NULL;
42 }
43
44 const char* Sleeptimer::SetTime()
45 {
46   if (sec < 890)
47   {
48     sec = 900;
49     if (active == false)
50     {
51       active = true;
52       threadStart();
53     }
54
55     return "0:15";
56   }
57   else if (sec <1790)
58   {
59     sec = 1800;
60     if (active == false)
61     {
62       active = true;
63       threadStart();
64     }
65
66     return "0:30";
67   }
68   else if (sec < 2690)
69   {
70     sec = 2700;
71     if (active==false)
72     {
73       active = true;
74       threadStart();
75     }
76
77     return "0:45";
78   }
79   else if (sec < 3590)
80   {
81     sec = 3600;
82     if (active==false)
83     {
84       active = true;
85       threadStart();
86     }
87
88     return "1:00";
89   }
90   else if (sec < 4490)
91   {
92     sec = 4500;
93     if (active==false)
94     {
95       active = true;
96       threadStart();
97     }
98
99     return "1:15";
100   }
101   else if (sec < 5390)
102   {
103     sec = 5400;
104     if (active==false)
105     {
106         active = true;
107         threadStart();
108     }
109
110     return "1:30";
111   }
112   else if (sec < 6290)
113   {
114     sec = 6300;
115     if (active==false)
116     {
117       active = true;
118       threadStart();
119     }
120
121     return "1:45";
122   }
123   else if (sec < 7190)
124   {
125     sec = 7200;
126     if (active==false)
127     {
128       active = true;
129       threadStart();
130     }
131
132     return "2:00";
133
134   }
135   else
136   {
137     sec = -1;
138     if (active==true)
139     shutdown();
140     return "AUS";
141   }
142 }
143
144 Sleeptimer* Sleeptimer::getInstance()
145 {
146   return instance;
147 }
148
149 void Sleeptimer::threadMethod()
150 {
151   while (sec>-1 && active==true)
152   {
153     sec--;
154     if (sec<31 && sec>-1)
155     {
156       VCountdown* count = new VCountdown();
157       char temp[20];
158       sprintf(temp, "0:%02d", sec);
159       count->drawClock(temp);
160
161       Message* m1 = new Message();
162       m1->message = Message::ADD_VIEW;
163       m1->to = BoxStack::getInstance();
164       m1->data = reinterpret_cast<void*>(count);
165       MessageQueue::getInstance()->postMessage(m1);
166     }
167     MILLISLEEP(1000);
168
169     if (sec == -1)
170     {
171       Message* m2 = new Message();
172       m2->message = Message::INPUT_EVENT;
173       m2->to = Command::getInstance();
174       m2->from = this;
175       m2->parameter = Input::POWER;
176       MessageQueue::getInstance()->postMessage(m2);
177       shutdown();
178     }
179   }
180 }
181
182 void Sleeptimer::shutdown()
183 {
184   if (active)
185   {
186     sec = -1;
187     threadCancel();
188     active = false;
189   }
190 }
191
192
193 VSleeptimer::VSleeptimer()
194 {
195   setSize(100, 28);
196   createBuffer();
197   if (Video::getInstance()->getFormat() == Video::PAL)
198   {
199     setPosition(100, 499);
200   }
201   else
202   {
203     setPosition(90, 400);
204   }
205 }
206
207 VSleeptimer::~VSleeptimer()
208 {
209   // Make sure the timer is deleted
210   Timers::getInstance()->cancelTimer(this, 1);
211 }
212
213 void VSleeptimer::draw()
214 {
215   fillColour(DrawStyle::VIEWBACKGROUND);
216   WSymbol w;
217   TEMPADD(&w);
218   w.nextSymbol = WSymbol::CLOCK;
219   w.setPosition(3, 0);
220   w.draw();
221  
222   Boxx::draw();
223   drawText(displaySleeptimer,50,2,DrawStyle::LIGHTTEXT);
224   Timers::getInstance()->setTimerD(this, 1, 2);
225 }
226
227 void VSleeptimer::timercall(int /* clientReference */)
228 {
229   // delete me!
230   Message* m = new Message(); // Delete self
231   m->message = Message::CLOSE_ME;
232   m->to = BoxStack::getInstance();
233   m->from = this;
234   MessageQueue::getInstance()->postMessage(m);
235 }
236
237 int VSleeptimer::handleCommand(int command)
238 {
239   switch(command)
240   {
241     case Input::GO:
242     {
243       displaySleeptimer = Sleeptimer::getInstance()->SetTime();
244       draw();
245       BoxStack::getInstance()->update(this);
246       // handled
247       return 2;
248     }
249   }
250
251   // allow command to drop through to other views
252   return 0;
253 }
254
255
256
257
258 VCountdown::VCountdown()
259 {
260
261   setSize(100, 28);
262   createBuffer();
263   if (Video::getInstance()->getFormat() == Video::PAL)
264   {
265     setPosition(100, 499);
266   }
267   else
268   {
269     setPosition(90, 400);
270   }
271 }
272
273 VCountdown::~VCountdown()
274 {
275   // Make sure the timer is deleted
276   Timers::getInstance()->cancelTimer(this, 1);
277 }
278
279 void VCountdown::drawClock(const char* sec)
280 {
281   fillColour(DrawStyle::VIEWBACKGROUND);
282   WSymbol w;
283   TEMPADD(&w);
284   w.nextSymbol = WSymbol::CLOCK;
285   w.nextColour = DrawStyle::RED;
286   w.setPosition(3, 0);
287   w.draw();
288
289   Boxx::draw();
290   drawText(sec, 50, 2, DrawStyle::RED);
291   Timers::getInstance()->setTimerD(this, 1, 1);
292 }
293
294 void VCountdown::timercall(int /* clientReference */)
295 {
296   // delete me!
297   Message* m = new Message(); // Delete self
298   m->message = Message::CLOSE_ME;
299   m->to = BoxStack::getInstance();
300   m->from = this;
301   MessageQueue::getInstance()->postMessage(m);
302 }