-! Copyright (C) 2008 Slava Pestov.\r
-! See http://factorcode.org/license.txt for BSD license.\r
-USING: threads io.backend namespaces init math kernel ;\r
-IN: io.thread\r
-\r
-! The Cocoa UI backend stops the I/O thread and takes over\r
-! completely.\r
-SYMBOL: io-thread-running?\r
-\r
-: io-thread ( -- )\r
- sleep-time io-multiplex yield ;\r
-\r
-: start-io-thread ( -- )\r
- [ [ io-thread-running? get-global ] [ io-thread ] while ]\r
- "I/O wait" spawn drop ;\r
-\r
-: stop-io-thread ( -- )\r
- f io-thread-running? set-global ;\r
-\r
-[\r
- t io-thread-running? set-global\r
- start-io-thread\r
-] "io.thread" add-startup-hook\r
+! Copyright (C) 2008 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: init io.backend kernel namespaces threads ;
+IN: io.thread
+
+! The Cocoa and Gtk UI backend stops the I/O thread and takes
+! over completely.
+SYMBOL: io-thread-running?
+
+: io-thread ( -- )
+ sleep-time io-multiplex yield ;
+
+: start-io-thread ( -- )
+ t io-thread-running? set-global
+ [ [ io-thread-running? get-global ] [ io-thread ] while ]
+ "I/O wait" spawn drop ;
+
+: stop-io-thread ( -- )
+ f io-thread-running? set-global ;
+
+[ start-io-thread ] "io.thread" add-startup-hook
-! Copyright (C) 2010 Anton Gorenko, Philipp Brüschweiler.
+! Copyright (C) 2010, 2011 Anton Gorenko, Philipp Brüschweiler.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.accessors alien.c-types alien.data
-alien.strings arrays assocs classes.struct command-line destructors
-gdk.ffi gdk.gl.ffi glib.ffi gobject-introspection.standard-types
-gobject.ffi gtk.ffi gtk.gl.ffi io.encodings.utf8 kernel libc literals
-locals math math.bitwise math.order math.vectors namespaces sequences
-strings system threads ui ui.backend ui.backend.gtk.input-methods
-ui.backend.gtk.io ui.clipboards ui.event-loop ui.gadgets
-ui.gadgets.private ui.gadgets.worlds ui.gestures ui.pixel-formats
+alien.strings arrays assocs classes.struct command-line continuations
+destructors gdk.ffi gdk.gl.ffi glib.ffi
+gobject-introspection.standard-types gobject.ffi gtk.ffi gtk.gl.ffi
+io.encodings.utf8 kernel libc literals locals math math.bitwise
+math.order math.vectors namespaces sequences strings system threads ui
+ui.backend ui.backend.gtk.input-methods ui.backend.gtk.io
+ui.clipboards ui.event-loop ui.gadgets ui.gadgets.private
+ui.gadgets.worlds ui.gestures ui.pixel-formats
ui.pixel-formats.private ui.private vocabs.loader ;
IN: ui.backend.gtk
gtk_clipboard_get <gtk-clipboard> swap set-global
] 2bi@ ;
-! Timeouts
+! Timer
-SYMBOL: next-timeout
+SYMBOL: next-fire-time
: set-timeout*-value ( alien value -- )
swap 0 set-alien-signed-4 ; inline
-: timeout-prepare ( source timeout* -- ? )
- nip next-timeout get-global nano-count [-]
+: timer-prepare ( source timeout* -- ? )
+ nip next-fire-time get-global nano-count [-]
[ 1,000,000 /i set-timeout*-value ] keep 0 = ;
-: timeout-check ( source -- ? )
- drop next-timeout get-global nano-count [-] 0 = ;
+: timer-check ( source -- ? )
+ drop next-fire-time get-global nano-count [-] 0 = ;
-: timeout-dispatch ( source callback user_data -- ? )
+: timer-dispatch ( source callback user_data -- ? )
3drop sleep-time [ 1,000,000,000 ] unless* nano-count +
- next-timeout set-global
+ next-fire-time set-global
yield t ;
-: init-timeout ( -- )
- GSourceFuncs malloc-struct &free
- [ timeout-prepare ] GSourceFuncsPrepareFunc >>prepare
- [ timeout-check ] GSourceFuncsCheckFunc >>check
- [ timeout-dispatch ] GSourceFuncsDispatchFunc >>dispatch
- GSource heap-size g_source_new &g_source_unref
- f g_source_attach drop
- nano-count next-timeout set-global ;
+: <timer-funcs> ( -- timer-funcs )
+ GSourceFuncs malloc-struct
+ [ timer-prepare ] GSourceFuncsPrepareFunc >>prepare
+ [ timer-check ] GSourceFuncsCheckFunc >>check
+ [ timer-dispatch ] GSourceFuncsDispatchFunc >>dispatch ;
+
+:: with-timer ( quot -- )
+ nano-count next-fire-time set-global
+ <timer-funcs> &free
+ GSource heap-size g_source_new &g_source_unref :> source
+ source f g_source_attach drop
+ [ quot call( -- ) ]
+ [ source g_source_destroy ] [ ] cleanup ;
! User input
init-clipboard
start-ui
[
- init-io-event-source
- init-timeout
- gtk_main
+ [ [ gtk_main ] with-timer ] with-event-loop
] with-destructors
] ui-running ;
! Copyright (C) 2011 Anton Gorenko.
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors alien.c-types classes.struct glib.ffi
-io.backend.unix.multiplexers io.thread kernel libc literals namespaces
-system threads ui.backend.gtk.io ;
+USING: accessors alien.c-types classes.struct continuations
+glib.ffi io.backend.unix.multiplexers io.thread kernel libc
+literals locals namespaces system threads ui.backend.gtk.io ;
IN: ui.backend.gtk.io.unix
: prepare ( source timeout -- ? )
0 mx get wait-for-events
yield t ;
+: <funcs> ( -- funcs )
+ GSourceFuncs malloc-struct
+ [ prepare ] GSourceFuncsPrepareFunc >>prepare
+ [ check ] GSourceFuncsCheckFunc >>check
+ [ dispatch ] GSourceFuncsDispatchFunc >>dispatch ;
+
CONSTANT: poll-fd-events
flags{
G_IO_IN
mx get fd>> >>fd
poll-fd-events >>events ;
-M: unix init-io-event-source
+M:: unix with-event-loop ( quot -- )
stop-io-thread
- GSourceFuncs malloc-struct &free
- [ prepare ] GSourceFuncsPrepareFunc >>prepare
- [ check ] GSourceFuncsCheckFunc >>check
- [ dispatch ] GSourceFuncsDispatchFunc >>dispatch
- GSource heap-size g_source_new &g_source_unref
- [ <poll-fd> g_source_add_poll ]
- [ f g_source_attach drop ] bi ;
+ <funcs> &free
+ GSource heap-size g_source_new &g_source_unref :> source
+ source <poll-fd> g_source_add_poll
+ source f g_source_attach drop
+ [ quot call( -- ) ]
+ [
+ source g_source_destroy
+ start-io-thread
+ ] [ ] cleanup ;