Compare commits

...

3 Commits

Author SHA1 Message Date
3e5bdc8c80
apps: Add clock
All checks were successful
continuous-integration/drone/push Build is passing
2023-10-09 22:05:30 +02:00
0824ba7e23
libos: Add timers to event loops
Only second precision for now, as alarm() is used to control the timers. Hopefully setitimer() or timer_create() can be added to the kernel soon to benefit from more precision.
2023-10-09 22:00:15 +02:00
0b2a835336
libui+libos: Move Action to libos 2023-10-07 15:31:50 +02:00
9 changed files with 134 additions and 5 deletions

View File

@ -47,3 +47,5 @@ luna_app(taskbar.cpp taskbar)
target_link_libraries(taskbar PUBLIC ui)
luna_app(2048.cpp 2048)
target_link_libraries(2048 PUBLIC ui)
luna_app(clock.cpp clock)
target_link_libraries(clock PUBLIC ui)

40
apps/clock.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <time.h>
#include <ui/App.h>
#include <ui/Label.h>
ui::Label* g_label;
void update_time()
{
time_t t = time(NULL);
struct tm* tp = localtime(&t);
static char buf[2048];
strftime(buf, sizeof(buf), "%H:%M:%S", tp);
g_label->set_text(StringView { buf });
os::EventLoop::the().register_timer(1, update_time);
ui::App::the().main_window()->draw();
}
Result<int> luna_main(int argc, char** argv)
{
ui::App app;
TRY(app.init(argc, argv));
auto* window = TRY(ui::Window::create(ui::Rect { 500, 400, 100, 50 }));
app.set_main_window(window);
window->set_title("Clock");
window->set_background(ui::GRAY);
g_label = TRY(make<ui::Label>("00:00:00", ui::BLACK, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center,
ui::Font::default_bold_font()));
window->set_main_widget(*g_label);
update_time();
return app.run();
}

View File

@ -62,6 +62,9 @@ Result<int> luna_main(int argc, char** argv)
StringView gol_command[] = { "/usr/bin/gol" };
TRY(create_widget_group_for_app(layout, { gol_command, 1 }, "/usr/share/icons/32x32/app-gol.tga"));
StringView clock_command[] = { "/usr/bin/clock" };
TRY(create_widget_group_for_app(layout, { clock_command, 1 }, "/usr/share/icons/32x32/app-clock.tga"));
window->draw();
return app.run();

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

View File

@ -1,7 +1,16 @@
/**
* @file Action.h
* @author apio (cloudapio.eu)
* @brief Wrapper for callable objects, like function pointers or lambdas.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
#pragma once
#include <luna/Heap.h>
namespace ui
namespace os
{
class Action
{

View File

@ -9,7 +9,9 @@
#pragma once
#include <luna/HashMap.h>
#include <luna/LinkedList.h>
#include <luna/Vector.h>
#include <os/Action.h>
#include <sys/poll.h>
namespace os
@ -52,9 +54,22 @@ namespace os
*
* Unlike standard POSIX signal handling, this handler will be run synchronously as part of the event loop,
* eliminating many problems with asynchronous signal handling.
*
* @param sig The signal to handle.
* @param handler The function to call when this signal is caught.
* @return Result<void> Whether the operation succeeded.
*/
Result<void> register_signal_handler(int sig, void (*handler)(int));
/**
* @brief Register a function to be called in a certain amount of time.
*
* @param seconds The number of seconds to wait before invoking the function.
* @param callback The function to call when the time is up.
* @return Result<void> Whether the operation succeeded.
*/
Result<void> register_timer(unsigned int seconds, Action&& callback);
/**
* @brief Run the event loop until it is asked to quit.
*
@ -76,10 +91,19 @@ namespace os
static void handle_signal(int sig);
static void handle_quit_signal(int);
static void handle_timer_signal(int);
bool m_should_quit { false };
int m_quit_status { 0 };
int m_signal_pipe[2];
struct Timer final : public LinkedListNode<Timer>
{
long target;
Action action;
};
LinkedList<Timer> m_timer_queue;
};
}

View File

@ -12,6 +12,7 @@
#include <os/File.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
static os::EventLoop* s_the = nullptr;
@ -28,6 +29,7 @@ namespace os
register_signal_handler(SIGTERM, EventLoop::handle_quit_signal).release_value();
register_signal_handler(SIGINT, EventLoop::handle_quit_signal).release_value();
register_signal_handler(SIGALRM, EventLoop::handle_timer_signal).release_value();
}
EventLoop::~EventLoop()
@ -60,6 +62,40 @@ namespace os
return {};
}
Result<void> EventLoop::register_timer(unsigned int seconds, Action&& callback)
{
long target = time(NULL) + seconds;
auto* timer = TRY(make<Timer>());
timer->target = target;
timer->action = move(callback);
Timer* next = nullptr;
for (auto* t : m_timer_queue)
{
if (target < t->target)
{
next = t;
break;
}
}
if (m_timer_queue.first().value_or(nullptr) == next)
{
alarm(seconds);
m_timer_queue.prepend(timer);
}
else if (next == nullptr) { m_timer_queue.append(timer); }
else
{
auto previous = m_timer_queue.previous(next).value();
m_timer_queue.add_after(previous, timer);
}
return {};
}
int EventLoop::run()
{
while (!m_should_quit)
@ -113,4 +149,19 @@ namespace os
os::println("EventLoop: quit requested by signal %d", sig);
the().quit(sig);
}
void EventLoop::handle_timer_signal(int)
{
auto& queue = the().m_timer_queue;
if (queue.count())
{
auto timer = queue.remove(queue.expect_first());
auto first = queue.first();
if (first.has_value()) { alarm((unsigned int)(first.value()->target - time(NULL))); }
timer->action();
delete timer;
}
}
}

View File

@ -8,7 +8,7 @@
*/
#pragma once
#include <ui/Action.h>
#include <os/Action.h>
#include <ui/Widget.h>
namespace ui
@ -19,7 +19,7 @@ namespace ui
Button(Rect rect);
void set_widget(Widget& widget);
void set_action(Action&& action);
void set_action(os::Action&& action);
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave() override;
@ -32,6 +32,6 @@ namespace ui
bool m_hovered { false };
bool m_clicked { false };
Widget* m_child;
Action m_action;
os::Action m_action;
};
}

View File

@ -24,7 +24,7 @@ namespace ui
widget.set_parent(this);
}
void Button::set_action(Action&& action)
void Button::set_action(os::Action&& action)
{
m_action = move(action);
}