Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
timer.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Here is where the extensions can get timed on when they load and
4 * unload. All of the timing is done in here.
5 *
6 * Authors:
7 * Ted Gould <ted@gould.cx>
8 *
9 * Copyright (C) 2004 Authors
10 *
11 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
12 */
13
14#include <glibmm/main.h>
15
16#include "extension.h"
17#include "timer.h"
18
19namespace Inkscape {
20namespace Extension {
21
22#define TIMER_SCALE_VALUE 20
23
24ExpirationTimer * ExpirationTimer::timer_list = nullptr;
25ExpirationTimer * ExpirationTimer::idle_start = nullptr;
28
38 locked(0),
39 extension(in_extension)
40{
41 /* Fix Me! */
42 if (timer_list == nullptr) {
43 next = this;
44 timer_list = this;
45 } else {
47 timer_list->next = this;
48 }
49
50 expiration = Glib::DateTime::create_now_utc().add_seconds(timeout);
51
52 if (!timer_started) {
53 Glib::signal_timeout().connect(sigc::ptr_fun(&timer_func), timeout * 1000 / TIMER_SCALE_VALUE);
54 timer_started = true;
55 }
56
57 return;
58}
59
68{
69 if (this != next) {
70 /* This will remove this entry from the circularly linked
71 list. */
72 ExpirationTimer * prev;
73 for (prev = timer_list;
74 prev->next != this;
75 prev = prev->next){};
76 prev->next = next;
77
78 if (idle_start == this)
80
81 /* This may occur more than you think, just because the guy
82 doing most of the deletions is the idle function, who tracks
83 where it is looking using the \c timer_list variable. */
84 if (timer_list == this)
86 } else {
87 /* If we're the only entry in the list, the list needs to go
88 to NULL */
89 timer_list = nullptr;
90 idle_start = nullptr;
91 }
92
93 return;
94}
95
105void
107{
108 auto const current = Glib::DateTime::create_now_utc();
109
110 auto time_left = expiration.difference(current);
111 if (time_left < 0) time_left = 0;
112 time_left /= 2;
113
114 expiration = current.add(time_left).add_seconds(timeout);
115 return;
116}
117
122bool
124{
125 if (locked > 0) return false;
126
127 auto const current = Glib::DateTime::create_now_utc();
128 return expiration.difference(current) < 0;
129}
130
131// int idle_cnt = 0;
132
146bool
148{
149 // std::cout << "Idle func pass: " << idle_cnt++ << " timer list: " << timer_list << std::endl;
150
151 /* see if this is the last */
152 if (timer_list == nullptr) {
153 timer_started = false;
154 return false;
155 }
156
157 /* evaluate current */
158 if (timer_list->expired()) {
160 }
161
162 /* see if this is the last */
163 if (timer_list == nullptr) {
164 timer_started = false;
165 return false;
166 }
167
168 if (timer_list->next == idle_start) {
169 /* if so, set up the timer and return FALSE */
170 /* Note: This may cause one to be missed on the evaluation if
171 the one before it expires and it is last in the list.
172 While this could be taken care of, it isn't worth the
173 complexity for this lazy removal that we're doing. It
174 should get picked up next time */
175 Glib::signal_timeout().connect(sigc::ptr_fun(&timer_func), timeout * 1000 / TIMER_SCALE_VALUE);
176 return false;
177 }
178
179 /* If nothing else, continue on */
181 return true;
182}
183
190bool
192{
193 // std::cout << "Timer func" << std::endl;
195 // idle_cnt = 0;
196 Glib::signal_idle().connect(sigc::ptr_fun(&idle_func));
197 return false;
198}
199
200}; }; /* namespace Inkscape, Extension */
201
202/*
203 Local Variables:
204 mode:c++
205 c-file-style:"stroustrup"
206 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
207 indent-tabs-mode:nil
208 fill-column:99
209 End:
210*/
211// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
static long timeout
What the current timeout is (in seconds)
Definition timer.h:32
static ExpirationTimer * timer_list
Circularly linked list of all timers.
Definition timer.h:28
int locked
Is this extension locked from being unloaded?
Definition timer.h:37
Glib::DateTime expiration
When this timer expires.
Definition timer.h:41
static bool timer_func()
A timer function to set up the idle function.
Definition timer.cpp:191
ExpirationTimer(Extension *in_extension)
Create a new expiration timer.
Definition timer.cpp:37
static bool timer_started
Has the timer been started?
Definition timer.h:34
virtual ~ExpirationTimer()
Deletes a ExpirationTimer.
Definition timer.cpp:67
Extension * extension
What extension this function relates to.
Definition timer.h:43
void touch()
Touches the timer to extend the length before it expires.
Definition timer.cpp:106
static bool idle_func()
This function goes in the idle loop to find expired extensions.
Definition timer.cpp:147
static ExpirationTimer * idle_start
Which timer was on top when we started the idle loop.
Definition timer.h:30
ExpirationTimer * next
Next entry in the list.
Definition timer.h:39
bool expired() const
Check to see if the timer has expired.
Definition timer.cpp:123
The object that is the basis for the Extension system.
Definition extension.h:133
@ STATE_UNLOADED
The extension has not been loaded.
Definition extension.h:138
void set_state(state_t in_state)
A function to set whether the extension should be loaded or unloaded.
static char const *const current
Definition dir-util.cpp:71
Inkscape::Extension::Extension: Frontend to certain, possibly pluggable, actions.
Helper class to stream background task notifications as a series of messages.