Compare commits

...

17 Commits

Author SHA1 Message Date
Bananymous 40617f0d5c WindowServer: Make mod+d open program launcher 2025-05-06 00:42:15 +03:00
Bananymous 09745a7835 userspace: Implement program launcher
This is kinda useless as I only have Terminal and test-window
implemented. Also the code is really messy as I don't have GUI widgets.
2025-05-06 00:41:22 +03:00
Bananymous 57f6f50939 Terminal: Implement dumb font bolding
This is just drawing character twice at one pixel offset
2025-05-05 23:09:53 +03:00
Bananymous f959905adf LibGUI: Rework window rendering
Abstract drawing surface to Texture. All windows now contain a texture
matching its size.
2025-05-05 23:08:01 +03:00
Bananymous f78c7e7926 LibGUI: Add API to block until window events are available 2025-05-05 22:33:00 +03:00
Bananymous 96496da0ab Kernel: Allow USB mouse to send extra2 button events 2025-05-05 22:32:26 +03:00
Bananymous 2dc4733ac1 test: improve pthread test 2025-05-05 19:21:47 +03:00
Bananymous f14774d034 libc: fix tmpnam when no name is found 2025-05-05 19:17:16 +03:00
Bananymous c08c63f420 libc: reorder destructor and fflush in exit 2025-05-05 19:16:52 +03:00
Bananymous eb79c6c47c mkdir: Add -p option 2025-05-05 19:16:29 +03:00
Bananymous bf1cbb4cde Terminal: Update terminal size and send SIGWINCH on resize 2025-05-05 19:09:21 +03:00
Bananymous 592675022e Kernel: Allow tcgetpgrp on pseudo terminal masters 2025-05-05 19:09:01 +03:00
Bananymous b2e10d7e6e Kernel: Remove unnecessary locks 2025-05-05 19:08:57 +03:00
Bananymous 0689954433 Terminal: Set minimum size to 8x2 cells 2025-05-05 18:20:22 +03:00
Bananymous 5c37f198cb WindowServer/LibGUI: Allow windows to set minimum size 2025-05-05 18:19:59 +03:00
Bananymous 3b02a9d4fe WindowServer: Resize from closest corner 2025-05-05 18:19:19 +03:00
Bananymous c9057aa498 WindowServer: Cleanup code and resize on fullscreen 2025-05-05 16:42:15 +03:00
22 changed files with 846 additions and 413 deletions

View File

@ -1568,38 +1568,32 @@ namespace Kernel
BAN::ErrorOr<long> Process::sys_dup2(int fildes, int fildes2)
{
LockGuard _(m_process_lock);
return TRY(m_open_file_descriptors.dup2(fildes, fildes2));
}
BAN::ErrorOr<long> Process::sys_fcntl(int fildes, int cmd, int extra)
{
LockGuard _(m_process_lock);
return TRY(m_open_file_descriptors.fcntl(fildes, cmd, extra));
}
BAN::ErrorOr<long> Process::sys_seek(int fd, off_t offset, int whence)
{
LockGuard _(m_process_lock);
return TRY(m_open_file_descriptors.seek(fd, offset, whence));
}
BAN::ErrorOr<long> Process::sys_tell(int fd)
{
LockGuard _(m_process_lock);
return TRY(m_open_file_descriptors.tell(fd));
}
BAN::ErrorOr<long> Process::sys_truncate(int fd, off_t length)
{
LockGuard _(m_process_lock);
TRY(m_open_file_descriptors.truncate(fd, length));
return 0;
}
BAN::ErrorOr<long> Process::sys_fsync(int fd)
{
LockGuard _(m_process_lock);
auto inode = TRY(m_open_file_descriptors.inode_of(fd));
TRY(inode->fsync());
return 0;
@ -2315,10 +2309,16 @@ namespace Kernel
{
LockGuard _(m_process_lock);
auto inode = TRY(m_open_file_descriptors.inode_of(fd));
// NOTE: This is a hack but I'm not sure how terminal is supposted to get
// slave's foreground pgroup while not having controlling terminal
if (TRY(m_open_file_descriptors.path_of(fd)) == "<ptmx>"_sv)
return TRY(static_cast<PseudoTerminalMaster*>(inode.ptr())->slave())->foreground_pgrp();
if (!m_controlling_terminal)
return BAN::Error::from_errno(ENOTTY);
auto inode = TRY(m_open_file_descriptors.inode_of(fd));
if (!inode->is_tty())
return BAN::Error::from_errno(ENOTTY);

View File

@ -79,7 +79,7 @@ namespace Kernel
}
break;
case 0x09: // button
if (usage == 0 || usage >= m_button_state_temp.size())
if (usage == 0 || usage > m_button_state_temp.size())
break;
m_button_state_temp[usage - 1] = state;
break;

View File

@ -979,9 +979,9 @@ char* tmpnam(char* storage)
struct stat st;
if (stat(storage, &st) == -1 && errno == ENOENT)
break;
return storage;
}
return storage;
return nullptr;
}
int ungetc_unlocked(int c, FILE* stream)

View File

@ -34,8 +34,8 @@ void abort(void)
void exit(int status)
{
fflush(nullptr);
__cxa_finalize(nullptr);
fflush(nullptr);
_exit(status);
ASSERT_NOT_REACHED();
}

View File

@ -1,4 +1,5 @@
set(LIBGUI_SOURCES
Texture.cpp
Window.cpp
)

View File

