wlmaker
Loading...
Searching...
No Matches
wlm_graph_shared.c File Reference
#include "wlm_graph_shared.h"
#include <cairo.h>
#include <errno.h>
#include <libbase/libbase.h>
#include <libwlclient/libwlclient.h>
#include <primitives/primitives.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libwlclient/icon.h"
Include dependency graph for wlm_graph_shared.c:

Classes

struct  wlm_graph_sample_t
struct  wlm_graph_state_t
struct  wlm_graph_font_spec_t
struct  wlm_graph_prefs_t
struct  wlm_graph_handle_t

Macros

#define WLM_GRAPH_BASE_ICON_SIZE   64
#define WLM_GRAPH_LABEL_FONT_SIZE_BASE   8
#define WLM_GRAPH_LABEL_COLOR   0xffc4c4c4
#define WLM_GRAPH_LABEL_FONT_FACE   "Monospace"
#define WLM_GRAPH_SOLID_BRIGHTNESS_MIN   32
#define WLM_GRAPH_SOLID_BRIGHTNESS_MAX   128
#define WLM_GRAPH_PIXEL_BLACK   0xff000000
#define WLM_GRAPH_PIXEL_LINE_DEFAULT   0xff008000
#define WLM_GRAPH_PIXEL_GRAY(b)
#define WLM_GRAPH_FONT_FACE_MAX   64
#define _str_match_prefix(str_ptr, len_ptr, prefix)

Typedefs

typedef struct wlm_graph_sample_t wlm_graph_sample_t

Enumerations

enum  wlm_graph_color_mode_t { WLM_GRAPH_COLOR_MODE_HEAT , WLM_GRAPH_COLOR_MODE_ALPHA }

Functions

static void _wlm_graph_sample_values_free (wlm_graph_sample_t *samples, const uint32_t count)
static void _wlm_graph_samples_init (wlm_graph_sample_t *samples, const uint32_t count)
static void _wlm_graph_sample_compute_peak (wlm_graph_sample_t *sample, const wlm_graph_mode_t accumulate_mode)
static uint32_t _wlm_graph_usage_to_y (const uint8_t usage, const uint32_t height)
static uint32_t _wlm_graph_usage_to_y_with_zero_check (const uint8_t usage, const uint32_t height)
static uint32_t _wlm_graph_column_render (wlm_graph_state_t *graph_state, const wlm_graph_sample_t *sample, const uint32_t column, const wlm_graph_mode_t accumulate_mode)
static void _wlm_graph_peak_connector_draw (uint32_t *graph_pixels, const uint32_t graph_width, const uint32_t pixel_line, const uint32_t x, const uint32_t y_range[2])
static void _wlm_graph_peak_connector_draw_between (uint32_t *graph_pixels, const uint32_t graph_size[2], const uint32_t pixel_line, const uint8_t usage_curr, const uint8_t usage_prev, const uint32_t column_curr, const uint32_t column_prev)
static void _wlm_graph_fill_columns_black (uint32_t *graph_pixels, const uint32_t graph_size[2], const uint32_t column_end)
static void _wlm_graph_scroll_left (uint32_t *graph_pixels, const uint32_t graph_size[2], const uint32_t y_start)
static void _wlm_graph_rebuild_from_samples (wlm_graph_state_t *graph_state, const wlm_graph_mode_t accumulate_mode)
static void _wlm_graph_update_with_sample (wlm_graph_state_t *graph_state, wlm_graph_sample_t *new_sample, const wlm_graph_mode_t accumulate_mode)
static uint64_t _wlm_graph_time_next_update (const uint64_t interval_usec)
static bool _wlm_graph_icon_render_callback (bs_gfxbuf_t *gfxbuf_ptr, void *ud_ptr)
static void _wlm_graph_sample_update (wlm_graph_handle_t *handle)
static void _wlm_graph_timer_callback (wlclient_t *client_ptr, void *ud_ptr)
static bool _wlm_graph_arg_parse_i32 (const char *opt_name, const char *str, const int32_t val_min, const int32_t val_max, int32_t *result_ptr)
static bool _wlm_graph_arg_parse_f64 (const char *opt_name, const char *str, const double val_min, const double val_max, double *result_ptr)
static bool _str_match_prefix_n (const char **str_ptr, size_t *len_ptr, const char *prefix, const size_t prefix_len)
static bool _wlm_graph_arg_parse_font (const char *opt_name, const char *str, wlm_graph_font_spec_t *font)
static void _wlm_graph_pixel_lut_init (uint32_t pixel_lut[256], uint32_t *pixel_line_ptr, const wlm_graph_color_mode_t color_mode)
static bool _wlm_graph_buffers_resize (wlm_graph_state_t *graph_state, const uint32_t size[2], const uint32_t margin_logical_px)
static void _wlm_graph_y_min_from_samples (wlm_graph_state_t *graph_state, wlm_graph_sample_t *current_sample)
static void _wlm_graph_blit_to_buffer (bs_gfxbuf_t *gfxbuf_ptr, const uint32_t *graph_pixels, const uint32_t graph_size[2], const uint32_t offset[2])
static bool _wlm_graph_bezel_draw (bs_gfxbuf_t *gfxbuf_ptr, const uint32_t margin_logical_px)
static void _wlm_graph_label_draw (bs_gfxbuf_t *gfxbuf_ptr, const uint32_t margin_px, const char *label, const wlm_graph_prefs_t *prefs)
static int _wlm_graph_args_parse (const int argc, const char **argv, const char *app_name, const char *app_help, const bool has_custom_lut, const bool has_label, wlm_graph_prefs_t *prefs)
static void _wlm_graph_state_free (wlm_graph_state_t *graph_state)
int wlm_graph_app_run (int argc, const char **argv, const wlm_graph_app_config_t *config)

