diff options
-rw-r--r-- | src/app.c | 27 | ||||
-rw-r--r-- | src/app.h | 2 | ||||
-rw-r--r-- | src/ui/window.c | 10 |
3 files changed, 32 insertions, 7 deletions
@@ -156,18 +156,21 @@ const iString *execPath_App(void) { | |||
156 | void processEvents_App(void) { | 156 | void processEvents_App(void) { |
157 | iApp *d = &app_; | 157 | iApp *d = &app_; |
158 | SDL_Event ev; | 158 | SDL_Event ev; |
159 | while (SDL_PollEvent(&ev)) { | 159 | while (SDL_WaitEvent(&ev)) { |
160 | switch (ev.type) { | 160 | switch (ev.type) { |
161 | case SDL_QUIT: | 161 | case SDL_QUIT: |
162 | // if (isModified_Song(d->song)) { | 162 | // if (isModified_Song(d->song)) { |
163 | // save_App_(d, autosavePath_App_(d)); | 163 | // save_App_(d, autosavePath_App_(d)); |
164 | // } | 164 | // } |
165 | d->running = iFalse; | 165 | d->running = iFalse; |
166 | break; | 166 | goto backToMainLoop; |
167 | case SDL_DROPFILE: | 167 | case SDL_DROPFILE: |
168 | postCommandf_App("open url:file://%s", ev.drop.file); | 168 | postCommandf_App("open url:file://%s", ev.drop.file); |
169 | break; | 169 | break; |
170 | default: { | 170 | default: { |
171 | if (ev.type == SDL_USEREVENT && ev.user.code == refresh_UserEventCode) { | ||
172 | goto backToMainLoop; | ||
173 | } | ||
171 | iBool wasUsed = processEvent_Window(d->window, &ev); | 174 | iBool wasUsed = processEvent_Window(d->window, &ev); |
172 | if (ev.type == SDL_USEREVENT && ev.user.code == command_UserEventCode) { | 175 | if (ev.type == SDL_USEREVENT && ev.user.code == command_UserEventCode) { |
173 | #if defined (iPlatformApple) && !defined (iPlatformIOS) | 176 | #if defined (iPlatformApple) && !defined (iPlatformIOS) |
@@ -180,19 +183,23 @@ void processEvents_App(void) { | |||
180 | /* No widget handled the command, so we'll do it. */ | 183 | /* No widget handled the command, so we'll do it. */ |
181 | handleCommand_App(ev.user.data1); | 184 | handleCommand_App(ev.user.data1); |
182 | } | 185 | } |
183 | /* Allocated by postCommand_App(). */ | 186 | /* Allocated by postCommand_Apps(). */ |
184 | free(ev.user.data1); | 187 | free(ev.user.data1); |
185 | } | 188 | } |
186 | break; | 189 | break; |
187 | } | 190 | } |
188 | } | 191 | } |
189 | } | 192 | } |
193 | backToMainLoop:; | ||
190 | } | 194 | } |
191 | 195 | ||
192 | static void runTickers_App_(iApp *d) { | 196 | static void runTickers_App_(iApp *d) { |
193 | /* Tickers may add themselves again, so we'll run off a copy. */ | 197 | /* Tickers may add themselves again, so we'll run off a copy. */ |
194 | iSortedArray *pending = copy_SortedArray(&d->tickers); | 198 | iSortedArray *pending = copy_SortedArray(&d->tickers); |
195 | clear_SortedArray(&d->tickers); | 199 | clear_SortedArray(&d->tickers); |
200 | if (!isEmpty_SortedArray(pending)) { | ||
201 | postRefresh_App(); | ||
202 | } | ||
196 | iConstForEach(Array, i, &pending->values) { | 203 | iConstForEach(Array, i, &pending->values) { |
197 | const iTicker *ticker = i.value; | 204 | const iTicker *ticker = i.value; |
198 | if (ticker->callback) { | 205 | if (ticker->callback) { |
@@ -208,8 +215,7 @@ static int run_App_(iApp *d) { | |||
208 | SDL_EventState(SDL_DROPFILE, SDL_ENABLE); /* open files via drag'n'drop */ | 215 | SDL_EventState(SDL_DROPFILE, SDL_ENABLE); /* open files via drag'n'drop */ |
209 | while (d->running) { | 216 | while (d->running) { |
210 | runTickers_App_(d); | 217 | runTickers_App_(d); |
211 | processEvents_App(); | 218 | processEvents_App(); /* may wait here for a while */ |
212 | destroyPending_Widget(); | ||
213 | refresh_App(); | 219 | refresh_App(); |
214 | } | 220 | } |
215 | return 0; | 221 | return 0; |
@@ -217,6 +223,7 @@ static int run_App_(iApp *d) { | |||
217 | 223 | ||
218 | void refresh_App(void) { | 224 | void refresh_App(void) { |
219 | iApp *d = &app_; | 225 | iApp *d = &app_; |
226 | destroyPending_Widget(); | ||
220 | draw_Window(d->window); | 227 | draw_Window(d->window); |
221 | recycle_Garbage(); | 228 | recycle_Garbage(); |
222 | } | 229 | } |
@@ -228,6 +235,16 @@ int run_App(int argc, char **argv) { | |||
228 | return rc; | 235 | return rc; |
229 | } | 236 | } |
230 | 237 | ||
238 | void postRefresh_App(void) { | ||
239 | SDL_Event ev; | ||
240 | ev.user.type = SDL_USEREVENT; | ||
241 | ev.user.code = refresh_UserEventCode; | ||
242 | ev.user.windowID = get_Window() ? SDL_GetWindowID(get_Window()->win) : 0; | ||
243 | ev.user.data1 = NULL; | ||
244 | ev.user.data2 = NULL; | ||
245 | SDL_PushEvent(&ev); | ||
246 | } | ||
247 | |||
231 | void postCommand_App(const char *command) { | 248 | void postCommand_App(const char *command) { |
232 | SDL_Event ev; | 249 | SDL_Event ev; |
233 | ev.user.type = SDL_USEREVENT; | 250 | ev.user.type = SDL_USEREVENT; |
@@ -8,6 +8,7 @@ iDeclareType(Window) | |||
8 | 8 | ||
9 | enum iUserEventCode { | 9 | enum iUserEventCode { |
10 | command_UserEventCode = 1, | 10 | command_UserEventCode = 1, |
11 | refresh_UserEventCode = 2, | ||
11 | }; | 12 | }; |
12 | 13 | ||
13 | const iString *execPath_App (void); | 14 | const iString *execPath_App (void); |
@@ -19,6 +20,7 @@ void refresh_App (void); | |||
19 | iAny * findWidget_App (const char *id); | 20 | iAny * findWidget_App (const char *id); |
20 | void addTicker_App (void (*ticker)(iAny *), iAny *context); | 21 | void addTicker_App (void (*ticker)(iAny *), iAny *context); |
21 | 22 | ||
23 | void postRefresh_App (void); | ||
22 | void postCommand_App (const char *command); | 24 | void postCommand_App (const char *command); |
23 | void postCommandf_App (const char *command, ...); | 25 | void postCommandf_App (const char *command, ...); |
24 | 26 | ||
diff --git a/src/ui/window.c b/src/ui/window.c index 8236bf69..c7ccdeb6 100644 --- a/src/ui/window.c +++ b/src/ui/window.c | |||
@@ -236,6 +236,7 @@ static void updateRootSize_Window_(iWindow *d) { | |||
236 | SDL_GetRendererOutputSize(d->render, &size->x, &size->y); | 236 | SDL_GetRendererOutputSize(d->render, &size->x, &size->y); |
237 | arrange_Widget(d->root); | 237 | arrange_Widget(d->root); |
238 | postCommandf_App("window.resized width:%d height:%d", size->x, size->y); | 238 | postCommandf_App("window.resized width:%d height:%d", size->x, size->y); |
239 | postRefresh_App(); | ||
239 | } | 240 | } |
240 | 241 | ||
241 | static float pixelRatio_Window_(const iWindow *d) { | 242 | static float pixelRatio_Window_(const iWindow *d) { |
@@ -260,7 +261,6 @@ void init_Window(iWindow *d) { | |||
260 | } | 261 | } |
261 | SDL_SetWindowMinimumSize(d->win, 640, 480); | 262 | SDL_SetWindowMinimumSize(d->win, 640, 480); |
262 | SDL_SetWindowTitle(d->win, "Lagrange"); | 263 | SDL_SetWindowTitle(d->win, "Lagrange"); |
263 | SDL_ShowWindow(d->win); | ||
264 | /* Some info. */ { | 264 | /* Some info. */ { |
265 | SDL_RendererInfo info; | 265 | SDL_RendererInfo info; |
266 | SDL_GetRendererInfo(d->render, &info); | 266 | SDL_GetRendererInfo(d->render, &info); |
@@ -316,7 +316,11 @@ SDL_Renderer *renderer_Window(const iWindow *d) { | |||
316 | 316 | ||
317 | static iBool handleWindowEvent_Window_(iWindow *d, const SDL_WindowEvent *ev) { | 317 | static iBool handleWindowEvent_Window_(iWindow *d, const SDL_WindowEvent *ev) { |
318 | switch (ev->event) { | 318 | switch (ev->event) { |
319 | case SDL_WINDOWEVENT_MOVED: | ||
320 | /* No need to do anything. */ | ||
321 | return iTrue; | ||
319 | case SDL_WINDOWEVENT_RESIZED: | 322 | case SDL_WINDOWEVENT_RESIZED: |
323 | case SDL_WINDOWEVENT_SIZE_CHANGED: | ||
320 | updateRootSize_Window_(d); | 324 | updateRootSize_Window_(d); |
321 | return iTrue; | 325 | return iTrue; |
322 | case SDL_WINDOWEVENT_LEAVE: | 326 | case SDL_WINDOWEVENT_LEAVE: |
@@ -359,6 +363,7 @@ iBool processEvent_Window(iWindow *d, const SDL_Event *ev) { | |||
359 | return iFalse; | 363 | return iFalse; |
360 | } | 364 | } |
361 | 365 | ||
366 | #if 0 | ||
362 | static void waitPresent_Window_(iWindow *d) { | 367 | static void waitPresent_Window_(iWindow *d) { |
363 | const double ticksPerFrame = 1000.0 / 60.0; | 368 | const double ticksPerFrame = 1000.0 / 60.0; |
364 | uint32_t nowTime = SDL_GetTicks(); | 369 | uint32_t nowTime = SDL_GetTicks(); |
@@ -375,6 +380,7 @@ static void waitPresent_Window_(iWindow *d) { | |||
375 | d->presentTime = nowTime; | 380 | d->presentTime = nowTime; |
376 | } | 381 | } |
377 | } | 382 | } |
383 | #endif | ||
378 | 384 | ||
379 | void draw_Window(iWindow *d) { | 385 | void draw_Window(iWindow *d) { |
380 | /* Clear the window. */ | 386 | /* Clear the window. */ |
@@ -392,7 +398,7 @@ void draw_Window(iWindow *d) { | |||
392 | SDL_RenderCopy(d->render, glyphCache_Text(), NULL, &rect); | 398 | SDL_RenderCopy(d->render, glyphCache_Text(), NULL, &rect); |
393 | } | 399 | } |
394 | #endif | 400 | #endif |
395 | waitPresent_Window_(d); | 401 | // waitPresent_Window_(d); |
396 | SDL_RenderPresent(d->render); | 402 | SDL_RenderPresent(d->render); |
397 | } | 403 | } |
398 | 404 | ||