~registry/dolphin-emu/triforce

« back to all changes in this revision

Viewing changes to Externals/wxWidgets3/src/generic/timer.cpp

  • Committer: Sérgio Benjamim
  • Date: 2015-02-13 05:54:40 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20150213055440-ey2rt3sjpy27km78
Dolphin Triforce branch from code.google, commit b957980 (4.0-315).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/////////////////////////////////////////////////////////////////////////////
 
2
// Name:        src/generic/timer.cpp
 
3
// Purpose:     wxTimer implementation
 
4
// Author:      Vaclav Slavik
 
5
// Copyright:   (c) Vaclav Slavik
 
6
// Licence:     wxWindows licence
 
7
/////////////////////////////////////////////////////////////////////////////
 
8
 
 
9
// For compilers that support precompilation, includes "wx.h".
 
10
#include "wx/wxprec.h"
 
11
 
 
12
#ifdef __BORLANDC__
 
13
    #pragma hdrstop
 
14
#endif
 
15
 
 
16
// ----------------------------------------------------------------------------
 
17
// NB: when using generic wxTimer implementation in your port, you *must* call
 
18
//     wxTimer::NotifyTimers() often enough. The ideal place for this
 
19
//     is in wxEventLoop::Dispatch().
 
20
// ----------------------------------------------------------------------------
 
21
 
 
22
#if wxUSE_TIMER
 
23
 
 
24
#ifndef WX_PRECOMP
 
25
    #include "wx/log.h"
 
26
    #include "wx/module.h"
 
27
#endif
 
28
 
 
29
#include "wx/apptrait.h"
 
30
#include "wx/generic/private/timer.h"
 
31
 
 
32
// ----------------------------------------------------------------------------
 
33
// Time input function
 
34
// ----------------------------------------------------------------------------
 
35
 
 
36
#define GetMillisecondsTime wxGetLocalTimeMillis
 
37
 
 
38
typedef wxLongLong wxTimerTick_t;
 
39
 
 
40
#if wxUSE_LONGLONG_WX
 
41
    #define wxTimerTickFmtSpec wxLongLongFmtSpec "d"
 
42
    #define wxTimerTickPrintfArg(tt) (tt.GetValue())
 
43
#else // using native wxLongLong
 
44
    #define wxTimerTickFmtSpec wxT("s")
 
45
    #define wxTimerTickPrintfArg(tt) (tt.ToString().c_str())
 
46
#endif // wx/native long long
 
47
 
 
48
inline bool wxTickGreaterEqual(wxTimerTick_t x, wxTimerTick_t y)
 
49
{
 
50
    return x >= y;
 
51
}
 
52
 
 
53
// ----------------------------------------------------------------------------
 
54
// helper structures and wxTimerScheduler
 
55
// ----------------------------------------------------------------------------
 
56
 
 
57
class wxTimerDesc
 
58
{
 
59
public:
 
60
    wxTimerDesc(wxGenericTimerImpl *t) :
 
61
        timer(t), running(false), next(NULL), prev(NULL),
 
62
        shotTime(0), deleteFlag(NULL) {}
 
63
 
 
64
    wxGenericTimerImpl  *timer;
 
65
    bool                running;
 
66
    wxTimerDesc         *next, *prev;
 
67
    wxTimerTick_t        shotTime;
 
68
    volatile bool       *deleteFlag; // see comment in ~wxTimer
 
69
};
 
70
 
 
71
class wxTimerScheduler
 
72
{
 
73
public:
 
74
    wxTimerScheduler() : m_timers(NULL) {}
 
75
 
 
76
    void QueueTimer(wxTimerDesc *desc, wxTimerTick_t when = 0);
 
77
    void RemoveTimer(wxTimerDesc *desc);
 
78
    void NotifyTimers();
 
79
 
 
80
private:
 
81
    wxTimerDesc *m_timers;
 
82
};
 
83
 
 
84
void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, wxTimerTick_t when)
 
85
{
 
86
    if ( desc->running )
 
87
        return; // already scheduled
 
88
 
 
89
    if ( when == 0 )
 
90
        when = GetMillisecondsTime() + desc->timer->GetInterval();
 
91
    desc->shotTime = when;
 
92
    desc->running = true;
 
93
 
 
94
    wxLogTrace( wxT("timer"),
 
95
                wxT("queued timer %p at tick %") wxTimerTickFmtSpec,
 
96
               desc->timer,  wxTimerTickPrintfArg(when));
 
97
 
 
98
    if ( m_timers )
 
99
    {
 
100
        wxTimerDesc *d = m_timers;
 
101
        while ( d->next && d->next->shotTime < when ) d = d->next;
 
102
        desc->next = d->next;
 
103
        desc->prev = d;
 
104
        if ( d->next )
 
105
            d->next->prev = desc;
 
106
        d->next = desc;
 
107
    }
 
108
    else
 
109
    {
 
110
        m_timers = desc;
 
111
        desc->prev = desc->next = NULL;
 
112
    }
 
113
}
 
114
 
 
115
void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
 
