global using Sys = Cosmos.Kernel.System; using System.Diagnostics; using System.Drawing; using System.Runtime; using System.Runtime.InteropServices; using Cosmos.Build.API.Attributes; using Cosmos.Kernel.Core; using Cosmos.Kernel.System.Graphics; using RemSox.Processing; using RemSox.Processing.IPC; using RemSox.UI.GUI.CLI; using RemSox.UI.GUI.CLI.Commands; using RemSox.UI.GUI.Rendering; using RemSox.UI.GUI.Windows; namespace RemSox; /// /// Main kernel class - inherits from Cosmos.Kernel.System.Kernel. /// public class Kernel : Sys.Kernel { private static Window? activeDragWindow = null; private static Point lastPointerPosition = Point.Empty; private static bool wasLeftButtonDown = false; private static int mousePollCounter = 0; protected override void BeforeRun() { Console.WriteLine("Cosmos booted successfully!"); Console.WriteLine("Type a command to get it executed."); WindowManager.AddRenderSource(new CanvasRenderSource()); CommandManager.RegisterCommands(new ICommand[] { new HelpCommand(), new ClearCommand(), new HaltCommand(), new SpawnTestProcessCommand(), new ListProcessesCommand(), new StopProcessCommand() }); Sys.Mouse.MouseManager.Initialize(); Sys.Keyboard.KeyboardManager.Initialize(); ProcessManager.SpawnProcess(); Console.WriteLine(CosmosFeatures.MouseEnabled); Console.WriteLine(CosmosFeatures.KeyboardEnabled); Console.WriteLine($"Canvas size: {FullScreenCanvas.GetFullScreenCanvas().Mode.Width}x{FullScreenCanvas.GetFullScreenCanvas().Mode.Height}"); } protected override void Run() { WindowInputTick(); return; Console.Write("> "); string? input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { return; } if (CommandManager.TryExecute(input)) { return; } Console.WriteLine($"\"{input}\" is not a command"); } private static void WindowInputTick() { mousePollCounter++; Sys.Mouse.MouseManager.Poll(); Point pointerPosition = new(Sys.Mouse.MouseManager.X, Sys.Mouse.MouseManager.Y); bool leftButtonDown = Sys.Mouse.MouseManager.LeftButton; if (leftButtonDown && !wasLeftButtonDown) { activeDragWindow = TryBeginDrag(pointerPosition); } else if (leftButtonDown && activeDragWindow is not null) { activeDragWindow.DragTo(pointerPosition); } else if (!leftButtonDown && activeDragWindow is not null) { activeDragWindow.EndDrag(); activeDragWindow = null; } wasLeftButtonDown = leftButtonDown; // Render all canvases and cursor every tick, or if changed Canvas canvas = FullScreenCanvas.GetFullScreenCanvas(); CanvasRenderSource.CompositeAndDisplay(canvas, pointerPosition); lastPointerPosition = pointerPosition; } private static Window? TryBeginDrag(Point pointerPosition) { foreach (Processing.Process process in ProcessManager.GetAllProcesses()) { List windows = WindowManager.GetWindowsForProcess(process); for (int index = windows.Count - 1; index >= 0; index--) { Window window = windows[index]; if (window.TryBeginDrag(pointerPosition)) { return window; } } } return null; } } public class TestProcess() : Processing.Process("Test Process") { internal override void Run() { Window window = WindowManager.CreateWindow(this, "Test Window", Point.Empty, new Size(200, 150)); window.AutoFlush = true; var circle = window.CreateUIElement(rect => { rect.Position = new Point(10, 10); rect.Radius = 50; rect.Color = Color.Red; }); circle.Radius = 40; SendMessageToAllProcesses(new TestMessage { SenderProcessId = Id }); while (!StopRequested) { Thread.Sleep(1000); } } internal override void HandleInterProcessMessage(Message message) { if (message is TestMessage testMessage && message.SenderProcessId != Id) { Console.WriteLine($"Received message in {Name} (ID: {Id}): {testMessage.MessageText}"); } } } internal class TestMessage : Message { public string MessageText { get; set; } = "Hello from TestMessage!"; } public static unsafe partial class StartupCodeHelpers { [RuntimeExport("fmod")] public static double fmod(double x, double y) { if (Math.Abs(y) < double.Epsilon) return double.NaN; double q = Math.Truncate(x / y); return x - q * y; } }