Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When Booted After 6 GUI Updates Screen Freexes/Stops updating #3149

Open
NeuraXCheat opened this issue Mar 15, 2025 · 2 comments
Open

When Booted After 6 GUI Updates Screen Freexes/Stops updating #3149

NeuraXCheat opened this issue Mar 15, 2025 · 2 comments

Comments

@NeuraXCheat
Copy link

NeuraXCheat commented Mar 15, 2025

using System;
using System.Collections.Generic;
using Cosmos.System;
using Cosmos.System.Graphics;
using Cosmos.HAL;
using Cosmos.Core;
using Cosmos.System.FileSystem;
using Cosmos.System.FileSystem.VFS;
using Cosmos.Debug.Kernel;
using Cosmos.System.Graphics.Fonts;
using System.Drawing;

namespace CosmosDesktopGUI
{
    public class Kernel : Cosmos.System.Kernel
    {
        // Canvas for drawing the GUI
        private Canvas canvas;

        // Desktop elements
        private List<DesktopIcon> desktopIcons;
        private Taskbar taskbar;
        private StartMenu startMenu;
        private bool startMenuOpen = false;

        // Mouse positioning
        private Point mousePosition;
        private bool mouseClicked = false;

        // File system
        private CosmosVFS fs;

        // Debugger
        private Debugger mDebugger = new Debugger("GUI", "Desktop");

        // Colors
        private static readonly Pen DESKTOP_BACKGROUND = new Pen(Color.FromArgb(0, 120, 215));
        private static readonly Pen TASKBAR_COLOR = new Pen(Color.FromArgb(50, 50, 50));
        private static readonly Pen ICON_TEXT_COLOR = new Pen(Color.White);

        protected override void BeforeRun()
        {
            // Explicitly using System.Console to avoid ambiguity
            System.Console.WriteLine("CosmosDesktopGUI Starting...");

            // Initialize mouse
            MouseManager.ScreenWidth = 800;
            MouseManager.ScreenHeight = 600;
            MouseManager.X = (uint)(MouseManager.ScreenWidth / 2);
            MouseManager.Y = (uint)(MouseManager.ScreenHeight / 2);

            // Create the canvas for drawing
            canvas = FullScreenCanvas.GetFullScreenCanvas();
            canvas.Clear(DESKTOP_BACKGROUND.ValueARGB);

            // Initialize the file system
            try
            {
                fs = new CosmosVFS();
                VFSManager.RegisterVFS(fs);
                System.Console.WriteLine("File system initialized successfully");
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Error initializing file system: " + e.Message);
            }

            // Initialize desktop elements
            InitializeDesktop();

            System.Console.WriteLine("GUI Initialized");
        }

        private void InitializeDesktop()
        {
            // Create taskbar at the bottom of the screen
            taskbar = new Taskbar(0, (int)MouseManager.ScreenHeight - 40, (int)MouseManager.ScreenWidth, 40);

            // Create start menu
            startMenu = new StartMenu(0, (int)MouseManager.ScreenHeight - 240, 200, 200);

            // Create desktop icons
            desktopIcons = new List<DesktopIcon>
            {
                new DesktopIcon("Documents", 30, 30, "folder"),
                new DesktopIcon("Pictures", 30, 120, "folder"),
                new DesktopIcon("Terminal", 30, 210, "app"),
                new DesktopIcon("Settings", 30, 300, "app")
            };
        }

        protected override void Run()
        {
            try
            {

                // Update mouse position
                mousePosition.X = (int)MouseManager.X;
                mousePosition.Y = (int)MouseManager.Y;
                mouseClicked = MouseManager.MouseState == MouseState.Left;

                // Clear the screen (except taskbar)
                canvas.Clear(DESKTOP_BACKGROUND.ValueARGB);

                // Draw desktop elements
                DrawDesktop();

                // Handle user interaction
                HandleInteraction();

                // Draw the mouse cursor
                DrawCursor();

                // Update the screen
                canvas.Display();

                // Wait for 1 second (1000 milliseconds)
                int start = DateTime.Now.Millisecond;
                while (DateTime.Now.Millisecond - start < 1000)
                {
                    // Do nothing and just wait
                }

            }
            catch (Exception e)
            {
                mDebugger.Send("Exception occurred: " + e.Message);
                Stop();
            }
        }



