diff --git a/src/main.c3 b/src/main.c3 index 7fce951..26f95c2 100644 --- a/src/main.c3 +++ b/src/main.c3 @@ -6,6 +6,7 @@ import std::encoding::json; import std::time; import conf; +import vm; import ugui; import ugui::sdl::ren; @@ -21,13 +22,6 @@ const bool VSYNC = true; fn int main(String[] args) { - String path = args[1]; - File file = file::open(path, "r")!!; - - // load configuration in memory as an object - Object* conf = json::parse(mem, &file)!!; - defer conf.free(); - // ---------------------------------------------- UI initialization ---------------------------------------------- // ArenaAllocator arena; char[] arena_mem = mem::new_array(char, 1024*1024); @@ -61,72 +55,53 @@ fn int main(String[] args) ren::pre(ren.win); // -------------------------------------------- End UI initialization -------------------------------------------- // - StrList cmd = conf::get_qemu_cmdline(mem, conf)!!; - defer cmd.free(); - String vm_name = conf.get_string("name")!!; - String vm_disk = conf.get_string("disk").to_expanded_path(tmem).str_view()!!; - String vm_disk_size = bytes_to_human_readable(tmem, file::get_size(vm_disk))!!; - - bool vm_on = false; - SubProcess vm_proc; - defer (void)vm_proc.join(); ConfFList conf_list = conf::get_config_list(tmem)!!; + VMList vm_list; + vm_list.init(tmem); + io::printn("Configurations found:"); foreach (cf : conf_list) { io::printn(cf); + vm_list.push(vm::new_from_path(tmem, cf.path))!!; } bool quit; Clock sleep_clock; - while (!quit) { + while (!quit) @pool() { sleep_clock.mark(); quit = ui.handle_events()!!; - vm_on = vm_proc.is_running()!!; /* Start UI Handling */ ui.frame_begin()!!; if (ui.check_key_combo(ugui::KMOD_CTRL, "q")) quit = true; - ui.@div(ugui::@grow(), ugui::@grow(), COLUMN) { - ui.@div(ugui::@grow(), ugui::@fit(20), ROW, LEFT) { - ui.text("Machine:")!!; - ui.separator(ugui::@grow(), ugui::@grow())!!; - ui.text(vm_name)!!; - - static bool popup; - static Point popup_pos; - if (ui.button("v")!!.mouse_release) { - popup = true; - popup_pos = ui.input.mouse.pos; + static usz which_vm = 0; + ui.@div(ugui::@grow(), ugui::@grow()) { + ui.@div(ugui::@fit((short)(ui.width*0.3)), ugui::@grow(), COLUMN, scroll_x: true, scroll_y: true) { + foreach_r (idx, &vm : vm_list) { + Id id = ui.@div(ugui::@grow(), ugui::@fit(50), ROW, LEFT, false,{},false,false, idx) { + ui.text(vm.name, idx)!!; + ui.separator(ugui::@grow(), ugui::@exact(1), idx)!!; + if (vm.is_running()) { + if (ui.button("stop", "", idx)!!.mouse_release) vm.stop()!!; + } else { + if (ui.button("start", "", idx)!!.mouse_release) vm.start()!!; + } + }!!; + ui.hor_line(idx)!!; + if (ui.focus_id == id) which_vm = idx; } - ui.@popup(&popup, popup_pos, ugui::@fit(), ugui::@fit(), COLUMN) { - foreach (idx, c : conf_list) { - ui.text(c.name, idx)!!; - } - }!!; - }!!; - ui.hor_line()!!; - ui.@div(ugui::@grow(), ugui::@grow(), COLUMN, scroll_y: true) { - ui.text(string::tformat("disk: %s (%s)", vm_disk, vm_disk_size))!!; - ui.text(string::tformat("runner: %s", conf.get_string("qemu")))!!; - ui.text(string::tformat("memory: %s", conf.get_string("memory")))!!; - ui.text(string::tformat("processors: %s", conf.get_string("processors")))!!; - }!!; - ui.hor_line()!!; - ui.@div(ugui::@grow(), ugui::@fit(), ROW) { - if (!vm_on) { - if (ui.button("START")!!.mouse_release) { - vm_proc = process::create(cmd.array_view(), {.inherit_stdio=true, .inherit_environment=true})!!; - vm_on = true; - } - } else { - if (ui.button("STOP")!!.mouse_release) { - vm_proc.terminate()!!; - vm_on = false; - } - } + ui.ver_line()!!; + ui.@div(ugui::@grow(), ugui::@grow(), COLUMN, scroll_x: true, scroll_y: true) { + // TODO: implement a "selectable div" to display the information of the right vm + VirtualMachineDesc* vm = &vm_list[which_vm]; + ui.text(string::tformat("Name: %s", vm.name))!!; + ui.text(string::tformat("Disk: %s (%s)", vm.disk_path.str_view(), bytes_to_human_readable(tmem, file::get_size(vm.disk_path.str_view()))))!!; + ui.text(string::tformat("Runner: %s", vm.config.get_string("qemu")))!!; + ui.text(string::tformat("Memory: %s", vm.config.get_string("memory")))!!; + ui.text(string::tformat("Processors: %s", vm.config.get_string("processors")))!!; }!!; }!!; @@ -143,7 +118,7 @@ fn int main(String[] args) int timeout = LIMIT_FPS ? (int)(100.0-sleep_clock.mark().to_ms()-0.5) : 0; if (ui.skip_frame) timeout = 0; ren::wait_events(timeout); - } + }; return 0; } diff --git a/src/vm.c3 b/src/vm.c3 index 8ff9144..71f6feb 100644 --- a/src/vm.c3 +++ b/src/vm.c3 @@ -4,6 +4,7 @@ import std::io::file; import std::os::process; import std::collections::object; import std::encoding::json; +import std::collections::list; import conf; @@ -17,6 +18,9 @@ struct VirtualMachineDesc { } +alias VMList = list::List{VirtualMachineDesc}; + + fn VirtualMachineDesc? new_from_path(Allocator allocator, Path path) { VirtualMachineDesc desc;