fltk 1.5.22

Rust bindings for the FLTK GUI library
Documentation
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
use crate::app::{init::init_all, init::is_initialized, widget::windows};
use crate::prelude::*;
use fltk_sys::fl;
use std::{mem, os::raw, panic, thread, time};

/// Runs the event loop
/// # Errors
/// Returns `FailedToRun`, this is fatal to the app
pub fn run() -> Result<(), FltkError> {
    unsafe {
        if !is_initialized() {
            init_all();
        }
        if !crate::app::is_ui_thread() {
            return Err(FltkError::Internal(FltkErrorKind::FailedToRun));
        }
        match fl::Fl_run() {
            0 => Ok(()),
            _ => Err(FltkError::Internal(FltkErrorKind::FailedToRun)),
        }
    }
}

/// Enable locks. This is called automatically in the beginning of the app initialization
pub fn enable_locks() -> Result<(), FltkError> {
    lock()?;
    Ok(())
}

/// Locks the main UI thread
/// # Errors
/// Returns `FailedToLock` if locking is unsupported. This is fatal to the app
pub fn lock() -> Result<(), FltkError> {
    unsafe {
        match fl::Fl_lock() {
            0 => Ok(()),
            _ => Err(FltkError::Internal(FltkErrorKind::FailedToLock)),
        }
    }
}

/// Unlocks the main UI thread
pub fn unlock() {
    unsafe {
        fl::Fl_unlock();
    }
}

/// Trigger event loop handling in the main thread
pub fn awake() {
    unsafe { fl::Fl_awake() }
}

/// Registers a function that will be called by the main thread during the next message handling cycle
pub fn awake_callback<F: FnMut() + 'static>(cb: F) {
    unsafe {
        unsafe extern "C" fn shim(data: *mut raw::c_void) {
            let mut a: Box<Box<dyn FnMut()>> = Box::from_raw(data as *mut Box<dyn FnMut()>);
            let f: &mut dyn FnMut() = &mut **a;
            let _ = panic::catch_unwind(panic::AssertUnwindSafe(f));
        }
        let a: *mut Box<dyn FnMut()> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: fl::Fl_Awake_Handler = Some(shim);
        fl::Fl_awake_callback(callback, data);
    }
}

/// Starts waiting for events.
/// Calls to redraw within wait require an explicit sleep
pub fn wait() -> bool {
    unsafe {
        if !is_initialized() {
            init_all();
        }
        assert!(crate::app::is_ui_thread());
        fl::Fl_wait() != 0
    }
}

/// Put the thread to sleep for `dur` seconds
pub fn sleep(dur: f64) {
    let dur = dur * 1000.;
    thread::sleep(time::Duration::from_millis(dur as u64));
}

/// Waits a maximum of `dur` seconds or until "something happens".
/// Returns true if an event happened (always true on windows).
/// Returns false if nothing happened.
/// # Errors
/// Can error out on X11 system if interrupted by a signal
pub fn wait_for(dur: f64) -> Result<bool, FltkError> {
    unsafe {
        if !is_initialized() {
            init_all();
        }
        if !crate::app::is_ui_thread() {
            return Err(FltkError::Internal(FltkErrorKind::FailedToRun));
        }
        match fl::Fl_wait_for(dur) as i32 {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(FltkError::Unknown(String::from(
                "The event loop was probably interrupted by an OS signal!",
            ))),
        }
    }
}

/// Returns whether a quit signal was sent
pub fn should_program_quit() -> bool {
    unsafe { fl::Fl_should_program_quit() != 0 }
}

/// Determines whether a program should quit
pub fn program_should_quit(flag: bool) {
    unsafe { fl::Fl_program_should_quit(flag as i32) }
}

/// Calling this during a big calculation will keep the screen up to date and the interface responsive.
pub fn check() -> bool {
    unsafe {
        if !is_initialized() {
            init_all();
        }
        assert!(crate::app::is_ui_thread());
        fl::Fl_check() != 0
    }
}

/// This is similar to app::check() except this does not call app::flush() or any callbacks,
/// which is useful if your program is in a state where such callbacks are illegal.
pub fn ready() -> bool {
    unsafe {
        if !is_initialized() {
            init_all();
        }
        assert!(crate::app::is_ui_thread());
        fl::Fl_ready() != 0
    }
}

/// Quit the app
pub fn quit() {
    if let Some(wins) = windows() {
        for mut i in wins {
            if i.shown() {
                i.hide();
            }
        }
    }
}