        private void DrawDesktop()
        {
            // Draw desktop icons
            foreach (var icon in desktopIcons)
            {
                DrawIcon(icon);
            }

            // Draw taskbar
            canvas.DrawFilledRectangle(TASKBAR_COLOR, taskbar.X, taskbar.Y, taskbar.Width, taskbar.Height);

            // Draw start button
            canvas.DrawFilledRectangle(new Pen(Color.FromArgb(0, 100, 200)), 5, taskbar.Y + 5, 80, taskbar.Height - 10);
            // Using PCScreenFont directly
            canvas.DrawString("Start", Cosmos.System.Graphics.Fonts.PCScreenFont.Default, ICON_TEXT_COLOR, new Cosmos.System.Graphics.Point(25, taskbar.Y + 15));

            // Draw time in taskbar
            string timeStr = DateTime.Now.ToString("HH:mm");
            canvas.DrawString(timeStr, Cosmos.System.Graphics.Fonts.PCScreenFont.Default, ICON_TEXT_COLOR, new Cosmos.System.Graphics.Point(taskbar.Width - 50, taskbar.Y + 15));

            // Draw start menu if open
            if (startMenuOpen)
            {
                canvas.DrawFilledRectangle(new Pen(Color.FromArgb(30, 30, 30)), startMenu.X, startMenu.Y, startMenu.Width, startMenu.Height);
                canvas.DrawRectangle(new Pen(Color.White), startMenu.X, startMenu.Y, startMenu.Width, startMenu.Height);

                // Start menu items
                string[] menuItems = { "Terminal", "File Explorer", "Settings", "Shutdown" };
                for (int i = 0; i < menuItems.Length; i++)
                {
                    canvas.DrawString(menuItems[i], Cosmos.System.Graphics.Fonts.PCScreenFont.Default, ICON_TEXT_COLOR, new Cosmos.System.Graphics.Point(startMenu.X + 20, startMenu.Y + 20 + (i * 40)));
                }
            }
        }

        private void DrawIcon(DesktopIcon icon)
        {
            // Draw icon background for visual feedback if hovered
            if (IsPointInRect(mousePosition, icon.X - 5, icon.Y - 5, 70, 80))
            {
                canvas.DrawFilledRectangle(new Pen(Color.FromArgb(100, 100, 100, 100)), icon.X - 5, icon.Y - 5, 70, 80);
            }

            // Draw icon
            if (icon.Type == "folder")
            {
                DrawFolderIcon(icon.X, icon.Y);
            }
            else
            {
                DrawAppIcon(icon.X, icon.Y);
            }

            // Draw icon text
            canvas.DrawString(icon.Name, Cosmos.System.Graphics.Fonts.PCScreenFont.Default, ICON_TEXT_COLOR, new Cosmos.System.Graphics.Point(icon.X, icon.Y + 50));
        }

        private void DrawFolderIcon(int x, int y)
        {
            // Simple folder icon
            canvas.DrawFilledRectangle(new Pen(Color.FromArgb(255, 200, 80)), x, y + 10, 40, 30);
            canvas.DrawFilledRectangle(new Pen(Color.FromArgb(255, 200, 80)), x - 5, y + 5, 30, 10);
        }

        private void DrawAppIcon(int x, int y)
        {
            // Simple app icon
            canvas.DrawFilledRectangle(new Pen(Color.FromArgb(70, 130, 180)), x, y, 40, 40);
        }

        private void DrawCursor()
        {
            // Simple arrow cursor (triangle)
            canvas.DrawLine(new Pen(Color.White), mousePosition.X, mousePosition.Y, mousePosition.X + 6, mousePosition.Y + 6);
            canvas.DrawLine(new Pen(Color.White), mousePosition.X, mousePosition.Y, mousePosition.X, mousePosition.Y + 10);
            canvas.DrawLine(new Pen(Color.White), mousePosition.X, mousePosition.Y + 10, mousePosition.X + 6, mousePosition.Y + 6);
        }


        private void HandleInteraction()
        {
            // Check for start button click
            if (mouseClicked && IsPointInRect(mousePosition, 5, taskbar.Y + 5, 80, taskbar.Height - 10))
            {
                startMenuOpen = !startMenuOpen;

                // Debounce click
                while (MouseManager.MouseState == MouseState.Left)
                {
                    MouseManager.ScreenHeight = MouseManager.ScreenHeight;
                }
            }

            // Check for clicks outside start menu to close it
            if (startMenuOpen && mouseClicked && !IsPointInRect(mousePosition, startMenu.X, startMenu.Y, startMenu.Width, startMenu.Height))
            {
                startMenuOpen = false;
            }

            // Check for desktop icon clicks
            foreach (var icon in desktopIcons)
            {
                if (mouseClicked && IsPointInRect(mousePosition, icon.X - 5, icon.Y - 5, 70, 80))
                {
                    HandleIconClick(icon);

                    // Debounce click
                    while (MouseManager.MouseState == MouseState.Left)
                    {
                        MouseManager.ScreenHeight = MouseManager.ScreenHeight;
                    }
                }
            }

            // Check for start menu item clicks
            if (startMenuOpen && mouseClicked)
            {
                string[] menuItems = { "Terminal", "File Explorer", "Settings", "Shutdown" };
                for (int i = 0; i < menuItems.Length; i++)
                {
                    if (IsPointInRect(mousePosition, startMenu.X + 20, startMenu.Y + 20 + (i * 40) - 15, startMenu.Width - 40, 30))
                    {
                        HandleStartMenuItemClick(menuItems[i]);
                        startMenuOpen = false;

                        // Debounce click
                        while (MouseManager.MouseState == MouseState.Left)
                        {
                            MouseManager.ScreenHeight = MouseManager.ScreenHeight;
                        }
                    }
                }
            }
        }

