Skip to content

Commit

Permalink
Basic window server
Browse files Browse the repository at this point in the history
  • Loading branch information
Twometer committed Apr 11, 2021
1 parent ed34b8c commit d0eb95d
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"${workspaceFolder}/libc/include",
"${workspaceFolder}/libelf/include",
"${workspaceFolder}/libneko/include",
"${workspaceFolder}/libpng/include"
"${workspaceFolder}/libpng/include",
"${workspaceFolder}/libgui/include"
]
}
],
Expand Down
11 changes: 11 additions & 0 deletions Nekofile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ default:
% libc
% libelf
% libpng
% libgui
% bootloader
% kernel

Expand All @@ -49,6 +50,7 @@ clean:
mkdir -p build/libc
mkdir -p build/libelf
mkdir -p build/libpng
mkdir -p build/libgui

; Sysroot
mkdir -p sysroot/usr/include
Expand All @@ -62,6 +64,7 @@ sysroot:
cp -RT libc/include sysroot/usr/include
cp -RT libelf/include sysroot/usr/include
cp -RT libpng/include sysroot/usr/include
cp -RT libgui/include sysroot/usr/include
cp -RT libneko/include sysroot/usr/include

; Compile kernel version of libc
Expand Down Expand Up @@ -103,6 +106,14 @@ libpng:
input: build/libpng/*.o
output: sysroot/usr/lib/libpng.a

libgui:
[libcc]
input: libgui/**.cpp
output: build/libgui/~.o
[ar]
input: build/libgui/*.o
output: sysroot/usr/lib/libgui.a

; Compiles the bootloader
bootloader:
[asm]
Expand Down
2 changes: 2 additions & 0 deletions libc/icxxabi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ uarch_t __atexitFuncCount = 0;
void *__dso_handle = 0;
#endif

void *__gxx_personality_v0;

int __cxa_atexit(void (*f)(void *), void *objptr, void *dso)
{
if (__atexitFuncCount >= ATEXIT_FUNC_MAX)
Expand Down
31 changes: 31 additions & 0 deletions libgui/application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <nekosys.h>
#include <libgui/application.h>
#include <libgui/packets.h>

using namespace Gui;

Application::Application()
{
}

void Application::Run()
{
while (!exitRequested)
{
connection.Receive();
}
}

void Application::Exit()
{
exitRequested = true;
}

void Application::OpenWindow(Window &win)
{
PCreateWindow packet{};
packet.width = win.width;
packet.height = win.height;
memcpy(packet.title, win.title.CStr(), win.title.Length());
connection.Send(ID_PCreateWindow, sizeof(packet), &packet);
}
36 changes: 36 additions & 0 deletions libgui/guiconnection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <nekosys.h>
#include <string.h>
#include <libgui/guiconnection.h>

using namespace Gui;

GuiConnection::GuiConnection()
{
recvBuffer = new uint8_t[16384];
pipe = pipe_open("sakura");
}

GuiConnection::~GuiConnection()
{
delete[] recvBuffer;
pipe_close(pipe);
}

void GuiConnection::Send(uint8_t packetId, size_t size, void *data)
{
uint8_t *packet = new uint8_t[size + 1];
packet[0] = packetId;
memcpy(packet + 1, data, size);
pipe_send(pipe, 0, size + 1, packet);
delete[] packet;
}

PacketData GuiConnection::Receive()
{
PacketData data{};
int size = pipe_recv(pipe, &data.source, 16384, recvBuffer) - 1;
data.packetId = recvBuffer[0];
data.size = size;
data.data = recvBuffer + 1;
return data;
}
29 changes: 29 additions & 0 deletions libgui/include/libgui/application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef _APPLICATION_H
#define _APPLICATION_H

#include <libgui/window.h>
#include <libgui/guiconnection.h>

namespace Gui
{

class Application
{
private:
GuiConnection connection;

bool exitRequested;

public:
Application();

void Run();

void Exit();

void OpenWindow(Window &window);
};

};

#endif
35 changes: 35 additions & 0 deletions libgui/include/libgui/guiconnection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _GUI_CONNECTION_H
#define _GUI_CONNECTION_H

#include <stdint.h>
#include <sys/types.h>

namespace Gui
{
struct PacketData
{
uint8_t packetId;
pid_t source;
size_t size;
void *data;
};

class GuiConnection
{
private:
int pipe;

uint8_t *recvBuffer;

public:
GuiConnection();

~GuiConnection();

void Send(uint8_t packetId, size_t size, void *data);

PacketData Receive();
};
};

#endif
20 changes: 20 additions & 0 deletions libgui/include/libgui/packets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef _PACKETS_H
#define _PACKETS_H

#include <stdint.h>

#define ID_PCreateWindow 1

namespace Gui
{

struct PCreateWindow
{
int width;
int height;
char title[128];
};

};

#endif
21 changes: 21 additions & 0 deletions libgui/include/libgui/window.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _WINDOW_H
#define _WINDOW_H

#include <nk/string.h>

namespace Gui
{

class Window
{
public:
nk::String title;
int width;
int height;

Window(const nk::String &title, int width, int height);
};

};

#endif
8 changes: 8 additions & 0 deletions libgui/window.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <libgui/window.h>

using namespace Gui;

Window::Window(const nk::String &title, int width, int height)
: title(title), width(width), height(height)
{
}
2 changes: 2 additions & 0 deletions userland/hellogui/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
neko-gcc -std=gnu++11 -fno-exceptions main.cpp -lgui -o ../../sysroot/bin/hellogui
13 changes: 13 additions & 0 deletions userland/hellogui/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <libgui/application.h>

int main()
{
Gui::Application application;

Gui::Window window("Test", 128, 128);
application.OpenWindow(window);

application.Run();

return 0;
}
2 changes: 1 addition & 1 deletion userland/sakura/build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
neko-gcc -std=gnu++11 -fno-exceptions main.cpp -o ../../sysroot/bin/sakura
neko-gcc -std=gnu++11 -fno-exceptions main.cpp -lgui -o ../../sysroot/bin/sakura
31 changes: 28 additions & 3 deletions userland/sakura/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,54 @@
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <libgui/guiconnection.h>
#include <libgui/packets.h>

using namespace Gui;

GuiConnection *connection;

void receiver_thread()
{
sleep(5);
while (true)
{
auto packetData = connection->Receive();

switch (packetData.packetId)
{
case ID_PCreateWindow:
{
auto packet = (PCreateWindow *)packetData.data;
printf("Creating window '%s' with size %dx%d\n", packet->title, packet->width, packet->height);
break;
}
default:
break;
}
}

thread_die(0);
}

int main(int argc, char **argv)
{
FRAMEBUF framebuf;
framebuf_acquire(&framebuf);
//framebuf_acquire(&framebuf);

connection = new GuiConnection();
int result = thread_create(receiver_thread);
if (result < 0)
{
return 1;
}
else
{
printf("[info] sakura window server started.");

thread_join(result);
}

framebuf_release();
//framebuf_release();
delete connection;
return 0;
}

0 comments on commit d0eb95d

Please sign in to comment.