Compare commits

..

No commits in common. "cf21eb4b39af7ed63c96c325506506f5532a26a7" and "a82f00cb70cbd4aed76482686ed8fb1e2eabd21c" have entirely different histories.

14 changed files with 65 additions and 195 deletions

View File

@ -325,6 +325,7 @@ namespace BAN::Math
template<floating_point T> template<floating_point T>
inline constexpr T sin(T x) inline constexpr T sin(T x)
{ {
x = fmod<T>(x, (T)2.0 * numbers::pi_v<T>);
asm("fsin" : "+t"(x)); asm("fsin" : "+t"(x));
return x; return x;
} }
@ -332,16 +333,12 @@ namespace BAN::Math
template<floating_point T> template<floating_point T>
inline constexpr T cos(T x) inline constexpr T cos(T x)
{ {
if (abs(x) >= (T)9223372036854775808.0)
x = fmod<T>(x, (T)2.0 * numbers::pi_v<T>);
asm("fcos" : "+t"(x)); asm("fcos" : "+t"(x));
return x; return x;
} }
template<floating_point T>
inline constexpr void sincos(T x, T& sin, T& cos)
{
asm("fsincos" : "=t"(cos), "=u"(sin) : "0"(x));
}
template<floating_point T> template<floating_point T>
inline constexpr T tan(T x) inline constexpr T tan(T x)
{ {

View File

@ -1,3 +1,10 @@
#pragma once #pragma once
#include <stddef.h>
#ifdef __banan_os__
inline void* operator new(size_t, void* addr) { return addr; }
inline void* operator new[](size_t, void* addr) { return addr; }
#else
#include <new> #include <new>
#endif

View File

@ -191,9 +191,9 @@ long lroundl(long double);
double modf(double, double*); double modf(double, double*);
float modff(float, float*); float modff(float, float*);
long double modfl(long double, long double*); long double modfl(long double, long double*);
double nan(const char*); #define nan(tagp) __builtin_nan(tagp)
float nanf(const char*); #define nanf(tagp) __builtin_nanf(tagp)
long double nanl(const char*); #define nanl(tagp) __builtin_nanl(tagp)
double nearbyint(double); double nearbyint(double);
float nearbyintf(float); float nearbyintf(float);
long double nearbyintl(long double); long double nearbyintl(long double);

View File

@ -249,9 +249,6 @@ BAN_FUNC1(logb)
FUNC_EXPR1_RET(lrint, long, BAN::Math::rint(a)) FUNC_EXPR1_RET(lrint, long, BAN::Math::rint(a))
FUNC_EXPR1_RET(lround, long, BAN::Math::round(a)) FUNC_EXPR1_RET(lround, long, BAN::Math::round(a))
BAN_FUNC2_PTR(modf) BAN_FUNC2_PTR(modf)
double nan(const char* tagp) { return __builtin_nan(tagp); }
float nanf(const char* tagp) { return __builtin_nanf(tagp); }
long double nanl(const char* tagp) { return __builtin_nanl(tagp); }
FUNC_EXPR1(nearbyint, BAN::Math::rint(a)) FUNC_EXPR1(nearbyint, BAN::Math::rint(a))
FUNC_EXPR2(nextafter, nextafter_impl(a, b)) FUNC_EXPR2(nextafter, nextafter_impl(a, b))
FUNC_EXPR2_TYPE(nexttoward, long double, nextafter_impl(a, b)) FUNC_EXPR2_TYPE(nexttoward, long double, nextafter_impl(a, b))

View File

@ -43,11 +43,6 @@ void exit(int status)
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
void _Exit(int status)
{
_exit(status);
}
int abs(int val) int abs(int val)
{ {
return val < 0 ? -val : val; return val < 0 ? -val : val;

View File

@ -267,20 +267,6 @@ namespace LibGUI
return true; return true;
} }
bool Window::set_mouse_capture(bool captured)
{
WindowPacket::WindowSetMouseCapture packet;
packet.captured = captured;
if (auto ret = packet.send_serialized(m_server_fd); ret.is_error())
{
dprintln("failed to set mouse capture: {}", ret.error());
return false;
}
return true;
}
bool Window::set_position(int32_t x, int32_t y) bool Window::set_position(int32_t x, int32_t y)
{ {
WindowPacket::WindowSetPosition packet; WindowPacket::WindowSetPosition packet;
@ -299,7 +285,10 @@ namespace LibGUI
bool Window::set_attributes(Attributes attributes) bool Window::set_attributes(Attributes attributes)
{ {
WindowPacket::WindowSetAttributes packet; WindowPacket::WindowSetAttributes packet;
packet.attributes = attributes; packet.title_bar = attributes.title_bar;
packet.movable = attributes.movable;
packet.rounded_corners = attributes.rounded_corners;
packet.alpha_channel = attributes.alpha_channel;
if (auto ret = packet.send_serialized(m_server_fd); ret.is_error()) if (auto ret = packet.send_serialized(m_server_fd); ret.is_error())
{ {

View File

@ -162,7 +162,6 @@ namespace LibGUI
WindowInvalidate, WindowInvalidate,
WindowSetPosition, WindowSetPosition,
WindowSetAttributes, WindowSetAttributes,
WindowSetMouseCapture,
DestroyWindowEvent, DestroyWindowEvent,
CloseWindowEvent, CloseWindowEvent,
@ -175,49 +174,35 @@ namespace LibGUI
namespace WindowPacket namespace WindowPacket
{ {
DEFINE_PACKET( DEFINE_PACKET(WindowCreate,
WindowCreate,
uint32_t, width, uint32_t, width,
uint32_t, height, uint32_t, height,
BAN::String, title BAN::String, title
); );
DEFINE_PACKET( DEFINE_PACKET(WindowCreateResponse,
WindowCreateResponse,
uint32_t, width, uint32_t, width,
uint32_t, height, uint32_t, height,
long, smo_key long, smo_key
); );
DEFINE_PACKET( DEFINE_PACKET(WindowInvalidate,
WindowInvalidate,
uint32_t, x, uint32_t, x,
uint32_t, y, uint32_t, y,
uint32_t, width, uint32_t, width,
uint32_t, height uint32_t, height
); );
DEFINE_PACKET( DEFINE_PACKET(WindowSetPosition,
WindowSetPosition,
int32_t, x, int32_t, x,
int32_t, y int32_t, y
); );
DEFINE_PACKET_EXTRA( DEFINE_PACKET(WindowSetAttributes,
WindowSetAttributes, bool, title_bar,
struct Attributes { bool, rounded_corners,
bool title_bar; bool, movable,
bool movable; bool, alpha_channel
bool focusable;
bool rounded_corners;
bool alpha_channel;
},
Attributes, attributes
);
DEFINE_PACKET(
WindowSetMouseCapture,
bool, captured
); );
} }

View File

@ -14,14 +14,12 @@ namespace LibGUI
class Window class Window
{ {
public: public:
using Attributes = WindowPacket::WindowSetAttributes::Attributes; struct Attributes
{
static constexpr Attributes default_attributes = { bool title_bar { true };
.title_bar = true, bool movable { true };
.movable = true, bool rounded_corners { true };
.focusable = true, bool alpha_channel { false };
.rounded_corners = true,
.alpha_channel = false,
}; };
public: public:
@ -60,8 +58,6 @@ namespace LibGUI
bool invalidate(int32_t x, int32_t y, uint32_t width, uint32_t height); bool invalidate(int32_t x, int32_t y, uint32_t width, uint32_t height);
bool invalidate() { return invalidate(0, 0, width(), height()); } bool invalidate() { return invalidate(0, 0, width(), height()); }
bool set_mouse_capture(bool captured);
bool set_position(int32_t x, int32_t y); bool set_position(int32_t x, int32_t y);
Attributes get_attributes() const { return m_attributes; } Attributes get_attributes() const { return m_attributes; }

View File

@ -3,17 +3,6 @@
#include <time.h> #include <time.h>
static BAN::String get_task_bar_string()
{
BAN::String result;
const time_t current_time = time(nullptr);
if (!result.append(ctime(&current_time)).is_error())
result.pop_back();
return result;
}
int main() int main()
{ {
constexpr uint32_t padding = 3; constexpr uint32_t padding = 3;
@ -24,14 +13,12 @@ int main()
auto window = MUST(LibGUI::Window::create(0, font.height() + 2 * padding, "TaskBar")); auto window = MUST(LibGUI::Window::create(0, font.height() + 2 * padding, "TaskBar"));
auto attributes = LibGUI::Window::default_attributes; auto attributes = window->get_attributes();
attributes.title_bar = false; attributes.title_bar = false;
attributes.movable = false; attributes.movable = false;
attributes.focusable = false;
attributes.alpha_channel = false; attributes.alpha_channel = false;
attributes.rounded_corners = false; attributes.rounded_corners = false;
window->set_attributes(attributes); window->set_attributes(attributes);
window->set_close_window_event_callback([]() {}); window->set_close_window_event_callback([]() {});
window->set_position(0, 0); window->set_position(0, 0);
@ -41,18 +28,21 @@ int main()
bool is_running = true; bool is_running = true;
time_t last_update;
const auto update_time_string = const auto update_time_string =
[&]() [&]()
{ {
const auto text = get_task_bar_string(); last_update = time(nullptr);
BAN::StringView time_sv = ctime(&last_update);
time_sv = time_sv.substring(0, time_sv.size() - 1); // new line
const uint32_t text_w = text.size() * font.width(); const uint32_t text_w = time_sv.size() * font.width();
const uint32_t text_h = font.height(); const uint32_t text_h = font.height();
const uint32_t text_x = window->width() - text_w - padding; const uint32_t text_x = window->width() - text_w - padding;
const uint32_t text_y = padding; const uint32_t text_y = padding;
window->fill_rect(text_x, text_y, text_w, text_h, bg_color); window->fill_rect(text_x, text_y, text_w, text_h, bg_color);
window->draw_text(text, font, text_x, text_y, fg_color); window->draw_text(time_sv, font, text_x, text_y, fg_color);
if (!window->invalidate(text_x, text_y, text_w, text_h)) if (!window->invalidate(text_x, text_y, text_w, text_h))
is_running = false; is_running = false;
}; };

View File

@ -112,7 +112,7 @@ void Terminal::run()
m_window = MUST(LibGUI::Window::create(600, 400, "Terminal"_sv)); m_window = MUST(LibGUI::Window::create(600, 400, "Terminal"_sv));
auto attributes = LibGUI::Window::default_attributes; auto attributes = m_window->get_attributes();
attributes.alpha_channel = true; attributes.alpha_channel = true;
m_window->set_attributes(attributes); m_window->set_attributes(attributes);

View File

@ -76,7 +76,7 @@ private:
uint32_t* m_title_bar_data { nullptr }; uint32_t* m_title_bar_data { nullptr };
BAN::String m_title; BAN::String m_title;
LibGUI::Window::Attributes m_attributes { LibGUI::Window::default_attributes }; LibGUI::Window::Attributes m_attributes;
friend class BAN::RefPtr<Window>; friend class BAN::RefPtr<Window>;
}; };

View File

@ -181,55 +181,14 @@ void WindowServer::on_window_set_attributes(int fd, const LibGUI::WindowPacket::
} }
const auto old_client_area = target_window->full_area(); const auto old_client_area = target_window->full_area();
target_window->set_attributes(packet.attributes); target_window->set_attributes({
.title_bar = packet.title_bar,
.movable = packet.movable,
.rounded_corners = packet.rounded_corners,
.alpha_channel = packet.alpha_channel,
});
const auto new_client_area = target_window->full_area(); const auto new_client_area = target_window->full_area();
invalidate(new_client_area.get_bounding_box(old_client_area)); invalidate(new_client_area.get_bounding_box(old_client_area));
if (!packet.attributes.focusable && m_focused_window == target_window)
{
m_focused_window = nullptr;
for (size_t i = m_client_windows.size(); i > 0; i--)
{
if (auto& window = m_client_windows[i - 1]; window->get_attributes().focusable)
{
set_focused_window(window);
break;
}
}
}
}
void WindowServer::on_window_set_mouse_capture(int fd, const LibGUI::WindowPacket::WindowSetMouseCapture& packet)
{
if (m_is_mouse_captured && packet.captured)
{
ASSERT(m_focused_window);
if (fd != m_focused_window->client_fd())
dwarnln("client tried to set mouse capture while other window has it already captured");
return;
}
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 mouse capture while not owning a window");
return;
}
if (packet.captured == m_is_mouse_captured)
return;
set_focused_window(target_window);
m_is_mouse_captured = packet.captured;
invalidate(cursor_area());
} }
void WindowServer::on_key_event(LibInput::KeyEvent event) void WindowServer::on_key_event(LibInput::KeyEvent event)
@ -288,20 +247,6 @@ void WindowServer::on_key_event(LibInput::KeyEvent event)
void WindowServer::on_mouse_button(LibInput::MouseButtonEvent event) void WindowServer::on_mouse_button(LibInput::MouseButtonEvent event)
{ {
if (m_is_mouse_captured)
{
ASSERT(m_focused_window);
LibGUI::EventPacket::MouseButtonEvent packet;
packet.event.button = event.button;
packet.event.pressed = event.pressed;
packet.event.x = 0;
packet.event.y = 0;
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
dwarnln("could not send mouse button event: {}", ret.error());
return;
}
BAN::RefPtr<Window> target_window; BAN::RefPtr<Window> target_window;
for (size_t i = m_client_windows.size(); i > 0; i--) for (size_t i = m_client_windows.size(); i > 0; i--)
{ {
@ -316,7 +261,6 @@ void WindowServer::on_mouse_button(LibInput::MouseButtonEvent event)
if (!target_window) if (!target_window)
return; return;
if (target_window->get_attributes().focusable)
set_focused_window(target_window); set_focused_window(target_window);
// Handle window moving when mod key is held or mouse press on title bar // Handle window moving when mod key is held or mouse press on title bar
@ -345,7 +289,7 @@ void WindowServer::on_mouse_button(LibInput::MouseButtonEvent event)
packet.event.y = m_cursor.y - m_focused_window->client_y(); 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()) if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
{ {
dwarnln("could not send mouse button event: {}", ret.error()); dwarnln("could not send mouse button event event: {}", ret.error());
return; return;
} }
} }
@ -353,18 +297,6 @@ void WindowServer::on_mouse_button(LibInput::MouseButtonEvent event)
void WindowServer::on_mouse_move(LibInput::MouseMoveEvent event) void WindowServer::on_mouse_move(LibInput::MouseMoveEvent event)
{ {
if (m_is_mouse_captured)
{
ASSERT(m_focused_window);
LibGUI::EventPacket::MouseMoveEvent packet;
packet.event.x = event.rel_x;
packet.event.y = -event.rel_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;
}
const int32_t new_x = BAN::Math::clamp(m_cursor.x + event.rel_x, 0, m_framebuffer.width); const int32_t new_x = BAN::Math::clamp(m_cursor.x + event.rel_x, 0, m_framebuffer.width);
const int32_t new_y = BAN::Math::clamp(m_cursor.y - event.rel_y, 0, m_framebuffer.height); const int32_t new_y = BAN::Math::clamp(m_cursor.y - event.rel_y, 0, m_framebuffer.height);
@ -409,7 +341,7 @@ void WindowServer::on_mouse_move(LibInput::MouseMoveEvent event)
packet.event.y = m_cursor.y - m_focused_window->client_y(); 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()) if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
{ {
dwarnln("could not send mouse move event: {}", ret.error()); dwarnln("could not send mouse move event event: {}", ret.error());
return; return;
} }
} }
@ -423,7 +355,7 @@ void WindowServer::on_mouse_scroll(LibInput::MouseScrollEvent event)
packet.event.scroll = event.scroll; packet.event.scroll = event.scroll;
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error()) if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
{ {
dwarnln("could not send mouse scroll event: {}", ret.error()); dwarnln("could not send mouse scroll event event: {}", ret.error());
return; return;
} }
} }
@ -434,12 +366,6 @@ void WindowServer::set_focused_window(BAN::RefPtr<Window> window)
if (m_focused_window == window) if (m_focused_window == window)
return; return;
if (m_is_mouse_captured)
{
m_is_mouse_captured = false;
invalidate(cursor_area());
}
for (size_t i = m_client_windows.size(); i > 0; i--) for (size_t i = m_client_windows.size(); i > 0; i--)
{ {
if (m_client_windows[i - 1] == window) if (m_client_windows[i - 1] == window)
@ -663,8 +589,6 @@ void WindowServer::invalidate(Rectangle area)
} }
} }
if (!m_is_mouse_captured)
{
auto cursor = cursor_area(); auto cursor = cursor_area();
if (auto overlap = cursor.get_overlap(area); overlap.has_value()) if (auto overlap = cursor.get_overlap(area); overlap.has_value())
{ {
@ -684,7 +608,6 @@ void WindowServer::invalidate(Rectangle area)
} }
} }
} }
}
const uintptr_t mmap_start = reinterpret_cast<uintptr_t>(m_framebuffer.mmap) + area.y * m_framebuffer.width * 4; const uintptr_t mmap_start = reinterpret_cast<uintptr_t>(m_framebuffer.mmap) + area.y * m_framebuffer.width * 4;
const uintptr_t mmap_end = mmap_start + (area.height + 1) * m_framebuffer.width * 4; const uintptr_t mmap_end = mmap_start + (area.height + 1) * m_framebuffer.width * 4;
@ -695,8 +618,6 @@ void WindowServer::invalidate(Rectangle area)
size_t index = (mmap_addr - reinterpret_cast<uintptr_t>(m_framebuffer.mmap)) / 4096; size_t index = (mmap_addr - reinterpret_cast<uintptr_t>(m_framebuffer.mmap)) / 4096;
size_t byte = index / 8; size_t byte = index / 8;
size_t bit = index % 8; size_t bit = index % 8;
//dprintln("{}/{}", byte, m_pages_to_sync_bitmap.size());
if (byte < m_pages_to_sync_bitmap.size())
m_pages_to_sync_bitmap[byte] |= 1 << bit; m_pages_to_sync_bitmap[byte] |= 1 << bit;
mmap_addr += 4096; mmap_addr += 4096;
} }

