3333#include <string.h>
3434
3535#include "libwlclient/icon.h"
36- #include "wlm_graph_utildefines.h"
3736
3837/* == Internal definitions ================================================= */
3938
@@ -171,10 +170,6 @@ typedef struct {
171170
172171/* == Forward declarations ================================================= */
173172
174- #ifdef __GNUC__
175- __attribute__((noreturn ))
176- #endif
177- static void _wlm_graph_panic (const char * msg );
178173static void _wlm_graph_sample_values_free (wlm_graph_sample_t * samples , const uint32_t count );
179174static void _wlm_graph_samples_init (wlm_graph_sample_t * samples , const uint32_t count );
180175static void _wlm_graph_sample_compute_peak (
@@ -246,8 +241,8 @@ static bool _wlm_graph_arg_parse_i32(
246241 errno = 0 ;
247242 const long val = strtol (str , & endptr , 10 );
248243 if ('\0' != * endptr || 0 != errno || val < val_min || val > val_max ) {
249- fprintf ( stderr , "Error: %s value '%s' must be %d-%d\n" ,
250- opt_name , str , val_min , val_max );
244+ bs_log ( BS_ERROR , "Error: %s value '%s' must be %d-%d\n" ,
245+ opt_name , str , val_min , val_max );
251246 return false;
252247 }
253248 * result_ptr = (int32_t )val ;
@@ -277,8 +272,7 @@ static bool _wlm_graph_arg_parse_f64(
277272 errno = 0 ;
278273 const double val = strtod (str , & endptr );
279274 if ('\0' != * endptr || 0 != errno || val < val_min || val > val_max ) {
280- fprintf (stderr , "Error: %s value '%s' must be %g-%g\n" ,
281- opt_name , str , val_min , val_max );
275+ bs_log (BS_ERROR , "%s value '%s' must be %g-%g" , opt_name , str , val_min , val_max );
282276 return false;
283277 }
284278 * result_ptr = val ;
@@ -334,7 +328,7 @@ static bool _wlm_graph_arg_parse_font(
334328 // Extract font name (everything before first colon, or entire string).
335329 const size_t name_len = (NULL != colon ) ? (size_t )(colon - str ) : strlen (str );
336330 if (0 == name_len || name_len >= WLM_GRAPH_FONT_FACE_MAX ) {
337- fprintf ( stderr , "Error: %s font name too long or empty\n " , opt_name );
331+ bs_log ( BS_ERROR , "%s font name too long or empty" , opt_name );
338332 return false;
339333 }
340334 memcpy (font -> face , str , name_len );
@@ -349,7 +343,7 @@ static bool _wlm_graph_arg_parse_font(
349343 if (_str_match_prefix (& pos , & len , "size=" )) {
350344 char size_buf [16 ];
351345 if (len >= sizeof (size_buf )) {
352- fprintf ( stderr , "Error: %s size value too long\n " , opt_name );
346+ bs_log ( BS_ERROR , "%s size value too long" , opt_name );
353347 return false;
354348 }
355349 memcpy (size_buf , pos , len );
@@ -366,7 +360,7 @@ static bool _wlm_graph_arg_parse_font(
366360 } else if (_str_match_prefix (& pos , & len , "bold" ) && 0 == len ) {
367361 font -> weight = CAIRO_FONT_WEIGHT_BOLD ;
368362 } else {
369- fprintf ( stderr , "Error: %s weight must be 'normal' or 'bold'\n " , opt_name );
363+ bs_log ( BS_ERROR , "%s weight must be 'normal' or 'bold'" , opt_name );
370364 return false;
371365 }
372366 } else if (_str_match_prefix (& pos , & len , "slant=" )) {
@@ -377,11 +371,11 @@ static bool _wlm_graph_arg_parse_font(
377371 } else if (_str_match_prefix (& pos , & len , "oblique" ) && 0 == len ) {
378372 font -> slant = CAIRO_FONT_SLANT_OBLIQUE ;
379373 } else {
380- fprintf ( stderr , "Error: %s slant must be 'normal', 'italic', or 'oblique'\n " , opt_name );
374+ bs_log ( BS_ERROR , "%s slant must be 'normal', 'italic', or 'oblique'" , opt_name );
381375 return false;
382376 }
383377 } else if (len > 0 ) {
384- fprintf ( stderr , "Error: Unknown %s option starting with '%.20s'\n " , opt_name , pos );
378+ bs_log ( BS_ERROR , "Unknown %s option starting with '%.20s'" , opt_name , pos );
385379 return false;
386380 }
387381 }
@@ -456,15 +450,13 @@ static void _wlm_graph_pixel_lut_init(
456450 * @param graph_state Graph state to resize.
457451 * @param size New icon dimensions [width, height].
458452 * @param margin_logical_px Margin in logical pixels (at base icon size).
459- * @param panic_fn Function to call on allocation failure.
460453 *
461454 * @return true if buffers were resized, false if dimensions are too small.
462455 */
463456static bool _wlm_graph_buffers_resize (
464457 wlm_graph_state_t * graph_state ,
465458 const uint32_t size [2 ],
466- const uint32_t margin_logical_px ,
467- void (* panic_fn )(const char * msg ))
459+ const uint32_t margin_logical_px )
468460{
469461 // Calculate inner dimensions (graph area inside bezel).
470462 const uint32_t margin_px = (margin_logical_px * size [0 ]) / WLM_GRAPH_BASE_ICON_SIZE ;
@@ -493,7 +485,7 @@ static bool _wlm_graph_buffers_resize(
493485 // Allocate sample buffer (values allocated lazily during capture).
494486 graph_state -> samples_alloc = calloc (inner_size [0 ], sizeof (wlm_graph_sample_t ));
495487 if (NULL == graph_state -> samples_alloc ) {
496- panic_fn ( "Failed to allocate sample buffer" );
488+ bs_log ( BS_FATAL | BS_ERRNO , "Failed to allocate sample buffer" );
497489 }
498490
499491 // Initialize circular doubly-linked list.
@@ -511,13 +503,13 @@ static bool _wlm_graph_buffers_resize(
511503 // Allocate graph data buffer.
512504 graph_state -> graph_pixels = malloc (sizeof (uint32_t ) * inner_size [0 ] * inner_size [1 ]);
513505 if (NULL == graph_state -> graph_pixels ) {
514- panic_fn ( "Failed to allocate graph data buffer" );
506+ bs_log ( BS_FATAL | BS_ERRNO , "Failed to allocate graph data buffer" );
515507 }
516508
517509 // Allocate row counts scratch buffer.
518510 graph_state -> row_counts = malloc (sizeof (uint32_t ) * inner_size [1 ]);
519511 if (NULL == graph_state -> row_counts ) {
520- panic_fn ( "Failed to allocate row counts buffer" );
512+ bs_log ( BS_FATAL | BS_ERRNO , "Failed to allocate row counts buffer" );
521513 }
522514
523515 graph_state -> graph_size [0 ] = inner_size [0 ];
@@ -665,7 +657,7 @@ static uint32_t _wlm_graph_column_render(
665657 if (0 == usage ) continue ;
666658
667659 cumulative += usage ;
668- const uint8_t level = (uint8_t )MIN2 (cumulative , 255 );
660+ const uint8_t level = (uint8_t )BS_MIN (cumulative , 255U );
669661
670662 // Calculate topmost line from cumulative usage.
671663 const uint32_t y_top = _wlm_graph_usage_to_y (level , graph_size [1 ]);
@@ -882,7 +874,7 @@ static bool _wlm_graph_bezel_draw(bs_gfxbuf_t *gfxbuf_ptr, const uint32_t margin
882874
883875 // Offset from edge to bezel position (scales with icon size).
884876 const uint32_t bezel_offset = ((margin_logical_px - 1 ) * size [0 ]) / WLM_GRAPH_BASE_ICON_SIZE ;
885- const uint32_t bezel_line_width = MAX2 (size [0 ] / WLM_GRAPH_BASE_ICON_SIZE , 1 );
877+ const uint32_t bezel_line_width = BS_MAX (size [0 ] / WLM_GRAPH_BASE_ICON_SIZE , 1U );
886878
887879 wlm_primitives_draw_bezel_at (
888880 cairo_ptr ,
@@ -1041,12 +1033,12 @@ static void _wlm_graph_sample_compute_peak(
10411033 for (uint32_t i = 0 ; i < values_num ; i ++ ) {
10421034 total += values [i ];
10431035 }
1044- sample -> value_peak = (uint8_t )MIN2 (total , 255 );
1036+ sample -> value_peak = (uint8_t )BS_MIN (total , 255U );
10451037 } else {
10461038 // Independent: peak is maximum of all values.
10471039 uint8_t max_val = 0 ;
10481040 for (uint32_t i = 0 ; i < values_num ; i ++ ) {
1049- max_val = MAX2 (max_val , values [i ]);
1041+ max_val = BS_MAX (max_val , values [i ]);
10501042 }
10511043 sample -> value_peak = max_val ;
10521044 }
@@ -1184,8 +1176,8 @@ static int _wlm_graph_args_parse(
11841176 } else if (0 == strcmp (argv [i + 1 ], "heat" )) {
11851177 prefs -> color_mode = WLM_GRAPH_COLOR_MODE_HEAT ;
11861178 } else {
1187- fprintf ( stderr , "Error: %s value '%s' must be 'alpha' or 'heat'\n " ,
1188- argv [i ], argv [i + 1 ]);
1179+ bs_log ( BS_ERROR , "%s value '%s' must be 'alpha' or 'heat'" ,
1180+ argv [i ], argv [i + 1 ]);
11891181 return -1 ;
11901182 }
11911183 i ++ ;
@@ -1213,8 +1205,8 @@ static int _wlm_graph_args_parse(
12131205 printf (" --help, -h Show this help\n" );
12141206 return 1 ;
12151207 } else {
1216- fprintf ( stderr , "Error: Unknown argument '%s'\n " , argv [i ]);
1217- fprintf ( stderr , "Try '%s --help' for usage.\n " , app_name );
1208+ bs_log ( BS_ERROR , "Unknown argument '%s'" , argv [i ]);
1209+ bs_log ( BS_ERROR , "Try '%s --help' for usage." , app_name );
12181210 return -1 ;
12191211 }
12201212 }
@@ -1246,7 +1238,7 @@ static bool _wlm_graph_icon_render_callback(bs_gfxbuf_t *gfxbuf_ptr, void *ud_pt
12461238
12471239 // Reset graph buffers if icon size changed.
12481240 if (size_changed ) {
1249- if (!_wlm_graph_buffers_resize (gs , size , margin_logical_px , _wlm_graph_panic )) {
1241+ if (!_wlm_graph_buffers_resize (gs , size , margin_logical_px )) {
12501242 bs_log (BS_ERROR , "Failed to reset graph buffers for %ux%u icon" ,
12511243 size [0 ], size [1 ]);
12521244 return false;
@@ -1381,21 +1373,6 @@ static void _wlm_graph_timer_callback(wlclient_t *client_ptr, void *ud_ptr)
13811373
13821374/* == Internal state management ============================================ */
13831375
1384- /* ------------------------------------------------------------------------- */
1385- /**
1386- * Prints error message to stderr and terminates the application.
1387- *
1388- * @param msg Error message to display.
1389- */
1390- #ifdef __GNUC__
1391- __attribute__((noreturn ))
1392- #endif
1393- static void _wlm_graph_panic (const char * msg )
1394- {
1395- fprintf (stderr , "wlm_graph: %s\n" , msg );
1396- exit (EXIT_FAILURE );
1397- }
1398-
13991376/* ------------------------------------------------------------------------- */
14001377/**
14011378 * Frees all resources associated with graph state.
@@ -1437,7 +1414,7 @@ int wlm_graph_app_run(
14371414 // Allocate graph state.
14381415 wlm_graph_state_t * graph_state = calloc (1 , sizeof (wlm_graph_state_t ));
14391416 if (NULL == graph_state ) {
1440- fprintf ( stderr , "%s: Failed to allocate graph state\n " , config -> app_name );
1417+ bs_log ( BS_ERROR , "%s: Failed to allocate graph state" , config -> app_name );
14411418 return EXIT_FAILURE ;
14421419 }
14431420
@@ -1451,8 +1428,8 @@ int wlm_graph_app_run(
14511428
14521429 // Initialize graph buffers with default icon size.
14531430 const uint32_t size_default [2 ] = {WLM_GRAPH_BASE_ICON_SIZE , WLM_GRAPH_BASE_ICON_SIZE };
1454- if (!_wlm_graph_buffers_resize (graph_state , size_default , prefs .margin_logical_px , _wlm_graph_panic )) {
1455- fprintf ( stderr , "Error: Icon dimensions too small for graph\n " );
1431+ if (!_wlm_graph_buffers_resize (graph_state , size_default , prefs .margin_logical_px )) {
1432+ bs_log ( BS_ERROR , "Icon dimensions too small for graph" );
14561433 _wlm_graph_state_free (graph_state );
14571434 return EXIT_FAILURE ;
14581435 }
0 commit comments