123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- // TODO: resizable window, fullscreen
- // TODO: web build
- // TODO: android build
- // TODO: proper support for wav, and streaming audio from disk
- // TODO: more audio formats (mp3, ogg, flac ... would be cool to learn some ffmpeg here)
- // TODO: quick count up/down by long press
- // TODO: treat the 3 number displays as single line for input (this is really hard to explain -_-)
- // TODO: progress bar at the bottom ( flashes when done )
- // TODO: loop toggle
- // TODO: topbar with recent set timers, with option to pin, displays mini ui(set time and progress bar)
- // TODO: run several timers at the same time
- // TODO: allow separate alarm sounds per timer, and mix the audio
- // TODO: more alarm settings with a visualization (alarm duration, included or excluded from next timer loop)
- // TODO: web version (file management)
- // TODO: android version (portrait layout, file management)
- // TODO: full set of command line parameters (set time, autostart, loop, bg/fg color, window size/position, gui on/off, etc.)
- // TODO: functional tests with xdotool and parecord
- // https://askubuntu.com/questions/60837/record-a-programs-output-with-pulseaudio
- // https://ro-che.info/articles/2017-07-21-record-audio-linux
- #include <atomic>
- #include <cstdio>
- #include <sstream>
- #include <iomanip>
- #include <cerrno>
- #include <iostream>
- #include "simple.hpp"
- #include "plain_button.h"
- #include "layout.h"
- #include "digits.h"
- #include "ui_factory.hpp"
- #include "utils.hpp"
- #include "digit_display.h"
- #include "timer.h"
- const std::chrono::steady_clock::duration max_duration = 99h + 59min + 59s;
- const std::chrono::steady_clock::duration min_duration{};
- int main(int argc, const char** argv) try
- {
- using support::ston;
- const auto main_color = argc > 5
- ? rgb_pixel::from(ston<rgb_pixel::int_type>(argv[5]))
- : 0x009dff_rgb; // or 0x3ba3cd_rgb;
- const auto second_color = argc > 6
- ? rgb_pixel::from(ston<rgb_pixel::int_type>(argv[6]))
- : 0x000000_rgb;
- const std::chrono::milliseconds fast_frametime (argc > 7 ? ston<unsigned>(argv[7]) : 33);
- const std::chrono::milliseconds slow_frametime (argc > 8 ? ston<unsigned>(argv[8]) : 256);
- if(main_color == second_color) // would be cool to have a smarter contrast check
- {
- std::fputs("foreground(5th) and background(6th) colors should differ", stderr);
- std::fputs("\n", stderr);
- return -1;
- }
- if(fast_frametime >= slow_frametime)
- {
- std::fputs("fast frame time(7th) should be lower than slow frame time(8th)", stderr);
- std::fputs("\n", stderr);
- return -2;
- }
- initializer init;
- ui_factory ui;
- auto frametime = fast_frametime;
- std::string icon_string =
- "---------" // "---------"
- "---------" // "-+-----+-"
- "-+-----+-" // "-++---++-"
- "-++---++-" // "-+-+-+-+-"
- "-+-+-+-+-" // "-+--+--+-"
- "-+--+--+-" // "-+-----+-"
- "-+-----+-" // "-+-----+-"
- "---------" // "-+-----+-"
- "---------" // "---------"
- ;
- surface icon_small(reinterpret_cast<surface::byte*>(icon_string.data()), {9,9},
- pixel_format(pixel_format::type::index8));
- icon_small.format().palette()->set_color('+', main_color);
- icon_small.format().palette()->set_color('-', 0x0_rgba);
- std::ostringstream title;
- title << std::setfill('0');
- auto music = argc > 1
- ? argv[1][0] != '\0' ? std::optional<musical::wav>(argv[1]) : std::nullopt
- : std::optional<musical::wav>("./melno.wav");
- graphical::software_window win("melno", {400,200});
- auto fg_color = win.surface().format().color(main_color);
- auto bg_color = win.surface().format().color(second_color);
- surface icon({64,64}, pixel_format(pixel_format::type::rgba8888));
- blit(convert( icon_small, icon.format()), icon, rect{icon.size()});
- win.icon(icon);
- rect button_rect{win.size()/8 + int2{5,5}};
- auto make_control_button = [&]() -> auto&
- {
- auto& button = ui.make<plain_button>(fg_color, button_rect);
- return button;
- };
- int2 digit_size{40,100};
- auto digit_spacing = int2::i(5);
- auto make_time_display = [&]() -> auto&
- {
- auto& display = ui.make<digit_display<>>(digit_size, digit_spacing, fg_color);
- return display;
- };
- std::atomic<bool> music_playing = false;
- std::atomic<bool> music_done = false;
- int autorestart_count = 0;
- auto autorestart_count_display_size = int2{8,15};
- auto& autorestart_count_display = ui.make<digit_display<1>>(autorestart_count_display_size, int2{1,0}, fg_color);
- autorestart_count_display.set(autorestart_count);
- auto player = [&music_playing, &music_done, i = loop(music->buffer()),
- max_play_count = argc > 9 ? ston<unsigned>(argv[9]) : unsigned(5)]
- (auto& device, auto buffer) mutable
- {
- if(!music_playing)
- {
- i.reset();
- std::fill(buffer.begin(), buffer.end(), device.silence());
- return;
- }
- auto buffer_size = buffer.end() - buffer.begin();
- std::copy_n(i, buffer_size, buffer.begin());
- i += buffer_size;
- if(max_play_count != 0 && unsigned(i.count()) == max_play_count)
- music_done = true;
- };
- using music_device = musical::device_with_callback<decltype(player)>;
- std::unique_ptr<music_device> device = nullptr;
- auto current_timer = timer{
- std::chrono::hours(argc > 2 ? ston<unsigned>(argv[2]) : 0) +
- std::chrono::minutes(argc > 3 ? ston<unsigned>(argv[3]) : 0) +
- std::chrono::seconds(argc > 4 ? ston<unsigned>(argv[4]) : 0),
- };
- current_timer = timer(clamp(current_timer.duration(), min_duration, max_duration));
- auto& hours_display = make_time_display();
- auto& minutes_display = make_time_display();
- auto& seconds_display = make_time_display();
- rect separator_rect{{13,100}};
- bounds_layout timer_layout(
- {
- &hours_display,
- &ui.make<digit_bitmap>(digit[10], fg_color, separator_rect),
- &minutes_display,
- &ui.make<digit_bitmap>(digit[10], fg_color, separator_rect),
- &seconds_display
- },
- int2::i(5)
- );
- timer_layout.update();
- hours_display.on_input.push_back([&](auto&&, int old_value, int new_value)
- {
- using namespace std::chrono;
- auto offset = hours(new_value) - hours(old_value);
- current_timer = timer(current_timer.remaining_duration() + offset);
- });
- minutes_display.on_input.push_back([&](auto&&, int old_value, int new_value)
- {
- using namespace std::chrono;
- auto new_minutes = minutes(new_value);
- if(new_minutes >= hours(1))
- new_minutes = hours(1) - minutes(1);
- auto offset = new_minutes - minutes(old_value);
- current_timer = timer(current_timer.remaining_duration() + offset);
- });
- seconds_display.on_input.push_back([&](auto&&, int old_value, int new_value)
- {
- using namespace std::chrono;
- auto new_seconds = seconds(new_value);
- if(new_seconds >= minutes(1))
- new_seconds = minutes(1) - seconds(1);
- auto offset = new_seconds - seconds(old_value);
- current_timer = timer(current_timer.remaining_duration() + offset);
- });
- focus_group main_focus_group{};
- auto focus_handler = [&main_focus_group](auto& element)
- {
- main_focus_group.focus(&element);
- };
- main_focus_group.elements.push_back(&hours_display);
- main_focus_group.elements.push_back(&minutes_display);
- main_focus_group.elements.push_back(&seconds_display);
- hours_display.on_press.push_back(focus_handler);
- minutes_display.on_press.push_back(focus_handler);
- seconds_display.on_press.push_back(focus_handler);
- std::optional<timer::clock::time_point> countup_point = std::nullopt;
- auto& up_button = make_control_button();
- auto& stop_button = make_control_button();
- auto& down_button = make_control_button();
- main_focus_group.elements.push_back(&up_button);
- main_focus_group.elements.push_back(&stop_button);
- main_focus_group.elements.push_back(&down_button);
- up_button.on_press.push_back(focus_handler);
- stop_button.on_press.push_back(focus_handler);
- down_button.on_press.push_back(focus_handler);
- up_button.on_click.push_back([&](auto&)
- {
- countup_point = timer::clock::now() - current_timer.remaining_duration();
- });
- auto reset_current_timer = [&]()
- {
- music_playing = false;
- music_done = false;
- init.graphics.screensaver.release_one();
- device = nullptr;
- current_timer = timer(current_timer.duration());
- };
- auto start_current_timer = [&](auto&&...)
- {
- if(countup_point)
- countup_point = std::nullopt;
- current_timer.resume();
- };
- stop_button.on_click.push_back([&](auto&)
- {
- if(music_playing)
- {
- reset_current_timer();
- }
- else
- {
- if(countup_point)
- countup_point = std::nullopt;
- current_timer.pause();
- }
- autorestart_count_display.set(autorestart_count = 0);
- });
- down_button.on_click.push_back(start_current_timer);
- timer stop_button_hold(1s);
- stop_button.on_press.push_back([&stop_button_hold](auto&)
- {
- stop_button_hold = timer(stop_button_hold.duration(), true);
- });
- stop_button.on_release.push_back([&stop_button_hold](auto&)
- {
- stop_button_hold.pause();
- });
- bounds_layout button_layout ({&up_button, &stop_button, &down_button}, int2::j(5));
- button_layout.update();
- button_layout += int2::j() * center(button_layout, timer_layout);
- bounds_layout main_layout({&timer_layout, &button_layout}, int2::i(15));
- main_layout.update();
- main_layout += center(main_layout, range2D{int2::zero(), win.size()});
- autorestart_count_display += stop_button.lower() + button_rect.size/2 - autorestart_count_display_size/2;
- bool done = false;
- while(!done)
- {
- const auto current_time = std::chrono::steady_clock::now();
- using namespace interactive;
- while(auto event = next_event())
- {
- std::visit(support::overload{
- [&done](quit_request) { done = true; },
- [&](window_minimized) { frametime = slow_frametime; },
- [&](window_restored) { frametime = fast_frametime; },
- // TODO: remove? or keep as general failsafe?
- // this is a workaround for restored event not firing in SDL 2.0.10
- // https://bugzilla.libsdl.org/show_bug.cgi?id=4821
- [&](mouse_down) { frametime = fast_frametime; },
- [](auto) { }
- }, *event);
- main_focus_group.pre_update(*event);
- for(auto&& interactive : ui.interactives())
- interactive->update(*event);
- main_focus_group.post_update(*event);
- }
- if(frametime == fast_frametime)
- {
- fill(win.surface(), bg_color);
- for(auto&& graphic : ui.graphics())
- if(graphic != &autorestart_count_display || autorestart_count > 0)
- graphic->draw(win.surface());
- win.update();
- }
- { using namespace std::chrono;
- auto [h, m, s] =
- split_duration<hours, minutes, seconds>(current_timer.remaining_duration());
- title <<
- std::setw(2) << h.count() << ":" <<
- std::setw(2) << m.count() << ":" <<
- std::setw(2) << s.count() << " -- melno";
- win.title(title.str().c_str());
- title.str("");
- title.clear();
- }
- up_button.enable(!music_playing && !countup_point.has_value());
- down_button.enable(!music_playing && current_timer.paused());
- hours_display.enable(!music_playing);
- minutes_display.enable(!music_playing);
- seconds_display.enable(!music_playing);
- if(stop_button_hold.check())
- reset_current_timer();
- if(current_timer.check())
- {
- if(!music_playing)
- {
- music_playing = true;
- init.graphics.screensaver.keep_alive();
- win.restore();
- win.raise();
- frametime = fast_frametime;
- main_focus_group.focus(&stop_button);
- if(music)
- {
- device = std::make_unique<music_device>(
- musical::basic_device_parameters{music->obtained()},
- player
- );
- device->play();
- }
- }
- current_timer.pause();
- }
- if(music_playing && music_done)
- {
- autorestart_count_display.set(++autorestart_count);
- reset_current_timer();
- start_current_timer();
- }
- if(countup_point)
- current_timer = timer(clamp(timer::clock::now() - *countup_point, min_duration, max_duration));
- auto duration = current_timer.remaining_duration();
- hours_display.set(extract_duration<std::chrono::hours>(duration).count());
- minutes_display.set(extract_duration<std::chrono::minutes>(duration).count());
- seconds_display.set(extract_duration<std::chrono::seconds>(duration).count());
- const auto next_frame_time = current_timer.paused()
- ? current_time + frametime
- : min(current_timer.target_time_point(), current_time + frametime);
- std::this_thread::sleep_until(next_frame_time);
- }
- return 0;
- }
- catch(...)
- {
- if(errno)
- std::perror("ERROR");
- const char* sdl_error = SDL_GetError();
- if(*sdl_error)
- {
- std::fputs(sdl_error, stderr);
- std::fputs("\n", stderr);
- }
- throw;
- }
|