116
{
 
117
    desc->running = false;
 
118
    if ( desc == m_timers )
 
119
        m_timers = desc->next;
 
120
    if ( desc->prev )
 
121
        desc->prev->next = desc->next;
 
122
    if ( desc->next )
 
123
        desc->next->prev = desc->prev;
 
124
    desc->prev = desc->next = NULL;
 
125
}
 
126
 
 
127
void wxTimerScheduler::NotifyTimers()
 
128
{
 
129
    if ( m_timers )
 
130
    {
 
131
        bool oneShot;
 
132
        volatile bool timerDeleted;
 
133
        wxTimerTick_t now = GetMillisecondsTime();
 
134
 
 
135
        for ( wxTimerDesc *desc = m_timers; desc; desc = desc->next )
 
136
        {
 
137
            if ( desc->running && wxTickGreaterEqual(now, desc->shotTime) )
 
138
            {
 
139
                oneShot = desc->timer->IsOneShot();
 
140
                RemoveTimer(desc);
 
141
 
 
142
                timerDeleted = false;
 
143
                desc->deleteFlag = &timerDeleted;
 
144
                desc->timer->Notify();
 
145
 
 
146
                if ( !timerDeleted )
 
147
                {
 
148
                    wxLogTrace( wxT("timer"),
 
149
                                wxT("notified timer %p sheduled for %")
 
150
                                wxTimerTickFmtSpec,
 
151
                                desc->timer,
 
152
                                wxTimerTickPrintfArg(desc->shotTime) );
 
153
 
 
154
                    desc->deleteFlag = NULL;
 
155
                    if ( !oneShot )
 
156
                        QueueTimer(desc, now + desc->timer->GetInterval());
 
157
                }
 
158
                else
 
159
                {
 
160
                    desc = m_timers;
 
161
                    if (!desc)
 
162
                        break;
 
163
                }
 
164
            }
 
165
        }
 
166
    }
 
167
}
 
168
 
 
169
 
 
170
// ----------------------------------------------------------------------------
 
171
// wxTimer
 
172
// ----------------------------------------------------------------------------
 
173
 
 
174
wxTimerScheduler *gs_scheduler = NULL;
 
175
 
 
176
void wxGenericTimerImpl::Init()
 
177
{
 
178
    if ( !gs_scheduler )
 
179
        gs_scheduler = new wxTimerScheduler;
 
180
    m_desc = new wxTimerDesc(this);
 
181
}
 
182
 
 
183
wxGenericTimerImpl::~wxGenericTimerImpl()
 
184
{
 
185
    wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this);
 
186
    if ( IsRunning() )
 
187
        Stop();
 
188
 
 
189
    // NB: this is a hack: wxTimerScheduler must have some way of knowing
 
190
    //     that wxTimer object was deleted under its hands -- this may
 
191
    //     happen if somebody is really nasty and deletes the timer
 
192
    //     from wxTimer::Notify()
 
193
    if ( m_desc->deleteFlag != NULL )
 
194
        *m_desc->deleteFlag = true;
 
195
 
 
196
    delete m_desc;
 
197
    wxLogTrace( wxT("timer"), wxT("    ...done destroying timer %p..."), this);
 
198
}
 
199
 
 
200
bool wxGenericTimerImpl::IsRunning() const
 
201
{
 
202
    return m_desc->running;
 
203
}
 
204
 
 
205
bool wxGenericTimerImpl::Start(int millisecs, bool oneShot)
 
206
{
 
207
    wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"),
 
208
               this, millisecs, oneShot);
 
209
 
 
210
     if ( !wxTimerImpl::Start(millisecs, oneShot) )
 
211
         return false;
 
212
 
 
213
    gs_scheduler->QueueTimer(m_desc);
 
214
    return true;
 
215
}
 
216
 
 
217
void wxGenericTimerImpl::Stop()
 
218
{
 
219
    if ( !m_desc->running ) return;
 
220
 
 
221
    gs_scheduler->RemoveTimer(m_desc);
 
222
}
 
223
 
 
224
/*static*/ void wxGenericTimerImpl::NotifyTimers()
 
225
{
 
226
    if ( gs_scheduler )
 
227
        gs_scheduler->NotifyTimers();
 
228
}
 
229
 
 
230
 
 
231
 
 
232
// A module to deallocate memory properly:
 
233
class wxTimerModule: public wxModule
 
234
{
 
235
DECLARE_DYNAMIC_CLASS(wxTimerModule)
 
236
public:
 
237
    wxTimerModule() {}
 
238
    bool OnInit() { return true; }
 
239
    void OnExit() { wxDELETE(gs_scheduler); }
 
240
};
 
241
 
 
242
IMPLEMENT_DYNAMIC_CLASS(wxTimerModule, wxModule)
 
243
 
 
244
// ----------------------------------------------------------------------------
 
245
// wxGUIAppTraits
 
246
// ----------------------------------------------------------------------------
 
247
 
 
248
wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
 
249
{
 
250
    return new wxGenericTimerImpl(timer);
 
251
}
 
252
 
 
253
 
 
254
#endif //wxUSE_TIMER