A db/main.tcl => db/main.tcl +5 -0
@@ 0,0 1,5 @@
+package require sqlite3
+package require logger
+
+set log [logger::init tclircc::db]
+
M main.tcl => main.tcl +18 -0
@@ 10,8 10,21 @@ set log [logger::init tclircc::main]
${log}::info "tclircc $version <https://amehut.dev/~aleteoryx/tclircc>"
${log}::info "running from $path"
+source state.tcl
+
+${log}::debug "starting db thread..."
+set db_thread [thread::create -preserved]
+state::manage_thread $db_thread tclircc::db
+thread::send $db_thread [list variable path $path version $version]
+if {[thread::send $db_thread [list source "$path[file separator]db[file separator]main.tcl"] result] == 1} {
+ ${log}::critical "couldn't start db thread: $result"
+ exit -1
+}
+${log}::debug "started db thread."
+
${log}::debug "starting ui thread..."
set ui_thread [thread::create -preserved]
+state::manage_thread $ui_thread tclircc::ui
thread::send $ui_thread [list variable path $path version $version]
if {[thread::send $ui_thread [list source "$path[file separator]ui[file separator]main.tcl"] result] == 1} {
${log}::critical "couldn't start ui thread: $result"
@@ 19,6 32,11 @@ if {[thread::send $ui_thread [list source "$path[file separator]ui[file separato
}
${log}::debug "started ui thread."
+update
+foreach key [array names state::ns] {
+ ${log}::debug "thread \"$key\" has id [set state::ns($key)]"
+}
+
${log}::debug "opening main window..."
thread::send $ui_thread {mk_toplevel name; return $name} mainwin
${log}::debug "main window opened: $mainwin"
A state.tcl => state.tcl +46 -0
@@ 0,0 1,46 @@
+namespace eval state {
+ variable threads [list main [thread::id]]
+ variable log [logger::init tclircc::state]
+ variable logt [logger::init tclircc::state::threads]
+
+ variable ns
+
+ proc manage_thread {tid name} {
+ variable threads
+ variable logt
+ dict set threads $name $tid
+ ${logt}::debug "managing thread $tid as \"$name\""
+
+ thread::send -head $tid {
+ namespace eval state {
+ variable ns
+ proc update {} {
+ variable ns
+ thread::send -head [set ns(main)] state::update
+ }
+ }
+ }
+
+ ::state::update
+ }
+ proc unmanage_thread {name} {
+ variable threads
+ variable logt
+ dict unset threads $name
+ ${logt}::debug "unmanaging thread $tid (\"$name\")"
+ ::state::update
+ }
+ proc update {} {
+ variable threads
+ dict for {name tid} $threads {
+ if ![thread::exists $tid] {dict unset $threads $name}
+ }
+ dict for {name tid} $threads {
+ set payload {array unset state::ns; }
+ dict for {n t} $threads {
+ append payload {set state::ns(} $n {) } $t {; }
+ }
+ thread::send -head $tid $payload
+ }
+ }
+}
M ui/main.tcl => ui/main.tcl +0 -2
@@ 1,5 1,3 @@
-#!/bin/env tclsh
-
variable toplevel_count 0
variable toplevel_tabs
variable toplevel_classes