|
1 | 1 | #lang racket/base |
2 | 2 | (require ffi/unsafe/objc |
3 | 3 | ffi/unsafe |
| 4 | + ffi/unsafe/global |
| 5 | + ffi/unsafe/schedule |
| 6 | + ffi/unsafe/custodian |
4 | 7 | racket/class |
5 | 8 | racket/draw/private/dc |
6 | 9 | "pool.rkt" |
|
10 | 13 | "../common/queue.rkt" |
11 | 14 | "../common/handlers.rkt" |
12 | 15 | "../../lock.rkt" |
13 | | - "../common/freeze.rkt" |
14 | | - "../common/keep-forever.rkt") |
| 16 | + "../common/freeze.rkt") |
15 | 17 |
|
16 | 18 | (provide |
17 | 19 | (protect-out app |
|
89 | 91 | [-a _void (applicationDidChangeScreenParameters: notification) |
90 | 92 | ;; Screen changes sometimes make the event loop get stuck; |
91 | 93 | ;; hack: schedule a wake-up call in 5 seconds |
92 | | - (let ([priviledged-custodian ((get-ffi-obj 'scheme_make_custodian #f (_fun _pointer -> _scheme)) #f)]) |
| 94 | + (let ([priviledged-custodian (make-custodian-at-root)]) |
93 | 95 | (parameterize ([current-custodian priviledged-custodian]) |
94 | 96 | (thread (lambda () (sleep 5.0))))) |
95 | 97 | (unless (version-10.10-or-later?) |
|
116 | 118 | -> _OSStatus)) |
117 | 119 | (define NSApplicationActivationPolicyRegular 0) |
118 | 120 | (define NSApplicationActivationPolicyAccessory 1) |
119 | | -(unless (scheme_register_process_global "PLT_IS_FOREGROUND_APP" #f) |
| 121 | +(unless (register-process-global #"PLT_IS_FOREGROUND_APP" #f) |
120 | 122 | (cond |
121 | 123 | [(version-10.6-or-later?) |
122 | 124 | ;; When a frame or root menu bar is created, we promote to |
|
132 | 134 | (tellv app setDelegate: app-delegate) |
133 | 135 |
|
134 | 136 | (define (bring-to-front) |
135 | | - (unless (scheme_register_process_global "Racket-GUI-no-front" #f) |
| 137 | + (unless (register-process-global #"Racket-GUI-no-front" #f) |
136 | 138 | (tellv app activateIgnoringOtherApps: #:type _BOOL #t) |
137 | 139 | ;; It may not be that easy... |
138 | 140 | (when (version-10.7-or-later?) |
|
311 | 313 | (define-cf CFRunLoopAddObserver (_fun _pointer _pointer _pointer -> _void)) |
312 | 314 | (define-cf CFRunLoopGetMain (_fun -> _pointer)) |
313 | 315 | (define kCFRunLoopExit (arithmetic-shift 1 7)) |
314 | | -(define-mz scheme_signal_received (_fun -> _void)) |
315 | 316 | (define already-exited? #f) |
316 | 317 | (define sleeping? #f) |
317 | 318 | (define (exiting-run-loop x y z) |
318 | 319 | (when sleeping? |
319 | 320 | (if already-exited? |
320 | | - (scheme_signal_received) |
| 321 | + (unsafe-signal-received) |
321 | 322 | (set! already-exited? #t)))) |
322 | 323 | (let ([o (CFRunLoopObserverCreate #f kCFRunLoopExit #t 0 exiting-run-loop #f)]) |
323 | 324 | (CFRunLoopAddObserver (CFRunLoopGetMain) o kCFRunLoopCommonModes)) |
|
468 | 469 | ;; Install an alternate "sleep" function (in the Racket core) |
469 | 470 | ;; that wakes up if any Cocoa event is ready. |
470 | 471 |
|
471 | | -(define-mz scheme_start_sleeper_thread (_fun _fpointer _float _pointer _int -> _void)) |
472 | | -(define-mz scheme_end_sleeper_thread (_fun -> _void)) |
473 | | - |
474 | | -(define-mz scheme_sleep _pointer) |
475 | | -(define-mz scheme_set_place_sleep (_fun _pointer -> _void)) |
476 | | - |
477 | 472 | ;; Called through an atomic callback: |
478 | | -(define (sleep-until-event secs fds) |
| 473 | +(define (sleep-until-event) |
479 | 474 | (set! sleeping? #t) |
480 | 475 | (set! already-exited? #f) |
481 | | - (scheme_start_sleeper_thread scheme_sleep secs fds write_sock) |
482 | | - (check-one-event #t #f) ; blocks until an event is ready |
483 | | - (scheme_end_sleeper_thread) |
| 476 | + ;; blocks until an event is ready, which can include a post from a |
| 477 | + ;; background `sleep` thread that that triggers `write_sock`: |
| 478 | + (check-one-event #t #f) |
484 | 479 | (set! sleeping? #f)) |
485 | 480 |
|
486 | 481 | (define (cocoa-install-event-wakeup) |
487 | 482 | (post-dummy-event) ; why do we need this? 'nextEventMatchingMask:' seems to hang if we don't use it |
488 | | - (scheme_set_place_sleep (function-ptr sleep-until-event |
489 | | - (_fun #:atomic? #t _float _gcpointer -> _void)))) |
490 | | -(keep-forever sleep-until-event) |
| 483 | + (unsafe-set-sleep-in-thread! sleep-until-event write_sock)) |
0 commit comments