Detailed Description

Shared graph rendering utilities for wlmaker dock-apps.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Macro Definition Documentation

◆ _str_match_prefix

#define _str_match_prefix ( str_ptr,
len_ptr,
prefix )
Value:
_str_match_prefix_n(str_ptr, len_ptr, prefix, sizeof(prefix) - 1)
static bool _str_match_prefix_n(const char **str_ptr, size_t *len_ptr, const char *prefix, const size_t prefix_len)
Definition wlm_graph_shared.c:297

Wrapper for _str_match_prefix_n using sizeof for string literal prefix.

◆ WLM_GRAPH_BASE_ICON_SIZE

#define WLM_GRAPH_BASE_ICON_SIZE   64

Base icon size for scaling calculations.

◆ WLM_GRAPH_FONT_FACE_MAX

#define WLM_GRAPH_FONT_FACE_MAX   64

Maximum length of font face name.

◆ WLM_GRAPH_LABEL_COLOR

#define WLM_GRAPH_LABEL_COLOR   0xffc4c4c4

Label text color (light grey).

◆ WLM_GRAPH_LABEL_FONT_FACE

#define WLM_GRAPH_LABEL_FONT_FACE   "Monospace"

Label font face.

◆ WLM_GRAPH_LABEL_FONT_SIZE_BASE

#define WLM_GRAPH_LABEL_FONT_SIZE_BASE   8

Base font size for label (at 64px icon size).

◆ WLM_GRAPH_PIXEL_BLACK

#define WLM_GRAPH_PIXEL_BLACK   0xff000000

Black pixel (fully opaque).

◆ WLM_GRAPH_PIXEL_GRAY

#define WLM_GRAPH_PIXEL_GRAY ( b)
Value:
(WLM_GRAPH_PIXEL_BLACK | ((uint32_t)(b) << 16) | \
((uint32_t)(b) << 8) | (uint32_t)(b))
#define WLM_GRAPH_PIXEL_BLACK
Definition wlm_graph_shared.c:109

Constructs a grayscale ARGB pixel from brightness value.

◆ WLM_GRAPH_PIXEL_LINE_DEFAULT

#define WLM_GRAPH_PIXEL_LINE_DEFAULT   0xff008000

Default line pixel color (green).

◆ WLM_GRAPH_SOLID_BRIGHTNESS_MAX

#define WLM_GRAPH_SOLID_BRIGHTNESS_MAX   128

Maximum brightness for solid area (for WLM_GRAPH_COLOR_MODE_ALPHA).

◆ WLM_GRAPH_SOLID_BRIGHTNESS_MIN

#define WLM_GRAPH_SOLID_BRIGHTNESS_MIN   32

Minimum brightness for solid area (for WLM_GRAPH_COLOR_MODE_ALPHA).

Typedef Documentation

◆ wlm_graph_sample_t

typedef struct wlm_graph_sample_t wlm_graph_sample_t

Single sample in the circular buffer.

Enumeration Type Documentation

◆ wlm_graph_color_mode_t

Color modes for the graph.

Enumerator
WLM_GRAPH_COLOR_MODE_HEAT 

Heat map from blue (cold) to red (hot).

WLM_GRAPH_COLOR_MODE_ALPHA 

Gray-scale with alpha-like intensity.

Function Documentation

◆ _str_match_prefix_n()

bool _str_match_prefix_n ( const char ** str_ptr,
size_t * len_ptr,
const char * prefix,
const size_t prefix_len )
static

Checks if string starts with prefix; on match advances string and adjusts length.

Parameters
str_ptrPointer to string pointer (advanced on match).
len_ptrPointer to remaining length (decremented on match).
prefixPrefix to match.
prefix_lenLength of prefix.
Returns
true if prefix matched and consumed, false otherwise.

◆ _wlm_graph_arg_parse_f64()

bool _wlm_graph_arg_parse_f64 ( const char * opt_name,
const char * str,
const double val_min,
const double val_max,
double * result_ptr )
static

Parses a string as a double within a specified range.

Parameters
opt_nameOption name for error messages.
strString to parse.
val_minMinimum allowed value.
val_maxMaximum allowed value.
result_ptrOutput: parsed value.
Returns
true on success.

◆ _wlm_graph_arg_parse_font()

bool _wlm_graph_arg_parse_font ( const char * opt_name,
const char * str,
wlm_graph_font_spec_t * font )
static

Parses XFT-style font specification: "Name:size=N:weight=W:slant=S".

Parameters
opt_nameOption name for error messages (e.g., "--font").
strFont specification string.
fontFont spec struct to populate.
Returns
true on success, false on error (message printed to stderr).

◆ _wlm_graph_arg_parse_i32()

bool _wlm_graph_arg_parse_i32 ( const char * opt_name,
const char * str,
const int32_t val_min,
const int32_t val_max,
int32_t * result_ptr )
static

Parses a string as a 32-bit integer within a specified range.

Parameters
opt_nameOption name for error messages.
strString to parse.
val_minMinimum allowed value.
val_maxMaximum allowed value.
result_ptrOutput: parsed value.
Returns
true on success.

◆ _wlm_graph_args_parse()

int _wlm_graph_args_parse ( const int argc,
const char ** argv,
const char * app_name,
const char * app_help,
const bool has_custom_lut,
const bool has_label,
wlm_graph_prefs_t * prefs )
static

Parses command-line arguments into preferences.

Parameters
argcArgument count.
argvArgument vector.
app_nameApplication name for error messages.
app_helpApplication help string for –help output.
has_custom_lutWhether app supports custom color lookup table.
has_labelWhether app supports label display.
prefsOutput: parsed preferences.
Returns
0 to continue, 1 if –help was shown, -1 on error.

◆ _wlm_graph_bezel_draw()

bool _wlm_graph_bezel_draw ( bs_gfxbuf_t * gfxbuf_ptr,
const uint32_t margin_logical_px )
static

Draws the bezel frame around the graph area.

Parameters
gfxbuf_ptrGraphics buffer to draw into.
margin_logical_pxMargin in logical pixels (at base icon size).
Returns
true on success.

◆ _wlm_graph_blit_to_buffer()

void _wlm_graph_blit_to_buffer ( bs_gfxbuf_t * gfxbuf_ptr,
const uint32_t * graph_pixels,
const uint32_t graph_size[2],
const uint32_t offset[2] )
static

Copies graph pixels to the destination graphics buffer.

Parameters
gfxbuf_ptrDestination graphics buffer.
graph_pixelsSource pixel data.
graph_sizeGraph dimensions [width, height].
offsetOffset in destination buffer [x, y].

◆ _wlm_graph_buffers_resize()