View File

@ -34,7 +34,6 @@ public:
void on_window_invalidate(int fd, const LibGUI::WindowPacket::WindowInvalidate&); void on_window_invalidate(int fd, const LibGUI::WindowPacket::WindowInvalidate&);
void on_window_set_position(int fd, const LibGUI::WindowPacket::WindowSetPosition&); void on_window_set_position(int fd, const LibGUI::WindowPacket::WindowSetPosition&);
void on_window_set_attributes(int fd, const LibGUI::WindowPacket::WindowSetAttributes&); 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_key_event(LibInput::KeyEvent event); void on_key_event(LibInput::KeyEvent event);
void on_mouse_button(LibInput::MouseButtonEvent event); void on_mouse_button(LibInput::MouseButtonEvent event);
@ -71,8 +70,6 @@ private:
BAN::RefPtr<Window> m_focused_window; BAN::RefPtr<Window> m_focused_window;
Position m_cursor; Position m_cursor;
bool m_is_mouse_captured { false };
bool m_deleted_window { false }; bool m_deleted_window { false };
bool m_is_stopped { false }; bool m_is_stopped { false };
bool m_is_bouncing_window = false; bool m_is_bouncing_window = false;

View File

@ -350,10 +350,6 @@ int main()
if (auto ret = LibGUI::WindowPacket::WindowSetAttributes::deserialize(client_data.packet_buffer.span()); !ret.is_error()) if (auto ret = LibGUI::WindowPacket::WindowSetAttributes::deserialize(client_data.packet_buffer.span()); !ret.is_error())
window_server.on_window_set_attributes(fd, ret.release_value()); window_server.on_window_set_attributes(fd, ret.release_value());
break; break;
case LibGUI::PacketType::WindowSetMouseCapture:
if (auto ret = LibGUI::WindowPacket::WindowSetMouseCapture::deserialize(client_data.packet_buffer.span()); !ret.is_error())
window_server.on_window_set_mouse_capture(fd, ret.release_value());
break;
default: default:
dprintln("unhandled packet type: {}", *reinterpret_cast<uint32_t*>(client_data.packet_buffer.data())); dprintln("unhandled packet type: {}", *reinterpret_cast<uint32_t*>(client_data.packet_buffer.data()));
} }