#[deprecated(since = "1.2.26", note = "please use `add_idle3` instead")]
/// Add an idle callback to run within the event loop.
/// Calls to [`WidgetExt::redraw`](`crate::prelude::WidgetExt::redraw`) within the callback require an explicit sleep
pub fn add_idle<F: FnMut() + 'static>(cb: F) {
    unsafe {
        unsafe extern "C" fn shim(data: *mut raw::c_void) {
            let a: *mut Box<dyn FnMut()> = data as *mut Box<dyn FnMut()>;
            let f: &mut dyn FnMut() = &mut **a;
            let _ = panic::catch_unwind(panic::AssertUnwindSafe(f));
        }
        let a: *mut Box<dyn FnMut()> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(shim);
        fl::Fl_add_idle(callback, data);
    }
}

#[deprecated(since = "1.2.26", note = "please use `remove_idle3` instead")]
/// Remove an idle function
pub fn remove_idle<F: FnMut() + 'static>(cb: F) {
    unsafe {
        unsafe extern "C" fn shim(data: *mut raw::c_void) {
            let a: *mut Box<dyn FnMut()> = data as *mut Box<dyn FnMut()>;
            let f: &mut dyn FnMut() = &mut **a;
            let _ = panic::catch_unwind(panic::AssertUnwindSafe(f));
        }
        let a: *mut Box<dyn FnMut()> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(shim);
        fl::Fl_remove_idle(callback, data);
    }
}

#[deprecated(since = "1.2.26", note = "please use `has_idle3` instead")]
/// Checks whether an idle function is installed
pub fn has_idle<F: FnMut() + 'static>(cb: F) -> bool {
    unsafe {
        unsafe extern "C" fn shim(data: *mut raw::c_void) {
            let a: *mut Box<dyn FnMut()> = data as *mut Box<dyn FnMut()>;
            let f: &mut dyn FnMut() = &mut **a;
            let _ = panic::catch_unwind(panic::AssertUnwindSafe(f));
        }
        let a: *mut Box<dyn FnMut()> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(shim);
        fl::Fl_has_idle(callback, data) != 0
    }
}

/// Add an idle callback to run within the event loop.
/// Calls to [`WidgetExt::redraw`](`crate::prelude::WidgetExt::redraw`) within the callback require an explicit sleep
pub fn add_idle2(cb: fn()) {
    unsafe {
        let data: *mut raw::c_void = std::ptr::null_mut();
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        fl::Fl_add_idle(callback, data);
    }
}

/// Remove an idle function
pub fn remove_idle2(cb: fn()) {
    unsafe {
        let data: *mut raw::c_void = std::ptr::null_mut();
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        fl::Fl_remove_idle(callback, data);
    }
}

/// Checks whether an idle function is installed
pub fn has_idle2(cb: fn()) -> bool {
    unsafe {
        let data: *mut raw::c_void = std::ptr::null_mut();
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        fl::Fl_has_idle(callback, data) != 0
    }
}

/// Handle object for interacting with idle callbacks
pub type IdleHandle = *mut ();

unsafe extern "C" fn idle_shim(data: *mut raw::c_void) {
    let a: *mut Box<dyn FnMut(IdleHandle)> = data as *mut Box<dyn FnMut(IdleHandle)>;
    let f: &mut dyn FnMut(IdleHandle) = &mut **a;
    let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| (*f)(data as _)));
}

/// Add an idle callback to run within the event loop.
/// This function returns a handle that can be used for future interaction with the callback.
/// Calls to [`WidgetExt::redraw`](`crate::prelude::WidgetExt::redraw`) within the callback require an explicit sleep
pub fn add_idle3<F: FnMut(IdleHandle) + 'static>(cb: F) -> IdleHandle {
    unsafe {
        let a: *mut Box<dyn FnMut(IdleHandle)> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_add_idle(callback, data);

        data as _
    }
}

/// Remove the idle function associated with the handle
pub fn remove_idle3(handle: IdleHandle) {
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_remove_idle(callback, data);
    }
}

/// Checks whether the idle function, associated with the handle, is installed
pub fn has_idle3(handle: IdleHandle) -> bool {
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_has_idle(callback, data) != 0
    }
}

/// Handle object for interacting with check callbacks
pub type CheckHandle = *mut ();

/// Add a check callback to run within the event loop.
/// This function returns a handle that can be used for future interaction with the callback.
pub fn add_check<F: FnMut(CheckHandle) + 'static>(cb: F) -> CheckHandle {
    unsafe {
        let a: *mut Box<dyn FnMut(CheckHandle)> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_add_check(callback, data);

        data as _
    }
}

/// Remove the check function associated with the handle
pub fn remove_check(handle: CheckHandle) {
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_remove_check(callback, data);
    }
}

