Kernel/Userspace: Replace my custom SHM system with SysV shm
This allows xbanan to use shm!
This commit is contained in:
@@ -5,8 +5,7 @@
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/banan-os.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
@@ -49,12 +48,10 @@ namespace LibAudio
|
||||
void Audio::clear()
|
||||
{
|
||||
if (m_audio_buffer)
|
||||
munmap(m_audio_buffer, m_smo_size);
|
||||
shmdt(m_audio_buffer);
|
||||
m_audio_buffer = nullptr;
|
||||
|
||||
if (m_smo_key != -1)
|
||||
smo_delete(m_smo_key);
|
||||
m_smo_key = -1;
|
||||
m_shmid = -1;
|
||||
|
||||
if (m_server_fd != -1)
|
||||
close(m_server_fd);
|
||||
@@ -68,14 +65,12 @@ namespace LibAudio
|
||||
clear();
|
||||
|
||||
m_server_fd = other.m_server_fd;
|
||||
m_smo_key = other.m_smo_key;
|
||||
m_smo_size = other.m_smo_size;
|
||||
m_shmid = other.m_shmid;
|
||||
m_audio_buffer = other.m_audio_buffer;
|
||||
m_audio_loader = BAN::move(other.m_audio_loader);
|
||||
|
||||
other.m_server_fd = -1;
|
||||
other.m_smo_key = -1;
|
||||
other.m_smo_size = 0;
|
||||
other.m_shmid = -1;
|
||||
other.m_audio_buffer = nullptr;
|
||||
|
||||
return *this;
|
||||
@@ -83,14 +78,16 @@ namespace LibAudio
|
||||
|
||||
BAN::ErrorOr<void> Audio::initialize(uint32_t total_samples)
|
||||
{
|
||||
m_smo_size = sizeof(AudioBuffer) + total_samples * sizeof(AudioBuffer::sample_t);
|
||||
const size_t shm_size = sizeof(AudioBuffer) + total_samples * sizeof(AudioBuffer::sample_t);
|
||||
|
||||
m_smo_key = smo_create(m_smo_size, PROT_READ | PROT_WRITE);
|
||||
if (m_smo_key == -1)
|
||||
m_shmid = shmget(IPC_PRIVATE, shm_size, 0666);
|
||||
if (m_shmid == -1)
|
||||
return BAN::Error::from_errno(errno);
|
||||
|
||||
m_audio_buffer = static_cast<AudioBuffer*>(smo_map(m_smo_key));
|
||||
if (m_audio_buffer == nullptr)
|
||||
m_audio_buffer = static_cast<AudioBuffer*>(shmat(m_shmid, nullptr, 0));
|
||||
shmctl(m_shmid, IPC_RMID, nullptr);
|
||||
|
||||
if (m_audio_buffer == SHM_FAILED)
|
||||
return BAN::Error::from_errno(errno);
|
||||
new (m_audio_buffer) AudioBuffer();
|
||||
memset(m_audio_buffer->samples, 0, total_samples * sizeof(AudioBuffer::sample_t));
|
||||
@@ -124,7 +121,7 @@ namespace LibAudio
|
||||
|
||||
const LibAudio::Packet packet {
|
||||
.type = LibAudio::Packet::RegisterBuffer,
|
||||
.parameter = static_cast<uint64_t>(m_smo_key),
|
||||
.parameter = static_cast<uint64_t>(m_shmid),
|
||||
};
|
||||
|
||||
const ssize_t nsend = send(m_server_fd, &packet, sizeof(packet), 0);
|
||||
|
||||
@@ -62,8 +62,7 @@ namespace LibAudio
|
||||
|
||||
BAN::UniqPtr<AudioLoader> m_audio_loader;
|
||||
|
||||
long m_smo_key { -1 };
|
||||
size_t m_smo_size { 0 };
|
||||
int m_shmid { -1 };
|
||||
AudioBuffer* m_audio_buffer { nullptr };
|
||||
};
|
||||
|
||||
|
||||
@@ -43,13 +43,6 @@ int poweroff(int command);
|
||||
|
||||
int load_keymap(const char* path);
|
||||
|
||||
// Create shared memory object and return its key or -1 on error
|
||||
long smo_create(size_t size, int prot);
|
||||
// Delete shared memory object such that it will be no longer accessible with smo_map(). Existing mappings are still valid
|
||||
int smo_delete(long key);
|
||||
// Map shared memory object defined by its key and return address or null on error. Mappings can be unmapped using munmap()
|
||||
void* smo_map(long key);
|
||||
|
||||
__END_DECLS
|
||||
|
||||
#endif
|
||||
|
||||
@@ -22,13 +22,15 @@ struct ipc_perm
|
||||
mode_t mode; /* Read/write permission. */
|
||||
};
|
||||
|
||||
#define IPC_CREAT 0x01
|
||||
#define IPC_EXCL 0x02
|
||||
#define IPC_NOWAIT 0x04
|
||||
#define IPC_PRIVATE 0x08
|
||||
#define IPC_RMID 0x10
|
||||
#define IPC_SET 0x20
|
||||
#define IPC_STAT 0x40
|
||||
#define IPC_CREAT 01000
|
||||
#define IPC_EXCL 02000
|
||||
#define IPC_NOWAIT 04000
|
||||
|
||||
#define IPC_PRIVATE 0
|
||||
|
||||
#define IPC_RMID 1
|
||||
#define IPC_SET 2
|
||||
#define IPC_STAT 3
|
||||
|
||||
key_t ftok(const char* path, int id);
|
||||
|
||||
|
||||
@@ -12,14 +12,14 @@ __BEGIN_DECLS
|
||||
#define __need_time_t
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/ipc.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#define SHM_RDONLY 0x01
|
||||
#define SHM_RND 0x02
|
||||
|
||||
#define SHMLBA (sysconf(_SC_PAGE_SIZE))
|
||||
#define SHM_FAILED ((void*)(intptr_t)-1)
|
||||
#define SHMLBA (getpagesize())
|
||||
|
||||
typedef unsigned int shmatt_t;
|
||||
|
||||
|
||||
@@ -72,9 +72,10 @@ __BEGIN_DECLS
|
||||
O(SYS_PSELECT, pselect) \
|
||||
O(SYS_PPOLL, ppoll) \
|
||||
O(SYS_FTRUNCATE, ftruncate) \
|
||||
O(SYS_SMO_CREATE, smo_create) \
|
||||
O(SYS_SMO_DELETE, smo_delete) \
|
||||
O(SYS_SMO_MAP, smo_map) \
|
||||
O(SYS_SHMAT, shmat) \
|
||||
O(SYS_SHMDT, shmdt) \
|
||||
O(SYS_SHMGET, shmget) \
|
||||
O(SYS_SHMCTL, shmctl) \
|
||||
O(SYS_GETSOCKNAME, getsockname) \
|
||||
O(SYS_GETPEERNAME, getpeername) \
|
||||
O(SYS_GETSOCKOPT, getsockopt) \
|
||||
|
||||
@@ -16,21 +16,3 @@ int load_keymap(const char* path)
|
||||
{
|
||||
return syscall(SYS_LOAD_KEYMAP, path);
|
||||
}
|
||||
|
||||
long smo_create(size_t size, int prot)
|
||||
{
|
||||
return syscall(SYS_SMO_CREATE, size, prot);
|
||||
}
|
||||
|
||||
int smo_delete(long key)
|
||||
{
|
||||
return syscall(SYS_SMO_DELETE, key);
|
||||
}
|
||||
|
||||
void* smo_map(long key)
|
||||
{
|
||||
long ret = syscall(SYS_SMO_MAP, key);
|
||||
if (ret < 0)
|
||||
return nullptr;
|
||||
return reinterpret_cast<void*>(ret);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,26 @@
|
||||
#include <BAN/Debug.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define TODO_FUNC(type, name, ...) type name(__VA_ARGS__) { dwarnln("TODO: " #name); errno = ENOTSUP; return (type)-1; }
|
||||
void* shmat(int shmid, const void* shmaddr, int shmflg)
|
||||
{
|
||||
const auto result = syscall(SYS_SHMAT, shmid, shmaddr, shmflg);
|
||||
if (result == -1)
|
||||
return SHM_FAILED;
|
||||
return reinterpret_cast<void*>(result);
|
||||
}
|
||||
|
||||
TODO_FUNC(void*, shmat, int, const void*, int)
|
||||
TODO_FUNC(int, shmctl, int, int, struct shmid_ds*)
|
||||
TODO_FUNC(int, shmdt, const void*)
|
||||
TODO_FUNC(int, shmget, key_t, size_t, int)
|
||||
int shmctl(int shmid, int cmd, struct shmid_ds* buf)
|
||||
{
|
||||
return syscall(SYS_SHMCTL, shmid, cmd, buf);
|
||||
}
|
||||
|
||||
int shmdt(const void* shmaddr)
|
||||
{
|
||||
return syscall(SYS_SHMDT, shmaddr);
|
||||
}
|
||||
|
||||
int shmget(key_t key, size_t size, int shmflg)
|
||||
{
|
||||
return syscall(SYS_SHMGET, key, size, shmflg);
|
||||
}
|
||||
|
||||
@@ -4,9 +4,8 @@
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/banan-os.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <time.h>
|
||||
@@ -165,7 +164,7 @@ namespace LibGUI
|
||||
if (width == m_width)
|
||||
{
|
||||
copy_func(
|
||||
&m_framebuffer_smo[y * m_width],
|
||||
&m_framebuffer_shm[y * m_width],
|
||||
&m_texture.pixels()[y * m_width],
|
||||
width * height * sizeof(uint32_t)
|
||||
);
|
||||
@@ -173,7 +172,7 @@ namespace LibGUI
|
||||
else for (uint32_t y_off = 0; y_off < height; y_off++)
|
||||
{
|
||||
copy_func(
|
||||
&m_framebuffer_smo[(y + y_off) * m_width + x],
|
||||
&m_framebuffer_shm[(y + y_off) * m_width + x],
|
||||
&m_texture.pixels()[(y + y_off) * m_width + x],
|
||||
width * sizeof(uint32_t)
|
||||
);
|
||||
@@ -311,31 +310,31 @@ namespace LibGUI
|
||||
|
||||
void Window::cleanup()
|
||||
{
|
||||
munmap(m_framebuffer_smo, m_width * m_height * 4);
|
||||
shmdt(m_framebuffer_shm);
|
||||
close(m_server_fd);
|
||||
close(m_epoll_fd);
|
||||
}
|
||||
|
||||
BAN::ErrorOr<bool> Window::handle_resize_event(const EventPacket::ResizeWindowEvent& event)
|
||||
{
|
||||
void* framebuffer_addr = smo_map(event.smo_key);
|
||||
if (framebuffer_addr == nullptr)
|
||||
void* framebuffer_addr = shmat(event.shmid, nullptr, 0);
|
||||
if (framebuffer_addr == SHM_FAILED)
|
||||
{
|
||||
if (errno == ENOENT)
|
||||
return false;
|
||||
return BAN::Error::from_errno(errno);
|
||||
}
|
||||
|
||||
if (m_framebuffer_smo)
|
||||
munmap(m_framebuffer_smo, m_width * m_height * 4);
|
||||
m_framebuffer_smo = nullptr;
|
||||
if (m_framebuffer_shm)
|
||||
shmdt(m_framebuffer_shm);
|
||||
m_framebuffer_shm = nullptr;
|
||||
|
||||
TRY(m_texture.resize(event.width, event.height));
|
||||
|
||||
if (m_root_widget)
|
||||
TRY(m_root_widget->set_fixed_geometry({ 0, 0, event.width, event.height }));
|
||||
|
||||
m_framebuffer_smo = static_cast<uint32_t*>(framebuffer_addr);
|
||||
m_framebuffer_shm = static_cast<uint32_t*>(framebuffer_addr);
|
||||
m_width = event.width;
|
||||
m_height = event.height;
|
||||
|
||||
|
||||
@@ -333,7 +333,7 @@ namespace LibGUI
|
||||
ResizeWindowEvent,
|
||||
uint32_t, width,
|
||||
uint32_t, height,
|
||||
long, smo_key
|
||||
long, shmid
|
||||
);
|
||||
|
||||
DEFINE_PACKET_EXTRA(
|
||||
|
||||
@@ -115,7 +115,7 @@ namespace LibGUI
|
||||
|
||||
Attributes m_attributes;
|
||||
|
||||
uint32_t* m_framebuffer_smo { nullptr };
|
||||
uint32_t* m_framebuffer_shm { nullptr };
|
||||
uint32_t m_width { 0 };
|
||||
uint32_t m_height { 0 };
|
||||
|
||||
|
||||
@@ -8,13 +8,13 @@
|
||||
#include <kernel/FS/VirtualFileSystem.h>
|
||||
#include <kernel/Process.h>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <ctype.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
namespace LibInput
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user