Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_reentrant.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
38#define ALLEGRO_UNSTABLE 1
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45#include "nilorea/n_common.h"
46#include "nilorea/n_log.h"
47#include "nilorea/n_gui.h"
48
49static int log_level = LOG_ERR;
50static int failures = 0;
51
52/* the button callback raises the popup window, reordering ctx->windows */
53static N_GUI_CTX* g_gui = NULL;
54static int g_popup_win = -1;
55static int g_click_count = 0;
56
57static void on_open_popup(int widget_id, void* user_data) {
58 (void)widget_id;
59 (void)user_data;
61 if (g_gui && g_popup_win >= 0) {
63 n_gui_raise_window(g_gui, g_popup_win); /* removes + re-pushes + re-sorts */
64 }
65}
66
67static void expect_true(const char* label, int cond) {
68 if (!cond) {
69 n_log(LOG_ERR, "%s: condition false", label);
70 failures++;
71 } else {
72 n_log(LOG_INFO, "%s: OK", label);
73 }
74}
75
76static int process_args(int argc, char** argv) {
77 int opt;
78 while ((opt = getopt(argc, argv, "V:")) != -1) {
79 switch (opt) {
80 case 'V':
81 if (!strcmp("LOG_NULL", optarg))
83 else if (!strcmp("LOG_NOTICE", optarg))
85 else if (!strcmp("LOG_INFO", optarg))
87 else if (!strcmp("LOG_ERR", optarg))
89 else if (!strcmp("LOG_DEBUG", optarg))
91 else
92 return -1;
93 break;
94 default:
95 return -1;
96 }
97 }
98 return 0;
99}
100
101/* Build a left-button mouse event at (x, y). */
102static ALLEGRO_EVENT mouse_event(int type, int x, int y) {
103 ALLEGRO_EVENT ev;
104 memset(&ev, 0, sizeof(ev));
105 ev.type = (unsigned int)type;
106 ev.mouse.x = x;
107 ev.mouse.y = y;
108 ev.mouse.button = 1;
109 return ev;
110}
111
112int main(int argc, char** argv) {
114 if (process_args(argc, argv) != 0) {
115 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
116 return 2;
117 }
119
120 if (!al_init()) {
121 n_log(LOG_ERR, "al_init failed");
122 return 1;
123 }
124 if (!al_init_font_addon()) {
125 n_log(LOG_ERR, "al_init_font_addon failed");
126 return 1;
127 }
128 /* Builtin font does not need a display. */
129 ALLEGRO_FONT* font = al_create_builtin_font();
130 if (!font) {
131 n_log(LOG_ERR, "al_create_builtin_font failed");
132 return 1;
133 }
134
135 N_GUI_CTX* gui = n_gui_new_ctx(font);
136 if (!gui) {
137 n_log(LOG_ERR, "n_gui_new_ctx failed");
138 al_destroy_font(font);
139 return 1;
140 }
141 g_gui = gui;
142 n_gui_set_display_size(gui, 800.0f, 600.0f);
143
144 /* Window A (frameless so the button's screen coords are trivial) holds the
145 button; the popup P sits clear of the button and after A in z-order, so
146 its node is the iterator's cached "next" when the click fires. */
147 int win_a = n_gui_add_window(gui, "A", 0.0f, 0.0f, 200.0f, 150.0f);
149 int btn = n_gui_add_button(gui, win_a, "Open", 10.0f, 10.0f, 80.0f, 30.0f, N_GUI_SHAPE_ROUNDED, on_open_popup, NULL);
150 n_gui_open_window(gui, win_a);
151
152 g_popup_win = n_gui_add_window(gui, "P", 300.0f, 0.0f, 200.0f, 150.0f);
155
156 expect_true("windows + button created", win_a >= 0 && g_popup_win >= 0 && btn >= 0);
157
158 /* Press then release over the button centre (50, 25). The release fires the
159 callback that raises P; before the fix this freed the cached "next" node
160 and the loop dereferenced it. */
161 {
162 ALLEGRO_EVENT down = mouse_event(ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 50, 25);
163 ALLEGRO_EVENT up = mouse_event(ALLEGRO_EVENT_MOUSE_BUTTON_UP, 50, 25);
165 n_gui_process_event(gui, up); /* must not crash */
166 }
167
168 expect_true("button callback fired exactly once", g_click_count == 1);
169 expect_true("popup is open after the click", n_gui_window_is_open(gui, g_popup_win));
170
171 /* A second identical click must also be safe and fire again. */
172 {
173 ALLEGRO_EVENT down = mouse_event(ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 50, 25);
174 ALLEGRO_EVENT up = mouse_event(ALLEGRO_EVENT_MOUSE_BUTTON_UP, 50, 25);
177 }
178 expect_true("button callback fired again", g_click_count == 2);
179
181 al_destroy_font(font);
182
183 if (failures > 0) {
184 n_log(LOG_ERR, "ex_gui_reentrant: %d check(s) failed", failures);
185 return 1;
186 }
187 n_log(LOG_NOTICE, "ex_gui_reentrant: all checks passed");
188 return 0;
189}
static int failures
int main(void)
void process_args(int argc, char **argv)
Definition ex_common.c:48
int log_level
Definition ex_fluid.c:60
static void expect_true(const char *label, int cond)
static N_GUI_CTX * gui
static ALLEGRO_EVENT mouse_event(int type, int x, int y)
static N_GUI_CTX * g_gui
static int g_click_count
static void on_open_popup(int widget_id, void *user_data)
static int g_popup_win
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
Set the display (viewport) size for global scrollbar computation.
Definition n_gui.c:9034
void n_gui_raise_window(N_GUI_CTX *ctx, int window_id)
Bring a window to the front (top of draw order).
Definition n_gui.c:1824
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
Definition n_gui.c:9836
void n_gui_window_set_zorder(N_GUI_CTX *ctx, int window_id, int z_mode, int z_value)
Set window z-order mode and value.
Definition n_gui.c:1903
#define N_GUI_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
Definition n_gui.h:233
int n_gui_window_is_open(N_GUI_CTX *ctx, int window_id)
Check if a window is currently visible.
Definition n_gui.c:1959
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
Definition n_gui.c:1063
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:903
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:995
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
Definition n_gui.c:1206
#define N_GUI_ZORDER_POPUP
popup/modal dialogs: drawn above EVERYTHING including ALWAYS_ON_TOP windows.
Definition n_gui.h:326
#define N_GUI_SHAPE_ROUNDED
rounded rectangle shape
Definition n_gui.h:149
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
Definition n_gui.c:1968
int n_gui_add_button(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int shape, void(*on_click)(int, void *), void *user_data)
Add a button widget to a window.
Definition n_gui.c:2124
The top-level GUI context that holds all windows.
Definition n_gui.h:1354
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.