screensaver/main.cpp

175 lines
5.2 KiB
C++

//
// main.cpp
// screensaver
//
// Created by Bob Polis at 2020-10-23
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdexcept>
#include <getopt.h>
#include <libscgui.hpp>
#include <unistd.h>
#include <cairo/cairo.h>
#include <signal.h>
#include <time.h>
#include <libscerror.hpp>
const int WIDTH {700};
const int HEIGHT {700};
sc::gui::Window* main_window {nullptr};
sc::gui::Image* main_image {nullptr};
cairo_t* cr {nullptr};
bool timer_expired {true};
void print_help() {
std::cout << "usage: screensaver [-h|--version]\n";
std::cout << " -h, --help show this help text and exit\n";
std::cout << " --version show version number and exit\n";
}
void print_version() {
std::cout << "screensaver version 1.0\n";
}
void timer_signal_handler(int /*sig*/, siginfo_t* /*si*/, void* /*uc*/) {
// timer_t* tmr {si->si_value.sival_ptr};
timer_expired = true;
}
void draw() {
static int x = 300;
static int y = 300;
if (timer_expired) {
timer_expired = false;
sc::gui::ImageLock lock {*main_image};
// white background
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_rectangle(cr, 0, 0, WIDTH, HEIGHT);
cairo_fill(cr);
// orange rectangle
cairo_rectangle(cr, x, y, 100, 100);
cairo_set_source_rgb(cr, 1, 0.5, 0);
cairo_fill(cr);
// black rectangle
cairo_rectangle(cr, 200, 200, 100, 100);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_fill(cr);
// dark green text
cairo_set_source_rgb(cr, 0.2, 0.5, 0);
cairo_select_font_face(cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 42);
cairo_move_to(cr, 50, 100);
cairo_show_text(cr, "abcdefghijklmnopqrstuvwxyz");
// show
main_window->show_image(*main_image);
// update
x++;
y++;
if (x > 400) x = 300;
if (y > 400) y = 300;
}
}
int main(int argc, const char * argv[]) {
try {
int opt_char, opt_val;
struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"version", no_argument, &opt_val, 1},
{nullptr, 0, nullptr, 0}
};
while ((opt_char = getopt_long(argc, const_cast<char* const *>(argv), "h", 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 '?':
throw std::runtime_error("unrecognized option");
}
}
// if (optind == argc) {
// // here when no file args
// }
// for (int i = optind; i < argc; ++i) {
// try {
// // process file argv[i]
// } catch (const std::runtime_error& ex) {
// std::cerr << "screensaver: " << ex.what() << '\n';
// }
// }
// install timer signal handler
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timer_signal_handler;
sigemptyset(&sa.sa_mask);
throw_if_min1(sigaction(SIGRTMAX, &sa, nullptr));
// setup repeating timer with 100 ms interval
timer_t timer;
struct sigevent sev;
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMAX;
sev.sigev_value.sival_ptr = &timer;
struct itimerspec ts;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 100000000;
ts.it_value.tv_sec = 0;
ts.it_value.tv_nsec = 1;
throw_if_min1(timer_create(CLOCK_REALTIME, &sev, &timer));
throw_if_min1(timer_settime(timer, 0, &ts, nullptr));
// RAII instances
sc::gui::SDLWrapper sdl2;
sc::gui::SDLImageWrapper sdl_image;
// main window
sc::gui::Window& window {sc::gui::Window::new_window("screensaver")};
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);
SDL_Surface* s {SDL_CreateRGBSurface(0, WIDTH, HEIGHT, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0)};
sc::gui::Image image {s};
main_image = &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)};
cr = cairo_create(cs);
sc::gui::app().add_run_loop_action(draw);
sc::gui::app().run();
cairo_surface_destroy(cs);
cairo_destroy(cr);
} catch (const std::exception& ex) {
std::cerr << "screensaver: " << ex.what() << '\n';
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}