Render window close button

This commit is contained in:
Johan Malm 2020-06-29 19:27:59 +01:00
parent 40e862f3ac
commit baca410560
17 changed files with 177 additions and 50 deletions

View file

@ -1,3 +1,5 @@
labwc_sources += files(
'theme.c',
)
subdir('xbm')

View file

@ -0,0 +1,5 @@
labwc_sources += files(
'parse.c',
'tokenize.c',
'xbm.c',
)

View file

@ -1,3 +1,9 @@
/*
* Parse xbm token to create pixmap
*
* Copyright Johan Malm 2020
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
@ -6,17 +12,17 @@
#include <stdbool.h>
#include "buf.h"
#include "xbm.h"
#include "theme/xbm/parse.h"
static unsigned char defaultcolor[] = { 127, 127, 127, 255 };
static unsigned char background[] = { 255, 255, 255, 255 };
/* TODO: should be window.active.button.unpressed.image.color */
static unsigned char defaultcolor[] = { 255, 255, 255, 255 };
static uint32_t *pixmap;
static uint32_t *data;
static void add_pixel(int position, unsigned char *rbga)
{
pixmap[position] = (rbga[3] << 24) | (rbga[0] << 16) | (rbga[1] << 8) |
rbga[0];
data[position] = (rbga[3] << 24) | (rbga[0] << 16) | (rbga[1] << 8) |
rbga[0];
}
static void init_pixmap(int w, int h)
@ -25,7 +31,7 @@ static void init_pixmap(int w, int h)
if (has_run)
return;
has_run = true;
pixmap = (uint32_t *)calloc(w * h, sizeof(uint32_t));
data = (uint32_t *)calloc(w * h, sizeof(uint32_t));
}
static void process_bytes(int height, int width, struct token *tokens)
@ -43,22 +49,16 @@ static void process_bytes(int height, int width, struct token *tokens)
return;
int value = (int)strtol(t->name, NULL, 0);
int bit = 1 << (col % 8);
if (value & bit) {
if (value & bit)
add_pixel(row * width + col, defaultcolor);
printf(".");
} else {
add_pixel(row * width + col, background);
printf(" ");
}
}
++t;
printf("\n");
}
}
cairo_surface_t *xbm_create_bitmap(struct token *tokens)
struct pixmap xbm_create_pixmap(struct token *tokens)
{
cairo_surface_t *g_surface;
struct pixmap pixmap;
int width = 0, height = 0;
for (struct token *t = tokens; t->type; t++) {
if (width && height) {
@ -72,20 +72,11 @@ cairo_surface_t *xbm_create_bitmap(struct token *tokens)
else if (strstr(t->name, "height"))
height = atoi((++t)->name);
}
out:
g_surface =
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
if (!g_surface) {
fprintf(stderr, "no surface\n");
return NULL;
}
unsigned char *surface_data = cairo_image_surface_get_data(g_surface);
cairo_surface_flush(g_surface);
memcpy(surface_data, pixmap, width * height * 4);
free(pixmap);
cairo_surface_mark_dirty(g_surface);
return g_surface;
pixmap.data = data;
pixmap.width = width;
pixmap.height = height;
return pixmap;
}
char *xbm_read_file(const char *filename)

View file

@ -1,8 +1,14 @@
/*
* XBM file tokenizer
*
* Copyright Johan Malm 2020
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xbm.h"
#include "theme/xbm/tokenize.h"
static char *current_buffer_position;
static struct token *tokens;

34
src/theme/xbm/xbm.c Normal file
View file

@ -0,0 +1,34 @@
/*
* Create wlr textures based on xbm data
*
* Copyright Johan Malm 2020
*/
#include <stdio.h>
#include <stdlib.h>
#include "theme/xbm/xbm.h"
#include "theme/xbm/parse.h"
static char filename[] = "/usr/share/themes/Bear2/openbox-3/close.xbm";
void xbm_load(struct wlr_renderer *renderer)
{
struct token *tokens;
char *buffer = xbm_read_file(filename);
if (!buffer) {
fprintf(stderr, "no buffer\n");
return;
}
tokens = xbm_tokenize(buffer);
free(buffer);
struct pixmap pixmap = xbm_create_pixmap(tokens);
free(tokens);
theme.xbm_close = wlr_texture_from_pixels(
renderer, WL_SHM_FORMAT_ARGB8888, pixmap.width * 4,
pixmap.width, pixmap.height, pixmap.data);
if (pixmap.data)
free(pixmap.data);
}