commit ea2821640d7101a93f41a298417a43a58b1a846e Author: Bob Polis Date: Sat Oct 24 17:57:45 2020 +0200 first commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..18829b9 --- /dev/null +++ b/Makefile @@ -0,0 +1,53 @@ +BIN := $(shell basename $$(pwd)) + +MANSECTION := 1 +MANPAGE := $(BIN).$(MANSECTION) + +SRCS := $(wildcard *.cpp) +OBJS := $(subst .cpp,.o,$(SRCS)) +DEPS := $(subst .cpp,.d,$(SRCS)) + +CXX ?= g++ +PKG_CONFIG ?= pkg-config + +PREFIX ?= /usr/local +BINDIR ?= $(PREFIX)/bin +CONFIGDIR ?= $(PREFIX)/etc +DATADIR ?= $(PREFIX)/share +MANDIR ?= $(DATADIR)/man/man +DOCDIR ?= $(DATADIR)/$(BIN)/doc + +RM := /bin/rm -f +INSTALL := /usr/bin/install -c + +CXXFLAGS := $(CXXFLAGS) -Wshadow -Wall -Wpedantic -Wextra -g -fno-strict-aliasing -std=c++17 -pthread +ifeq ($(DEBUG),1) + CXXFLAGS += -D DEBUG -O0 +else + CXXFLAGS += -D NDEBUG -O3 +endif + +LDLIBS := -lm -lpthread -lscgui -lSDL2 -lcairo + +.PHONY: all clean install + +all: $(BIN) + +$(BIN): $(OBJS) $(DEPS) + $(CXX) $(OBJS) $(LDFLAGS) $(LDLIBS) -o $(BIN) + +%.o: %.cpp %.d Makefile + $(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.d -c $< + +-include *.d + +%.d: ; + +clean: + $(RM) $(OBJS) $(DEPS) $(BIN) + +install: $(BIN) + $(INSTALL) -d $(BINDIR) + $(INSTALL) $(BIN) $(BINDIR) + $(INSTALL) -d $(MANDIR)$(MANSECTION) + $(INSTALL) $(MANPAGE) $(MANDIR)$(MANSECTION) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..c2f1f72 --- /dev/null +++ b/main.cpp @@ -0,0 +1,100 @@ +// +// main.cpp +// screensaver +// +// Created by Bob Polis at 2020-10-23 +// Copyright (c) 2020 SwiftCoder. All rights reserved. +// + +#include +#include +#include +#include +#include +#include +#include +#include + +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 draw(sc::gui::Window& window) { + SDL_Surface* s {SDL_CreateRGBSurface(0, 700, 700, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0)}; + sc::gui::Image image {s}; + SDL_FillRect(s, nullptr, SDL_MapRGB(s->format, 255, 255, 255)); + SDL_Rect rect { 300, 300, 100, 100 }; + SDL_FillRect(s, &rect, SDL_MapRGB(s->format, 255, 128, 0)); + sc::gui::ImageLock lock {image}; + cairo_surface_t* cs {cairo_image_surface_create_for_data(static_cast(s->pixels), + CAIRO_FORMAT_RGB24, s->w, s->h, s->pitch)}; + cairo_t* cr {cairo_create(cs)}; + cairo_set_line_width(cr, 0.1); + cairo_set_source_rgb(cr, 0, 0, 0); + cairo_rectangle(cr, 200, 200, 100, 100); + cairo_fill(cr); + window.show_image(image); + cairo_surface_destroy(cs); + cairo_destroy(cr); +} + +int main(int argc, const char * argv[]) { + try { + // RAII instances + sc::gui::SDLWrapper sdl2; + sc::gui::SDLImageWrapper sdl_image; + + // main window + sc::gui::Window& window {sc::gui::Window::new_window("screensaver")}; + + 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(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'; + } + } + + draw(window); + sc::gui::app().run(); + + } catch (const std::exception& ex) { + std::cerr << "screensaver: " << ex.what() << '\n'; + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} diff --git a/screensaver.1 b/screensaver.1 new file mode 100644 index 0000000..17473d7 --- /dev/null +++ b/screensaver.1 @@ -0,0 +1,57 @@ +.Dd $Mdocdate$ +.Dt screensaver 1 +.Os +.Sh NAME +.Nm screensaver +.Nd one line about what it does +.\" .Sh LIBRARY +.\" For sections 2, 3, and 9 only. +.\" Not used in OpenBSD. +.Sh SYNOPSIS +.Nm +.Fl h | v +.Nm +.Op Ar +.Sh DESCRIPTION +The +.Nm +utility processes files ... +When no file arguments are given, +.Nm +will read from the standard input. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl h, \-help +Print help text and exit. +.It Fl v, \-version +Print version info and exit. +.El +.\" .Sh CONTEXT +.\" For section 9 functions only. +.\" .Sh IMPLEMENTATION NOTES +.\" Not used in OpenBSD. +.\" .Sh RETURN VALUES +.\" For sections 2, 3, and 9 function return values only. +.\" .Sh ENVIRONMENT +.\" For sections 1, 6, 7, and 8 only. +.\" .Sh FILES +.Sh EXIT STATUS +.\" For sections 1, 6, and 8 only. +.Nm +exits 0 on success, and 1 if an error occurs. +.\" .Sh EXAMPLES +.\" .Sh DIAGNOSTICS +.\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only. +.\" .Sh ERRORS +.\" For sections 2, 3, 4, and 9 errno settings only. +.\" .Sh SEE ALSO +.\" .Xr foobar 1 +.\" .Sh STANDARDS +.\" .Sh HISTORY +.Sh AUTHORS +Bob Polis +.\" .Sh CAVEATS +.\" .Sh BUGS +.\" .Sh SECURITY CONSIDERATIONS +.\" Not used in OpenBSD.