/// Checks whether the check function, associated with the handle, is installed
pub fn has_check(handle: CheckHandle) -> bool {
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_has_check(callback, data) != 0
    }
}

/// Register a callback whenever there is a change to the selection buffer or the clipboard.
/// The clipboard is source 1 and the selection buffer is source 0
pub fn add_clipboard_notify(cb: fn(source: i32)) {
    unsafe {
        let data: *mut raw::c_void = std::ptr::null_mut();
        let callback: Option<unsafe extern "C" fn(source: i32, arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        fl::Fl_add_clipboard_notify(callback, data);
    }
}

/// Stop calling the specified callback when there are changes to the selection
/// buffer or the clipboard.
/// The clipboard is source 1 and the selection buffer is source 0
pub fn remove_clipboard_notify(cb: fn(source: i32)) {
    unsafe {
        let callback: Option<unsafe extern "C" fn(source: i32, arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        fl::Fl_remove_clipboard_notify(callback);
    }
}

#[deprecated(since = "1.2.26", note = "please use `add_clipboard_notify3` instead")]
/// Register a callback whenever there is a change to the selection buffer or the clipboard.
/// The clipboard is source 1 and the selection buffer is source 0.
/// A callback via closure cannot be removed!
pub fn add_clipboard_notify2<F: FnMut(i32) + 'static>(cb: F) {
    unsafe {
        unsafe extern "C" fn shim(source: i32, data: *mut raw::c_void) {
            let a: *mut Box<dyn FnMut(i32)> = data as *mut Box<dyn FnMut(i32)>;
            let f: &mut dyn FnMut(i32) = &mut **a;
            let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| f(source)));
        }
        let a: *mut Box<dyn FnMut(i32)> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(source: i32, arg1: *mut raw::c_void)> =
            Some(shim);
        fl::Fl_add_clipboard_notify(callback, data);
    }
}

unsafe extern "C" fn clipboard_notify_shim(source: i32, data: *mut raw::c_void) {
    let a: *mut Box<dyn FnMut(i32)> = data as *mut Box<dyn FnMut(i32)>;
    let f: &mut dyn FnMut(i32) = &mut **a;
    let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| (*f)(source)));
}

/// Register a callback whenever there is a change to the selection buffer or the clipboard.
/// The clipboard is source 1 and the selection buffer is source 0.
/// A callback via closure cannot be removed!
pub fn add_clipboard_notify3<F: FnMut(i32) + 'static>(cb: F) {
    unsafe {
        let a: *mut Box<dyn FnMut(i32)> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(source: i32, arg1: *mut raw::c_void)> =
            Some(clipboard_notify_shim);
        fl::Fl_add_clipboard_notify(callback, data);
    }
}

/// Stop calling the specified callback when there are changes to the selection
/// buffer or the clipboard.
/// The clipboard is source 1 and the selection buffer is source 0
pub fn remove_clipboard_notify3() {
    unsafe {
        let callback: Option<unsafe extern "C" fn(source: i32, arg1: *mut raw::c_void)> =
            Some(clipboard_notify_shim);
        fl::Fl_remove_clipboard_notify(callback);
    }
}

#[deprecated(since = "1.2.26", note = "please use `add_timeout3` instead")]
/**
    Adds a one-shot timeout callback. The timeout duration `tm` is indicated in seconds
    Example:
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn callback() {
        println!("TICK");
        app::repeat_timeout(1.0, callback);
    }
    fn main() {
        let app = app::App::default();
        let mut wind = window::Window::new(100, 100, 400, 300, "");
        wind.show();
        app::add_timeout(1.0, callback);
        app.run().unwrap();
    }
    ```
*/
pub fn add_timeout<F: FnMut() + 'static>(tm: f64, cb: F) {
    unsafe {
        assert!(crate::app::is_ui_thread());
        unsafe extern "C" fn shim(data: *mut raw::c_void) {
            let mut a: Box<Box<dyn FnMut()>> = Box::from_raw(data as *mut Box<dyn FnMut()>);
            let f: &mut dyn FnMut() = &mut **a;
            let _ = panic::catch_unwind(panic::AssertUnwindSafe(f));
        }
        let a: *mut Box<dyn FnMut()> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(shim);
        fl::Fl_add_timeout(tm, callback, data);
    }
}

