screensaver/main.cpp

172 lines
5.7 KiB
C++
Raw Normal View History

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.
//
// C++
2020-10-24 17:57:45 +02:00
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdexcept>
#include <cmath>
#include <thread>
// POSIX
2020-10-24 17:57:45 +02:00
#include <getopt.h>
#include <unistd.h>
// libraries
2020-10-24 17:57:45 +02:00
#include <cairo/cairo.h>
#include <libscgui.hpp>
#include <libscerror.hpp>
#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};
sc::gui::Window* main_window {nullptr};
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-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";
}
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);
}
void draw() {
main_saver->update();
2020-11-15 09:08:56 +01:00
sc::gui::ImageLock lock {*main_image};
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 {
// gather plugins
2021-02-07 17:05:19 +01:00
#if DEBUG
sc::plugin<ScreensaverPlugin>::scan_plugins(sc::dirname(sc::tool_path(argv[0])) + "/plugins", "saver");
2021-02-07 17:05:19 +01:00
#else
sc::plugin<ScreensaverPlugin>::scan_plugins("/usr/local/share/screensaver/plugins", "saver");
#endif
std::unique_ptr<ScreensaverPlugin> saver;
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'},
2020-10-24 17:57:45 +02:00
{"version", no_argument, &opt_val, 1},
{nullptr, 0, nullptr, 0}
};
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;
case 'l':
list_plugins();
return EXIT_SUCCESS;
2020-10-24 17:57:45 +02:00
case '?':
throw std::runtime_error("unrecognized option");
}
}
if (optind == argc) {
// here when no file args
saver = sc::plugin<ScreensaverPlugin>::get("Default")();
}
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
// RAII instances
sc::gui::SDLWrapper sdl2;
sc::gui::SDLImageWrapper sdl_image;
// main window
2021-02-01 10:35:59 +01:00
sc::gui::Window& window {sc::gui::Window::new_window("Living Art")};
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-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
// setup screen saver module
if (!saver) {
saver = sc::plugin<ScreensaverPlugin>::get("Default")();
std::cerr << "screensaver: using standard 'Default' module\n";
}
main_saver = saver.get();
2021-02-01 10:35:59 +01:00
main_saver->setup(main_context.get(), {0, 0, WIDTH, HEIGHT});
2020-11-15 09:08:56 +01:00
sc::gui::app().fps(main_saver->fps());
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;
}