summaryrefslogtreecommitdiff
path: root/src/periodic.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-03-18 11:26:11 +0200
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-03-18 11:26:11 +0200
commitfa174461abdc5c33de16428109c7d46b4f150093 (patch)
tree21cf16a2426f8a6b76195e1816ceea69778fbd70 /src/periodic.c
parent21f6248a3dc906a0c296b937dd1aa697784e6ea3 (diff)
Scrollbar fading and periodic commands
Added a new mechanism to issue periodic but not per-frame commands. This is used for main-thread operations like checking if it's time to fade away the scrollbars. Scrollbars are faded away completely on Apple platforms. Adjusted list right margins accordingly.
Diffstat (limited to 'src/periodic.c')
-rw-r--r--src/periodic.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/src/periodic.c b/src/periodic.c
new file mode 100644
index 00000000..c840e63c
--- /dev/null
+++ b/src/periodic.c
@@ -0,0 +1,107 @@
1/* Copyright 2021 Jaakko Keränen <jaakko.keranen@iki.fi>
2
3Redistribution and use in source and binary forms, with or without
4modification, are permitted provided that the following conditions are met:
5
61. Redistributions of source code must retain the above copyright notice, this
7 list of conditions and the following disclaimer.
82. Redistributions in binary form must reproduce the above copyright notice,
9 this list of conditions and the following disclaimer in the documentation
10 and/or other materials provided with the distribution.
11
12THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
16ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
22
23#include "periodic.h"
24#include "app.h"
25
26#include <the_Foundation/string.h>
27#include <SDL_timer.h>
28
29iDeclareType(PeriodicCommand)
30
31struct Impl_PeriodicCommand {
32 iAny * context;
33 iString command;
34};
35
36static void init_PeriodicCommand(iPeriodicCommand *d, iAny *context, const char *command) {
37 d->context = context;
38 initCStr_String(&d->command, command);
39}
40
41static void deinit_PeriodicCommand(iPeriodicCommand *d) {
42 deinit_String(&d->command);
43}
44
45static int cmp_PeriodicCommand_(const void *a, const void *b) {
46 const iPeriodicCommand *elems[2] = { a, b };
47 return iCmp(elems[0]->context, elems[1]->context);
48}
49
50iDefineTypeConstructionArgs(PeriodicCommand, (iAny *ctx, const char *cmd), ctx, cmd)
51
52/*----------------------------------------------------------------------------------------------*/
53
54static uint32_t postCommands_Periodic_(uint32_t interval, void *param) {
55 iPeriodic *d = param;
56 lock_Mutex(d->mutex);
57 iConstForEach(Array, i, &d->commands.values) {
58 postCommandString_App(&((const iPeriodicCommand *) i.value)->command);
59 }
60 unlock_Mutex(d->mutex);
61 return interval;
62}
63
64void init_Periodic(iPeriodic *d) {
65 d->mutex = new_Mutex();
66 init_SortedArray(&d->commands, sizeof(iPeriodicCommand), cmp_PeriodicCommand_);
67 d->timer = SDL_AddTimer(500, postCommands_Periodic_, d);
68}
69
70void deinit_Periodic(iPeriodic *d) {
71 SDL_RemoveTimer(d->timer);
72 iGuardMutex(d->mutex, {
73 iForEach(Array, i, &d->commands.values) {
74 deinit_PeriodicCommand(i.value);
75 }
76 deinit_SortedArray(&d->commands);
77 });
78 delete_Mutex(d->mutex);
79}
80
81void add_Periodic(iPeriodic *d, iAny *context, const char *command) {
82 lock_Mutex(d->mutex);
83 size_t pos;
84 iPeriodicCommand key = { .context = context };
85 if (locate_SortedArray(&d->commands, &key, &pos)) {
86 iPeriodicCommand *pc = at_SortedArray(&d->commands, pos);
87 setCStr_String(&pc->command, command);
88 }
89 else {
90 iPeriodicCommand pc;
91 init_PeriodicCommand(&pc, context, command);
92 insert_SortedArray(&d->commands, &pc);
93 }
94 unlock_Mutex(d->mutex);
95}
96
97void remove_Periodic(iPeriodic *d, iAny *context) {
98 lock_Mutex(d->mutex);
99 size_t pos;
100 iPeriodicCommand key = { .context = context };
101 if (locate_SortedArray(&d->commands, &key, &pos)) {
102 iPeriodicCommand *pc = at_SortedArray(&d->commands, pos);
103 deinit_PeriodicCommand(pc);
104 remove_Array(&d->commands.values, pos);
105 }
106 unlock_Mutex(d->mutex);
107}