// // main.cpp // screensaver // // Created by Bob Polis at 2020-10-23 // Copyright (c) 2020 SwiftCoder. All rights reserved. // // C++ #include #include #include #include #include #include #include // POSIX #include #include // libraries #include #include #include #include #include #include const int WIDTH {1600}; const int HEIGHT {900}; sc::gui::Window* main_window {nullptr}; sc::gui::Image* main_image {nullptr}; ScreensaverPlugin* main_saver {nullptr}; bool expired {true}; bool should_run {true}; void print_help() { std::cout << "usage: screensaver [-h|-l|--version]\n"; std::cout << " -h, --help show this help text and exit\n"; std::cout << " -l, --list show available screensaver modules and exit\n"; std::cout << " --version show version number and exit\n"; } void print_version() { std::cout << "screensaver version 1.0\n"; } void list_plugins() { for (const std::string& name : sc::plugin::names()) { std::cout << name << '\n'; } } void update_frame() { while (should_run) { expired = true; struct timespec delay; delay.tv_sec = 0; delay.tv_nsec = static_cast(round(1000000000.0 / main_saver->fps())); nanosleep(&delay, nullptr); } } void draw() { if (expired) { expired = false; sc::gui::ImageLock lock {*main_image}; main_saver->draw_frame(); main_window->show_image(*main_image); } } int main(int argc, const char * argv[]) { try { // gather plugins sc::plugin::scan_plugins(sc::dirname(sc::tool_path(argv[0])) + "/plugins", "saver"); std::unique_ptr saver; int opt_char, opt_val; struct option long_options[] = { {"help", no_argument, nullptr, 'h'}, {"list", no_argument, nullptr, 'l'}, {"version", no_argument, &opt_val, 1}, {nullptr, 0, nullptr, 0} }; while ((opt_char = getopt_long(argc, const_cast(argv), "hl", long_options, nullptr)) != -1) { std::string arg {optarg ? optarg : ""}; switch (opt_char) { case 0: { // handle long-only options here switch (opt_val) { case 1: print_version(); return EXIT_SUCCESS; } break; } case 'h': print_help(); return EXIT_SUCCESS; case 'l': list_plugins(); return EXIT_SUCCESS; case '?': throw std::runtime_error("unrecognized option"); } } if (optind == argc) { // here when no file args saver = sc::plugin::get("rects")(); } for (int i = optind; i < argc; ++i) { try { // process file argv[i] saver = sc::plugin::get(argv[i])(); break; // only first one used } catch (const std::runtime_error& ex) { std::cerr << "screensaver: " << ex.what() << '\n'; } catch (const std::out_of_range& ex) { std::cerr << "screensaver: no such screensaver module: " << argv[i] << '\n'; } } // RAII instances sc::gui::SDLWrapper sdl2; sc::gui::SDLImageWrapper sdl_image; // main window sc::gui::Window& window {sc::gui::Window::new_window("living art")}; main_window = &window; window.add_event_handler([](const SDL_Event& event, bool quit) -> bool { std::cerr << "closing window " << event.window.windowID << '\n'; return quit; }, SDL_WINDOWEVENT_CLOSE); // create an SDL drawing surface and connect it to cairo SDL_Surface* s {SDL_CreateRGBSurface(0, WIDTH, HEIGHT, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0)}; sc::gui::Image image {s}; main_image = ℑ cairo_surface_t* cs {cairo_image_surface_create_for_data(static_cast(s->pixels), CAIRO_FORMAT_RGB24, s->w, s->h, s->pitch)}; std::unique_ptr cs_ {cs, cairo_surface_destroy}; cairo_t* cr {cairo_create(cs)}; std::unique_ptr cr_ {cr, cairo_destroy}; // setup screen saver module if (!saver) { saver = sc::plugin::get("rects")(); std::cerr << "screensaver: using standard 'rects' module\n"; } main_saver = saver.get(); main_saver->setup(cr, {0, 0, WIDTH, HEIGHT}); // setup thread which periodically sets 'expired' std::thread frame_timer {update_frame}; sc::gui::app().add_run_loop_action(draw); sc::gui::app().run(); should_run = false; frame_timer.join(); } catch (const std::exception& ex) { std::cerr << "screensaver: " << ex.what() << '\n'; return EXIT_FAILURE; } return EXIT_SUCCESS; }