bool _wlm_graph_buffers_resize ( wlm_graph_state_t * graph_state,
const uint32_t size[2],
const uint32_t margin_logical_px )
static

Resizes graph buffers when icon dimensions change.

Parameters
graph_stateGraph state to resize.
sizeNew icon dimensions [width, height].
margin_logical_pxMargin in logical pixels (at base icon size).
Returns
true if buffers were resized, false if dimensions are too small.

◆ _wlm_graph_column_render()

uint32_t _wlm_graph_column_render ( wlm_graph_state_t * graph_state,
const wlm_graph_sample_t * sample,
const uint32_t column,
const wlm_graph_mode_t accumulate_mode )
static

Renders a single column of the graph from sample data.

Parameters
graph_stateGraph state containing pixel buffer and LUT.
sampleSample data for this column.
columnColumn index to render.
accumulate_modeSample accumulation mode.
Returns
Y coordinate of the peak line for this column.

◆ _wlm_graph_fill_columns_black()

void _wlm_graph_fill_columns_black ( uint32_t * graph_pixels,
const uint32_t graph_size[2],
const uint32_t column_end )
static

Fills columns with black pixels.

Parameters
graph_pixelsPixel buffer to fill.
graph_sizeGraph dimensions [width, height].
column_endNumber of columns to fill (0 to column_end-1).

◆ _wlm_graph_icon_render_callback()

bool _wlm_graph_icon_render_callback ( bs_gfxbuf_t * gfxbuf_ptr,
void * ud_ptr )
static

Callback invoked when the icon needs to be rendered.

Parameters
gfxbuf_ptrGraphics buffer to render into.
ud_ptrUser data pointer (wlm_graph_handle_t).
Returns
true on success.

◆ _wlm_graph_label_draw()

void _wlm_graph_label_draw ( bs_gfxbuf_t * gfxbuf_ptr,
const uint32_t margin_px,
const char * label,
const wlm_graph_prefs_t * prefs )
static

Draws the label text in the top-left corner of the graph.

Parameters
gfxbuf_ptrGraphics buffer to draw into.
margin_pxMargin in pixels (scaled to current icon size).
labelLabel text to draw.
prefsPreferences containing font settings.

◆ _wlm_graph_peak_connector_draw()

void _wlm_graph_peak_connector_draw ( uint32_t * graph_pixels,
const uint32_t graph_width,
const uint32_t pixel_line,
const uint32_t x,
const uint32_t y_range[2] )
static

Draws a vertical connector line between y coordinates.

Parameters
graph_pixelsPixel buffer to draw into.
graph_widthWidth of the graph in pixels.
pixel_lineColor for the connector line.
xX coordinate (column) to draw at.
y_rangeY range [start, end) to draw.

◆ _wlm_graph_peak_connector_draw_between()

void _wlm_graph_peak_connector_draw_between ( uint32_t * graph_pixels,
const uint32_t graph_size[2],
const uint32_t pixel_line,
const uint8_t usage_curr,
const uint8_t usage_prev,
const uint32_t column_curr,
const uint32_t column_prev )
static

Draws a vertical connector between adjacent columns when peaks differ.

Parameters
graph_pixelsPixel buffer to draw into.
graph_sizeGraph dimensions [width, height].
pixel_lineColor for the connector line.
usage_currUsage value for current column.
usage_prevUsage value for previous column.
column_currCurrent column index.
column_prevPrevious column index.

◆ _wlm_graph_pixel_lut_init()

void _wlm_graph_pixel_lut_init ( uint32_t pixel_lut[256],
uint32_t * pixel_line_ptr,
const wlm_graph_color_mode_t color_mode )
static

Initializes the pixel lookup table for graph coloring.

Parameters
pixel_lutOutput: 256-entry lookup table for value-to-color mapping.
pixel_line_ptrOutput: color for the line/peak indicator.
color_modeColor mode (heat map or alpha/grayscale).

◆ _wlm_graph_rebuild_from_samples()

void _wlm_graph_rebuild_from_samples ( wlm_graph_state_t * graph_state,
const wlm_graph_mode_t accumulate_mode )
static

Rebuilds the entire graph from stored samples.

Used after resize to re-render all columns from sample history.

