refactored to use plugins, possibly requested from command line

This commit is contained in:
Bob Polis
2020-10-27 17:19:12 +01:00
parent bf1b16dae9
commit c5da8561fe

View File

@ -6,26 +6,33 @@
// Copyright (c) 2020 SwiftCoder. All rights reserved. // Copyright (c) 2020 SwiftCoder. All rights reserved.
// //
// C++
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include <getopt.h>
#include <libscgui.hpp>
#include <unistd.h>
#include <cairo/cairo.h>
#include <time.h> #include <time.h>
#include <libscerror.hpp>
#include <cmath> #include <cmath>
#include <thread> #include <thread>
#include "RectSaver.hpp"
// POSIX
#include <getopt.h>
#include <unistd.h>
// libraries
#include <cairo/cairo.h>
#include <libscgui.hpp>
#include <libscerror.hpp>
#include <libscscreensaver.hpp>
#include <libscstring.hpp>
#include <sc/plugin.hpp>
const int WIDTH {1600}; const int WIDTH {1600};
const int HEIGHT {900}; const int HEIGHT {900};
sc::gui::Window* main_window {nullptr}; sc::gui::Window* main_window {nullptr};
sc::gui::Image* main_image {nullptr}; sc::gui::Image* main_image {nullptr};
RectSaver* main_saver {nullptr}; ScreensaverPlugin* main_saver {nullptr};
bool expired {true}; bool expired {true};
bool should_run {true}; bool should_run {true};
@ -39,6 +46,12 @@ void print_version() {
std::cout << "screensaver version 1.0\n"; std::cout << "screensaver version 1.0\n";
} }
void list_plugins() {
for (const std::string& name : sc::plugin<ScreensaverPlugin>::names()) {
std::cout << name << '\n';
}
}
void update_frame() { void update_frame() {
while (should_run) { while (should_run) {
expired = true; expired = true;
@ -61,13 +74,18 @@ void draw() {
int main(int argc, const char * argv[]) { int main(int argc, const char * argv[]) {
try { try {
// gather plugins
sc::plugin<ScreensaverPlugin>::scan_plugins(sc::dirname(sc::tool_path(argv[0])) + "/plugins", "saver");
std::unique_ptr<ScreensaverPlugin> saver;
int opt_char, opt_val; int opt_char, opt_val;
struct option long_options[] = { struct option long_options[] = {
{"help", no_argument, nullptr, 'h'}, {"help", no_argument, nullptr, 'h'},
{"list", no_argument, nullptr, 'l'},
{"version", no_argument, &opt_val, 1}, {"version", no_argument, &opt_val, 1},
{nullptr, 0, nullptr, 0} {nullptr, 0, nullptr, 0}
}; };
while ((opt_char = getopt_long(argc, const_cast<char* const *>(argv), "h", long_options, nullptr)) != -1) { while ((opt_char = getopt_long(argc, const_cast<char* const *>(argv), "hl", long_options, nullptr)) != -1) {
std::string arg {optarg ? optarg : ""}; std::string arg {optarg ? optarg : ""};
switch (opt_char) { switch (opt_char) {
case 0: { case 0: {
@ -82,20 +100,28 @@ int main(int argc, const char * argv[]) {
case 'h': case 'h':
print_help(); print_help();
return EXIT_SUCCESS; return EXIT_SUCCESS;
case 'l':
list_plugins();
return EXIT_SUCCESS;
case '?': case '?':
throw std::runtime_error("unrecognized option"); throw std::runtime_error("unrecognized option");
} }
} }
// if (optind == argc) { if (optind == argc) {
// // here when no file args // here when no file args
// } saver = sc::plugin<ScreensaverPlugin>::get("rects")();
// for (int i = optind; i < argc; ++i) { }
// try { for (int i = optind; i < argc; ++i) {
// // process file argv[i] try {
// } catch (const std::runtime_error& ex) { // process file argv[i]
// std::cerr << "screensaver: " << ex.what() << '\n'; saver = sc::plugin<ScreensaverPlugin>::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 // RAII instances
sc::gui::SDLWrapper sdl2; sc::gui::SDLWrapper sdl2;
@ -109,15 +135,23 @@ int main(int argc, const char * argv[]) {
return quit; return quit;
}, SDL_WINDOWEVENT_CLOSE); }, 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)}; SDL_Surface* s {SDL_CreateRGBSurface(0, WIDTH, HEIGHT, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0)};
sc::gui::Image image {s}; sc::gui::Image image {s};
main_image = &image; main_image = &image;
cairo_surface_t* cs {cairo_image_surface_create_for_data(static_cast<unsigned char*>(s->pixels), 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)}; CAIRO_FORMAT_RGB24, s->w, s->h, s->pitch)};
std::unique_ptr<cairo_surface_t, void(*)(cairo_surface_t*)> cs_ {cs, cairo_surface_destroy};
cairo_t* cr {cairo_create(cs)}; cairo_t* cr {cairo_create(cs)};
std::unique_ptr<cairo_t, void(*)(cairo_t*)> cr_ {cr, cairo_destroy};
RectSaver saver {cr, {0, 0, WIDTH, HEIGHT}}; // setup screen saver module
main_saver = &saver; if (!saver) {
saver = sc::plugin<ScreensaverPlugin>::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' // setup thread which periodically sets 'expired'
std::thread frame_timer {update_frame}; std::thread frame_timer {update_frame};
@ -125,9 +159,6 @@ int main(int argc, const char * argv[]) {
sc::gui::app().add_run_loop_action(draw); sc::gui::app().add_run_loop_action(draw);
sc::gui::app().run(); sc::gui::app().run();
cairo_surface_destroy(cs);
cairo_destroy(cr);
should_run = false; should_run = false;
frame_timer.join(); frame_timer.join();