2021-09-24 21:45:48 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2020-06-29 19:27:59 +01:00
|
|
|
/*
|
|
|
|
|
* Parse xbm token to create pixmap
|
|
|
|
|
*
|
|
|
|
|
* Copyright Johan Malm 2020
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-23 07:17:07 +01:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2020-09-28 20:53:59 +01:00
|
|
|
#include <assert.h>
|
2020-09-28 20:41:41 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
2020-06-23 07:17:07 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2022-09-16 18:41:02 -04:00
|
|
|
#include "common/mem.h"
|
2020-09-28 20:41:41 +01:00
|
|
|
#include "xbm/parse.h"
|
2020-06-23 07:17:07 +01:00
|
|
|
|
2020-08-21 19:43:34 +01:00
|
|
|
static uint32_t color;
|
2020-06-26 21:52:38 +01:00
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static uint32_t
|
|
|
|
|
u32(float *rgba)
|
2020-06-26 21:52:38 +01:00
|
|
|
{
|
2020-08-12 19:42:59 +01:00
|
|
|
uint32_t r[4] = { 0 };
|
2020-09-28 20:41:41 +01:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2020-08-21 19:43:34 +01:00
|
|
|
r[i] = rgba[i] * 255;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-08-21 19:43:34 +01:00
|
|
|
return ((r[3] & 0xff) << 24) | ((r[0] & 0xff) << 16) |
|
|
|
|
|
((r[1] & 0xff) << 8) | (r[2] & 0xff);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
void
|
|
|
|
|
parse_set_color(float *rgba)
|
2020-08-21 19:43:34 +01:00
|
|
|
{
|
|
|
|
|
color = u32(rgba);
|
2020-06-26 21:52:38 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
process_bytes(struct pixmap *pixmap, struct token *tokens)
|
2020-06-26 21:52:38 +01:00
|
|
|
{
|
2022-09-16 18:41:02 -04:00
|
|
|
pixmap->data = (uint32_t *)xzalloc(
|
|
|
|
|
pixmap->width * pixmap->height * sizeof(uint32_t));
|
2020-06-23 07:17:07 +01:00
|
|
|
struct token *t = tokens;
|
2020-07-03 22:03:44 +01:00
|
|
|
for (int row = 0; row < pixmap->height; row++) {
|
2020-06-23 07:17:07 +01:00
|
|
|
int byte = 1;
|
2020-07-03 22:03:44 +01:00
|
|
|
for (int col = 0; col < pixmap->width; col++) {
|
2020-06-23 07:17:07 +01:00
|
|
|
if (col == byte * 8) {
|
|
|
|
|
++byte;
|
|
|
|
|
++t;
|
|
|
|
|
}
|
2020-09-28 20:41:41 +01:00
|
|
|
if (!t->type) {
|
2020-06-23 07:17:07 +01:00
|
|
|
return;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
|
|
|
|
if (t->type != TOKEN_INT) {
|
2020-07-06 21:58:51 +01:00
|
|
|
return;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-06-23 07:17:07 +01:00
|
|
|
int bit = 1 << (col % 8);
|
2020-09-28 20:41:41 +01:00
|
|
|
if (t->value & bit) {
|
2020-08-21 19:43:34 +01:00
|
|
|
pixmap->data[row * pixmap->width + col] = color;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-06-23 07:17:07 +01:00
|
|
|
}
|
|
|
|
|
++t;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
struct pixmap
|
|
|
|
|
parse_xbm_tokens(struct token *tokens)
|
2020-06-23 07:17:07 +01:00
|
|
|
{
|
2020-07-03 22:03:44 +01:00
|
|
|
struct pixmap pixmap = { 0 };
|
|
|
|
|
|
2020-06-23 07:17:07 +01:00
|
|
|
for (struct token *t = tokens; t->type; t++) {
|
2020-07-03 22:03:44 +01:00
|
|
|
if (pixmap.width && pixmap.height) {
|
2020-09-28 20:41:41 +01:00
|
|
|
if (t->type != TOKEN_INT) {
|
2020-06-23 07:17:07 +01:00
|
|
|
continue;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-07-03 22:03:44 +01:00
|
|
|
process_bytes(&pixmap, t);
|
2020-06-26 21:52:38 +01:00
|
|
|
goto out;
|
2020-06-23 07:17:07 +01:00
|
|
|
}
|
2020-09-28 20:41:41 +01:00
|
|
|
if (strstr(t->name, "width")) {
|
2020-07-03 22:03:44 +01:00
|
|
|
pixmap.width = atoi((++t)->name);
|
2020-09-28 20:41:41 +01:00
|
|
|
} else if (strstr(t->name, "height")) {
|
2020-07-03 22:03:44 +01:00
|
|
|
pixmap.height = atoi((++t)->name);
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-06-23 07:17:07 +01:00
|
|
|
}
|
2020-06-26 21:52:38 +01:00
|
|
|
out:
|
2020-06-29 19:27:59 +01:00
|
|
|
return pixmap;
|
2020-06-23 07:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-11 21:45:52 +01:00
|
|
|
/*
|
|
|
|
|
* Openbox built-in icons are not bigger than 8x8, so have only written this
|
|
|
|
|
* function to cope wit that max size
|
|
|
|
|
*/
|
|
|
|
|
#define LABWC_BUILTIN_ICON_MAX_SIZE (8)
|
2020-09-28 20:41:41 +01:00
|
|
|
struct pixmap
|
|
|
|
|
parse_xbm_builtin(const char *button, int size)
|
2020-07-06 21:58:51 +01:00
|
|
|
{
|
|
|
|
|
struct pixmap pixmap = { 0 };
|
|
|
|
|
|
2020-09-28 20:53:59 +01:00
|
|
|
assert(size <= LABWC_BUILTIN_ICON_MAX_SIZE);
|
2020-08-11 21:45:52 +01:00
|
|
|
pixmap.width = size;
|
|
|
|
|
pixmap.height = size;
|
2020-07-06 21:58:51 +01:00
|
|
|
|
2020-08-11 21:45:52 +01:00
|
|
|
struct token t[LABWC_BUILTIN_ICON_MAX_SIZE + 1];
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
2020-07-06 21:58:51 +01:00
|
|
|
t[i].value = button[i];
|
|
|
|
|
t[i].type = TOKEN_INT;
|
|
|
|
|
}
|
2020-08-11 21:45:52 +01:00
|
|
|
t[size].type = 0;
|
2020-07-06 21:58:51 +01:00
|
|
|
process_bytes(&pixmap, t);
|
|
|
|
|
return pixmap;
|
|
|
|
|
}
|