diff options
Diffstat (limited to 'src/periodic.c')
-rw-r--r-- | src/periodic.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/periodic.c b/src/periodic.c index ef3d8033..b4f51ed3 100644 --- a/src/periodic.c +++ b/src/periodic.c | |||
@@ -57,6 +57,25 @@ iDefineTypeConstructionArgs(PeriodicCommand, (iAny *ctx, const char *cmd), ctx, | |||
57 | 57 | ||
58 | static const uint32_t postingInterval_Periodic_ = 500; | 58 | static const uint32_t postingInterval_Periodic_ = 500; |
59 | 59 | ||
60 | static uint32_t postEvent_Periodic_(uint32_t interval, void *context) { | ||
61 | iUnused(context); | ||
62 | SDL_UserEvent ev = { .type = SDL_USEREVENT, | ||
63 | .timestamp = SDL_GetTicks(), | ||
64 | .code = periodic_UserEventCode }; | ||
65 | SDL_PushEvent((SDL_Event *) &ev); | ||
66 | return interval; | ||
67 | } | ||
68 | |||
69 | static void startOrStopWakeupTimer_Periodic_(iPeriodic *d, iBool start) { | ||
70 | if (start && !d->wakeupTimer) { | ||
71 | d->wakeupTimer = SDL_AddTimer(postingInterval_Periodic_, postEvent_Periodic_, d); | ||
72 | } | ||
73 | else if (!start && d->wakeupTimer) { | ||
74 | SDL_RemoveTimer(d->wakeupTimer); | ||
75 | d->wakeupTimer = 0; | ||
76 | } | ||
77 | } | ||
78 | |||
60 | static void removePending_Periodic_(iPeriodic *d) { | 79 | static void removePending_Periodic_(iPeriodic *d) { |
61 | iForEach(PtrSet, i, &d->pendingRemoval) { | 80 | iForEach(PtrSet, i, &d->pendingRemoval) { |
62 | size_t pos; | 81 | size_t pos; |
@@ -68,6 +87,9 @@ static void removePending_Periodic_(iPeriodic *d) { | |||
68 | } | 87 | } |
69 | } | 88 | } |
70 | clear_PtrSet(&d->pendingRemoval); | 89 | clear_PtrSet(&d->pendingRemoval); |
90 | if (isEmpty_SortedArray(&d->commands)) { | ||
91 | startOrStopWakeupTimer_Periodic_(d, iFalse); | ||
92 | } | ||
71 | } | 93 | } |
72 | 94 | ||
73 | static iBool isDispatching_; | 95 | static iBool isDispatching_; |
@@ -109,9 +131,11 @@ void init_Periodic(iPeriodic *d) { | |||
109 | init_SortedArray(&d->commands, sizeof(iPeriodicCommand), cmp_PeriodicCommand_); | 131 | init_SortedArray(&d->commands, sizeof(iPeriodicCommand), cmp_PeriodicCommand_); |
110 | d->lastPostTime = 0; | 132 | d->lastPostTime = 0; |
111 | init_PtrSet(&d->pendingRemoval); | 133 | init_PtrSet(&d->pendingRemoval); |
134 | d->wakeupTimer = 0; | ||
112 | } | 135 | } |
113 | 136 | ||
114 | void deinit_Periodic(iPeriodic *d) { | 137 | void deinit_Periodic(iPeriodic *d) { |
138 | startOrStopWakeupTimer_Periodic_(d, iFalse); | ||
115 | deinit_PtrSet(&d->pendingRemoval); | 139 | deinit_PtrSet(&d->pendingRemoval); |
116 | iForEach(Array, i, &d->commands.values) { | 140 | iForEach(Array, i, &d->commands.values) { |
117 | deinit_PeriodicCommand(i.value); | 141 | deinit_PeriodicCommand(i.value); |
@@ -121,6 +145,7 @@ void deinit_Periodic(iPeriodic *d) { | |||
121 | } | 145 | } |
122 | 146 | ||
123 | void add_Periodic(iPeriodic *d, iAny *context, const char *command) { | 147 | void add_Periodic(iPeriodic *d, iAny *context, const char *command) { |
148 | iAssert(isInstance_Object(context, &Class_Widget)); | ||
124 | lock_Mutex(d->mutex); | 149 | lock_Mutex(d->mutex); |
125 | size_t pos; | 150 | size_t pos; |
126 | iPeriodicCommand key = { .context = context }; | 151 | iPeriodicCommand key = { .context = context }; |
@@ -133,6 +158,7 @@ void add_Periodic(iPeriodic *d, iAny *context, const char *command) { | |||
133 | init_PeriodicCommand(&pc, context, command); | 158 | init_PeriodicCommand(&pc, context, command); |
134 | insert_SortedArray(&d->commands, &pc); | 159 | insert_SortedArray(&d->commands, &pc); |
135 | } | 160 | } |
161 | startOrStopWakeupTimer_Periodic_(d, iTrue); | ||
136 | unlock_Mutex(d->mutex); | 162 | unlock_Mutex(d->mutex); |
137 | } | 163 | } |
138 | 164 | ||