@ -1,9 +1,7 @@
#include "LibGUI/Window.h"
#include <LibGUI/Window.h>
#include <BAN/ScopeGuard.h>
#include <LibFont/Font.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/banan-os.h>
@ -115,149 +113,13 @@ namespace LibGUI
return window;
}
void Window::fill_rect(int32_t x, int32_t y, uint32_t width, uint32_t height, uint32_t color)
{
if (!clamp_to_framebuffer(x, y, width, height))
return;
for (uint32_t y_off = 0; y_off < height; y_off++)
for (uint32_t x_off = 0; x_off < width; x_off++)
set_pixel(x + x_off, y + y_off, color);
}
void Window::draw_character(uint32_t codepoint, const LibFont::Font& font, int32_t tl_x, int32_t tl_y, uint32_t color)
{
if (tl_y + (int32_t)font.height() < 0 || tl_y >= (int32_t)height())
return;
if (tl_x + (int32_t)font.width() < 0 || tl_x >= (int32_t)width())
return;
auto glyph = font.glyph(codepoint);
if (glyph == nullptr)
return;
for (int32_t off_y = 0; off_y < (int32_t)font.height(); off_y++)
{
if (tl_y + off_y < 0)
continue;
uint32_t abs_y = tl_y + off_y;
if (abs_y >= height())
break;
for (int32_t off_x = 0; off_x < (int32_t)font.width(); off_x++)
{
if (tl_x + off_x < 0)
continue;
uint32_t abs_x = tl_x + off_x;
if (abs_x >= width())
break;
const uint8_t bitmask = 1 << (font.width() - off_x - 1);
if (glyph[off_y * font.pitch()] & bitmask)
set_pixel(abs_x, abs_y, color);
}
}
}
void Window::draw_text(BAN::StringView text, const LibFont::Font& font, int32_t tl_x, int32_t tl_y, uint32_t color)
{
for (size_t i = 0; i < text.size(); i++)
draw_character(text[i], font, tl_x + (int32_t)(i * font.width()), tl_y, color);
}
void Window::shift_vertical(int32_t amount, uint32_t fill_color)
{
const uint32_t amount_abs = BAN::Math::abs(amount);
if (amount_abs == 0 || amount_abs >= height())
return;
uint32_t* dst = (amount > 0) ? m_framebuffer.data() + width() * amount_abs : m_framebuffer.data();
uint32_t* src = (amount < 0) ? m_framebuffer.data() + width() * amount_abs : m_framebuffer.data();
memmove(dst, src, width() * (height() - amount_abs) * 4);
const uint32_t y_lo = (amount < 0) ? height() - amount_abs : 0;
const uint32_t y_hi = (amount < 0) ? height() : amount_abs;
for (uint32_t y = y_lo; y < y_hi; y++)
for (uint32_t x = 0; x < width(); x++)
set_pixel(x, y, fill_color);
}
void Window::copy_horizontal_slice(int32_t dst_y, int32_t src_y, uint32_t uamount, uint32_t fill_color)
{
int32_t amount = uamount;
if (dst_y < 0)
{
amount -= -dst_y;
src_y += -dst_y;
dst_y = 0;
}
amount = BAN::Math::min<int32_t>(amount, height() - dst_y);
if (amount <= 0)
return;
const int32_t copy_src_y = BAN::Math::clamp<int32_t>(src_y, 0, height());
const int32_t copy_amount = BAN::Math::clamp<int32_t>(src_y + amount, 0, height()) - copy_src_y;
if (copy_amount > 0)
{
memmove(
&m_framebuffer[width() * (dst_y + (copy_src_y - src_y))],
&m_framebuffer[width() * copy_src_y],
copy_amount * width() * 4
);
}
const uint32_t fill_y_off = (src_y < copy_src_y) ? 0 : copy_amount;
const uint32_t fill_amount = amount - copy_amount;
for (uint32_t i = 0; i < fill_amount; i++)
for (uint32_t x = 0; x < width(); x++)
set_pixel(x, dst_y + fill_y_off + i, fill_color);
}
void Window::copy_rect(int32_t dst_x, int32_t dst_y, int32_t src_x, int32_t src_y, uint32_t width, uint32_t height, uint32_t fill_color)
{
fill_rect(dst_x, dst_y, width, height, fill_color);
if (!clamp_to_framebuffer(dst_x, dst_y, width, height))
return;
if (!clamp_to_framebuffer(src_x, src_y, width, height))
return;
const bool copy_dir = dst_y < src_y;
for (uint32_t i = 0; i < height; i++)
{
const uint32_t y_off = copy_dir ? i : height - i - 1;
memmove(
&m_framebuffer[(dst_y + y_off) * this->width()],
&m_framebuffer[(src_y + y_off) * this->width()],
width * 4
);
}
}
bool Window::clamp_to_framebuffer(int32_t& signed_x, int32_t& signed_y, uint32_t& width, uint32_t& height) const
{
int32_t min_x = BAN::Math::max<int32_t>(signed_x, 0);
int32_t min_y = BAN::Math::max<int32_t>(signed_y, 0);
int32_t max_x = BAN::Math::min<int32_t>(this->width(), signed_x + (int32_t)width);
int32_t max_y = BAN::Math::min<int32_t>(this->height(), signed_y + (int32_t)height);
if (min_x >= max_x)
return false;
if (min_y >= max_y)
return false;
signed_x = min_x;
signed_y = min_y;
width = max_x - min_x;
height = max_y - min_y;
return true;
}
void Window::invalidate(int32_t x, int32_t y, uint32_t width, uint32_t height)
{
if (!clamp_to_framebuffer(x, y, width, height))
if (!m_texture.clamp_to_texture(x, y, width, height))
return;
for (uint32_t i = 0; i < height; i++)
memcpy(&m_framebuffer_smo[(y + i) * m_width + x], &m_framebuffer[(y + i) * m_width + x], width * sizeof(uint32_t));
memcpy(&m_framebuffer_smo[(y + i) * m_width + x], &m_texture.pixels()[(y + i) * m_width + x], width * sizeof(uint32_t));
WindowPacket::WindowInvalidate packet;
packet.x = x;
@ -306,6 +168,16 @@ namespace LibGUI
return on_socket_error(__FUNCTION__);
}
void Window::set_min_size(uint32_t width, uint32_t height)
{
WindowPacket::WindowSetMinSize packet;
packet.width = width;
packet.height = height;
if (auto ret = packet.send_serialized(m_server_fd); ret.is_error())
return on_socket_error(__FUNCTION__);
}
void Window::set_attributes(Attributes attributes)
{
WindowPacket::WindowSetAttributes packet;
@ -354,21 +226,13 @@ namespace LibGUI
munmap(m_framebuffer_smo, m_width * m_height * 4);
m_framebuffer_smo = nullptr;
BAN::Vector<uint32_t> framebuffer;
TRY(framebuffer.resize(event.width * event.height, m_bg_color));
TRY(m_texture.resize(event.width, event.height));
void* framebuffer_addr = smo_map(event.smo_key);
if (framebuffer_addr == nullptr)
return BAN::Error::from_errno(errno);
const uint32_t min_x = BAN::Math::min(m_width, event.width);
const uint32_t min_y = BAN::Math::min(m_height, event.height);
for (uint32_t y = 0; y < min_y; y++)
for (uint32_t x = 0; x < min_x; x++)
framebuffer[y * event.width + x] = m_framebuffer[y * m_width + x];
m_framebuffer_smo = static_cast<uint32_t*>(framebuffer_addr);
m_framebuffer = BAN::move(framebuffer);
m_width = event.width;
m_height = event.height;
@ -377,10 +241,17 @@ namespace LibGUI
return {};
}
#define TRY_OR_BREAK(...) ({ auto&& e = (__VA_ARGS__); if (e.is_error()) break; e.release_value(); })
void Window::wait_events()
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(m_server_fd, &fds);
select(m_server_fd + 1, &fds, nullptr, nullptr, nullptr);
}
void Window::poll_events()
{
#define TRY_OR_BREAK(...) ({ auto&& e = (__VA_ARGS__); if (e.is_error()) break; e.release_value(); })
for (;;)
{
fd_set fds;
@ -434,6 +305,7 @@ namespace LibGUI
break;
}
}
#undef TRY_OR_BREAK
}
}

View File