        private void HandleIconClick(DesktopIcon icon)
        {
            if (icon.Name == "Terminal")
            {
                // Switch to terminal
                System.Console.Clear();
                System.Console.WriteLine("Terminal application launched");
                System.Console.WriteLine("Type 'exit' to return to the GUI");

                string input = "";
                while (input != "exit")
                {
                    System.Console.Write("> ");
                    input = System.Console.ReadLine();

                    // Simple command handling
                    if (input == "clear")
                    {
                        System.Console.Clear();
                    }
                    else if (input == "help")
                    {
                        System.Console.WriteLine("Available commands: clear, help, dir, exit");
                    }
                    else if (input == "dir")
                    {
                        try
                        {
                            var directory = System.IO.Directory.GetFiles(@"0:\");
                            foreach (var file in directory)
                            {
                                System.Console.WriteLine(file);
                            }
                        }
                        catch (Exception e)
                        {
                            System.Console.WriteLine("Error: " + e.Message);
                        }
                    }
                    else if (input != "exit")
                    {
                        System.Console.WriteLine("Unknown command: " + input);
                    }
                }

                // Redraw the GUI after exiting terminal
                canvas.Clear(DESKTOP_BACKGROUND.ValueARGB);
                DrawDesktop();
            }
            else if (icon.Name == "Settings")
            {
                System.Console.Clear();
                System.Console.WriteLine("Settings application launched");
                System.Console.WriteLine("No settings available at this time.");
                System.Console.WriteLine("Press any key to return to the desktop...");
                System.Console.ReadKey();

                // Redraw the GUI after exiting settings
                canvas.Clear(DESKTOP_BACKGROUND.ValueARGB);
                DrawDesktop();
            }
        }

        private void HandleStartMenuItemClick(string menuItem)
        {
            if (menuItem == "Shutdown")
            {
                Stop();
            }
            else if (menuItem != "Terminal")
            {
                HandleIconClick(desktopIcons.Find(icon => icon.Name == "Terminal"));
            }
            else if (menuItem == "Settings")
            {
                HandleIconClick(desktopIcons.Find(icon => icon.Name == "Settings"));
            }
        }

        private bool IsPointInRect(Point point, int x, int y, int width, int height)
        {
            return point.X >= x && point.X <= x + width && point.Y >= y && point.Y <= y + height;
        }
    }

    // Supporting classes
    public class Point
    {
        public int X { get; set; }
        public int Y { get; set; }

        public Point(int x, int y)
        {
            X = x;
            Y = y;
        }
    }

    public class DesktopIcon
    {
        public string Name { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        public string Type { get; set; }

        public DesktopIcon(string name, int x, int y, string type)
        {
            Name = name;
            X = x;
            Y = y;
            Type = type;
        }
    }

    public class Taskbar
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }

        public Taskbar(int x, int y, int width, int height)
        {
            X = x;
            Y = y;
            Width = width;
            Height = height;
        }
    }

    public class StartMenu
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }

        public StartMenu(int x, int y, int width, int height)
        {
            X = x;
            Y = y;
            Width = width;
            Height = height;
        }
    }
}

thats my kernel.c and thats my entire project and sometimes mouse doesent work for osmereason i

i have been trying to make a gui os for so long and im so close but i get that freeze thing please anyone help

@Dimkqo
Copy link

Dimkqo commented Mar 15, 2025

If you can dive to Canvas classes, you can see that some VideoDriver puts some bytes to Memory Registers. In some situations these Registers can be locked or damaged. When your kernel call canvas.Display(), canvas waits until these some Registers be able for writing. Now it haves minimum two ways:

  • enable that Registers
  • plug multithreading, wait for some time and reset graphics system.

I won't say that may right, but I think it could be a reason.

@9xbt
Copy link
Contributor

9xbt commented Mar 18, 2025

You're not collecting heap garbage, and you're running out of memory. Heap.Collect()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants