Manual doublebuffering. Hopefully fixes flickering.

This commit is contained in:
itycodes 2023-02-26 10:04:53 +01:00
parent 0d40df404e
commit 725ad80d90

63
main.c
View file

@ -12,7 +12,7 @@
*/
// --------------
// TextBar v0.1.1
// TextBar v0.1.2
// --------------
// Usage: <text generator> | textbar | <input handler>
//
@ -23,9 +23,6 @@
// Line buffered
// Prints all mouse clicks to stdout
// In the format of `X Y` (without the '`')
//
// When the X server is lagging, the bar will set the background color to 0,0,0,0 for a single frame in a given frequency. The more X is lagging, the more frequently it will do so.
// I swear this is an intented feature.
#include <pthread.h>
@ -48,7 +45,9 @@
Display* dpy;
Window win;
cairo_surface_t* sfc;
cairo_surface_t* sfc2;
cairo_t* ctx;
cairo_t* ctx2;
char text[256] = {0};
char text_other[256] = {0};
int width;
@ -74,8 +73,8 @@ void init() {
XMatchVisualInfo(dpy, screen, 32, TrueColor, &visualinfo);
XSetWindowAttributes attr;
attr.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy), visualinfo.visual, AllocNone);
attr.background_pixel = 0;
attr.border_pixel = 0;
attr.background_pixel = 0;
attr.border_pixel = 0;
win = XCreateWindow(dpy, DefaultRootWindow(dpy),
0, 0, width, HEIGHT,
0, visualinfo.depth, InputOutput, visualinfo.visual, CWBackPixel | CWColormap | CWBorderPixel, &attr);
@ -102,35 +101,46 @@ void init() {
XMapWindow(dpy, win);
sfc = cairo_xlib_surface_create(dpy, win, visualinfo.visual, DisplayWidth(dpy, screen), HEIGHT);
sfc2 = cairo_surface_create_similar(sfc, CAIRO_CONTENT_COLOR_ALPHA, DisplayWidth(dpy, screen), HEIGHT);
ctx = cairo_create(sfc);
ctx2 = cairo_create(sfc2);
}
pthread_mutex_t do_paint;
void paint(Display* dpy) {
if(!pthread_mutex_lock(&do_paint)) {
//cairo_set_source_rgba(ctx, 0.0, 0.0, 0.0, 0.0);
//cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
//cairo_paint(ctx);
//TODO: fix alpha
//NOTE: comment the following line if you do not use transparency.
XClearWindow(dpy, win);
cairo_push_group(ctx);
cairo_set_operator(ctx, CAIRO_OPERATOR_OVER);
cairo_set_source_rgba(ctx, BG_COLOR);
cairo_paint(ctx);
cairo_move_to(ctx, FONT_SIZE / 2, FONT_POS);
cairo_select_font_face(ctx, FONT, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(ctx, FONT_SIZE);
cairo_set_source_rgba(ctx, FG_COLOR);
cairo_show_text(ctx, text);
// Clear
cairo_set_source_rgba(ctx2, 0.0, 0.0, 0.0, 0.0);
cairo_set_operator(ctx2, CAIRO_OPERATOR_SOURCE);
cairo_paint(ctx2);
cairo_set_operator(ctx2, CAIRO_OPERATOR_OVER);
// Background
cairo_set_source_rgba(ctx2, BG_COLOR);
cairo_paint(ctx2);
// Left text
cairo_move_to(ctx2, FONT_SIZE / 2, FONT_POS);
cairo_select_font_face(ctx2, FONT, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(ctx2, FONT_SIZE);
cairo_set_source_rgba(ctx2, FG_COLOR);
cairo_show_text(ctx2, text);
// Right text
cairo_text_extents_t extents;
cairo_text_extents(ctx, text_other, &extents);
cairo_move_to(ctx, (width-extents.width) - FONT_SIZE / 2, FONT_POS);
cairo_show_text(ctx, text_other);
cairo_pop_group_to_source(ctx);
cairo_text_extents(ctx2, text_other, &extents);
cairo_move_to(ctx2, (width-extents.width) - FONT_SIZE / 2, FONT_POS);
cairo_show_text(ctx2, text_other);
// Backbuffer -> Frontbuffer
cairo_set_source_surface(ctx, sfc2, 0, 0);
cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
cairo_paint(ctx);
// Flushing
cairo_surface_flush(sfc);
XFlush(dpy);
pthread_mutex_unlock(&do_paint);
}
}
@ -156,8 +166,7 @@ void x_loop() {
if(e.type == ButtonPress) {
printf("%d %d\n", e.xbutton.x, e.xbutton.y);
fflush(stdout);
} else
if(e.type == ConfigureNotify) {
} else if(e.type == ConfigureNotify) {
cairo_xlib_surface_set_size(sfc, e.xconfigure.width, e.xconfigure.height);
} else
if(e.type == Expose) {