From 32473754af870cca01c6041e666e159a526d6c46 Mon Sep 17 00:00:00 2001 From: MadcowOG Date: Tue, 21 Feb 2023 15:59:29 -0800 Subject: [PATCH] Changes to be merged into standard dwl-bar --- .gitignore | 1 + src/bar.c | 133 +++++++++++++++++++++++++-------------------------- src/common.h | 2 +- src/main.c | 24 ++++------ src/util.h | 25 ---------- 5 files changed, 76 insertions(+), 109 deletions(-) delete mode 100644 src/util.h diff --git a/.gitignore b/.gitignore index 782e264..971f1a3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ bar run compile_flags.txt +log.txt diff --git a/src/bar.c b/src/bar.c index 4a163e3..c68e417 100644 --- a/src/bar.c +++ b/src/bar.c @@ -19,35 +19,16 @@ #include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "xdg-shell-client-protocol.h" -typedef struct Font 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 { +typedef struct Font { PangoFontDescription* description; uint height; /* This is also the same as lrpad from dwm. */ -}; +} Font; -struct BarComponent { +typedef struct BarComponent { PangoLayout* layout; int x; /* Right bound of box */ -}; +} BarComponent; typedef struct { uint occupied; @@ -61,7 +42,6 @@ struct Bar { Tag tags[9]; PangoContext* context; - Font font; /* Colors */ int background[4], foreground[4]; @@ -76,16 +56,31 @@ struct Bar { 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. -const wl_callback_listener frameListener = { -.done = frame -}; +const wl_callback_listener frameListener = {.done = frame}; // 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). -const zwlr_layer_surface_v1_listener layerSurfaceListener = { -.configure = layerSurface -}; +const zwlr_layer_surface_v1_listener layerSurfaceListener = {.configure = layerSurface}; void layerSurface(void* data, zwlr_layer_surface_v1* _, uint32_t serial, uint32_t width, uint32_t height) { Bar* bar = data; @@ -138,6 +133,13 @@ Font getFont(void) { 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 w; 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); 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); } void bar_tags_render(Bar* bar, cairo_t* painter, int* x) { - for ( int i = 0; i < LENGTH(tags); i++ ) { - Tag tag = bar->tags[i]; - uint tagWidth = bar_component_width(&tag.component) + bar->font.height; + for ( int i = 0; i < LENGTH(bar->tags); i++ ){ + Tag* tag = &bar->tags[i]; + uint tagWidth = bar_component_width(&tag->component) + bar_font.height; /* Creating the tag */ - if (tag.state & TAG_ACTIVE) { + if (tag->state & TAG_ACTIVE) { 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]); } else { 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; /* Creating the occupied tag box */ - int boxHeight = bar->font.height / 9; - int boxWidth = bar->font.height / 6 + 1; + int boxHeight = bar_font.height / 9; + int boxWidth = bar_font.height / 6 + 1; - if (tag.focusedClient) { + if (tag->focusedClient) { cairo_rectangle(painter, *x + boxHeight, boxHeight, boxWidth, boxWidth); cairo_fill(painter); } else { @@ -223,7 +225,7 @@ void bar_layout_render(Bar* bar, cairo_t* painter, int* x) { if (!bar) 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_component_render(bar, &bar->layout, painter, layoutWidth, x); @@ -236,7 +238,7 @@ void bar_title_render(Bar* bar, cairo_t* painter, int* x) { return; // 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]); @@ -245,8 +247,8 @@ void bar_title_render(Bar* bar, cairo_t* painter, int* x) { if (!bar->floating) goto done; - int boxHeight = bar->font.height / 9; - int boxWidth = bar->font.height / 6 + 1; + int boxHeight = bar_font.height / 9; + int boxWidth = bar_font.height / 6 + 1; set_color(painter, grey3); 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) 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]); if (!bar->active && status_on_active) @@ -310,30 +312,22 @@ Bar* bar_create(void) { if (!bar->context) die("pango context"); - bar->font = getFont(); - bar->layout.layout = pango_layout_new(bar->context); - bar->title.layout = pango_layout_new(bar->context); - bar->status.layout = pango_layout_new(bar->context); + if (!bar_font.description) + bar_font = getFont(); - bar->layout.x = 0; - bar->title.x = 0; - bar->status.x = 0; - - 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); + bar->layout = bar_component_create(bar->context, bar_font.description); + bar->title = bar_component_create(bar->context, bar_font.description); + bar->status = bar_component_create(bar->context, bar_font.description); + /* Default status */ char* status = ecalloc(8, sizeof(*status)); snprintf(status, 8, "dwl %.1f", VERSION); + pango_layout_set_text(bar->status.layout, status, strlen(status)); - pango_layout_set_text(bar->layout.layout, "[]=", -1); - pango_layout_set_text(bar->status.layout, status, -1); - - for ( int i = 0; i < LENGTH(tags); i++ ) { // Initalize the tags - 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 }; + for (int i = 0; i < LENGTH(tags); i++) { + BarComponent component = bar_component_create(bar->context, bar_font.description); + pango_layout_set_text(component.layout, tags[i], strlen(tags[i])); + Tag tag = { 0, 0, 0, component }; 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, 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_exclusive_zone(bar->layer_surface, height); 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) { - Arg* arg = NULL; + Arg *argp = NULL, arg; Clicked location = Click_None; if (x < bar->tags[LENGTH(bar->tags)-1].component.x) { location = Click_Tag; for (int i = 0; i < LENGTH(bar->tags); i++) { if (x < bar->tags[i].component.x) { - arg->ui = 1< #include -#define LENGTH(X) (sizeof X / sizeof X[0] ) #define VERSION 1.0 +#define LENGTH(X) (sizeof X / sizeof X[0] ) // Commonly used typedefs which makes using these structs easier. typedef struct wl_registry wl_registry; diff --git a/src/main.c b/src/main.c index 1ff83a7..6d246aa 100644 --- a/src/main.c +++ b/src/main.c @@ -56,7 +56,6 @@ typedef struct { Monitor* focused_monitor; int x, y; - uint32_t* buttons; uint size; } 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 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_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) {} @@ -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, uint32_t button, uint32_t state) { Seat seat = *(Seat*)data; + uint32_t* new_buttons = NULL; int i, prev = -1; /* The index of this button */ for (i = 0; i < seat.pointer->size; i++) { - _logf("'%d' ", seat.pointer->buttons[i]); if (seat.pointer->buttons[i] == button) prev = i; } /* If this button was newly pressed. */ 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++) { if (i == seat.pointer->size) { 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]; } - free(seat.pointer->buttons); - seat.pointer->buttons = new_buttons; seat.pointer->size++; - return; } /* If this button was released and we have it. */ 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++) { if (i == prev) 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]; } - free(seat.pointer->buttons); - seat.pointer->buttons = new_buttons; seat.pointer->size--; - - return; } + + free(seat.pointer->buttons); + seat.pointer->buttons = new_buttons; + return; } 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 - * start_end is a pointer to a ulong that we will use to base our starting location. - * Then fill in after as the ending point. To be used later on. + * start_end is a ulong that we will use to base our starting location. + * Then replace as the end point to be used later on. */ char* to_delimiter(char* string, ulong *start_end, char delimiter) { char* output; diff --git a/src/util.h b/src/util.h deleted file mode 100644 index 8e61726..0000000 --- a/src/util.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef UTIL_H_ -#define UTIL_H_ -#include -#include - -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_