Kernel/Userspace: Replace my custom SHM system with SysV shm

This allows xbanan to use shm!
This commit is contained in:
2026-08-08 01:52:12 +03:00
parent 2c4fee4a82
commit fc40820150
23 changed files with 327 additions and 225 deletions
@@ -1,8 +1,7 @@
#include "AudioServer.h"
#include <sys/banan-os.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <unistd.h>
@@ -23,10 +22,7 @@ void AudioServer::on_client_disconnect(int fd)
ASSERT(it != m_audio_buffers.end());
if (it->value.buffer != nullptr)
{
const size_t bytes = sizeof(LibAudio::AudioBuffer) + it->value.buffer->capacity * sizeof(LibAudio::AudioBuffer::sample_t);
munmap(it->value.buffer, bytes);
}
shmdt(it->value.buffer);
m_audio_buffers.remove(it);
@@ -54,9 +50,9 @@ bool AudioServer::on_client_packet(int fd, LibAudio::Packet packet)
dwarnln("Client tried to map second audio buffer??");
return false;
}
audio_buffer.buffer = static_cast<LibAudio::AudioBuffer*>(smo_map(packet.parameter));
audio_buffer.buffer = static_cast<LibAudio::AudioBuffer*>(shmat(packet.parameter, nullptr, 0));
audio_buffer.queued_head = audio_buffer.buffer->tail;
if (audio_buffer.buffer == nullptr)
if (audio_buffer.buffer == SHM_FAILED)
{
dwarnln("Failed to map audio buffer: {}", strerror(errno));
return false;
+14 -19
View File
@@ -1,19 +1,16 @@
#include "Window.h"
#include <BAN/Debug.h>
#include <BAN/ScopeGuard.h>
#include <LibGUI/Window.h>
#include <sys/banan-os.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <unistd.h>
Window::~Window()
{
munmap(m_fb_addr, client_width() * client_height() * 4);
smo_delete(m_smo_key);
shmdt(m_fb_addr);
LibGUI::EventPacket::DestroyWindowEvent packet;
@@ -45,16 +42,16 @@ BAN::ErrorOr<void> Window::resize(uint32_t width, uint32_t height)
{
const size_t fb_bytes = width * height * 4;
long smo_key = smo_create(fb_bytes, PROT_READ | PROT_WRITE);
if (smo_key == -1)
const int shmid = shmget(IPC_PRIVATE, fb_bytes, 0666);
if (shmid == -1)
return BAN::Error::from_errno(errno);
BAN::ScopeGuard smo_deleter([&]() { smo_delete(smo_key); });
uint32_t* fb_addr = static_cast<uint32_t*>(smo_map(smo_key));
if (fb_addr == nullptr)
uint32_t* fb_addr = static_cast<uint32_t*>(shmat(shmid, nullptr, 0));
shmctl(shmid, IPC_RMID, nullptr);
if (fb_addr == SHM_FAILED)
return BAN::Error::from_errno(errno);
memset(fb_addr, 0xFF, fb_bytes);
BAN::ScopeGuard smo_unmapper([&]() { munmap(fb_addr, fb_bytes); });
{
const auto old_area = m_client_area;
@@ -65,19 +62,17 @@ BAN::ErrorOr<void> Window::resize(uint32_t width, uint32_t height)
m_client_area = old_area;
if (title_bar_ret.is_error())
{
shmdt(fb_addr);
return title_bar_ret.release_error();
}
}
smo_deleter.disable();
smo_unmapper.disable();
if (m_fb_addr)
munmap(m_fb_addr, client_width() * client_height() * 4);
if (m_smo_key)
smo_delete(m_smo_key);
if (m_fb_addr != nullptr)
shmdt(m_fb_addr);
m_fb_addr = fb_addr;
m_smo_key = smo_key;
m_shmid = shmid;
m_client_area.max_x = m_client_area.min_x + width;
m_client_area.max_y = m_client_area.min_y + height;
+2 -2
View File
@@ -42,7 +42,7 @@ public:
}
int client_fd() const { return m_client_fd; }
long smo_key() const { return m_smo_key; }
int shmid() const { return m_shmid; }
int32_t client_x() const { return m_client_area.min_x; }
int32_t client_y() const { return m_client_area.min_y; }
@@ -113,7 +113,7 @@ private:
Rectangle m_client_area { 0, 0, 0, 0 };
Rectangle m_min_size { 0, 0, m_title_bar_height, 0 };
Rectangle m_max_size { 0, 0, 10'000, 10'000 };
long m_smo_key { 0 };
int m_shmid { -1 };
uint32_t* m_fb_addr { nullptr };
BAN::String m_title;
@@ -10,7 +10,6 @@
#include <stdlib.h>
#include <sys/banan-os.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <unistd.h>
@@ -128,7 +127,7 @@ void WindowServer::on_window_create(int fd, const LibGUI::WindowPacket::WindowCr
const LibGUI::EventPacket::ResizeWindowEvent event_packet {
.width = static_cast<uint32_t>(window->client_width()),
.height = static_cast<uint32_t>(window->client_height()),
.smo_key = window->smo_key(),
.shmid = window->shmid(),
};
if (auto ret = append_serialized_packet(event_packet, fd); ret.is_error())
{
@@ -731,7 +730,7 @@ void WindowServer::on_mouse_button(LibInput::MouseButtonEvent event)
const LibGUI::EventPacket::ResizeWindowEvent event_packet {
.width = static_cast<uint32_t>(m_focused_window->client_width()),
.height = static_cast<uint32_t>(m_focused_window->client_height()),
.smo_key = m_focused_window->smo_key(),
.shmid = m_focused_window->shmid(),
};
if (auto ret = append_serialized_packet(event_packet, m_focused_window->client_fd()); ret.is_error())
{
@@ -1771,7 +1770,7 @@ bool WindowServer::resize_window(BAN::RefPtr<Window> window, uint32_t width, uin
const LibGUI::EventPacket::ResizeWindowEvent event_packet {
.width = static_cast<uint32_t>(window->client_width()),
.height = static_cast<uint32_t>(window->client_height()),
.smo_key = window->smo_key(),
.shmid = window->shmid(),
};
if (auto ret = append_serialized_packet(event_packet, window->client_fd()); ret.is_error())
{