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