selk | Keyboard-only rectangle selector for X |
| download: https://git.y1.nz/archives/selk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 0aee9ed3d766d3234f5793eddcebb6e39246f165 parent ce1a7bc6685440a31cd95da8b8882dcee004b122 Author: Emma Weaver <emma@waeaves.com> Date: Sat, 18 Apr 2026 13:56:54 -0400 clean(ish) compilation checkpoint Diffstat:
| M | Makefile | 9 | +++++++-- |
| M | config.mk | 7 | +++++-- |
| A | draw.c | 144 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | draw.h | 48 | ++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | main-draw.c | 9 | +++++++++ |
| A | util.c | 37 | +++++++++++++++++++++++++++++++++++++ |
| A | util.h | 9 | +++++++++ |
7 files changed, 259 insertions(+), 4 deletions(-)
diff --git a/Makefile b/Makefile @@ -2,10 +2,15 @@ include config.mk grabs: bmp.c bmp.h -grabs: config.h +grabs: config.h config.mk grabs: main.c cc main.c ${CFLAGS} ${LDFLAGS} -o grabs +draw: draw.c draw.h +draw: config.h config.mk +draw: main-draw.c + cc main-draw.c ${CFLAGS} ${LDFLAGS} -o draw + config.h: config.default.h cp config.default.h config.h @@ -25,5 +30,5 @@ config.h: config.default.h # chmod 644 $(DESTDIR)$(MANPREFIX)/man1/st.1 # tic -sx st.info -.PHONY: test bmp clean +.PHONY: test draw clean diff --git a/config.mk b/config.mk @@ -10,9 +10,12 @@ MANPREFIX = ${PREFIX}/share/man X11INC = /usr/X11R6/include X11LIB = /usr/X11R6/lib +FREETYPELIBS = -lfontconfig -lXft +FREETYPEINC = /usr/include/freetype2 + # includes and libs -INCS = -I${X11INC} -LIBS = -L${X11LIB} -lX11 +INCS = -I${X11INC} -I${FREETYPEINC} +LIBS = -L${X11LIB} -lX11 ${FREETYPELIBS} # flags CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700L -DVERSION=\"${VERSION}\" diff --git a/draw.c b/draw.c @@ -0,0 +1,144 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <X11/Xlib.h> +#include <X11/Xft/Xft.h> + +#include "draw.h" +#include "util.h" + +Draw * +draw_create(Display * display, int screen, Window root, unsigned int w, unsigned int h) +{ + Draw * draw = ecalloc(1, sizeof(Draw)); + + draw->display = display; + draw->screen = screen; + draw->root = root; + draw->w = w; + draw->h = h; + draw->drawable = XCreatePixmap(display, root, w, h, DefaultDepth(display, screen)); + draw->gc = XCreateGC(display, root, 0, NULL); + XSetLineAttributes(display, draw->gc, 1, LineSolid, CapButt, JoinMiter); + + return draw; +} + +void +draw_resize(Draw * draw, unsigned int w, unsigned int h) +{ + if (!draw) + return; + + draw->w = w; + draw->h = h; + if (draw->drawable) + XFreePixmap(draw->display, draw->drawable); + draw->drawable = XCreatePixmap(draw->display, draw->root, w, h, DefaultDepth(draw->display, draw->screen)); +} + +void +draw_free(Draw * draw) +{ + XFreePixmap(draw->display, draw->drawable); + XFreeGC(draw->display, draw->gc); + free(draw); +} + +void +draw_color_create(Draw * draw, ColorScheme * dest, const char * color_name) +{ + if (!draw || !dest || !color_name) + return; + + if (!XftColorAllocName(draw->display, DefaultVisual(draw->display, draw->screen), + DefaultColormap(draw->display, draw->screen), + color_name, dest)) + die("error, cannot allocate color '%s'", color_name); +} + +/* Create color schemes. */ +ColorScheme * +draw_scheme_create(Draw * draw, const char * color_names[], size_t color_count) +{ + size_t i; + ColorScheme * ret; + + /* need at least two colors for a scheme */ + if (!draw || !color_names || color_count < 2 || !(ret = ecalloc(color_count, sizeof(ColorScheme)))) + return NULL; + + for (i = 0; i < color_count; i++) + draw_color_create(draw, &ret[i], color_names[i]); + return ret; +} + +void +draw_color_free(Draw * draw, ColorScheme * c) +{ + if (!draw || !c) + return; + + /* c is typedef XftColor ColorScheme */ + XftColorFree( + draw->display, + DefaultVisual(draw->display, draw->screen), + DefaultColormap(draw->display, draw->screen), + c + ); +} + +void +draw_scheme_free(Draw * draw, ColorScheme * scheme, size_t color_count) +{ + size_t i; + + if (!draw || !scheme) + return; + + for (i = 0; i < color_count; i++) + draw_color_free(draw, &scheme[i]); + free(scheme); +} + +void +draw_setscheme(Draw * draw, ColorScheme * scheme) +{ + if (draw) + draw->scheme = scheme; +} + +void +draw_rect(Draw * draw, int x, int y, unsigned int w, unsigned int h, enum ColorSchemeIndex color) +{ + if (!draw || !draw->scheme) + return; + XSetForeground(draw->display, draw->gc, draw->scheme[color].pixel); + XFillRectangle(draw->display, draw->drawable, draw->gc, x, y, w, h); /* filled? */ + // XDrawRectangle(draw->display, draw->drawable, draw->gc, x, y, w - 1, h - 1); /* unfilled... */ +} + +void +draw_frame( + Draw * draw, + int x, int y, unsigned int w, unsigned int h, + int gx, int gy, unsigned int gw, unsigned int gh, + enum ColorSchemeIndex color +) +{ + draw_rect(draw, x, y, w, gy-y, color); /* top */ + draw_rect(draw, x, gy, gx-x, gh, color); /* left */ + draw_rect(draw, gx+gw, gy, x+w-(gx+gw), gh, color); /* right */ + draw_rect(draw, x, gy+gh, w, y+h-(gy+gh), color); /* bottom */ +} + +void +draw_map(Draw * draw, Window window, int x, int y, unsigned int w, unsigned int h) +{ + if (!draw) + return; + + XCopyArea(draw->display, draw->drawable, window, draw->gc, x, y, w, h, x, y); + XSync(draw->display, False); +} diff --git a/draw.h b/draw.h @@ -0,0 +1,48 @@ +/* See LICENSE file for copyright and license details. */ + +enum ColorSchemeIndex { + ForegroundColor, + BackgroundColor, + BorderColor +} ColorSchemeIndex; +typedef XftColor ColorScheme; + +typedef struct { + unsigned int w, h; + Display * display; + int screen; + Window root; + Drawable drawable; + GC gc; + ColorScheme * scheme; +} Draw; + +/* Drawable abstraction */ +Draw * draw_create(Display * display, int screen, Window window, unsigned int w, unsigned int h); +void draw_resize(Draw * draw, unsigned int w, unsigned int h); +void draw_free(Draw * draw); + +/* Colorscheme abstraction */ +void draw_color_create(Draw * draw, ColorScheme * dest, const char * color_name); +void draw_color_free(Draw * draw, ColorScheme * c); +ColorScheme * draw_scheme_create(Draw * draw, const char * color_names[], size_t color_count); +void draw_scheme_free(Draw * draw, ColorScheme * scheme, size_t color_count); + +/* Drawing context manipulation */ +void draw_setscheme(Draw * draw, ColorScheme * scheme); + +/* Drawing functions */ +void draw_rect( + Draw * draw, + int x, int y, unsigned int w, unsigned int h, + enum ColorSchemeIndex color +); +void draw_frame( + Draw * draw, + int x, int y, unsigned int w, unsigned int h, + int gx, int gy, unsigned int gw, unsigned int gh, + enum ColorSchemeIndex color +); + +/* Map functions */ +void draw_map(Draw * draw, Window window, int x, int y, unsigned int w, unsigned int h); diff --git a/main-draw.c b/main-draw.c @@ -0,0 +1,9 @@ +#include <stdio.h> + +#include "draw.c" + +int +main() +{ + printf("Ran it. Nice!" "\n"); +} diff --git a/util.c b/util.c @@ -0,0 +1,37 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "util.h" + +void +die(const char *fmt, ...) +{ + va_list ap; + int saved_errno; + + saved_errno = errno; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt)-1] == ':') + fprintf(stderr, " %s", strerror(saved_errno)); + fputc('\n', stderr); + + exit(1); +} + +void * +ecalloc(size_t nmemb, size_t size) +{ + void *p; + + if (!(p = calloc(nmemb, size))) + die("calloc:"); + return p; +} diff --git a/util.h b/util.h @@ -0,0 +1,9 @@ +/* See LICENSE file for copyright and license details. */ + +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) +#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) +#define LENGTH(X) (sizeof (X) / sizeof (X)[0]) + +void die(const char *fmt, ...); +void *ecalloc(size_t nmemb, size_t size);
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.