Changes to be merged into standard dwl-bar

master
MadcowOG 2023-02-21 15:59:29 -08:00
parent 8edf4c3fa0
commit 32473754af
5 changed files with 76 additions and 109 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
bar bar
run run
compile_flags.txt compile_flags.txt
log.txt

133
src/bar.c
View File

@ -19,35 +19,16 @@
#include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h" #include "xdg-shell-client-protocol.h"
typedef struct Font Font; typedef struct Font {
typedef struct BarComponent BarComponent;
static void layerSurface(void* data, zwlr_layer_surface_v1*, uint32_t serial, uint32_t width, uint32_t height);
static void frame(void* data, wl_callback* callback, uint32_t callback_data);
static void bar_render(Bar* bar);
static void bar_tags_render(Bar* bar, cairo_t* painter, int* x);
static void bar_layout_render(Bar* bar, cairo_t* painter, int* x);
static void bar_title_render(Bar* bar, cairo_t* painter, int* x);
static void bar_status_render(Bar* bar, cairo_t* painter, int* x);
static void bar_set_colorscheme(Bar* bar, const int** scheme);
static void set_color(cairo_t* painter, const int rgba[4]);
static void bar_color_background(Bar* bar, cairo_t* painter);
static void bar_color_foreground(Bar* bar, cairo_t* painter);
static Font getFont(void);
static void bar_component_render(Bar* bar, BarComponent* component, cairo_t* painter, uint width, int* x);
static int bar_component_width(BarComponent* component);
static int bar_component_height(BarComponent* component);
struct Font {
PangoFontDescription* description; PangoFontDescription* description;
uint height; /* This is also the same as lrpad from dwm. */ uint height; /* This is also the same as lrpad from dwm. */
}; } Font;
struct BarComponent { typedef struct BarComponent {
PangoLayout* layout; PangoLayout* layout;
int x; /* Right bound of box */ int x; /* Right bound of box */
}; } BarComponent;
typedef struct { typedef struct {
uint occupied; uint occupied;
@ -61,7 +42,6 @@ struct Bar {
Tag tags[9]; Tag tags[9];
PangoContext* context; PangoContext* context;
Font font;
/* Colors */ /* Colors */
int background[4], foreground[4]; int background[4], foreground[4];
@ -76,16 +56,31 @@ struct Bar {
Shm* shm; Shm* shm;
}; };
static void layerSurface(void* data, zwlr_layer_surface_v1*, uint32_t serial, uint32_t width, uint32_t height);
static void frame(void* data, wl_callback* callback, uint32_t callback_data);
static void bar_render(Bar* bar);
static void bar_tags_render(Bar* bar, cairo_t* painter, int* x);
static void bar_layout_render(Bar* bar, cairo_t* painter, int* x);
static void bar_title_render(Bar* bar, cairo_t* painter, int* x);
static void bar_status_render(Bar* bar, cairo_t* painter, int* x);
static void bar_set_colorscheme(Bar* bar, const int** scheme);
static void set_color(cairo_t* painter, const int rgba[4]);
static void bar_color_background(Bar* bar, cairo_t* painter);
static void bar_color_foreground(Bar* bar, cairo_t* painter);
static Font getFont(void);
static BarComponent bar_component_create(PangoContext* context, PangoFontDescription* description);
static void bar_component_render(Bar* bar, BarComponent* component, cairo_t* painter, uint width, int* x);
static int bar_component_width(BarComponent* component);
static int bar_component_height(BarComponent* component);
static Font bar_font = {NULL, 0};
// So that the compositor can tell us when it's a good time to render again. // So that the compositor can tell us when it's a good time to render again.
const wl_callback_listener frameListener = { const wl_callback_listener frameListener = {.done = frame};
.done = frame
};
// So that wlroots can tell us we need to resize. // So that wlroots can tell us we need to resize.
// We really only need to worry about this when the bar is visible (sometimes it isn't). // We really only need to worry about this when the bar is visible (sometimes it isn't).
const zwlr_layer_surface_v1_listener layerSurfaceListener = { const zwlr_layer_surface_v1_listener layerSurfaceListener = {.configure = layerSurface};
.configure = layerSurface
};
void layerSurface(void* data, zwlr_layer_surface_v1* _, uint32_t serial, uint32_t width, uint32_t height) { void layerSurface(void* data, zwlr_layer_surface_v1* _, uint32_t serial, uint32_t width, uint32_t height) {
Bar* bar = data; Bar* bar = data;
@ -138,6 +133,13 @@ Font getFont(void) {
return in; return in;
} }
BarComponent bar_component_create(PangoContext* context, PangoFontDescription* description) {
PangoLayout* layout = pango_layout_new(context);
pango_layout_set_font_description(layout, description);
return (BarComponent){ layout, 0 };
}
int bar_component_width(BarComponent* component) { int bar_component_width(BarComponent* component) {
int w; int w;
pango_layout_get_size(component->layout, &w, NULL); pango_layout_get_size(component->layout, &w, NULL);
@ -178,34 +180,34 @@ void bar_component_render(Bar* bar, BarComponent* component, cairo_t* painter, u
cairo_fill(painter); cairo_fill(painter);
bar_color_foreground(bar, painter); bar_color_foreground(bar, painter);
cairo_move_to(painter, *x+(bar->font.height/2.0), 1); cairo_move_to(painter, *x+(bar_font.height/2.0), 1);
pango_cairo_show_layout(painter, component->layout); pango_cairo_show_layout(painter, component->layout);
} }
void bar_tags_render(Bar* bar, cairo_t* painter, int* x) { void bar_tags_render(Bar* bar, cairo_t* painter, int* x) {
for ( int i = 0; i < LENGTH(tags); i++ ) { for ( int i = 0; i < LENGTH(bar->tags); i++ ){
Tag tag = bar->tags[i]; Tag* tag = &bar->tags[i];
uint tagWidth = bar_component_width(&tag.component) + bar->font.height; uint tagWidth = bar_component_width(&tag->component) + bar_font.height;
/* Creating the tag */ /* Creating the tag */
if (tag.state & TAG_ACTIVE) { if (tag->state & TAG_ACTIVE) {
bar_set_colorscheme(bar, schemes[Active_Scheme]); bar_set_colorscheme(bar, schemes[Active_Scheme]);
} else if (tag.state & TAG_URGENT) { } else if (tag->state & TAG_URGENT) {
bar_set_colorscheme(bar, schemes[Urgent_Scheme]); bar_set_colorscheme(bar, schemes[Urgent_Scheme]);
} else { } else {
bar_set_colorscheme(bar, schemes[InActive_Scheme]); bar_set_colorscheme(bar, schemes[InActive_Scheme]);
} }
bar_component_render(bar, &tag.component, painter, tagWidth, x); bar_component_render(bar, &tag->component, painter, tagWidth, x);
if (!tag.occupied) if (!tag->occupied)
goto done; goto done;
/* Creating the occupied tag box */ /* Creating the occupied tag box */
int boxHeight = bar->font.height / 9; int boxHeight = bar_font.height / 9;
int boxWidth = bar->font.height / 6 + 1; int boxWidth = bar_font.height / 6 + 1;
if (tag.focusedClient) { if (tag->focusedClient) {
cairo_rectangle(painter, *x + boxHeight, boxHeight, boxWidth, boxWidth); cairo_rectangle(painter, *x + boxHeight, boxHeight, boxWidth, boxWidth);
cairo_fill(painter); cairo_fill(painter);
} else { } else {
@ -223,7 +225,7 @@ void bar_layout_render(Bar* bar, cairo_t* painter, int* x) {
if (!bar) if (!bar)
return; return;
uint layoutWidth = bar_component_width(&bar->layout) + bar->font.height; uint layoutWidth = bar_component_width(&bar->layout) + bar_font.height;
bar_set_colorscheme(bar, schemes[InActive_Scheme]); bar_set_colorscheme(bar, schemes[InActive_Scheme]);
bar_component_render(bar, &bar->layout, painter, layoutWidth, x); bar_component_render(bar, &bar->layout, painter, layoutWidth, x);
@ -236,7 +238,7 @@ void bar_title_render(Bar* bar, cairo_t* painter, int* x) {
return; return;
// HUH For some reason ww - x - (status width) works, but ww - x - status width doesn't? // HUH For some reason ww - x - (status width) works, but ww - x - status width doesn't?
uint titleWidth = bar->shm->width - *x - (bar_component_width(&bar->status) + bar->font.height); uint titleWidth = bar->shm->width - *x - (bar_component_width(&bar->status) + bar_font.height);
bar->active ? bar_set_colorscheme(bar, schemes[Active_Scheme]) : bar_set_colorscheme(bar, schemes[InActive_Scheme]); bar->active ? bar_set_colorscheme(bar, schemes[Active_Scheme]) : bar_set_colorscheme(bar, schemes[InActive_Scheme]);
@ -245,8 +247,8 @@ void bar_title_render(Bar* bar, cairo_t* painter, int* x) {
if (!bar->floating) if (!bar->floating)
goto done; goto done;
int boxHeight = bar->font.height / 9; int boxHeight = bar_font.height / 9;
int boxWidth = bar->font.height / 6 + 1; int boxWidth = bar_font.height / 6 + 1;
set_color(painter, grey3); set_color(painter, grey3);
cairo_rectangle(painter, *x + boxHeight + 0.5, boxHeight + 0.5, boxWidth, boxWidth); cairo_rectangle(painter, *x + boxHeight + 0.5, boxHeight + 0.5, boxWidth, boxWidth);
@ -261,7 +263,7 @@ void bar_status_render(Bar* bar, cairo_t* painter, int* x) {
if (!bar) if (!bar)
return; return;
uint statusWidth = bar_component_width(&bar->status) + bar->font.height; uint statusWidth = bar_component_width(&bar->status) + bar_font.height;
bar_set_colorscheme(bar, schemes[InActive_Scheme]); bar_set_colorscheme(bar, schemes[InActive_Scheme]);
if (!bar->active && status_on_active) if (!bar->active && status_on_active)
@ -310,30 +312,22 @@ Bar* bar_create(void) {
if (!bar->context) if (!bar->context)
die("pango context"); die("pango context");
bar->font = getFont(); if (!bar_font.description)
bar->layout.layout = pango_layout_new(bar->context); bar_font = getFont();
bar->title.layout = pango_layout_new(bar->context);
bar->status.layout = pango_layout_new(bar->context);
bar->layout.x = 0; bar->layout = bar_component_create(bar->context, bar_font.description);
bar->title.x = 0; bar->title = bar_component_create(bar->context, bar_font.description);
bar->status.x = 0; bar->status = bar_component_create(bar->context, bar_font.description);
pango_layout_set_font_description(bar->layout.layout, bar->font.description);
pango_layout_set_font_description(bar->title.layout, bar->font.description);
pango_layout_set_font_description(bar->status.layout, bar->font.description);
/* Default status */
char* status = ecalloc(8, sizeof(*status)); char* status = ecalloc(8, sizeof(*status));
snprintf(status, 8, "dwl %.1f", VERSION); snprintf(status, 8, "dwl %.1f", VERSION);
pango_layout_set_text(bar->status.layout, status, strlen(status));
pango_layout_set_text(bar->layout.layout, "[]=", -1); for (int i = 0; i < LENGTH(tags); i++) {
pango_layout_set_text(bar->status.layout, status, -1); BarComponent component = bar_component_create(bar->context, bar_font.description);
pango_layout_set_text(component.layout, tags[i], strlen(tags[i]));
for ( int i = 0; i < LENGTH(tags); i++ ) { // Initalize the tags Tag tag = { 0, 0, 0, component };
PangoLayout* layout = pango_layout_new(bar->context);
pango_layout_set_text(layout, tags[i], strlen(tags[i]));
pango_layout_set_font_description(layout, bar->font.description);
Tag tag = { 0, 0, 0, layout };
bar->tags[i] = tag; bar->tags[i] = tag;
} }
@ -402,7 +396,7 @@ void bar_show(Bar* bar, wl_output* output) {
zwlr_layer_surface_v1_set_anchor(bar->layer_surface, zwlr_layer_surface_v1_set_anchor(bar->layer_surface,
anchor | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT); anchor | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT);
int height = bar->font.height + 2; int height = bar_font.height + 2;
zwlr_layer_surface_v1_set_size(bar->layer_surface, 0, height); zwlr_layer_surface_v1_set_size(bar->layer_surface, 0, height);
zwlr_layer_surface_v1_set_exclusive_zone(bar->layer_surface, height); zwlr_layer_surface_v1_set_exclusive_zone(bar->layer_surface, height);
wl_surface_commit(bar->surface); wl_surface_commit(bar->surface);
@ -449,14 +443,15 @@ wl_surface* bar_get_surface(Bar *bar) {
} }
void bar_click(Bar* bar, struct Monitor* monitor, int x, int y, uint32_t button) { void bar_click(Bar* bar, struct Monitor* monitor, int x, int y, uint32_t button) {
Arg* arg = NULL; Arg *argp = NULL, arg;
Clicked location = Click_None; Clicked location = Click_None;
if (x < bar->tags[LENGTH(bar->tags)-1].component.x) { if (x < bar->tags[LENGTH(bar->tags)-1].component.x) {
location = Click_Tag; location = Click_Tag;
for (int i = 0; i < LENGTH(bar->tags); i++) { for (int i = 0; i < LENGTH(bar->tags); i++) {
if (x < bar->tags[i].component.x) { if (x < bar->tags[i].component.x) {
arg->ui = 1<<i; arg.ui = 1<<i;
argp = &arg;
break; break;
} }
} }
@ -473,7 +468,7 @@ void bar_click(Bar* bar, struct Monitor* monitor, int x, int y, uint32_t button)
for (int i = 0; i < LENGTH(buttons); i++) { for (int i = 0; i < LENGTH(buttons); i++) {
if (buttons[i].location == location && buttons[i].button == button) { if (buttons[i].location == location && buttons[i].button == button) {
buttons[i].func(monitor, arg ? arg : &buttons[i].arg); buttons[i].func(monitor, argp ? argp : &buttons[i].arg);
return; return;
} }
} }

View File

@ -8,8 +8,8 @@
#include <linux/input-event-codes.h> #include <linux/input-event-codes.h>
#include <wayland-cursor.h> #include <wayland-cursor.h>
#define LENGTH(X) (sizeof X / sizeof X[0] )
#define VERSION 1.0 #define VERSION 1.0
#define LENGTH(X) (sizeof X / sizeof X[0] )
// Commonly used typedefs which makes using these structs easier. // Commonly used typedefs which makes using these structs easier.
typedef struct wl_registry wl_registry; typedef struct wl_registry wl_registry;

View File

@ -56,7 +56,6 @@ typedef struct {
Monitor* focused_monitor; Monitor* focused_monitor;
int x, y; int x, y;
uint32_t* buttons; uint32_t* buttons;
uint size; uint size;
} Pointer; } Pointer;
@ -121,7 +120,7 @@ static void motion(void* data, wl_pointer* pointer, uint32_t time, wl_fixed_t x,
static void button(void* data, wl_pointer* pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state); static void button(void* data, wl_pointer* pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state);
static void frame(void* data, wl_pointer* pointer); static void frame(void* data, wl_pointer* pointer);
/* We don't do anything in these functions */ /* Also pointer listener members, but we don't do anything in these functions */
static void axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {} static void axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
static void axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t axis_source) {} static void axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t axis_source) {}
static void axis_stop(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis) {} static void axis_stop(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis) {}
@ -230,17 +229,17 @@ void motion(void* data, wl_pointer* pointer, uint32_t time, wl_fixed_t x, wl_fix
void button(void *data, wl_pointer *pointer, uint32_t serial, uint32_t time, void button(void *data, wl_pointer *pointer, uint32_t serial, uint32_t time,
uint32_t button, uint32_t state) { uint32_t button, uint32_t state) {
Seat seat = *(Seat*)data; Seat seat = *(Seat*)data;
uint32_t* new_buttons = NULL;
int i, prev = -1; /* The index of this button */ int i, prev = -1; /* The index of this button */
for (i = 0; i < seat.pointer->size; i++) { for (i = 0; i < seat.pointer->size; i++) {
_logf("'%d' ", seat.pointer->buttons[i]);
if (seat.pointer->buttons[i] == button) if (seat.pointer->buttons[i] == button)
prev = i; prev = i;
} }
/* If this button was newly pressed. */ /* If this button was newly pressed. */
if (state == WL_POINTER_BUTTON_STATE_PRESSED && prev == -1) { if (state == WL_POINTER_BUTTON_STATE_PRESSED && prev == -1) {
uint32_t* new_buttons = ecalloc(seat.pointer->size+1, sizeof(uint32_t)); new_buttons = ecalloc(seat.pointer->size+1, sizeof(uint32_t));
for (i = 0; i < seat.pointer->size+1; i++) { for (i = 0; i < seat.pointer->size+1; i++) {
if (i == seat.pointer->size) { if (i == seat.pointer->size) {
new_buttons[i] = button; new_buttons[i] = button;
@ -250,15 +249,12 @@ void button(void *data, wl_pointer *pointer, uint32_t serial, uint32_t time,
new_buttons[i] = seat.pointer->buttons[i]; new_buttons[i] = seat.pointer->buttons[i];
} }
free(seat.pointer->buttons);
seat.pointer->buttons = new_buttons;
seat.pointer->size++; seat.pointer->size++;
return;
} }
/* If this button was released and we have it. */ /* If this button was released and we have it. */
if(state == WL_KEYBOARD_KEY_STATE_RELEASED && prev != -1) { if(state == WL_KEYBOARD_KEY_STATE_RELEASED && prev != -1) {
uint32_t* new_buttons = ecalloc(seat.pointer->size-1, sizeof(uint32_t)); new_buttons = ecalloc(seat.pointer->size-1, sizeof(uint32_t));
for (i = 0; i < seat.pointer->size; i++) { for (i = 0; i < seat.pointer->size; i++) {
if (i == prev) if (i == prev)
continue; continue;
@ -270,12 +266,12 @@ void button(void *data, wl_pointer *pointer, uint32_t serial, uint32_t time,
new_buttons[i-1] = seat.pointer->buttons[i]; new_buttons[i-1] = seat.pointer->buttons[i];
} }
seat.pointer->size--;
}
free(seat.pointer->buttons); free(seat.pointer->buttons);
seat.pointer->buttons = new_buttons; seat.pointer->buttons = new_buttons;
seat.pointer->size--;
return; return;
}
} }
static void frame(void* data, wl_pointer* pointer) { static void frame(void* data, wl_pointer* pointer) {
@ -542,8 +538,8 @@ Monitor* monitor_from_surface(wl_surface* surface) {
/* /*
* Parse and extract a substring based on a delimiter * Parse and extract a substring based on a delimiter
* start_end is a pointer to a ulong that we will use to base our starting location. * start_end is a ulong that we will use to base our starting location.
* Then fill in after as the ending point. To be used later on. * Then replace as the end point to be used later on.
*/ */
char* to_delimiter(char* string, ulong *start_end, char delimiter) { char* to_delimiter(char* string, ulong *start_end, char delimiter) {
char* output; char* output;

View File

@ -1,25 +0,0 @@
#ifndef UTIL_H_
#define UTIL_H_
#include <sys/types.h>
#include <stddef.h>
typedef struct {
void* ptr;
uint size;
} Stack;
#define stack_for_each(pos, stack) \
for (pos = (stack)->data); \
(const char*) pos < ((const char*) (stack)->data + (stack)->size); \
(pos)++)
inline void stack_init(Stack* stack) {
stack->ptr = NULL;
stack->size = 0;
}
void* stack_end(Stack* stack);
void* stack_push(Stack* stack);
#endif // UTIL_H_