comparison ios/dw.m @ 2584:2acc7ba5dea0

Add HTIMER type and change dw_timer_() functions to use it. On existing platforms, HTIMER will be int for now allowing API backward compatibility. On new platforms, iOS and Android, these will be pointers to the timer objects. This allows simplifying the code paths on those platforms. May change it on other platforms such as Mac in a future major version.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 23 May 2021 21:39:25 +0000
parents 1d2f5c4eccc5
children 461006160d11
comparison
equal deleted inserted replaced
2583:1d2f5c4eccc5 2584:2acc7ba5dea0
9359 if(window1 && window2 && window1 == window2) 9359 if(window1 && window2 && window1 == window2)
9360 return TRUE; 9360 return TRUE;
9361 return FALSE; 9361 return FALSE;
9362 } 9362 }
9363 9363
9364 #define DW_TIMER_MAX 64
9365 static NSTimer *DWTimers[DW_TIMER_MAX];
9366
9367 /* 9364 /*
9368 * Add a callback to a timer event. 9365 * Add a callback to a timer event.
9369 * Parameters: 9366 * Parameters:
9370 * interval: Milliseconds to delay between calls. 9367 * interval: Milliseconds to delay between calls.
9371 * sigfunc: The pointer to the function to be used as the callback. 9368 * sigfunc: The pointer to the function to be used as the callback.
9372 * data: User data to be passed to the handler function. 9369 * data: User data to be passed to the handler function.
9373 * Returns: 9370 * Returns:
9374 * Timer ID for use with dw_timer_disconnect(), 0 on error. 9371 * Timer ID for use with dw_timer_disconnect(), 0 on error.
9375 */ 9372 */
9376 DW_FUNCTION_DEFINITION(dw_timer_connect, int, int interval, void *sigfunc, void *data) 9373 DW_FUNCTION_DEFINITION(dw_timer_connect, HTIMER, int interval, void *sigfunc, void *data)
9377 DW_FUNCTION_ADD_PARAM3(interval, sigfunc, data) 9374 DW_FUNCTION_ADD_PARAM3(interval, sigfunc, data)
9378 DW_FUNCTION_RETURN(dw_timer_connect, int) 9375 DW_FUNCTION_RETURN(dw_timer_connect, HTIMER)
9379 DW_FUNCTION_RESTORE_PARAM3(interval, int, sigfunc, void *, data, void *) 9376 DW_FUNCTION_RESTORE_PARAM3(interval, int, sigfunc, void *, data, void *)
9380 { 9377 {
9381 int z, retval = 0; 9378 HTIMER retval = 0;
9382 9379
9383 for(z=0;z<DW_TIMER_MAX;z++) 9380 if(sigfunc)
9384 {
9385 if(!DWTimers[z])
9386 {
9387 break;
9388 }
9389 }
9390
9391 if(sigfunc && !DWTimers[z])
9392 { 9381 {
9393 NSTimeInterval seconds = (double)interval / 1000.0; 9382 NSTimeInterval seconds = (double)interval / 1000.0;
9394 NSTimer *thistimer = DWTimers[z] = [NSTimer scheduledTimerWithTimeInterval:seconds target:DWHandler selector:@selector(runTimer:) userInfo:nil repeats:YES]; 9383
9395 _dw_new_signal(0, thistimer, z+1, sigfunc, NULL, data); 9384 retval = [NSTimer scheduledTimerWithTimeInterval:seconds target:DWHandler selector:@selector(runTimer:) userInfo:nil repeats:YES];
9396 retval = z+1; 9385 _dw_new_signal(0, retval, 0, sigfunc, NULL, data);
9397 } 9386 }
9398 DW_FUNCTION_RETURN_THIS(retval); 9387 DW_FUNCTION_RETURN_THIS(retval);
9399 } 9388 }
9400 9389
9401 /* 9390 /*
9402 * Removes timer callback. 9391 * Removes timer callback.
9403 * Parameters: 9392 * Parameters:
9404 * id: Timer ID returned by dw_timer_connect(). 9393 * id: Timer ID returned by dw_timer_connect().
9405 */ 9394 */
9406 DW_FUNCTION_DEFINITION(dw_timer_disconnect, void, int timerid) 9395 DW_FUNCTION_DEFINITION(dw_timer_disconnect, void, HTIMER timerid)
9407 DW_FUNCTION_ADD_PARAM1(timerid) 9396 DW_FUNCTION_ADD_PARAM1(timerid)
9408 DW_FUNCTION_NO_RETURN(dw_timer_disconnect) 9397 DW_FUNCTION_NO_RETURN(dw_timer_disconnect)
9409 DW_FUNCTION_RESTORE_PARAM1(timerid, int) 9398 DW_FUNCTION_RESTORE_PARAM1(timerid, HTIMER)
9410 { 9399 {
9411 SignalHandler *prev = NULL, *tmp = DWRoot; 9400 SignalHandler *prev = NULL, *tmp = DWRoot;
9412 NSTimer *thistimer;
9413 9401
9414 /* 0 is an invalid timer ID */ 9402 /* 0 is an invalid timer ID */
9415 if(timerid > 0 && timerid < DW_TIMER_MAX && DWTimers[timerid-1]) 9403 if(timerid)
9416 { 9404 {
9417 thistimer = DWTimers[timerid-1]; 9405 NSTimer *thistimer = timerid;
9418 DWTimers[timerid-1] = nil;
9419 9406
9420 [thistimer invalidate]; 9407 [thistimer invalidate];
9421 9408
9422 while(tmp) 9409 while(tmp)
9423 { 9410 {
9424 if(tmp->id == timerid && tmp->window == thistimer) 9411 if(tmp->window == thistimer)
9425 { 9412 {
9426 if(prev) 9413 if(prev)
9427 { 9414 {
9428 prev->next = tmp->next; 9415 prev->next = tmp->next;
9429 free(tmp); 9416 free(tmp);