Simplify a lot

This commit is contained in:
Jacob 2023-08-07 06:19:15 +02:00
parent 0d4f3960b8
commit 61d81819f4
8 changed files with 108 additions and 95 deletions

View file

@ -1,10 +0,0 @@
/* See LICENSE file for copyright and license details. */
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
&& MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
#define LENGTH(X) (sizeof X / sizeof X[0])
#define TEXTW(X) (draw_font_getwidth(draw, (X), False) + sp.lrpad)
#define TEXTWM(X) (draw_font_getwidth(draw, (X), True) + sp.lrpad)
#define NUMBERSMAXDIGITS 100
#define NUMBERSBUFSIZE (NUMBERSMAXDIGITS * 2) + 1
#define MAXITEMLENGTH 1024

View file

@ -8,7 +8,6 @@
#include <math.h> #include <math.h>
#include "draw.h" #include "draw.h"
#include "../main.h"
#ifndef X11 #ifndef X11
#define USEX 0 #define USEX 0
@ -26,7 +25,7 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
#endif #endif
void cairo_set_source_hex(cairo_t* cr, const char *col, int alpha) { void draw_cairo_set_source_hex(cairo_t* cr, const char *col, int alpha) {
unsigned int hex; unsigned int hex;
sscanf(col, "#%x", &hex); sscanf(col, "#%x", &hex);
@ -40,7 +39,7 @@ void cairo_set_source_hex(cairo_t* cr, const char *col, int alpha) {
#if USEX #if USEX
Draw_t *draw_create_x11(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap, int protocol) { Draw_t *draw_create_x11(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap, int protocol) {
Draw_t *draw = ecalloc(1, sizeof(Draw_t)); Draw_t *draw = draw_calloc(1, sizeof(Draw_t));
draw->protocol = protocol; draw->protocol = protocol;
draw->dpy = dpy; draw->dpy = dpy;
@ -61,7 +60,7 @@ Draw_t *draw_create_x11(Display *dpy, int screen, Window root, unsigned int w, u
#if USEWAYLAND #if USEWAYLAND
Draw_t *draw_create_wl(int protocol) { Draw_t *draw_create_wl(int protocol) {
Draw_t *draw = ecalloc(1, sizeof(Draw_t)); Draw_t *draw = draw_calloc(1, sizeof(Draw_t));
draw->protocol = protocol; draw->protocol = protocol;
@ -112,10 +111,10 @@ static Fnt *font_create(Draw_t *draw, const char *fontname) {
PangoFontMetrics *metrics; PangoFontMetrics *metrics;
if (!fontname) { if (!fontname) {
die("spmenu: no font specified."); draw_die("font_create(): no font specified.");
} }
font = ecalloc(1, sizeof(Fnt)); font = draw_calloc(1, sizeof(Fnt));
font->dpy = draw->dpy; font->dpy = draw->dpy;
fontmap = pango_cairo_font_map_new(); fontmap = pango_cairo_font_map_new();
@ -176,7 +175,7 @@ void draw_arrow(Draw_t *draw, int x, int y, unsigned int w, unsigned int h, int
} }
cairo_t *cr = cairo_create(sf); cairo_t *cr = cairo_create(sf);
cairo_set_source_hex(cr, prevcol, prevalpha); draw_cairo_set_source_hex(cr, prevcol, prevalpha);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_rectangle(cr, x, y, w, h); cairo_rectangle(cr, x, y, w, h);
@ -187,7 +186,7 @@ void draw_arrow(Draw_t *draw, int x, int y, unsigned int w, unsigned int h, int
cairo_line_to(cr, x, y + h); cairo_line_to(cr, x, y + h);
cairo_close_path(cr); cairo_close_path(cr);
cairo_set_source_hex(cr, nextcol, nextalpha); draw_cairo_set_source_hex(cr, nextcol, nextalpha);
cairo_fill(cr); cairo_fill(cr);
cairo_destroy(cr); cairo_destroy(cr);
@ -208,7 +207,7 @@ void draw_circle(Draw_t *draw, int x, int y, unsigned int w, unsigned int h, int
cairo_t *cr = cairo_create(sf); cairo_t *cr = cairo_create(sf);
cairo_set_source_hex(cr, prevcol, prevalpha); draw_cairo_set_source_hex(cr, prevcol, prevalpha);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
// draw rectangle // draw rectangle
@ -226,7 +225,7 @@ void draw_circle(Draw_t *draw, int x, int y, unsigned int w, unsigned int h, int
cairo_arc(cr, cx, cy, rad, start, end); cairo_arc(cr, cx, cy, rad, start, end);
cairo_close_path(cr); cairo_close_path(cr);
cairo_set_source_hex(cr, nextcol, nextalpha); draw_cairo_set_source_hex(cr, nextcol, nextalpha);
cairo_fill(cr); cairo_fill(cr);
cairo_destroy(cr); cairo_destroy(cr);
@ -248,10 +247,10 @@ void draw_rect(Draw_t *draw, int x, int y, unsigned int w, unsigned int h, int f
cairo_t *cr = cairo_create(sf); cairo_t *cr = cairo_create(sf);
if (!cr) { if (!cr) {
die("failed to create cairo context"); draw_die("draw_rect(): failed to create cairo context");
} }
cairo_set_source_hex(cr, invert ? bgcol : fgcol, invert ? bgalpha : fgalpha); draw_cairo_set_source_hex(cr, invert ? bgcol : fgcol, invert ? bgalpha : fgalpha);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
if (filled) { if (filled) {
@ -292,7 +291,7 @@ int draw_text(Draw_t *draw, int x, int y, unsigned int w, unsigned int h, unsign
draw->d = cairo_create(draw->surface); draw->d = cairo_create(draw->surface);
// draw bg // draw bg
cairo_set_source_hex(draw->d, invert ? fgcol : bgcol, invert ? fgalpha : bgalpha); draw_cairo_set_source_hex(draw->d, invert ? fgcol : bgcol, invert ? fgalpha : bgalpha);
cairo_set_operator(draw->d, CAIRO_OPERATOR_SOURCE); cairo_set_operator(draw->d, CAIRO_OPERATOR_SOURCE);
cairo_rectangle(draw->d, x - lpad, y, w + lpad, h); cairo_rectangle(draw->d, x - lpad, y, w + lpad, h);
cairo_fill(draw->d); cairo_fill(draw->d);
@ -328,7 +327,7 @@ int draw_text(Draw_t *draw, int x, int y, unsigned int w, unsigned int h, unsign
pango_layout_set_single_paragraph_mode(draw->font->layout, True); pango_layout_set_single_paragraph_mode(draw->font->layout, True);
// draw fg // draw fg
cairo_set_source_hex(draw->d, fgcol, fgalpha); draw_cairo_set_source_hex(draw->d, fgcol, fgalpha);
cairo_move_to(draw->d, x, y + (h - draw->font->h) / 2); cairo_move_to(draw->d, x, y + (h - draw->font->h) / 2);
// update and show layout // update and show layout
@ -433,3 +432,28 @@ unsigned int draw_fontset_getwidth_clamp(Draw_t *draw, const char *text, unsigne
tmp = draw_text(draw, 0, 0, 0, 0, 0, text, n, markup, "#000000", "#000000", 255, 255); tmp = draw_text(draw, 0, 0, 0, 0, 0, text, n, markup, "#000000", "#000000", 255, 255);
return MIN(n, tmp); return MIN(n, tmp);
} }
void draw_die(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
exit(1);
}
void *draw_calloc(size_t nmemb, size_t size) {
void *p;
if (!(p = calloc(nmemb, size)))
draw_die("draw_calloc:");
return p;
}

View file

@ -33,7 +33,7 @@ typedef struct {
} Draw_t; } Draw_t;
/* Cairo color convertion */ /* Cairo color convertion */
void cairo_set_source_hex(cairo_t* cr, const char *col, int alpha); void draw_cairo_set_source_hex(cairo_t* cr, const char *col, int alpha);
/* Cairo image drawing */ /* Cairo image drawing */
void draw_img(Draw_t *draw, int x, int y); void draw_img(Draw_t *draw, int x, int y);
@ -67,3 +67,7 @@ void draw_circle(Draw_t *draw, int x, int y, unsigned int w, unsigned int h, int
/* Screenshot functions */ /* Screenshot functions */
void draw_save_screen(Draw_t *draw, const char *file); void draw_save_screen(Draw_t *draw, const char *file);
/* Misc */
void draw_die(const char *fmt, ...);
void *draw_calloc(size_t nmemb, size_t size);

View file

@ -1,18 +1,45 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "main.h" #ifndef MAX
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#endif
#ifndef MIN
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#endif
#ifndef BETWEEN
#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
#endif
#ifndef INTERSECT
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
&& MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
#endif
#ifndef LENGTH
#define LENGTH(X) (sizeof X / sizeof X[0])
#endif
#ifndef TEXTW
#define TEXTW(X) (draw_font_getwidth(draw, (X), False) + sp.lrpad)
#endif
#ifndef TEXTWM
#define TEXTWM(X) (draw_font_getwidth(draw, (X), True) + sp.lrpad)
#endif
#ifndef NUMBERSMAXDIGITS
#define NUMBERSMAXDIGITS 100
#endif
#ifndef NUMBERSBUFSIZE
#define NUMBERSBUFSIZE (NUMBERSMAXDIGITS * 2) + 1
#endif
#ifndef MAXITEMLENGTH
#define MAXITEMLENGTH 1024
#endif
void * ecalloc(size_t nmemb, size_t size) { size_t sp_strncpy(char *restrict dst, const char *restrict src, size_t size);
void *p; void die(const char *fmt, ...);
void *ecalloc(size_t nmemb, size_t size);
if (!(p = calloc(nmemb, size)))
die("calloc:");
return p;
}
size_t sp_strncpy(char *restrict dst, const char *restrict src, size_t size) { size_t sp_strncpy(char *restrict dst, const char *restrict src, size_t size) {
int offset; int offset;
@ -55,3 +82,11 @@ void die(const char *fmt, ...) {
exit(1); exit(1);
} }
void *ecalloc(size_t nmemb, size_t size) {
void *p;
if (!(p = calloc(nmemb, size)))
die("calloc:");
return p;
}

View file

@ -1,13 +0,0 @@
/* See LICENSE file for copyright and license details. */
#ifndef MAX
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#endif
#ifndef MIN
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#endif
#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
void die(const char *fmt, ...);
void *ecalloc(size_t nmemb, size_t size);
size_t sp_strncpy(char *dest, const char *src, size_t size);

View file

@ -1,5 +1,9 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#if USERTL
#include <fribidi.h>
#endif
static char fribidi_text[BUFSIZ] = ""; static char fribidi_text[BUFSIZ] = "";
static void apply_fribidi(char *str); static void apply_fribidi(char *str);

View file

@ -10,7 +10,6 @@ cc = meson.get_compiler('c')
project_source_files = [ project_source_files = [
'libs/draw/draw.c', 'libs/draw/draw.c',
'spmenu.c', 'spmenu.c',
'libs/main.c',
] ]
project_dependencies = [ project_dependencies = [

View file

@ -12,90 +12,62 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <fcntl.h> #include <fcntl.h>
#include "libs/draw/draw.h"
#include "libs/main.c"
// set version
#ifndef VERSION #ifndef VERSION
#define VERSION "unknown" #define VERSION "unknown"
#endif #endif
// check if we should enable right to left language support
#ifndef RTL #ifndef RTL
#define USERTL 0 #define USERTL 0
#else #else
#define USERTL 1 #define USERTL 1
#endif #endif
// check if we should enable image support
#ifndef IMAGE #ifndef IMAGE
#define USEIMAGE 0 #define USEIMAGE 0
#else #else
#define USEIMAGE 1 #define USEIMAGE 1
#endif #endif
// check if we should enable multimonitor support using libXinerama
#ifndef XINERAMA #ifndef XINERAMA
#define USEXINERAMA 0 #define USEXINERAMA 0
#else #else
#define USEXINERAMA 1 #define USEXINERAMA 1
#endif #endif
// check if we should enable config file support using libconfig
#ifndef CONFIG #ifndef CONFIG
#define USECONFIG 0 #define USECONFIG 0
#else #else
#define USECONFIG 1 #define USECONFIG 1
#endif #endif
// check if we should enable .Xresources support
#ifndef XRESOURCES #ifndef XRESOURCES
#define USEXRESOURCES 0 #define USEXRESOURCES 0
#else #else
#define USEXRESOURCES 1 #define USEXRESOURCES 1
#endif #endif
// check if we should enable Wayland support
#ifndef WAYLAND #ifndef WAYLAND
#define USEWAYLAND 0 #define USEWAYLAND 0
#else #else
#define USEWAYLAND 1 #define USEWAYLAND 1
#endif #endif
// check if we should enable X11 support
#ifndef X11 #ifndef X11
#define USEX 0 #define USEX 0
#else #else
#define USEX 1 #define USEX 1
#endif #endif
// check if we should enable regex matching
#ifndef REGEX #ifndef REGEX
#define USEREGEX 0 #define USEREGEX 0
#else #else
#define USEREGEX 1 #define USEREGEX 1
#endif #endif
// include fribidi used for right to left language support enum {
#if USERTL
#include <fribidi.h>
#endif
// include libraries used for image support
#if USEIMAGE
#include <errno.h>
#include <pwd.h>
#include <Imlib2.h>
#include <openssl/md5.h>
#endif
// include Xlib
#if USEX
#include <X11/Xlib.h>
#endif
// include macros and other defines
#include "libs/define.c"
enum { // click enum
ClickWindow, ClickWindow,
ClickPrompt, ClickPrompt,
ClickInput, ClickInput,
@ -209,8 +181,6 @@ static struct item *selecteditem; // selected item
static struct item *mouseitem; // clicked item static struct item *mouseitem; // clicked item
// various headers // various headers
#include "libs/draw/draw.h"
#include "libs/main.h"
#include "libs/draw.h" #include "libs/draw.h"
#include "libs/stream.h" #include "libs/stream.h"
#include "libs/arg.h" #include "libs/arg.h"
@ -262,7 +232,7 @@ static char *(*fstrstr)(const char *, const char *) = cistrstr;
#if USEX #if USEX
static void pastesel(void); static void pastesel(void);
static void grabfocus(void); // focus doesn't need to be grabbed on wayland static void grabfocus(void);
#endif #endif
static char **list; static char **list;
@ -273,6 +243,20 @@ static size_t listsize;
static char *fonts[] = { font }; static char *fonts[] = { font };
// include functions
#include "libs/img.c"
#include "libs/icon.c"
#include "libs/rtl.c"
#include "libs/sort.c"
#include "libs/match.c"
#include "libs/schemes.c"
#include "libs/draw.c"
#include "libs/conf/config.c"
#include "libs/argv.c"
#include "libs/history.c"
#include "libs/arg.c"
#include "libs/stream.c"
#if USEX #if USEX
static Key keys[] = { static Key keys[] = {
{ -1, 0, XK_Return, selectitem, {.i = +1 } }, { -1, 0, XK_Return, selectitem, {.i = +1 } },
@ -349,20 +333,6 @@ static WlMouse wl_buttons[] = {
}; };
#endif #endif
// include functions
#include "libs/img.c"
#include "libs/icon.c"
#include "libs/rtl.c"
#include "libs/sort.c"
#include "libs/match.c"
#include "libs/schemes.c"
#include "libs/draw.c"
#include "libs/conf/config.c"
#include "libs/argv.c"
#include "libs/history.c"
#include "libs/arg.c"
#include "libs/stream.c"
#include "libs/x11/inc.c" #include "libs/x11/inc.c"
#include "libs/wl/inc.c" #include "libs/wl/inc.c"