]> git.vomp.tv Git - vompclient.git/blob - timers.h
Mouse code for windows
[vompclient.git] / timers.h
1
2
3
4
5
6
7
8
9
10 /*
11
12
13 FYI
14
15 Timers ... deprecated. Maybe.
16
17 This whole thing will be replaced with a timed-message idea. It will be possible
18 to send yourself (or whoever) a message with a delay, or delivery time. The message
19 will be handed to Command as usual, but command will do the right thing. The messages
20 will be delivered to the recipient _with the gui mutex locked_, meaning updates can
21 be done there and then in the message handler.
22
23 Good points:
24 * Cuts down on code lines
25 * Most (all?) timercall()s eventually send a message to command in order to
26   do something within The Big Mutex. This makes it easier and simpler code wise
27 * Gets rid of Timers.
28 * Hopefully gets rid of most postMessageFromOuterSpace calls
29
30 Bad points:
31 * Timers become gui only features. Solve this with a MessageReceiver interface and
32   have command deliver messages straight to the recipients rather than through BoxStack.
33 * Timer delivery accuracy becomes dependant on everything that uses The Big Mutex.
34   It will become more important to not block The Big Mutex.
35 * Cancelling timers... hmm
36
37 If you have any comments about the new design, like, "It's just as flawed as the old one",
38 then I'd appreciate hearing it before I start writing it all :)
39
40
41 */
42
43
44
45
46
47 /*
48     Copyright 2004-2005 Chris Tallon
49
50     This file is part of VOMP.
51
52     VOMP is free software; you can redistribute it and/or modify
53     it under the terms of the GNU General Public License as published by
54     the Free Software Foundation; either version 2 of the License, or
55     (at your option) any later version.
56
57     VOMP is distributed in the hope that it will be useful,
58     but WITHOUT ANY WARRANTY; without even the implied warranty of
59     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
60     GNU General Public License for more details.
61
62     You should have received a copy of the GNU General Public License
63     along with VOMP; if not, write to the Free Software
64     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
65 */
66
67 #ifndef TIMERS_H
68 #define TIMERS_H
69
70 #include <stdio.h>
71 #include <list>
72
73 #ifndef WIN32
74 #include "threadp.h"
75 #else
76 #include "threadwin.h"
77 #endif
78
79 #include "defines.h"
80
81
82 class Log;
83 class TimerReceiver;
84
85 /*
86
87 Timers documentation
88
89 Call setTimer to set a timer.... cancelTimer to delete a running timer.
90 Derive your object from TimerReceiver (timerreceiver.h), implement timercall() in your class
91 and supply your 'this' pointer to setTimer.
92
93 Once a timer has fired it does not exist anymore, you have to keep creating them if you want
94 a constant pulse.
95
96 clientReference is any int of your choice. It will be supplied back to you in the timercall()
97 so you can identify which timer has fired if you have more than one.
98
99 You can reset a timer by calling setTimer again. This will not create 2 timers, it will overwrite the first one.
100
101 You must not allow a timer to fire on an object that has been deleted already, unless you want
102 segfaulty hell.
103
104 ??
105
106 You must call cancelTimer before deleting object. cancelTimer guarantees that timercall
107 will not be called again.
108
109 */
110
111 class TimerEvent : public Thread_TYPE
112 {
113   public:
114     TimerEvent();
115
116     virtual void run();
117     virtual void threadMethod();
118     virtual void threadPostStopCleanup() {};
119
120     TimerReceiver* client;
121     int clientReference;
122     struct timespec requestedTime;
123
124     bool running;
125     bool restartAfterFinish;
126 };
127
128
129 using namespace std;
130
131 typedef list<TimerEvent*> TimerList;
132
133 class Timers : public Thread_TYPE
134 {
135   public:
136     Timers();
137     virtual ~Timers();
138     static Timers* getInstance();
139
140     int init();
141     int shutdown();
142
143     bool setTimerT(TimerReceiver* client, int clientReference, long int requestedTime, long int requestedTimeNSEC=0);
144     bool setTimerD(TimerReceiver* client, int clientReference, long int requestedSecs, long int requestedNSecs=0);
145     bool cancelTimer(TimerReceiver* client, int clientReference);
146
147     // Thread stuff
148     virtual void threadMethod();
149     virtual void threadPostStopCleanup() {};
150
151     void timerEventFinished(TimerEvent* timerEvent); // internal use only, does not return
152
153   private:
154     static Timers* instance;
155     Log* logger;
156     bool initted;
157     TimerList timerList;
158     bool resetThreadFlag;
159 };
160
161 #endif