1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/ui/timers.h"
#include "gate/results.h"
#if defined(GATE_UI_WINAPI)
#include "gate/platforms.h"
#include "gate/ui/gateui_winapi.h"
static gate_bool_t gate_ui_timer_events(void* hwnd, gate_ui_ctrl_t* ctrl, gate_uint32_t msg, gate_uintptr_t wParam, gate_intptr_t lParam, gate_intptr_t* lresult)
{
HWND hwndCtrl;
gate_ui_timer_t* timer;<--- The scope of the variable 'timer' can be reduced. [+]The scope of the variable 'timer' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
if (ctrl != NULL)
{
hwndCtrl = GATE_UI_WINAPI_GET_HWND(ctrl);
if (hwnd == hwndCtrl)
{
timer = (gate_ui_timer_t*)ctrl;
switch (msg)
{
case WM_TIMER:
{
if (timer->on_interval != NULL)
{
timer->on_interval(ctrl);
}
*lresult = 0;
return true;
break;<--- Consecutive return, break, continue, goto or throw statements are unnecessary. [+]Consecutive return, break, continue, goto or throw statements are unnecessary. The second statement can never be executed, and so should be removed.
}
}
}
}
return false;
}
gate_result_t gate_ui_timer_create(gate_ui_timer_t* timer, gate_ui_host_t* host, void* userparam)
{
gate_result_t ret;
gate_uint32_t styles, exstyles;
gate_ui_position_t position;
position.pos.x = 0;
position.pos.y = 0;
position.size.width = 1;
position.size.height = 1;
gate_mem_clear(timer, sizeof(gate_ui_timer_t));
exstyles = 0;
styles = WS_DISABLED;
/*styles |= WS_GROUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;*/
#if !defined(GATE_SYS_WINCE)
/*styles |= (WS_OVERLAPPED | WS_CAPTION);*/
#endif
ret = gate_ui_winapi_create(&timer->ctrl, host, NULL, NULL, &position, styles, exstyles, NULL, userparam, false);
if (GATE_SUCCEEDED(ret))
{
gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&timer->ctrl), WM_TIMER, &gate_ui_timer_events, &timer->ctrl);
}
return ret;
}
#ifndef USER_TIMER_MINIMUM
#define USER_TIMER_MINIMUM 0x0a
#endif
#ifndef USER_TIMER_MAXIMUM
#define USER_TIMER_MAXIMUM 0x7FFFFFFF
#endif
gate_result_t gate_ui_timer_start(gate_ui_timer_t* timer, gate_uint32_t interval)
{
HWND hwnd = GATE_UI_WINAPI_GET_HWND(&timer->ctrl);
if (interval < USER_TIMER_MINIMUM) interval = USER_TIMER_MINIMUM;
if (interval > USER_TIMER_MAXIMUM) interval = USER_TIMER_MAXIMUM;
EnableWindow(hwnd, TRUE);
if (0 != SetTimer(hwnd, 1, interval, NULL))
{
return GATE_RESULT_OK;
}
else
{
EnableWindow(hwnd, FALSE);
return GATE_RESULT_FAILED;
}
}
gate_bool_t gate_ui_timer_is_started(gate_ui_timer_t* timer)
{
HWND hwnd = GATE_UI_WINAPI_GET_HWND(&timer->ctrl);
return IsWindowEnabled(hwnd) == FALSE ? false : true;
}
gate_result_t gate_ui_timer_stop(gate_ui_timer_t* timer)
{
HWND hwnd = GATE_UI_WINAPI_GET_HWND(&timer->ctrl);
if (KillTimer(hwnd, 1))
{
EnableWindow(hwnd, FALSE);
return GATE_RESULT_OK;
}
else
{
return GATE_RESULT_FAILED;
}
}
#endif /* GATE_UI_WINAPI */
#if defined(GATE_UI_GTK)
#include "gate/ui/gateui_gtk.h"
typedef struct gate_ui_gtk_timer_param
{
gate_ui_timer_t* timer;
gate_bool_t started;
} gate_ui_gtk_timer_param_t;
static gboolean gate_ui_gtk_timer_callback(gpointer user_data)
{
gate_ui_gtk_timer_param_t* param = (gate_ui_gtk_timer_param_t*)user_data;
if (!param)
{
return FALSE;
}
if (param->started)
{
if (NULL != param->timer)
{
if (NULL != param->timer->on_interval)
{
param->timer->on_interval(¶m->timer->ctrl);
}
}
}
if (!param->started)
{
return FALSE;
}
else
{
return TRUE;
}
}
static gate_uint32_t gate_ui_gtk_timer_get_text_length(gate_ui_ctrl_t* ctrl)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t gate_ui_gtk_timer_get_text(gate_ui_ctrl_t* ctrl, gate_string_t* text)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t gate_ui_gtk_timer_set_text(gate_ui_ctrl_t* ctrl, gate_string_t const* text)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t gate_ui_gtk_timer_get_state(gate_ui_ctrl_t* ctrl, gate_int32_t* value)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_ui_gtk_timer_param_t* param = (gate_ui_gtk_timer_param_t*)GATE_UI_GTK_GET_CTRL_PRIVATE_ARG(ctrl);
if (param == NULL)
{
ret = GATE_RESULT_INVALIDSTATE;
}
else
{
if (value)
{
*value = param->started ? 1 : 0;
}
ret = GATE_RESULT_OK;
}
return ret;
}
static gate_result_t gate_ui_gtk_timer_set_state(gate_ui_ctrl_t* ctrl, gate_int32_t value)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t gate_ui_gtk_timer_refresh(gate_ui_ctrl_t* ctrl)
{
return GATE_RESULT_OK;
}
static gate_result_t gate_ui_gtk_timer_destroy(gate_ui_ctrl_t* ctrl)
{
gate_ui_gtk_timer_param_t* param = (gate_ui_gtk_timer_param_t*)GATE_UI_GTK_GET_CTRL_PRIVATE_ARG(ctrl);
if (param)
{
gate_mem_dealloc(param);
GATE_UI_GTK_SET_CTRL_PRIVATE_ARG(ctrl, NULL);
}
return GATE_RESULT_OK;
}
static gate_ui_gtk_dispatcher_t gate_ui_gtk_timer_dispatcher =
{
&gate_ui_gtk_timer_get_text_length,
&gate_ui_gtk_timer_get_text,
&gate_ui_gtk_timer_set_text,
&gate_ui_gtk_timer_get_state,
&gate_ui_gtk_timer_set_state,
&gate_ui_gtk_timer_refresh,
&gate_ui_gtk_timer_destroy
};
gate_result_t gate_ui_timer_create(
gate_ui_timer_t* timer, gate_ui_host_t* host,
void* userparam
)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_ui_gtk_timer_param_t* param = (gate_ui_gtk_timer_param_t*)gate_mem_alloc(sizeof(gate_ui_gtk_timer_param_t));
do
{
if (param == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
param->timer = timer;
param->started = false;
gate_mem_clear(timer, sizeof(gate_ui_timer_t));
GATE_UI_GTK_SET_CTRL_WIDGET_DIRECT(&timer->ctrl, GATE_UI_GTK_NO_WIDGET);
GATE_UI_GTK_SET_CTRL_HOST(&timer->ctrl, host);
GATE_UI_GTK_SET_CTRL_DISP(&timer->ctrl, &gate_ui_gtk_timer_dispatcher);
GATE_UI_GTK_SET_CTRL_USER_PARAM(&timer->ctrl, userparam);
GATE_UI_GTK_SET_CTRL_CONTAINER(&timer->ctrl, NULL);
GATE_UI_GTK_SET_CTRL_PARENT(&timer->ctrl, NULL);
GATE_UI_GTK_SET_CTRL_PRIVATE_ARG(&timer->ctrl, param);
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_ui_timer_start(gate_ui_timer_t* timer, gate_uint32_t interval)
{
gate_result_t ret;
gate_ui_gtk_timer_param_t* param = (gate_ui_gtk_timer_param_t*)GATE_UI_GTK_GET_CTRL_PRIVATE_ARG(&timer->ctrl);
if (param == NULL)
{
ret = GATE_RESULT_INVALIDSTATE;
}
else
{
param->started = true;
g_timeout_add(interval, &gate_ui_gtk_timer_callback, param);
ret = GATE_RESULT_OK;
}
return ret;
}
gate_result_t gate_ui_timer_stop(gate_ui_timer_t* timer)
{
gate_result_t ret;
gate_ui_gtk_timer_param_t* param = (gate_ui_gtk_timer_param_t*)GATE_UI_GTK_GET_CTRL_PRIVATE_ARG(&timer->ctrl);
if (param == NULL)
{
ret = GATE_RESULT_INVALIDSTATE;
}
else
{
param->started = false;
GATE_UI_GTK_SET_CTRL_PRIVATE_ARG(&timer->ctrl, NULL);
ret = GATE_RESULT_OK;
}
return ret;
}
gate_bool_t gate_ui_timer_is_started(gate_ui_timer_t* timer)
{
gate_ui_gtk_timer_param_t* param = (gate_ui_gtk_timer_param_t*)GATE_UI_GTK_GET_CTRL_PRIVATE_ARG(&timer->ctrl);
if (param == NULL)
{
return false;
}
else
{
return param->started;
}
}
#endif /* GATE_UI_GTK */
#if defined(GATE_UI_MOTIF)
#include "gate/ui/gateui_motif.h"
gate_result_t gate_ui_timer_create(gate_ui_timer_t* timer, gate_ui_host_t* host, void* userparam)
{
Widget w = (Widget)(gate_intptr_t)-1;
GATE_UI_MOTIF_SET_CTRL_HOST(&timer->ctrl, host);
GATE_UI_MOTIF_SET_CTRL_WIDGET(&timer->ctrl, w);
GATE_UI_MOTIF_SET_CTRL_CONTAINER(&timer->ctrl, NULL);
GATE_UI_MOTIF_SET_CTRL_USER_PARAM(&timer->ctrl, userparam);
GATE_UI_MOTIF_SET_CTRL_PARENT(&timer->ctrl, NULL);
GATE_UI_MOTIF_SET_CTRL_DISP(&timer->ctrl, NULL);
GATE_UI_MOTIF_SET_CTRL_PRIVATE_ARG(&timer->ctrl, NULL);
return GATE_RESULT_OK;
}
typedef struct gate_ui_motif_timer_info_class
{
gate_ui_timer_t* ctrl;
XtIntervalId id;
gate_uint32_t interval;
} gate_ui_motif_timer_info_t;
static void gate_ui_timer_callback(XtPointer client_arg, XtIntervalId* id)
{
gate_ui_motif_timer_info_t* ptr_timerinfo = (gate_ui_motif_timer_info_t*)client_arg;
gate_ui_timer_t* ptr_timer = NULL;
unsigned long next_interval = 0;
XtIntervalId next_id;
gate_ui_host_t* ptr_host;
XtAppContext app_context;
if (!ptr_timerinfo)
{
return;
}
ptr_timer = ptr_timerinfo->ctrl;
next_interval = (unsigned long)ptr_timerinfo->interval;
if (!ptr_timer)
{
gate_mem_dealloc(ptr_timerinfo);
return;
}
ptr_host = GATE_UI_MOTIF_GET_CTRL_HOST(&ptr_timer->ctrl);
if (!ptr_host)
{
gate_mem_dealloc(ptr_timerinfo);
return;
}
app_context = (XtAppContext)GATE_UI_MOTIF_GET_HOST_APPHANDLE(ptr_host);
ptr_timer->on_interval(&ptr_timer->ctrl);
next_id = XtAppAddTimeOut(app_context, next_interval, &gate_ui_timer_callback, (XtPointer)ptr_timerinfo);
ptr_timerinfo->id = next_id;
}
gate_result_t gate_ui_timer_start(gate_ui_timer_t* timer, gate_uint32_t interval)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_ui_host_t* ptr_host;<--- The scope of the variable 'ptr_host' can be reduced. [+]The scope of the variable 'ptr_host' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
XtAppContext app_context;
XtIntervalId id;
gate_ui_motif_timer_info_t* ptr_timerinfo = NULL;
do
{
ptr_host = GATE_UI_MOTIF_GET_CTRL_HOST(&timer->ctrl);
app_context = (XtAppContext)GATE_UI_MOTIF_GET_HOST_APPHANDLE(ptr_host);
ptr_timerinfo = gate_mem_alloc(sizeof(gate_ui_motif_timer_info_t));
if (NULL == ptr_timerinfo)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ptr_timerinfo->ctrl = timer;
ptr_timerinfo->interval = interval;
id = XtAppAddTimeOut(app_context, interval, &gate_ui_timer_callback, (XtPointer)ptr_timerinfo);
ptr_timerinfo->id = id;
GATE_UI_MOTIF_SET_CTRL_PRIVATE_ARG(&timer->ctrl, ptr_timerinfo);
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_ui_timer_stop(gate_ui_timer_t* timer)
{
gate_ui_motif_timer_info_t* ptr_timerinfo = (gate_ui_motif_timer_info_t*)GATE_UI_MOTIF_GET_CTRL_PRIVATE_ARG(&timer->ctrl);
XtIntervalId id = (XtIntervalId)(void*)GATE_UI_MOTIF_GET_CTRL_WIDGET(&timer->ctrl);
if (ptr_timerinfo)
{
GATE_UI_MOTIF_SET_CTRL_PRIVATE_ARG(&timer->ctrl, NULL);
XtRemoveTimeOut(ptr_timerinfo->id);
gate_mem_dealloc(ptr_timerinfo);
return GATE_RESULT_OK;
}
return GATE_RESULT_INVALIDSTATE;
}
gate_bool_t gate_ui_timer_is_started(gate_ui_timer_t* timer)
{
gate_ui_motif_timer_info_t* ptr_timerinfo = (gate_ui_motif_timer_info_t*)GATE_UI_MOTIF_GET_CTRL_PRIVATE_ARG(&timer->ctrl);
if (ptr_timerinfo)
{
return true;
}
return false;
}
#endif /* GATE_UI_MOTIF */
|