2020-10-24 17:57:45 +02:00
|
|
|
//
|
|
|
|
// main.cpp
|
|
|
|
// screensaver
|
|
|
|
//
|
|
|
|
// Created by Bob Polis at 2020-10-23
|
|
|
|
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
|
|
|
//
|
|
|
|
|
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 <time.h>
|
|
|
|
#include <cmath>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
// 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>
|
|
|
|
#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};
|
|
|
|
sc::gui::Image* main_image {nullptr};
|
2020-10-27 17:19:12 +01:00
|
|
|
ScreensaverPlugin* main_saver {nullptr};
|
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";
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char * argv[]) {
|
|
|
|
try {
|
2020-10-27 17:19:12 +01:00
|
|
|
// gather plugins
|
|
|
|
sc::plugin<ScreensaverPlugin>::scan_plugins(sc::dirname(sc::tool_path(argv[0])) + "/plugins", "saver");
|
|
|
|
std::unique_ptr<ScreensaverPlugin> saver;
|
|
|
|
|
2020-10-24 17:57:45 +02:00
|
|
|
int opt_char, opt_val;
|
|
|
|
struct option long_options[] = {
|
2020-10-27 17:19:12 +01:00
|
|
|
{"help", no_argument, nullptr, 'h'},
|
|
|
|
{"list", no_argument, nullptr, 'l'},
|
2020-10-24 17:57:45 +02:00
|
|
|
{"version", no_argument, &opt_val, 1},
|
|
|
|
{nullptr, 0, nullptr, 0}
|
|
|
|
};
|
2020-10-27 17:19:12 +01:00
|
|
|
while ((opt_char = getopt_long(argc, const_cast<char* const *>(argv), "hl", 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;
|
2020-10-24 17:57:45 +02:00
|
|
|
case '?':
|
|
|
|
throw std::runtime_error("unrecognized option");
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 17:19:12 +01:00
|
|
|
if (optind == argc) {
|
|
|
|
// here when no file args
|
|
|
|
saver = sc::plugin<ScreensaverPlugin>::get("rects")();
|
|
|
|
}
|
|
|
|
for (int i = optind; i < argc; ++i) {
|
|
|
|
try {
|
|
|
|
// process file argv[i]
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
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
|
2020-10-25 21:55:04 +01:00
|
|
|
sc::gui::Window& window {sc::gui::Window::new_window("living art")};
|
2020-10-25 20:31:59 +01:00
|
|
|
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);
|
|
|
|
|
2020-10-27 17:19:12 +01:00
|
|
|
// create an SDL drawing surface and connect it to cairo
|
2020-10-25 20:31:59 +01:00
|
|
|
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<unsigned char*>(s->pixels),
|
|
|
|
CAIRO_FORMAT_RGB24, s->w, s->h, s->pitch)};
|
2020-10-27 17:19:12 +01:00
|
|
|
std::unique_ptr<cairo_surface_t, void(*)(cairo_surface_t*)> cs_ {cs, cairo_surface_destroy};
|
2020-10-26 23:04:49 +01:00
|
|
|
cairo_t* cr {cairo_create(cs)};
|
2020-10-27 17:19:12 +01:00
|
|
|
std::unique_ptr<cairo_t, void(*)(cairo_t*)> cr_ {cr, cairo_destroy};
|
2020-10-26 23:04:49 +01:00
|
|
|
|
2020-10-27 17:19:12 +01:00
|
|
|
// setup screen saver module
|
|
|
|
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});
|
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;
|
|
|
|
}
|