Parameters
graph_stateGraph state containing samples and pixel buffer.
accumulate_modeSample accumulation mode.

◆ _wlm_graph_sample_compute_peak()

void _wlm_graph_sample_compute_peak ( wlm_graph_sample_t * sample,
const wlm_graph_mode_t accumulate_mode )
static

Computes the peak value for a sample from its values array.

Parameters
sampleSample with values already populated.
accumulate_modeSample accumulation mode.

◆ _wlm_graph_sample_update()

void _wlm_graph_sample_update ( wlm_graph_handle_t * handle)
static

Reads stats and updates the graph with a new sample.

Parameters
handleGraph handle containing state and config.

◆ _wlm_graph_sample_values_free()

void _wlm_graph_sample_values_free ( wlm_graph_sample_t * samples,
const uint32_t count )
static

Frees the values arrays within each sample.

Parameters
samplesArray of samples to free values from.
countNumber of samples in the array.

◆ _wlm_graph_samples_init()

void _wlm_graph_samples_init ( wlm_graph_sample_t * samples,
const uint32_t count )
static

Initializes sample array as a circular doubly-linked list.

Parameters
samplesArray of samples to initialize.
countNumber of samples in the array.

◆ _wlm_graph_scroll_left()

void _wlm_graph_scroll_left ( uint32_t * graph_pixels,
const uint32_t graph_size[2],
const uint32_t y_start )
static

Scrolls graph pixels left by one column.

Parameters
graph_pixelsPixel buffer to scroll.
graph_sizeGraph dimensions [width, height].
y_startStarting Y coordinate (skip rows above this).

◆ _wlm_graph_state_free()

void _wlm_graph_state_free ( wlm_graph_state_t * graph_state)
static

Frees all resources associated with graph state.

Parameters
graph_stateGraph state to free, or NULL.

◆ _wlm_graph_time_next_update()

uint64_t _wlm_graph_time_next_update ( const uint64_t interval_usec)
static

Calculates the timestamp for the next update.

Parameters
interval_usecUpdate interval in microseconds.
Returns
Timestamp in microseconds for the next update.

◆ _wlm_graph_timer_callback()

void _wlm_graph_timer_callback ( wlclient_t * client_ptr,
void * ud_ptr )
static

Timer callback for periodic graph updates.

Parameters
client_ptrWayland client instance.
ud_ptrUser data pointer (wlm_graph_handle_t).

◆ _wlm_graph_update_with_sample()

void _wlm_graph_update_with_sample ( wlm_graph_state_t * graph_state,
wlm_graph_sample_t * new_sample,
const wlm_graph_mode_t accumulate_mode )
static

Updates the graph with a new sample, scrolling and rendering.

Parameters
graph_stateGraph state to update.
new_sampleSample containing the new data.
accumulate_modeSample accumulation mode.

◆ _wlm_graph_usage_to_y()

uint32_t _wlm_graph_usage_to_y ( const uint8_t usage,
const uint32_t height )
static

Maps usage (0-255) to y coordinate: y=0 is top, higher usage = higher on screen.

◆ _wlm_graph_usage_to_y_with_zero_check()

uint32_t _wlm_graph_usage_to_y_with_zero_check ( const uint8_t usage,
const uint32_t height )
static

Maps usage (0-255) to y coordinate. Returns height for usage=0 (no bar).

◆ _wlm_graph_y_min_from_samples()

void _wlm_graph_y_min_from_samples ( wlm_graph_state_t * graph_state,
wlm_graph_sample_t * current_sample )
static

Finds sample with highest peak, sets y_min and sample_peak in graph_state.

Iterates newest-to-oldest and uses strict < so the newest sample wins ties. This keeps sample_peak in the buffer longer, reducing rescan frequency.

◆ wlm_graph_app_run()

int wlm_graph_app_run ( int argc,
const char ** argv,
const wlm_graph_app_config_t * config )

Runs a graph application.

Handles argument parsing, wlclient setup, icon creation, callback registration, main loop, and cleanup. Apps just need to initialize their state and provide a configuration.

Graph state is managed internally by this function.

Parameters
argcArgument count.
argvArgument vector.
configApplication configuration.
Returns
EXIT_SUCCESS or EXIT_FAILURE.