xutils.h

#ifndef _X_UTILS_H_
#define _X_UTILS_H_
/**
 * Copyright (c) 2024, SWGY, Inc. <ron@sw.gy>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <X11/Xlib.h>

#include <sys/time.h>

#include "constants.h"
struct render_ctx {
	/* Hold X color palette entries */
	unsigned long  colors[PAL_NUM];
	/* Font structures */
	XFontStruct   *fonts[TXT_SZ_NUM];
	Window         w;
	GC             gc;
	Display       *d;
	XEvent         event;
	Colormap       colormap;
	int            screen_num;
	unsigned int   width;
	unsigned int   height;
	int            needs_redraw;
	/* Event handler for keypress events */
	void (*on_key_press)(XKeyEvent *event);
};

struct geom {
	int          x;
	int          y;
	unsigned int w;
	unsigned int h;
};

struct style {
	unsigned int fg;
	unsigned int bg;
	unsigned int txt_sz;
};

int render_init(struct geom *g, struct render_ctx *ctx, struct style *s);
void render_destroy(struct render_ctx *ctx);

int create_window(struct geom *g, struct render_ctx *c, struct style *s);
int destroy_window(struct render_ctx *c);

/*
 * Draw text within the indicated region
 */
void draw_text(struct geom *g, struct style *s, int text, struct render_ctx *c);
void draw_timer(struct geom *g, struct style *s, struct timespec *val,
    struct render_ctx *c);
void draw_score(struct geom *g, struct style *s, int score, struct render_ctx *c);
void draw_final_score(struct geom *g, struct style *s, int score,
    int total_tasks, struct render_ctx *c);

void draw_rounded_border(struct geom *g, struct style *s, int corner_radius,
		struct render_ctx *ctx);

void draw_text_with_border(struct geom *g, struct style *s, int text,
    struct render_ctx *c);

int get_str_width(int weight, int strnum);
int get_str_height(int weight);

#endif