2020-10-27 17:19:12 +01:00
|
|
|
|
// C++
|
2020-10-24 17:57:45 +02:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <stdexcept>
|
2020-10-27 17:19:12 +01:00
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <thread>
|
2021-10-06 14:13:49 +02:00
|
|
|
|
#include <fstream>
|
2021-10-07 12:42:13 +02:00
|
|
|
|
#include <algorithm>
|
2020-10-27 17:19:12 +01:00
|
|
|
|
|
|
|
|
|
// POSIX
|
2020-10-24 17:57:45 +02:00
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <unistd.h>
|
2020-10-27 17:19:12 +01:00
|
|
|
|
|
|
|
|
|
// libraries
|
2020-10-24 17:57:45 +02:00
|
|
|
|
#include <cairo/cairo.h>
|
2020-10-27 17:19:12 +01:00
|
|
|
|
#include <libscgui.hpp>
|
2020-10-25 21:06:26 +01:00
|
|
|
|
#include <libscerror.hpp>
|
2020-10-27 17:19:12 +01:00
|
|
|
|
#include <libscscreensaver.hpp>
|
|
|
|
|
#include <libscstring.hpp>
|
2021-10-06 11:08:12 +02:00
|
|
|
|
#include <libscnumerics.hpp>
|
2020-10-27 17:19:12 +01:00
|
|
|
|
#include <sc/plugin.hpp>
|
2020-10-24 17:57:45 +02:00
|
|
|
|
|
2020-10-26 23:20:45 +01:00
|
|
|
|
const int WIDTH {1600};
|
|
|
|
|
const int HEIGHT {900};
|
2020-10-25 20:31:59 +01:00
|
|
|
|
|
|
|
|
|
sc::gui::Window* main_window {nullptr};
|
2020-10-27 17:19:12 +01:00
|
|
|
|
ScreensaverPlugin* main_saver {nullptr};
|
2021-02-01 10:35:59 +01:00
|
|
|
|
std::unique_ptr<sc::gui::Image> main_image {nullptr};
|
|
|
|
|
std::unique_ptr<cairo_t, void(*)(cairo_t*)> main_context {nullptr, cairo_destroy};
|
|
|
|
|
std::unique_ptr<cairo_surface_t, void(*)(cairo_surface_t*)> main_surface {nullptr, cairo_surface_destroy};
|
2020-10-25 20:31:59 +01:00
|
|
|
|
|
2020-10-24 17:57:45 +02:00
|
|
|
|
void print_help() {
|
2020-10-28 15:46:48 +01:00
|
|
|
|
std::cout << "usage: screensaver [-h|-l|--version]\n";
|
2020-10-24 17:57:45 +02:00
|
|
|
|
std::cout << " -h, --help show this help text and exit\n";
|
2020-10-28 15:46:48 +01:00
|
|
|
|
std::cout << " -l, --list show available screensaver modules and exit\n";
|
2021-10-05 14:25:37 +02:00
|
|
|
|
std::cout << " -r, --random choose random module from available ones\n";
|
2020-10-24 17:57:45 +02:00
|
|
|
|
std::cout << " --version show version number and exit\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void print_version() {
|
|
|
|
|
std::cout << "screensaver version 1.0\n";
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 17:19:12 +01:00
|
|
|
|
void list_plugins() {
|
|
|
|
|
for (const std::string& name : sc::plugin<ScreensaverPlugin>::names()) {
|
|
|
|
|
std::cout << name << '\n';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 10:35:59 +01:00
|
|
|
|
void create_image(int w, int h) {
|
|
|
|
|
// create an SDL drawing surface and connect it to cairo
|
|
|
|
|
main_image.reset(new sc::gui::Image {w, h});
|
|
|
|
|
SDL_Surface* s = main_image->surface();
|
|
|
|
|
cairo_surface_t* cs {cairo_image_surface_create_for_data(static_cast<unsigned char*>(s->pixels),
|
|
|
|
|
CAIRO_FORMAT_RGB24, s->w, s->h, s->pitch)};
|
|
|
|
|
main_surface.reset(cs);
|
|
|
|
|
cairo_t* cr {cairo_create(cs)};
|
|
|
|
|
main_context.reset(cr);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 20:31:59 +01:00
|
|
|
|
void draw() {
|
2020-11-15 13:16:49 +01:00
|
|
|
|
main_saver->update();
|
2020-11-15 09:08:56 +01:00
|
|
|
|
sc::gui::ImageLock lock {*main_image};
|
2020-11-15 13:16:49 +01:00
|
|
|
|
main_saver->render();
|
2020-11-15 09:08:56 +01:00
|
|
|
|
main_window->dirty(true);
|
|
|
|
|
main_window->show_image(*main_image);
|
2020-10-24 17:57:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 10:35:59 +01:00
|
|
|
|
bool handle_window_resize(const SDL_Event& event, bool quit) {
|
|
|
|
|
if (event.window.data1 != main_image->width() || event.window.data2 != main_image->height()) {
|
|
|
|
|
std::cerr << "window resized to " << event.window.data1 << "×" << event.window.data2 << " pixels\n";
|
|
|
|
|
create_image(event.window.data1, event.window.data2);
|
|
|
|
|
cairo_rectangle_t r {0, 0, static_cast<double>(event.window.data1), static_cast<double>(event.window.data2)};
|
|
|
|
|
if (main_saver) {
|
|
|
|
|
main_saver->setup(main_context.get(), r);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return quit;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 17:57:45 +02:00
|
|
|
|
int main(int argc, const char * argv[]) {
|
|
|
|
|
try {
|
2020-10-27 17:19:12 +01:00
|
|
|
|
// gather plugins
|
2021-02-07 17:05:19 +01:00
|
|
|
|
#if DEBUG
|
2021-10-06 11:08:12 +02:00
|
|
|
|
std::string plugin_dir {sc::dirname(sc::tool_path(argv[0])) + "/plugins"};
|
2021-02-07 17:05:19 +01:00
|
|
|
|
#else
|
2021-10-06 11:08:12 +02:00
|
|
|
|
std::string plugin_dir {"/usr/local/share/screensaver/plugins"};
|
2021-02-07 17:05:19 +01:00
|
|
|
|
#endif
|
2021-10-06 11:08:12 +02:00
|
|
|
|
sc::plugin<ScreensaverPlugin>::scan_plugins(plugin_dir, "saver");
|
2021-10-06 14:13:49 +02:00
|
|
|
|
std::string saver_name;
|
2021-10-05 14:25:37 +02:00
|
|
|
|
bool random_saver {false};
|
2020-10-24 17:57:45 +02:00
|
|
|
|
int opt_char, opt_val;
|
|
|
|
|
struct option long_options[] = {
|
2021-08-18 11:30:53 +02:00
|
|
|
|
{"help", no_argument, nullptr, 'h'},
|
|
|
|
|
{"list", no_argument, nullptr, 'l'},
|
2021-10-05 14:25:37 +02:00
|
|
|
|
{"random", no_argument, nullptr, 'r'},
|
2020-10-24 17:57:45 +02:00
|
|
|
|
{"version", no_argument, &opt_val, 1},
|
|
|
|
|
{nullptr, 0, nullptr, 0}
|
|
|
|
|
};
|
2021-10-05 14:25:37 +02:00
|
|
|
|
while ((opt_char = getopt_long(argc, const_cast<char* const *>(argv), "hlr", long_options, nullptr)) != -1) {
|
2020-10-24 17:57:45 +02:00
|
|
|
|
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;
|
2020-10-27 17:19:12 +01:00
|
|
|
|
case 'l':
|
|
|
|
|
list_plugins();
|
|
|
|
|
return EXIT_SUCCESS;
|
2021-10-05 14:25:37 +02:00
|
|
|
|
case 'r':
|
|
|
|
|
random_saver = true;
|
|
|
|
|
break;
|
2020-10-24 17:57:45 +02:00
|
|
|
|
case '?':
|
|
|
|
|
throw std::runtime_error("unrecognized option");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-07 12:42:13 +02:00
|
|
|
|
std::vector<std::string> names {sc::plugin<ScreensaverPlugin>::names()};
|
2020-10-27 17:19:12 +01:00
|
|
|
|
if (optind == argc) {
|
|
|
|
|
// here when no file args
|
2021-10-05 14:25:37 +02:00
|
|
|
|
if (random_saver) {
|
2021-10-06 14:13:49 +02:00
|
|
|
|
saver_name = sc::random::choice(names);
|
2021-10-05 14:25:37 +02:00
|
|
|
|
} else {
|
2021-10-06 14:13:49 +02:00
|
|
|
|
saver_name = "Default";
|
2021-10-05 14:25:37 +02:00
|
|
|
|
}
|
2020-10-27 17:19:12 +01:00
|
|
|
|
}
|
|
|
|
|
for (int i = optind; i < argc; ++i) {
|
|
|
|
|
try {
|
|
|
|
|
// process file argv[i]
|
2021-10-06 14:13:49 +02:00
|
|
|
|
saver_name = argv[i];
|
2021-10-05 14:29:12 +02:00
|
|
|
|
if (random_saver) {
|
|
|
|
|
std::cerr << "screensaver: warning: -r option overridden by file arg\n";
|
|
|
|
|
}
|
2021-10-07 12:42:13 +02:00
|
|
|
|
auto it = std::find(names.begin(), names.end(), saver_name);
|
|
|
|
|
if (it == names.end()) {
|
|
|
|
|
saver_name = "";
|
|
|
|
|
throw std::out_of_range(saver_name);
|
|
|
|
|
}
|
2020-10-27 17:19:12 +01:00
|
|
|
|
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';
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-24 17:57:45 +02:00
|
|
|
|
|
2020-10-25 20:31:59 +01:00
|
|
|
|
// RAII instances
|
|
|
|
|
sc::gui::SDLWrapper sdl2;
|
|
|
|
|
sc::gui::SDLImageWrapper sdl_image;
|
|
|
|
|
|
|
|
|
|
// main window
|
2021-10-10 11:55:53 +02:00
|
|
|
|
sc::gui::Window& window {sc::gui::Window::new_window("Living Art", SDL_WINDOW_FULLSCREEN_DESKTOP)};
|
2020-10-25 20:31:59 +01:00
|
|
|
|
main_window = &window;
|
2021-02-01 10:35:59 +01:00
|
|
|
|
window.add_event_handler(handle_window_resize, SDL_WINDOWEVENT_RESIZED);
|
|
|
|
|
window.add_event_handler(handle_window_resize, SDL_WINDOWEVENT_SIZE_CHANGED);
|
2021-10-10 11:55:53 +02:00
|
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
2020-10-25 20:31:59 +01:00
|
|
|
|
|
2021-02-01 10:35:59 +01:00
|
|
|
|
// setup sdl surface and connect to cairo
|
|
|
|
|
create_image(WIDTH, HEIGHT);
|
2020-10-26 23:04:49 +01:00
|
|
|
|
|
2020-10-27 17:19:12 +01:00
|
|
|
|
// setup screen saver module
|
2021-10-06 14:13:49 +02:00
|
|
|
|
if (saver_name.size() == 0) {
|
|
|
|
|
saver_name = "Default";
|
2021-01-26 16:00:09 +01:00
|
|
|
|
std::cerr << "screensaver: using standard 'Default' module\n";
|
2020-10-27 17:19:12 +01:00
|
|
|
|
}
|
2021-10-06 14:13:49 +02:00
|
|
|
|
std::unique_ptr<ScreensaverPlugin> saver {sc::plugin<ScreensaverPlugin>::get(saver_name)()};
|
2020-10-27 17:19:12 +01:00
|
|
|
|
main_saver = saver.get();
|
2021-02-01 10:35:59 +01:00
|
|
|
|
main_saver->setup(main_context.get(), {0, 0, WIDTH, HEIGHT});
|
2021-10-06 14:13:49 +02:00
|
|
|
|
std::string config_path {plugin_dir + "/" + saver_name + ".json"};
|
2021-10-06 14:49:32 +02:00
|
|
|
|
std::ifstream config_file {config_path};
|
|
|
|
|
if (config_file) {
|
|
|
|
|
std::cerr << "screensaver: reading config file " << config_path << '\n';
|
2021-10-06 14:13:49 +02:00
|
|
|
|
nlohmann::json j;
|
|
|
|
|
config_file >> j;
|
2021-10-06 14:50:52 +02:00
|
|
|
|
main_saver->config(std::move(j));
|
2021-10-06 14:13:49 +02:00
|
|
|
|
}
|
2020-10-25 20:31:59 +01:00
|
|
|
|
|
2020-11-15 09:08:56 +01:00
|
|
|
|
sc::gui::app().fps(main_saver->fps());
|
2020-10-25 20:31:59 +01:00
|
|
|
|
sc::gui::app().add_run_loop_action(draw);
|
2020-10-24 17:57:45 +02:00
|
|
|
|
sc::gui::app().run();
|
|
|
|
|
|
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
|
std::cerr << "screensaver: " << ex.what() << '\n';
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|