#[deprecated(since = "1.2.26", note = "please use `repeat_timeout3` instead")]
/**
    Repeats a timeout callback from the expiration of the previous timeout.
    You may only call this method inside a timeout callback.
    The timeout duration `tm` is indicated in seconds
    Example:
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn callback() {
        println!("TICK");
        app::repeat_timeout(1.0, callback);
    }
    fn main() {
        let app = app::App::default();
        let mut wind = window::Window::new(100, 100, 400, 300, "");
        wind.show();
        app::add_timeout(1.0, callback);
        app.run().unwrap();
    }
    ```
*/
pub fn repeat_timeout<F: FnMut() + 'static>(tm: f64, cb: F) {
    assert!(crate::app::is_ui_thread());
    unsafe {
        unsafe extern "C" fn shim(data: *mut raw::c_void) {
            let a: *mut Box<dyn FnMut()> = data as *mut Box<dyn FnMut()>;
            let f: &mut dyn FnMut() = &mut **a;
            let _ = panic::catch_unwind(panic::AssertUnwindSafe(f));
        }
        let a: *mut Box<dyn FnMut()> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(shim);
        fl::Fl_repeat_timeout(tm, callback, data);
    }
}

#[deprecated(since = "1.2.26", note = "please use `remove_timeout3` instead")]
/// Removes a timeout callback
pub fn remove_timeout<F: FnMut() + 'static>(cb: F) {
    assert!(crate::app::is_ui_thread());
    unsafe {
        unsafe extern "C" fn shim(data: *mut raw::c_void) {
            let a: *mut Box<dyn FnMut()> = data as *mut Box<dyn FnMut()>;
            let f: &mut dyn FnMut() = &mut **a;
            let _ = panic::catch_unwind(panic::AssertUnwindSafe(f));
        }
        let a: *mut Box<dyn FnMut()> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(shim);
        fl::Fl_remove_timeout(callback, data);
    }
}

#[deprecated(since = "1.2.26", note = "please use `has_timeout3` instead")]
/// Check whether a timeout is installed
pub fn has_timeout<F: FnMut() + 'static>(cb: F) -> bool {
    unsafe {
        unsafe extern "C" fn shim(data: *mut raw::c_void) {
            let a: *mut Box<dyn FnMut()> = data as *mut Box<dyn FnMut()>;
            let f: &mut dyn FnMut() = &mut **a;
            let _ = panic::catch_unwind(panic::AssertUnwindSafe(f));
        }
        let a: *mut Box<dyn FnMut()> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(shim);
        fl::Fl_has_timeout(callback, data) != 0
    }
}

/**
    Adds a one-shot timeout callback. The timeout duration `tm` is indicated in seconds
    Example:
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn callback() {
        println!("TICK");
        app::repeat_timeout2(1.0, callback);
    }
    fn main() {
        let app = app::App::default();
        let mut wind = window::Window::new(100, 100, 400, 300, "");
        wind.show();
        app::add_timeout2(1.0, callback);
        app.run().unwrap();
    }
    ```
*/
pub fn add_timeout2(tm: f64, cb: fn()) {
    assert!(crate::app::is_ui_thread());
    unsafe {
        let data: *mut raw::c_void = std::ptr::null_mut();
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        fl::Fl_add_timeout(tm, callback, data);
    }
}

/**
    Repeats a timeout callback from the expiration of the previous timeout.
    You may only call this method inside a timeout callback.
    The timeout duration `tm` is indicated in seconds
    Example:
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn callback() {
        println!("TICK");
        app::repeat_timeout2(1.0, callback);
    }
    fn main() {
        let app = app::App::default();
        let mut wind = window::Window::new(100, 100, 400, 300, "");
        wind.show();
        app::add_timeout2(1.0, callback);
        app.run().unwrap();
    }
    ```
*/
pub fn repeat_timeout2(tm: f64, cb: fn()) {
    assert!(crate::app::is_ui_thread());
    unsafe {
        let data: *mut raw::c_void = std::ptr::null_mut();
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        fl::Fl_repeat_timeout(tm, callback, data);
    }
}

/// Removes a timeout callback
pub fn remove_timeout2(cb: fn()) {
    assert!(crate::app::is_ui_thread());
    if has_timeout2(cb) {
        unsafe {
            let data: *mut raw::c_void = std::ptr::null_mut();
            let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
                Some(mem::transmute(cb));
            fl::Fl_remove_timeout(callback, data);
        }
    }
}

/// Check whether a timeout is installed
pub fn has_timeout2(cb: fn()) -> bool {
    unsafe {
        let data: *mut raw::c_void = std::ptr::null_mut();
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        fl::Fl_has_timeout(callback, data) != 0
    }
}

/// Handle object for interacting with timeouts
pub type TimeoutHandle = *mut ();