@ -164,6 +164,7 @@ namespace LibGUI
WindowSetAttributes,
WindowSetMouseCapture,
WindowSetSize,
WindowSetMinSize,
WindowSetFullscreen,
WindowSetTitle,
@ -227,6 +228,12 @@ namespace LibGUI
uint32_t, height
);
DEFINE_PACKET(
WindowSetMinSize,
uint32_t, width,
uint32_t, height
);
DEFINE_PACKET(
WindowSetFullscreen,
bool, fullscreen

View File

@ -5,6 +5,7 @@
#include <BAN/UniqPtr.h>
#include <LibGUI/Packet.h>
#include <LibGUI/Texture.h>
namespace LibFont { class Font; }
@ -30,39 +31,8 @@ namespace LibGUI
static BAN::ErrorOr<BAN::UniqPtr<Window>> create(uint32_t width, uint32_t height, BAN::StringView title, Attributes attributes = default_attributes);
void set_pixel(uint32_t x, uint32_t y, uint32_t color)
{
ASSERT(x < m_width);
ASSERT(y < m_height);
m_framebuffer[y * m_width + x] = color;
}
uint32_t get_pixel(uint32_t x, uint32_t y) const
{
ASSERT(x < m_width);
ASSERT(y < m_height);
return m_framebuffer[y * m_width + x];
}
BAN::Span<uint32_t> pixels() { return m_framebuffer.span(); }
void fill_rect(int32_t x, int32_t y, uint32_t width, uint32_t height, uint32_t color);
void fill(uint32_t color) { return fill_rect(0, 0, width(), height(), color); }
void draw_character(uint32_t codepoint, const LibFont::Font& font, int32_t x, int32_t y, uint32_t color);
void draw_text(BAN::StringView text, const LibFont::Font& font, int32_t x, int32_t y, uint32_t color);
// shift whole vertically by amount pixels, sign determines the direction
// fill_color is used to fill "new" data
void shift_vertical(int32_t amount, uint32_t fill_color);
// copy horizontal slice [src_y, src_y + amount[ to [dst_y, dst_y + amount[
// fill_color is used when copying data outside of window bounds
void copy_horizontal_slice(int32_t dst_y, int32_t src_y, uint32_t amount, uint32_t fill_color);
// copy rect (src_x, src_y, width, height) to (dst_x, dst_y, width, height)
// fill_color is used when copying data outside of window bounds
void copy_rect(int32_t dst_x, int32_t dst_y, int32_t src_x, int32_t src_y, uint32_t width, uint32_t height, uint32_t fill_color);
Texture& texture() { return m_texture; }
const Texture& texture() const { return m_texture; }
void invalidate(int32_t x, int32_t y, uint32_t width, uint32_t height);
void invalidate() { return invalidate(0, 0, width(), height()); }
@ -76,6 +46,8 @@ namespace LibGUI
Attributes get_attributes() const { return m_attributes; }
void set_attributes(Attributes attributes);
void set_min_size(uint32_t width, uint32_t height);
// send resize request to window server
// actual resize is only done after resize callback is called
void request_resize(uint32_t width, uint32_t height);
@ -83,10 +55,9 @@ namespace LibGUI
uint32_t width() const { return m_width; }
uint32_t height() const { return m_height; }
// used on resize to fill empty space
void set_bg_color(uint32_t bg_color) { m_bg_color = bg_color; }
void wait_events();
void poll_events();
void set_socket_error_callback(BAN::Function<void()> callback) { m_socket_error_callback = callback; }
void set_close_window_event_callback(BAN::Function<void()> callback) { m_close_window_event_callback = callback; }
void set_resize_window_event_callback(BAN::Function<void()> callback) { m_resize_window_event_callback = callback; }
@ -103,8 +74,6 @@ namespace LibGUI
, m_attributes(attributes)
{ }
bool clamp_to_framebuffer(int32_t& x, int32_t& y, uint32_t& width, uint32_t& height) const;
void on_socket_error(BAN::StringView function);
void cleanup();
@ -117,12 +86,11 @@ namespace LibGUI
Attributes m_attributes;
BAN::Vector<uint32_t> m_framebuffer;
uint32_t* m_framebuffer_smo { nullptr };
uint32_t m_width { 0 };
uint32_t m_height { 0 };
uint32_t m_bg_color { 0xFFFFFFFF };
Texture m_texture;
BAN::Function<void()> m_socket_error_callback;
BAN::Function<void()> m_close_window_event_callback;

View File

@ -24,6 +24,7 @@ set(USERSPACE_PROGRAMS
mkdir
nslookup
poweroff
ProgramLauncher
resolver
rm
Shell

View File

@ -0,0 +1,12 @@
set(SOURCES
main.cpp
)
add_executable(ProgramLauncher ${SOURCES})
banan_link_library(ProgramLauncher ban)
banan_link_library(ProgramLauncher libc)
banan_link_library(ProgramLauncher libfont)
banan_link_library(ProgramLauncher libgui)
banan_link_library(ProgramLauncher libinput)
install(TARGETS ProgramLauncher OPTIONAL)

View File

@ -0,0 +1,344 @@
#include <BAN/HashSet.h>
#include <BAN/Sort.h>
#include <BAN/UTF8.h>
#include <BAN/Vector.h>
#include <LibFont/Font.h>
#include <LibGUI/Window.h>
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
static constexpr uint32_t s_line_chars = 64;
static constexpr uint32_t s_list_height = 15;
static constexpr uint32_t s_padding = 3;
static constexpr uint32_t s_margin = 5;
static constexpr uint32_t s_separator_h = 2;
static constexpr uint32_t s_scroll_w = 10;
static constexpr uint32_t s_scroll_h_min = 5;
static constexpr uint32_t s_color_bg1 = 0xCC'404040;
static constexpr uint32_t s_color_bg2 = 0xCC'606060;
static constexpr uint32_t s_color_selected = 0xCC'0000FF;
static constexpr uint32_t s_color_text = 0xCC'FFFFFF;
static constexpr uint32_t s_color_separator = 0xCC'FFFFFF;
static constexpr uint32_t s_color_scroll = 0xCC'808080;
static uint32_t line_w(const LibFont::Font& font)
{
return s_line_chars * font.width() + 2 * s_padding;
}
static uint32_t line_h(const LibFont::Font& font)
{
return font.height() + 2 * s_padding;
}
static const BAN::Vector<BAN::String> get_program_list()
{
BAN::HashSet<BAN::String> paths;
if (const char* path_env = getenv("PATH"))
{
auto path_env_copy = BAN::String(path_env);
const char* token = strtok(path_env_copy.data(), ":");
do
MUST(paths.insert(BAN::StringView(token)));
while ((token = strtok(nullptr, ":")));
}
BAN::HashSet<BAN::String> program_set;
for (const auto& path : paths)
{
DIR* dirp = opendir(path.data());
if (dirp == nullptr)
continue;
dirent* dent;
while ((dent = readdir(dirp)))
{
if (dent->d_type != DT_REG && dent->d_type != DT_LNK)
continue;
struct stat st;
if (fstatat(dirfd(dirp), dent->d_name, &st, 0) == -1)
continue;
if (!S_ISREG(st.st_mode))
continue;
if (!(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
continue;
MUST(program_set.insert(BAN::String(dent->d_name)));
}
closedir(dirp);
}
BAN::Vector<BAN::String> programs;
MUST(programs.reserve(program_set.size()));
for (auto& program : program_set)
MUST(programs.emplace_back(BAN::move(program)));
BAN::sort::sort(programs.begin(), programs.end(),
[](const auto& a, const auto& b) -> bool
{
const size_t min_size = BAN::Math::min(a.size(), b.size());
for (size_t i = 0; i < min_size; i++)
if (a[i] != b[i])
return a[i] < b[i];
return a.size() < b.size();
}
);
return programs;
}
static BAN::Vector<BAN::StringView> get_filtered_program_list(BAN::Span<BAN::String> program_list, BAN::StringView prompt)
{
BAN::Vector<BAN::StringView> filtered_list;
for (const auto& program : program_list)
{
for (size_t i = 0; i + prompt.size() <= program.size(); i++)
{
bool match = true;
for (size_t j = 0; j < prompt.size() && match; j++)
if (tolower(prompt[j]) != tolower(program[i + j]))
match = false;
if (!match)
continue;
MUST(filtered_list.push_back(program.sv()));
break;
}
}
return filtered_list;
}
void render_search_box(LibGUI::Texture& texture, const LibFont::Font& font, BAN::StringView prompt)
{
char buffer[s_line_chars + 1];
snprintf(buffer, sizeof(buffer), "search: %.*s", (int)prompt.size(), prompt.data());
texture.fill(s_color_bg1);
texture.draw_text(buffer, font, s_padding, s_padding, s_color_text);
}
void render_list(LibGUI::Texture& texture, const LibFont::Font& font, BAN::Span<BAN::StringView> programs, size_t selected)
{
texture.fill(s_color_bg1);
const size_t start = selected / s_list_height * s_list_height;
const size_t count = BAN::Math::min<size_t>(s_list_height, programs.size() - start);
const uint32_t line_w = ::line_w(font);
const uint32_t line_h = ::line_h(font);
for (size_t i = 0; i < count; i++)
{
uint32_t color = (i % 2) ? s_color_bg2 : s_color_bg1;
if (start + i == selected)
color = s_color_selected;
texture.fill_rect(0, line_h * i, line_w, line_h, color);
texture.draw_text(programs[start + i], font, s_padding, s_padding + line_h * i, s_color_text);
}
}
void render_scroll(LibGUI::Texture& texture, BAN::Span<BAN::StringView> programs, size_t selected)
{
texture.fill(s_color_bg1);
if (programs.empty())
return;
texture.fill_rect(
s_padding,
texture.height() * selected / programs.size(),
s_scroll_w,
BAN::Math::max<uint32_t>(texture.height() / programs.size(), s_scroll_h_min),
s_color_scroll
);
}
void render_initial_window(LibGUI::Window& window, const LibFont::Font& font)
{
auto& texture = window.texture();
texture.fill(s_color_bg1);
texture.fill_rect(s_margin, s_margin + line_h(font) + s_padding, line_w(font), s_separator_h, s_color_separator);
}
struct Rectangle { uint32_t x, y, w, h; };
int main()
{
auto attributes = LibGUI::Window::default_attributes;
attributes.alpha_channel = true;
attributes.title_bar = false;
auto font = MUST(LibFont::Font::load("/usr/share/fonts/lat0-16.psfu"_sv));
const auto full_program_list = get_program_list();
// FIXME: implement widgets
const Rectangle search_area {
.x = s_margin,
.y = s_margin,
.w = line_w(font),
.h = line_h(font),
};
const Rectangle list_area {
.x = s_margin,
.y = search_area.x + search_area.h + s_padding + s_separator_h + s_padding,
.w = line_w(font),
.h = line_h(font) * s_list_height,
};
const Rectangle scroll_area {
.x = list_area.x + list_area.w - (s_padding + s_scroll_w + s_padding),
.y = list_area.y,
.w = s_padding + s_scroll_w + s_padding,
.h = list_area.h,
};
auto search_texture = MUST(LibGUI::Texture::create(
search_area.w,
search_area.h,
s_color_bg1
));
auto list_texture = MUST(LibGUI::Texture::create(
list_area.w,
list_area.h,
s_color_bg1
));
auto scroll_texture = MUST(LibGUI::Texture::create(
scroll_area.w,
scroll_area.h,
s_color_bg1
));
auto window = MUST(LibGUI::Window::create(
scroll_area.x + scroll_area.w + s_margin,
scroll_area.y + scroll_area.h + s_margin,
""_sv,
attributes
));
BAN::String prompt;
size_t selected = 0;
auto filtered_list = get_filtered_program_list(full_program_list.span(), prompt);
const auto refresh_selected =
[&]()
{
render_list(list_texture, font, filtered_list.span(), selected);
window->texture().copy_texture(list_texture, list_area.x, list_area.y);
window->invalidate(list_area.x, list_area.y, list_area.w, list_area.h);
if (filtered_list.size() > s_list_height)
{
render_scroll(scroll_texture, filtered_list.span(), selected);
window->texture().copy_texture(scroll_texture, scroll_area.x, scroll_area.y);
window->invalidate(scroll_area.x, scroll_area.y, scroll_area.w, scroll_area.h);
}
};
const auto refresh_search =
[&]()
{
selected = 0;
filtered_list = get_filtered_program_list(full_program_list.span(), prompt);
render_search_box(search_texture, font, prompt);
window->texture().copy_texture(search_texture, search_area.x, search_area.y);
window->invalidate(search_area.x, search_area.y, search_area.w, search_area.h);
render_list(list_texture, font, filtered_list.span(), selected);
window->texture().copy_texture(list_texture, list_area.x, list_area.y);
window->invalidate(list_area.x, list_area.y, list_area.w, list_area.h);
if (filtered_list.size() > s_list_height)
{
render_scroll(scroll_texture, filtered_list.span(), selected);
window->texture().copy_texture(scroll_texture, scroll_area.x, scroll_area.y);
window->invalidate(scroll_area.x, scroll_area.y, scroll_area.w, scroll_area.h);
}
};
window->set_key_event_callback(
[&](LibGUI::EventPacket::KeyEvent::event_t event)
{
if (!event.pressed())
return;
switch (event.key)
{
case LibInput::Key::ArrowUp:
selected = (selected + filtered_list.size() - 1) % filtered_list.size();
refresh_selected();
break;
case LibInput::Key::ArrowDown:
selected = (selected + 1) % filtered_list.size();
refresh_selected();
break;
case LibInput::Key::Escape:
exit(0);
case LibInput::Key::Enter:
{
const char* program = filtered_list.empty() ? prompt.data() : filtered_list[selected].data();
int null = open("/dev/null", O_RDWR | O_CLOEXEC);
if (null == -1)
dwarnln("open: {}", strerror(errno));
else
{
dup2(null, STDIN_FILENO);
dup2(null, STDOUT_FILENO);
dup2(null, STDERR_FILENO);
}
execlp(program, program, nullptr);
dwarnln("execlp: {}", strerror(errno));
exit(1);
}
case LibInput::Key::Backspace:
if (prompt.empty())
break;
while (!prompt.empty() && (prompt.back() & 0xC0) == 0x80)
prompt.pop_back();
if (!prompt.empty())
prompt.pop_back();
refresh_search();
break;
default:
const char* utf8 = LibInput::key_to_utf8(event.key, event.modifier);
if (utf8 == nullptr)
break;
MUST(prompt.append(utf8));
refresh_search();
break;
}
}
);
render_initial_window(*window, font);
refresh_search();
window->invalidate();
for (;;)
{
window->wait_events();
window->poll_events();
}
}

View File

@ -90,7 +90,7 @@ int main()
window->set_position(0, 0);
window->fill(bg_color);
window->texture().fill(bg_color);
window->invalidate();
bool is_running = true;
@ -108,8 +108,9 @@ int main()
const uint32_t text_x = window->width() - text_w - padding;
const uint32_t text_y = padding;
window->fill_rect(text_x, text_y, text_w, text_h, bg_color);
window->draw_text(text, font, text_x, text_y, fg_color);
auto& texture = window->texture();
texture.fill_rect(text_x, text_y, text_w, text_h, bg_color);
texture.draw_text(text, font, text_x, text_y, fg_color);
window->invalidate(text_x, text_y, text_w, text_h);
};

View File

@ -119,12 +119,14 @@ void Terminal::run()
attributes.resizable = true;
m_window = MUST(LibGUI::Window::create(600, 400, "Terminal"_sv, attributes));
m_window->fill(m_bg_color);
m_window->texture().fill(m_bg_color);
m_window->texture().set_bg_color(m_bg_color);
m_window->invalidate();
m_window->set_bg_color(m_bg_color);
m_font = MUST(LibFont::Font::load("/usr/share/fonts/lat0-16.psfu"_sv));
m_window->set_min_size(m_font.width() * 8, m_font.height() * 2);
{
winsize winsize;
winsize.ws_col = cols();
@ -147,23 +149,46 @@ void Terminal::run()
m_window->set_resize_window_event_callback([&] {
if (const auto rem = m_window->height() % m_font.height())
{
m_window->fill_rect(0, m_window->height() - rem, m_window->width(), rem, m_bg_color);
m_window->texture().fill_rect(0, m_window->height() - rem, m_window->width(), rem, m_bg_color);
m_window->invalidate(0, m_window->height() - rem, m_window->width(), rem);
}
if (const auto rem = m_window->width() % m_font.width())
{
m_window->fill_rect(m_window->width() - rem, 0, rem, m_window->height(), m_bg_color);
m_window->texture().fill_rect(m_window->width() - rem, 0, rem, m_window->height(), m_bg_color);
m_window->invalidate(m_window->width() - rem, 0, rem, m_window->height());
}
if (m_cursor.x < cols() && m_cursor.y < rows())
return;
if (m_cursor.x >= cols() || m_cursor.y >= rows())
{
m_cursor.x = BAN::Math::min(m_cursor.x, cols() - 1);
m_cursor.y = BAN::Math::min(m_cursor.y, rows() - 1);
for (auto& pixel : m_cursor_buffer)
pixel = m_bg_color;
}
m_cursor.x = BAN::Math::min(m_cursor.x, cols() - 1);
m_cursor.y = BAN::Math::min(m_cursor.y, rows() - 1);
for (auto& pixel : m_cursor_buffer)
pixel = m_bg_color;
const winsize winsize {
.ws_row = static_cast<unsigned short>(rows()),
.ws_col = static_cast<unsigned short>(cols()),
};
if (ioctl(m_shell_info.pts_master, TIOCSWINSZ, &winsize) == -1)
{
perror("ioctl");
return;
}
const pid_t fgpgrp = tcgetpgrp(m_shell_info.pts_master);
if (fgpgrp == -1)
{
perror("tcgetpgrp");
return;
}
if (kill(-fgpgrp, SIGWINCH) == -1)
{
perror("kill");
return;
}
});
const int max_fd = BAN::Math::max(m_shell_info.pts_master, m_window->server_fd());
@ -210,11 +235,12 @@ void Terminal::hide_cursor()
{
if (m_cursor.x == cols())
return;
auto& texture = m_window->texture();
const uint32_t cursor_base_x = m_cursor.x * m_font.width();
const uint32_t cursor_base_y = m_cursor.y * m_font.height();
for (uint32_t y = 0; y < m_font.height(); y++)
for (uint32_t x = 0; x < m_font.width(); x++)
m_window->set_pixel(cursor_base_x + x, cursor_base_y + y, m_cursor_buffer[y * m_font.width() + x]);
texture.set_pixel(cursor_base_x + x, cursor_base_y + y, m_cursor_buffer[y * m_font.width() + x]);
m_window->invalidate(cursor_base_x, cursor_base_y, m_font.width(), m_font.height());
}
@ -222,16 +248,17 @@ void Terminal::show_cursor()
{
if (m_cursor.x == cols())
return;
auto& texture = m_window->texture();
const uint32_t cursor_base_x = m_cursor.x * m_font.width();
const uint32_t cursor_base_y = m_cursor.y * m_font.height();
for (uint32_t y = 0; y < m_font.height(); y++)
for (uint32_t x = 0; x < m_font.width(); x++)
m_cursor_buffer[y * m_font.width() + x] = m_window->get_pixel(cursor_base_x + x, cursor_base_y + y);
m_cursor_buffer[y * m_font.width() + x] = texture.get_pixel(cursor_base_x + x, cursor_base_y + y);
if (m_cursor_shown && m_cursor_blink_shown)
{
for (uint32_t y = m_font.height() * 13 / 16; y < m_font.height() - 1; y++)
for (uint32_t x = 0; x < m_font.width(); x++)
m_window->set_pixel(cursor_base_x + x, cursor_base_y + y, 0xFFFFFFFF);
texture.set_pixel(cursor_base_x + x, cursor_base_y + y, 0xFFFFFFFF);
m_window->invalidate(cursor_base_x, cursor_base_y, m_font.width(), m_font.height());
}
}
@ -281,7 +308,7 @@ bool Terminal::read_shell()
{
const uint32_t scroll = m_cursor.y + newline_count - rows() + 1;
m_cursor.y -= scroll;
m_window->shift_vertical(-scroll * (int32_t)m_font.height(), m_bg_color);
m_window->texture().shift_vertical(-scroll * (int32_t)m_font.height(), m_bg_color);
should_invalidate = { 0, 0, m_window->width(), m_window->height() };
}
@ -311,9 +338,10 @@ void Terminal::handle_sgr(int32_t value)
m_bg_color = s_default_bg_color;
m_fg_color = s_default_fg_color;
m_colors_inverted = false;
m_is_bold = false;
break;
case 1:
// FIXME: bold
m_is_bold = true;
break;
case 7:
m_colors_inverted = true;
@ -409,6 +437,8 @@ Rectangle Terminal::handle_csi(char ch)
}
Rectangle should_invalidate;
auto& texture = m_window->texture();
switch (ch)
{
case 'A':
@ -483,7 +513,7 @@ Rectangle Terminal::handle_csi(char ch)
for (size_t i = 0; i < rect_count; i++)
{
m_window->fill_rect(rects[i].x, rects[i].y, rects[i].width, rects[i].height, m_bg_color);
texture.fill_rect(rects[i].x, rects[i].y, rects[i].width, rects[i].height, m_bg_color);
should_invalidate = should_invalidate.get_bounding_box(rects[i]);
}
@ -499,7 +529,7 @@ Rectangle Terminal::handle_csi(char ch)
rect.width = (m_csi_info.fields[0] == 1) ? m_cursor.x * m_font.width() : m_window->width() - rect.x;
rect.height = m_font.height();
m_window->fill_rect(rect.x, rect.y, rect.width, rect.height, m_bg_color);
texture.fill_rect(rect.x, rect.y, rect.width, rect.height, m_bg_color);
should_invalidate = rect;
break;
@ -510,8 +540,8 @@ Rectangle Terminal::handle_csi(char ch)
const uint32_t src_y = m_cursor.y * m_font.height();
const uint32_t dst_y = src_y + count * m_font.height();
m_window->copy_horizontal_slice(dst_y, src_y, m_window->height() - dst_y, m_bg_color);
m_window->fill_rect(0, src_y, m_window->width(), count * m_font.height(), m_bg_color);
texture.copy_horizontal_slice(dst_y, src_y, m_window->height() - dst_y, m_bg_color);
texture.fill_rect(0, src_y, m_window->width(), count * m_font.height(), m_bg_color);
should_invalidate = {
0,
src_y,
@ -527,8 +557,8 @@ Rectangle Terminal::handle_csi(char ch)
const uint32_t dst_y = m_cursor.y * m_font.height();
const uint32_t src_y = dst_y + count * m_font.height();
m_window->copy_horizontal_slice(dst_y, src_y, m_window->height() - dst_y, m_bg_color);
m_window->fill_rect(0, m_window->height() - count * m_font.height(), m_window->width(), count * m_font.height(), m_bg_color);
texture.copy_horizontal_slice(dst_y, src_y, m_window->height() - dst_y, m_bg_color);
texture.fill_rect(0, m_window->height() - count * m_font.height(), m_window->width(), count * m_font.height(), m_bg_color);
should_invalidate = {
0,
src_y,
@ -545,8 +575,8 @@ Rectangle Terminal::handle_csi(char ch)
const uint32_t src_x = m_cursor.x * m_font.width();
const uint32_t y = m_cursor.y * m_font.height();
m_window->copy_rect(dst_x, y, src_x, y, m_window->width() - dst_x, m_font.height(), m_bg_color);
m_window->fill_rect(src_x, y, count * m_font.width(), m_font.height(), m_bg_color);
texture.copy_rect(dst_x, y, src_x, y, m_window->width() - dst_x, m_font.height(), m_bg_color);
texture.fill_rect(src_x, y, count * m_font.width(), m_font.height(), m_bg_color);
should_invalidate = {
src_x,
y,
@ -622,6 +652,7 @@ Rectangle Terminal::putcodepoint(uint32_t codepoint)
{
Rectangle should_invalidate;
auto& texture = m_window->texture();
switch (codepoint)
{
case '\e':
@ -650,7 +681,7 @@ Rectangle Terminal::putcodepoint(uint32_t codepoint)
{
const uint32_t scroll = m_cursor.y - rows() + 1;
m_cursor.y -= scroll;
m_window->shift_vertical(-scroll * (int32_t)m_font.height(), m_bg_color);
texture.shift_vertical(-scroll * (int32_t)m_font.height(), m_bg_color);
should_invalidate = { 0, 0, m_window->width(), m_window->height() };
}
@ -662,8 +693,10 @@ Rectangle Terminal::putcodepoint(uint32_t codepoint)
const auto fg_color = m_colors_inverted ? m_bg_color : m_fg_color;
const auto bg_color = m_colors_inverted ? m_fg_color : m_bg_color;
m_window->fill_rect(cell_x, cell_y, cell_w, cell_h, bg_color);
m_window->draw_character(codepoint, m_font, cell_x, cell_y, fg_color);
texture.fill_rect(cell_x, cell_y, cell_w, cell_h, bg_color);
texture.draw_character(codepoint, m_font, cell_x, cell_y, fg_color);
if (m_is_bold)
texture.draw_character(codepoint, m_font, cell_x + 1, cell_y, fg_color);
m_last_graphic_char = codepoint;
should_invalidate = { cell_x, cell_y, cell_w, cell_h };
m_cursor.x++;
@ -675,7 +708,7 @@ Rectangle Terminal::putcodepoint(uint32_t codepoint)
{
const uint32_t scroll = m_cursor.y - rows() + 1;
m_cursor.y -= scroll;
m_window->shift_vertical(-scroll * (int32_t)m_font.height(), m_bg_color);
texture.shift_vertical(-scroll * (int32_t)m_font.height(), m_bg_color);
should_invalidate = { 0, 0, m_window->width(), m_window->height() };
}

View File

@ -102,4 +102,5 @@ private:
uint32_t m_fg_color { 0 };
uint32_t m_bg_color { 0 };
bool m_colors_inverted { false };
bool m_is_bold { false };
};

View File

@ -52,6 +52,9 @@ public:
LibGUI::Window::Attributes get_attributes() const { return m_attributes; };
void set_attributes(LibGUI::Window::Attributes attributes) { m_attributes = attributes; };
Rectangle get_min_size() const { return m_min_size; }
void set_min_size(Rectangle min_size) { m_min_size = min_size.get_bounding_box({ 0, 0, m_title_bar_height, 0 }); }
BAN::ErrorOr<void> set_title(BAN::StringView title) { m_title.clear(); TRY(m_title.append(title)); TRY(prepare_title_bar()); return {}; }
const uint32_t* framebuffer() const { return m_fb_addr; }
@ -84,6 +87,7 @@ private:
const int m_client_fd { -1 };
Rectangle m_client_area { 0, 0, 0, 0 };
Rectangle m_min_size { 0, 0, m_title_bar_height, 0 };
long m_smo_key { 0 };
uint32_t* m_fb_addr { nullptr };
BAN::String m_title;

View File

@ -101,7 +101,7 @@ void WindowServer::on_window_invalidate(int fd, const LibGUI::WindowPacket::Wind
if (packet.width == 0 || packet.height == 0)
return;
if (m_is_fullscreen_window)
if (m_state == State::Fullscreen)
{
ASSERT(m_focused_window);
if (m_focused_window->client_fd() != fd)
@ -133,8 +133,12 @@ void WindowServer::on_window_invalidate(int fd, const LibGUI::WindowPacket::Wind
void WindowServer::on_window_set_position(int fd, const LibGUI::WindowPacket::WindowSetPosition& packet)
{
if (m_is_fullscreen_window && m_focused_window->client_fd() == fd)
return;
if (m_state == State::Fullscreen)
{
ASSERT(m_focused_window);
if (m_focused_window->client_fd() != fd)
return;
}
BAN::RefPtr<Window> target_window;
for (auto& window : m_client_windows)
@ -251,37 +255,51 @@ void WindowServer::on_window_set_size(int fd, const LibGUI::WindowPacket::Window
const uint32_t width = packet.width ? packet.width : m_framebuffer.width;
const uint32_t height = packet.height ? packet.height : m_framebuffer.height;
if (auto ret = target_window->resize(width, height); ret.is_error())
{
dwarnln("could not resize client window {}", ret.error());
if (!resize_window(target_window, width, height))
return;
}
LibGUI::EventPacket::ResizeWindowEvent response;
response.width = target_window->client_width();
response.height = target_window->client_height();
response.smo_key = target_window->smo_key();
if (auto ret = response.send_serialized(fd); ret.is_error())
{
dwarnln("could not respond to window resize request: {}", ret.error());
return;
}
invalidate(target_window->full_area().get_bounding_box(old_area));
}
void WindowServer::on_window_set_min_size(int fd, const LibGUI::WindowPacket::WindowSetMinSize& packet)
{
BAN::RefPtr<Window> target_window;
for (auto& window : m_client_windows)
{
if (window->client_fd() != fd)
continue;
target_window = window;
break;
}
if (!target_window)
{
dwarnln("client tried to set window min size while not owning a window");
return;
}
target_window->set_min_size({ 0, 0, static_cast<int32_t>(packet.width), static_cast<int32_t>(packet.height) });
}
void WindowServer::on_window_set_fullscreen(int fd, const LibGUI::WindowPacket::WindowSetFullscreen& packet)
{
if (m_is_fullscreen_window)
if (m_state == State::Fullscreen)
{
ASSERT(m_focused_window);
if (m_focused_window->client_fd() != fd)
dwarnln("client tried to set fullscreen window size while another window is already fullscreen");
else if (!packet.fullscreen)
{
m_is_fullscreen_window = false;
invalidate(m_framebuffer.area());
dwarnln("client tried to set fullscreen state while another window is fullscreen");
return;
}
if (packet.fullscreen)
return;
if (m_focused_window->get_attributes().resizable)
{
if (!resize_window(m_focused_window, m_non_full_screen_rect.width, m_non_full_screen_rect.height))
return;
m_focused_window->set_position({ m_non_full_screen_rect.x, m_non_full_screen_rect.y });
}
m_state = State::Normal;
invalidate(m_framebuffer.area());
return;
}
@ -303,7 +321,16 @@ void WindowServer::on_window_set_fullscreen(int fd, const LibGUI::WindowPacket::
return;
}
m_is_fullscreen_window = true;
if (target_window->get_attributes().resizable)
{
const auto old_area = target_window->client_area();
if (!resize_window(target_window, m_framebuffer.width, m_framebuffer.height))
return;
target_window->set_position({ 0, 0 });
m_non_full_screen_rect = old_area;
}
m_state = State::Fullscreen;
set_focused_window(target_window);
invalidate(m_framebuffer.area());
}
@ -337,7 +364,7 @@ void WindowServer::on_window_set_title(int fd, const LibGUI::WindowPacket::Windo
void WindowServer::on_key_event(LibInput::KeyEvent event)
{
// Mod key is not passed to clients
if (event.key == LibInput::Key::Super)
if (event.key == LibInput::Key::LeftAlt)
{
m_is_mod_key_held = event.pressed();
return;
@ -364,11 +391,33 @@ void WindowServer::on_key_event(LibInput::KeyEvent event)
return;
}
// Start program launcher with mod+d
if (m_is_mod_key_held && event.pressed() && event.key == LibInput::Key::D)
{
pid_t pid = fork();
if (pid == 0)
{
execl("/usr/bin/ProgramLauncher", "ProgramLauncher", nullptr);
exit(1);
}
if (pid == -1)
perror("fork");
return;
}
// Toggle window bounce with F2
if (event.pressed() && event.key == LibInput::Key::F2)
{
m_is_bouncing_window = !m_is_bouncing_window;
return;
}
if (!m_focused_window)
return;
// Kill window with mod+Q
if (m_is_mod_key_held && event.pressed() && event.key == LibInput::Key::Q)
{
if (!m_focused_window)
return;
LibGUI::EventPacket::CloseWindowEvent packet;
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
dwarnln("could not send window close event: {}", ret.error());
@ -377,29 +426,37 @@ void WindowServer::on_key_event(LibInput::KeyEvent event)
if (m_is_mod_key_held && event.pressed() && event.key == LibInput::Key::F)
{
if (!m_focused_window)
return;
m_is_fullscreen_window = !m_is_fullscreen_window;
if (m_is_fullscreen_window)
if (m_state == State::Fullscreen)
{
m_is_moving_window = false;
m_is_resizing_window = false;
if (m_focused_window->get_attributes().resizable)
{
if (!resize_window(m_focused_window, m_non_full_screen_rect.width, m_non_full_screen_rect.height))
return;
m_focused_window->set_position({ m_non_full_screen_rect.x, m_non_full_screen_rect.y });
}
m_state = State::Normal;
}
else
{
if (m_focused_window->get_attributes().resizable)
{
const auto old_area = m_focused_window->client_area();
if (!resize_window(m_focused_window, m_framebuffer.width, m_framebuffer.height))
return;
m_focused_window->set_position({ 0, 0 });
m_non_full_screen_rect = old_area;
}
m_state = State::Fullscreen;
}
invalidate(m_framebuffer.area());
return;
}
// Toggle window bounce with F2
if (!m_is_fullscreen_window && event.pressed() && event.key == LibInput::Key::F2)
m_is_bouncing_window = !m_is_bouncing_window;
if (m_focused_window)
{
LibGUI::EventPacket::KeyEvent packet;
packet.event = event;
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
dwarnln("could not send key event: {}", ret.error());
}
LibGUI::EventPacket::KeyEvent packet;
packet.event = event;
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
dwarnln("could not send key event: {}", ret.error());
}
void WindowServer::on_mouse_button(LibInput::MouseButtonEvent event)
@ -428,92 +485,97 @@ void WindowServer::on_mouse_button(LibInput::MouseButtonEvent event)
}
}
if (m_is_moving_window && event.button == LibInput::MouseButton::Left && !event.pressed)
switch (m_state)
{
m_is_moving_window = false;
return;
}
case State::Normal:
if (!target_window)
break;
if (m_is_resizing_window && event.button == LibInput::MouseButton::Right && !event.pressed)
{
const auto resize_area = this->resize_area(m_cursor);
m_is_resizing_window = false;
invalidate(resize_area.get_bounding_box(m_focused_window->full_area()));
if (target_window->get_attributes().focusable)
set_focused_window(target_window);
const auto old_area = m_focused_window->full_area();
if (auto ret = m_focused_window->resize(resize_area.width, resize_area.height - m_focused_window->title_bar_height()); ret.is_error())
{
dwarnln("could not resize client window {}", ret.error());
return;
}
if (event.button == LibInput::MouseButton::Left && event.pressed)
{
const bool can_start_move = m_is_mod_key_held || target_window->title_text_area().contains(m_cursor);
if (can_start_move && target_window->get_attributes().movable)
{
m_state = State::Moving;
break;
}
}
LibGUI::EventPacket::ResizeWindowEvent event;
event.width = m_focused_window->client_width();
event.height = m_focused_window->client_height();
event.smo_key = m_focused_window->smo_key();
if (auto ret = event.send_serialized(m_focused_window->client_fd()); ret.is_error())
{
dwarnln("could not respond to window resize request: {}", ret.error());
return;
}
if (event.button == LibInput::MouseButton::Right && event.pressed)
{
const bool can_start_resize = m_is_mod_key_held;
if (can_start_resize && target_window->get_attributes().resizable)
{
m_state = State::Resizing;
m_resize_start = m_cursor;
invalidate(m_focused_window->full_area().get_bounding_box(old_area));
const bool right = m_cursor.x >= target_window->full_x() + target_window->full_width() / 2;
const bool bottom = m_cursor.y >= target_window->full_y() + target_window->full_height() / 2;
m_resize_quadrant = right + 2 * bottom;
return;
}
break;
}
}
// Ignore mouse button events which are not on top of a window
if (!target_window)
return;
if (event.button == LibInput::MouseButton::Left && !event.pressed && target_window->close_button_area().contains(m_cursor))
{
LibGUI::EventPacket::CloseWindowEvent packet;
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
dwarnln("could not send close window event: {}", ret.error());
break;
}
if (target_window->get_attributes().focusable)
set_focused_window(target_window);
[[fallthrough]];
case State::Fullscreen:
if (target_window && target_window->client_area().contains(m_cursor))
{
LibGUI::EventPacket::MouseButtonEvent packet;
packet.event.button = event.button;
packet.event.pressed = event.pressed;
packet.event.x = m_cursor.x - m_focused_window->client_x();
packet.event.y = m_cursor.y - m_focused_window->client_y();
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
{
dwarnln("could not send mouse button event: {}", ret.error());
return;
}
}
break;
case State::Moving:
if (event.button == LibInput::MouseButton::Left && !event.pressed)
m_state = State::Normal;
break;
case State::Resizing:
if (event.button == LibInput::MouseButton::Right && !event.pressed)
{
const auto resize_area = this->resize_area(m_cursor);
m_state = State::Normal;
invalidate(resize_area.get_bounding_box(m_focused_window->full_area()));
if (!m_is_fullscreen_window && event.button == LibInput::MouseButton::Left)
{
const bool can_start_move = m_is_mod_key_held || target_window->title_text_area().contains(m_cursor);
if (can_start_move && !m_is_moving_window && event.pressed)
{
m_is_moving_window = target_window->get_attributes().movable;
return;
}
}
const auto old_area = m_focused_window->full_area();
if (auto ret = m_focused_window->resize(resize_area.width, resize_area.height - m_focused_window->title_bar_height()); ret.is_error())
{
dwarnln("could not resize client window {}", ret.error());
return;
}
m_focused_window->set_position({ resize_area.x, resize_area.y + m_focused_window->title_bar_height() });
if (!m_is_fullscreen_window && event.button == LibInput::MouseButton::Right)
{
const bool can_start_resize = m_is_mod_key_held;
if (can_start_resize && !m_is_resizing_window && event.pressed)
{
m_is_resizing_window = target_window->get_attributes().resizable;
if (m_is_resizing_window)
m_resize_start = m_cursor;
return;
}
}
LibGUI::EventPacket::ResizeWindowEvent event;
event.width = m_focused_window->client_width();
event.height = m_focused_window->client_height();
event.smo_key = m_focused_window->smo_key();
if (auto ret = event.send_serialized(m_focused_window->client_fd()); ret.is_error())
{
dwarnln("could not respond to window resize request: {}", ret.error());
return;
}
if (!event.pressed && event.button == LibInput::MouseButton::Left && target_window->close_button_area().contains(m_cursor))
{
// NOTE: we always have target window if code reaches here
LibGUI::EventPacket::CloseWindowEvent packet;
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
{
dwarnln("could not send close window event: {}", ret.error());
return;
}
}
else if (target_window->client_area().contains(m_cursor))
{
// NOTE: we always have target window if code reaches here
LibGUI::EventPacket::MouseButtonEvent packet;
packet.event.button = event.button;
packet.event.pressed = event.pressed;
packet.event.x = m_cursor.x - m_focused_window->client_x();
packet.event.y = m_cursor.y - m_focused_window->client_y();
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
{
dwarnln("could not send mouse button event: {}", ret.error());
return;
}
invalidate(m_focused_window->full_area().get_bounding_box(old_area));
}
break;
}
}
@ -536,7 +598,7 @@ void WindowServer::on_mouse_move(LibInput::MouseMoveEvent event)
{
const int32_t new_x = m_cursor.x + event.rel_x;
const int32_t new_y = m_cursor.y - event.rel_y;
return m_is_fullscreen_window
return (m_state == State::Fullscreen)
? Position {
.x = BAN::Math::clamp(new_x, m_focused_window->client_x(), m_focused_window->client_x() + m_focused_window->client_width()),
.y = BAN::Math::clamp(new_y, m_focused_window->client_y(), m_focused_window->client_y() + m_focused_window->client_height())
@ -568,38 +630,42 @@ void WindowServer::on_mouse_move(LibInput::MouseMoveEvent event)
invalidate(title_bar);
}
if (m_is_moving_window)
{
auto old_window = m_focused_window->full_area();
m_focused_window->set_position({
m_focused_window->client_x() + event.rel_x,
m_focused_window->client_y() + event.rel_y,
});
auto new_window = m_focused_window->full_area();
invalidate(old_window);
invalidate(new_window);
if (!m_focused_window)
return;
}
if (m_is_resizing_window)
switch (m_state)
{
const auto max_cursor = Position {
.x = BAN::Math::max(old_cursor.x, new_cursor.x),
.y = BAN::Math::max(old_cursor.y, new_cursor.y),
};
invalidate(resize_area(max_cursor).get_bounding_box(m_focused_window->full_area()));
return;
}
if (m_focused_window)
{
LibGUI::EventPacket::MouseMoveEvent packet;
packet.event.x = m_cursor.x - m_focused_window->client_x();
packet.event.y = m_cursor.y - m_focused_window->client_y();
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
case State::Normal:
case State::Fullscreen:
{
dwarnln("could not send mouse move event: {}", ret.error());
return;
LibGUI::EventPacket::MouseMoveEvent packet;
packet.event.x = m_cursor.x - m_focused_window->client_x();
packet.event.y = m_cursor.y - m_focused_window->client_y();
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
{
dwarnln("could not send mouse move event: {}", ret.error());
return;
}
break;
}
case State::Moving:
{
auto old_window = m_focused_window->full_area();
m_focused_window->set_position({
m_focused_window->client_x() + event.rel_x,
m_focused_window->client_y() + event.rel_y,
});
auto new_window = m_focused_window->full_area();
invalidate(old_window);
invalidate(new_window);
break;
}
case State::Resizing:
{
const auto old_resize_area = resize_area({ old_cursor.x, old_cursor.y });
const auto new_resize_area = resize_area({ new_cursor.x, new_cursor.y });
invalidate(old_resize_area.get_bounding_box(new_resize_area));
break;
}
}
}
@ -676,7 +742,7 @@ void WindowServer::invalidate(Rectangle area)
return color;
};
if (m_is_fullscreen_window)
if (m_state == State::Fullscreen)
{
ASSERT(m_focused_window);
area.x -= m_focused_window->client_x();
@ -970,7 +1036,7 @@ void WindowServer::invalidate(Rectangle area)
}
}
if (m_is_resizing_window)
if (m_state == State::Resizing)
{
if (const auto overlap = resize_area(m_cursor).get_overlap(area); overlap.has_value())
{
@ -1141,15 +1207,55 @@ Rectangle WindowServer::cursor_area() const
Rectangle WindowServer::resize_area(Position cursor) const
{
ASSERT(m_is_resizing_window);
const auto min_size = m_focused_window->get_min_size();
int32_t diff_x = m_resize_start.x - cursor.x;
if (m_resize_quadrant % 2)
diff_x = -diff_x;
diff_x = BAN::Math::max(diff_x, -m_focused_window->client_width() + min_size.width);
int32_t diff_y = m_resize_start.y - cursor.y;
if (m_resize_quadrant / 2)
diff_y = -diff_y;
diff_y = BAN::Math::max(diff_y, -m_focused_window->client_height() + min_size.height);
int32_t off_x = 0;
if (m_resize_quadrant % 2 == 0)
off_x = -diff_x;
int32_t off_y = 0;
if (m_resize_quadrant / 2 == 0)
off_y = -diff_y;
return {
.x = m_focused_window->full_x(),
.y = m_focused_window->full_y(),
.width = BAN::Math::max<int32_t>(20, m_focused_window->full_width() + cursor.x - m_resize_start.x),
.height = BAN::Math::max<int32_t>(20, m_focused_window->full_height() + cursor.y - m_resize_start.y),
.x = off_x + m_focused_window->full_x(),
.y = off_y + m_focused_window->full_y(),
.width = diff_x + m_focused_window->full_width(),
.height = diff_y + m_focused_window->full_height(),
};
}
bool WindowServer::resize_window(BAN::RefPtr<Window> window, uint32_t width, uint32_t height) const
{
if (auto ret = window->resize(width, height); ret.is_error())
{
dwarnln("could not resize client window {}", ret.error());
return false;
}
LibGUI::EventPacket::ResizeWindowEvent response;
response.width = window->client_width();
response.height = window->client_height();
response.smo_key = window->smo_key();
if (auto ret = response.send_serialized(window->client_fd()); ret.is_error())
{
dwarnln("could not respond to window resize request: {}", ret.error());
return false;
}
return true;
}
void WindowServer::add_client_fd(int fd)
{
if (auto ret = m_client_data.emplace(fd); ret.is_error())
@ -1166,9 +1272,9 @@ void WindowServer::remove_client_fd(int fd)
return;
m_client_data.remove(it);
if (m_is_fullscreen_window && m_focused_window->client_fd() == fd)
if (m_state == State::Fullscreen && m_focused_window->client_fd() == fd)
{
m_is_fullscreen_window = false;
m_state = State::Normal;
invalidate(m_framebuffer.area());
}

View File

@ -37,6 +37,7 @@ public:
void on_window_set_attributes(int fd, const LibGUI::WindowPacket::WindowSetAttributes&);
void on_window_set_mouse_capture(int fd, const LibGUI::WindowPacket::WindowSetMouseCapture&);
void on_window_set_size(int fd, const LibGUI::WindowPacket::WindowSetSize&);
void on_window_set_min_size(int fd, const LibGUI::WindowPacket::WindowSetMinSize&);
void on_window_set_fullscreen(int fd, const LibGUI::WindowPacket::WindowSetFullscreen&);
void on_window_set_title(int fd, const LibGUI::WindowPacket::WindowSetTitle&);
@ -62,6 +63,8 @@ public:
private:
void mark_pending_sync(Rectangle area);
bool resize_window(BAN::RefPtr<Window> window, uint32_t width, uint32_t height) const;
private:
struct RangeList
{
@ -70,6 +73,14 @@ private:
void add_range(const Range& range);
};
enum class State
{
Normal,
Fullscreen,
Moving,
Resizing,
};
private:
Framebuffer& m_framebuffer;
BAN::Vector<BAN::RefPtr<Window>> m_client_windows;
@ -82,13 +93,14 @@ private:
BAN::UniqPtr<LibImage::Image> m_background_image;
State m_state { State::Normal };
bool m_is_mod_key_held { false };
bool m_is_moving_window { false };
bool m_is_fullscreen_window { false };
BAN::RefPtr<Window> m_focused_window;
Position m_cursor;
bool m_is_resizing_window { false };
Rectangle m_non_full_screen_rect;
uint8_t m_resize_quadrant { 0 };
Position m_resize_start;
bool m_is_mouse_captured { false };

View File

@ -375,6 +375,10 @@ int main()
if (auto ret = LibGUI::WindowPacket::WindowSetSize::deserialize(client_data.packet_buffer.span()); !ret.is_error())
window_server.on_window_set_size(fd, ret.release_value());
break;
case LibGUI::PacketType::WindowSetMinSize:
if (auto ret = LibGUI::WindowPacket::WindowSetMinSize::deserialize(client_data.packet_buffer.span()); !ret.is_error())
window_server.on_window_set_min_size(fd, ret.release_value());
break;
case LibGUI::PacketType::WindowSetFullscreen:
if (auto ret = LibGUI::WindowPacket::WindowSetFullscreen::deserialize(client_data.packet_buffer.span()); !ret.is_error())
window_server.on_window_set_fullscreen(fd, ret.release_value());

View File

@ -1,23 +1,55 @@
#include <limits.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
int main(int argc, char** argv)
int create_directory(const char* path, bool create_parents)
{
if (argc <= 1)
const size_t pathlen = strlen(path);
if (pathlen == 0 || pathlen >= PATH_MAX)
{
fprintf(stderr, "Missing operand\n");
fprintf(stderr, "mkdir: %s\n", strerror(ENOENT));
return 1;
}
int ret = 0;
for (int i = 1; i < argc; i++)
if (!create_parents)
{
if (mkdir(argv[i], 0755) == -1)
{
const int ret = mkdir(path, 0755);
if (ret == -1)
perror("mkdir");
ret = 1;
}
return -ret;
}
int ret = 0;
char buffer[PATH_MAX];
for (size_t i = 0; path[i];)
{
for (; path[i] && path[i] != '/'; i++)
buffer[i] = path[i];
for (; path[i] && path[i] == '/'; i++)
buffer[i] = path[i];
buffer[i] = '\0';
ret = mkdir(buffer, 0755);
}
return ret;
}
int main(int argc, char** argv)
{
const bool create_parents = argc >= 2 && strcmp(argv[1], "-p") == 0;
if (argc <= 1 + create_parents)
{
fprintf(stderr, "missing operand\n");
return 1;
}
int ret = 0;
for (int i = 1 + create_parents; i < argc; i++)
if (create_directory(argv[i], create_parents) == -1)
ret = 1;
return ret;
}

View File

@ -5,6 +5,7 @@ set(USERSPACE_TESTS
test-mmap-shared
test-mouse
test-popen
test-pthread
test-setjmp
test-shared
test-sort

View File

@ -1,28 +1,61 @@
#include <stdio.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
pthread_spinlock_t spinlock;
void* thread_func(void*)
{
printf("hello from thread\n");
return nullptr;
printf("[THREAD] locking spinlock\n");
pthread_spin_lock(&spinlock);
printf("[THREAD] got spinlock\n");
sleep(1);
printf("[THREAD] releasing spinlock\n");
pthread_spin_unlock(&spinlock);
int* value = static_cast<int*>(malloc(sizeof(int)));
if (value == nullptr)
{
perror("malloc");
return nullptr;
}
*value = 69;
printf("[THREAD] exiting with %d\n", *value);
return value;
}
int main(int argc, char** argv)
{
pthread_t tid;
pthread_spin_init(&spinlock, 0);
printf("creating thread\n");
printf("[MAIN] locking spinlock\n");
pthread_spin_lock(&spinlock);
if (pthread_create(&tid, nullptr, &thread_func, nullptr) == -1)
{
perror("pthread_create");
return 1;
}
printf("[MAIN] creating thread\n");
pthread_t thread;
pthread_create(&thread, nullptr, &thread_func, nullptr);
sleep(1);
printf("exiting\n");
printf("[MAIN] releasing spinlock\n");
pthread_spin_unlock(&spinlock);
printf("[MAIN] joining thread\n");
void* value;
pthread_join(thread, &value);
if (value == nullptr)
printf("[MAIN] thread returned NULL\n");
else
printf("[MAIN] thread returned %d\n", *static_cast<int*>(value));
return 0;
}

View File

@ -7,10 +7,11 @@
void randomize_color(BAN::UniqPtr<LibGUI::Window>& window)
{
auto& texture = window->texture();
uint32_t color = ((rand() % 255) << 16) | ((rand() % 255) << 8) | ((rand() % 255) << 0);
for (uint32_t y = 0; y < window->height(); y++)
for (uint32_t x = 0; x < window->width(); x++)
window->set_pixel(x, y, 0xFF000000 | color);
texture.set_pixel(x, y, 0xFF000000 | color);
window->invalidate();
}