unsafe extern "C" fn timeout_shim(data: *mut raw::c_void) {
    let a: *mut Box<dyn FnMut(TimeoutHandle)> = data as *mut Box<dyn FnMut(TimeoutHandle)>;
    let f: &mut dyn FnMut(TimeoutHandle) = &mut **a;
    let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| (*f)(data as _)));
}

/**
    Adds a one-shot timeout callback. The timeout duration `tm` is indicated in seconds
    This function returns a handle that can be use for future interaction with the timeout
    Example:
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn main() {
        let callback = |_handle| {
            println!("FIRED");
        };

        let app = app::App::default();
        let mut wind = window::Window::new(100, 100, 400, 300, "");
        wind.show();
        let _handle = app::add_timeout3(1.0, callback);
        app.run().unwrap();
    }
    ```
*/
pub fn add_timeout3<F: FnMut(TimeoutHandle) + 'static>(tm: f64, cb: F) -> TimeoutHandle {
    assert!(crate::app::is_ui_thread());
    unsafe {
        let a: *mut Box<dyn FnMut(TimeoutHandle)> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(timeout_shim);
        fl::Fl_add_timeout(tm, callback, data);

        data as _
    }
}

/**
    Repeats the timeout callback, associated with the hadle, from the expiration of the previous timeout.
    You may only call this method inside a timeout callback.
    The timeout duration `tm` is indicated in seconds
    Example:
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn main() {
        let callback = |handle| {
            println!("TICK");
            app::repeat_timeout3(1.0, handle);
        };

        let app = app::App::default();
        let mut wind = window::Window::new(100, 100, 400, 300, "");
        wind.show();
        app::add_timeout3(1.0, callback);
        app.run().unwrap();
    }
    ```
*/
pub fn repeat_timeout3(tm: f64, handle: TimeoutHandle) {
    assert!(crate::app::is_ui_thread());
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(timeout_shim);
        fl::Fl_repeat_timeout(tm, callback, data);
    }
}

/**
    Removes the timeout callback associated with the handle
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn main() {
        let callback = |handle| {
            println!("FIRED");
        };

        let app = app::App::default();
        let mut wind = window::Window::new(100, 100, 400, 300, "");
        wind.show();
        let handle = app::add_timeout3(1.0, callback);
        app::remove_timeout3(handle);
        app.run().unwrap();
    }
    ```
*/
pub fn remove_timeout3(handle: TimeoutHandle) {
    assert!(crate::app::is_ui_thread());
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(timeout_shim);
        fl::Fl_remove_timeout(callback, data);
    }
}

/// Check whether the timeout, associated with the handle, is installed
pub fn has_timeout3(handle: TimeoutHandle) -> bool {
    assert!(crate::app::is_ui_thread());
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(timeout_shim);
        fl::Fl_has_timeout(callback, data) != 0
    }
}

#[doc(hidden)]
pub fn add_raw_timeout<T>(tm: f64, cb: fn(*mut T), data: *mut T) {
    unsafe {
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        let data: *mut raw::c_void = data as *mut raw::c_void;
        fl::Fl_add_timeout(tm, callback, data);
    }
}

#[doc(hidden)]
pub fn repeat_raw_timeout<T>(tm: f64, cb: fn(*mut T), data: *mut T) {
    unsafe {
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        let data: *mut raw::c_void = data as *mut raw::c_void;
        fl::Fl_repeat_timeout(tm, callback, data);
    }
}

#[doc(hidden)]
pub fn remove_raw_timeout<T>(cb: fn(*mut T), data: *mut T) {
    unsafe {
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        let data: *mut raw::c_void = data as *mut raw::c_void;
        fl::Fl_remove_timeout(callback, data);
    }
}

#[doc(hidden)]
pub fn has_raw_timeout<T>(cb: fn(*mut T), data: *mut T) -> bool {
    unsafe {
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        let data: *mut raw::c_void = data as *mut raw::c_void;
        fl::Fl_has_timeout(callback, data) != 0
    }
}

/// Add a system handler
/// # Safety
/// FLTK makes no assurances regarding handling by the system handler
pub unsafe fn add_system_handler(
    cb: Option<unsafe extern "C" fn(*mut raw::c_void, *mut raw::c_void) -> i32>,
    data: *mut raw::c_void,
) {
    assert!(crate::app::is_ui_thread());
    fl::Fl_add_system_handler(cb, data);
}

/// Add a system handler
/// # Safety
/// FLTK makes no assurances regarding handling by the system handler
pub unsafe fn remove_system_handler(
    cb: Option<unsafe extern "C" fn(*mut raw::c_void, *mut raw::c_void) -> i32>,
) {
    assert!(crate::app::is_ui_thread());
    fl::Fl_remove_system_handler(cb);
}