wlmaker
Loading...
Searching...
No Matches
wlmbattery.c File Reference
#include <dirent.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <primitives/primitives.h>
#include <libwlclient/libwlclient.h>
#include <libbase/libbase.h>
Include dependency graph for wlmbattery.c:

Classes

struct  wlm_power_supply
struct  wlm_power_adapter
struct  wlm_battery
struct  callback_arg

Macros

#define SYSFS_BATTERY_PATH_MAX_LEN   256
#define VAL_BUF_LEN   64

Enumerations

enum  battery_status {
  BATTERY_STATUS_UNKNOWN = 0 , BATTERY_STATUS_CHARGING , BATTERY_STATUS_DISCHARGING , BATTERY_STATUS_NOT_CHARGING ,
  BATTERY_STATUS_FULL
}
enum  adapter_status { ADAPTER_STATUS_UNKNOWN = 0 , ADAPTER_STATUS_ONLINE , ADAPTER_STATUS_OFFLINE }

Functions

static struct wlm_power_supplywlm_power_supply_create (void)
static void wlm_power_supply_destroy (struct wlm_power_supply *ps)
static void wlm_power_supply_read (struct wlm_power_supply *ps)
static size_t wlm_power_supply_num_batteries (struct wlm_power_supply *ps)
static struct wlm_batterywlm_power_supply_battery (struct wlm_power_supply *ps, size_t index)
static bool wlm_power_supply_connected (struct wlm_power_supply *ps)
static struct wlm_batterywlm_battery_create (const char *name_ptr, const char *power_supply_dir)
static void wlm_battery_destroy (bs_dllist_node_t *dlnode_ptr, void *ud_ptr)
static void wlm_battery_read (struct wlm_battery *bat)
static struct wlm_power_adapterwlm_power_adapter_create (const char *name_ptr, const char *power_supply_dir)
static void wlm_power_adapter_destroy (bs_dllist_node_t *dlnode_ptr, void *ud_ptr)
static bool wlm_power_adapter_connected (bs_dllist_node_t *node_ptr, void *ud_ptr)
static void wlm_power_adapter_read (struct wlm_power_adapter *adapter)
static bool wlm_vread_buffer (char *v, size_t v_size, const char *fmt_ptr, va_list ap)
static bool wlm_read_buffer (char *v, size_t v_size, const char *fmt_ptr,...)
static bool wlm_read_uint64 (uint64_t *u64_ptr, const char *fmt_ptr,...)
static enum battery_status parse_battery_status (const char *status_str)
bool icon_callback (bs_gfxbuf_t *gfxbuf_ptr, void *ud_ptr)
void timer_callback (wlclient_t *client_ptr, void *ud_ptr)
int main (void)

Detailed Description

Displays battery capacity from /sys/class/power_supply in a DockApp.

TODO(kaese.nosp@m.r@gu.nosp@m.bbe.c.nosp@m.h):

  • Only update when the numbers have actually changed.
  • Support more than one battery (eg. by clicking through?)
  • Use graphics for visualization, this initial version is very crude.
  • Permit dynamic size of the app, currently it's fixed to 64x64.

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

◆ SYSFS_BATTERY_PATH_MAX_LEN

#define SYSFS_BATTERY_PATH_MAX_LEN   256

Sufficient for the typical paths, although there is no strict maximum.

◆ VAL_BUF_LEN

#define VAL_BUF_LEN   64

Small buffer for reading single values (like capacity).

Enumeration Type Documentation

◆ adapter_status

Connection status of power adapter.

◆ battery_status

Internal battery status enum.

Function Documentation

◆ icon_callback()

bool icon_callback ( bs_gfxbuf_t * gfxbuf_ptr,
void * ud_ptr )

Draws current byttery status into the icon buffer.

Parameters
gfxbuf_ptr
ud_ptr

◆ main()

int main ( void )
Returns
EXIT_SUCCESS on success.

◆ parse_battery_status()

enum battery_status parse_battery_status ( const char * status_str)
static

Maps standard sysfs status string to internal enum.

Parameters
status_strString literal describing the battery status.
Returns
The corresponding parsed internal enum status.

◆ timer_callback()

void timer_callback ( wlclient_t * client_ptr,
void * ud_ptr )

Called once per second.

◆ wlm_battery_create()

struct wlm_battery * wlm_battery_create ( const char * name_ptr,
const char * power_supply_dir )
static

Creates battery struct spanning the specific name_ptr.

Parameters
name_ptrNull-terminated property name segment (e.g. "BAT0").
power_supply_dirThe root directory where sysfs devices live.
Returns
Pointer to allocated battery structure, or NULL on error.

◆ wlm_battery_destroy()

void wlm_battery_destroy ( bs_dllist_node_t * dlnode_ptr,
void * ud_ptr )
static

Destroys battery struct.

Parameters
dlnode_ptr
ud_ptr

◆ wlm_battery_read()

void wlm_battery_read ( struct wlm_battery * bat)
static

Helper to pull all readings into the structural cache representation.

Parameters
batPointer to the target battery object tracking the metric states.

◆ wlm_power_adapter_connected()

bool wlm_power_adapter_connected ( bs_dllist_node_t * node_ptr,
void * ud_ptr )
static

Iterator function to verify whether a power adapter node is connected.

Parameters
node_ptrThe doubly-linked list node embedded in a wlm_power_adapter.
ud_ptrIgnored user data.
Returns
True if the adapter is online.

◆ wlm_power_adapter_create()

struct wlm_power_adapter * wlm_power_adapter_create ( const char * name_ptr,
const char * power_supply_dir )
static

Creates adapter struct spanning the specific name_ptr.

Parameters
name_ptrNull-terminated property name segment (e.g. "AC0").
power_supply_dirThe root directory where sysfs devices live.
Returns
Pointer to allocated adapter structure, or NULL on error.

◆ wlm_power_adapter_destroy()

void wlm_power_adapter_destroy ( bs_dllist_node_t * dlnode_ptr,
void * ud_ptr )
static

Destroys power adapter struct.

Parameters
dlnode_ptrThe list node of the power adapter to destroy.
ud_ptr

◆ wlm_power_adapter_read()

void wlm_power_adapter_read ( struct wlm_power_adapter * adapter)
static

Reads power adapter properties into the structural cache representation.

Parameters
adapterPointer to the target power adapter object.

◆ wlm_power_supply_battery()

struct wlm_battery * wlm_power_supply_battery ( struct wlm_power_supply * ps,
size_t index )
static

Extract battery handle at specified index linearly.

Parameters
psPointer to the structured system state container.
indexIndex integer for node traversal limit bounding.
Returns
Sliced battery memory, NULL if invalid parameter passed.

◆ wlm_power_supply_connected()

bool wlm_power_supply_connected ( struct wlm_power_supply * ps)
static

Indicates if any of the power adapters is online.

Parameters
psPointer to the target power supply.
Returns
True if at least one adapter is connected and online.

◆ wlm_power_supply_create()

struct wlm_power_supply * wlm_power_supply_create ( void )
static

Iterates through /sys/class/power_supply and creates corresponding battery and adapter items.

Returns
Pointer to the power supply, or NULL on error.

◆ wlm_power_supply_destroy()

void wlm_power_supply_destroy ( struct wlm_power_supply * ps)
static

Destroys the power supply container and internal batteries/adapters.

Parameters
psPointer to the target power supply.

◆ wlm_power_supply_num_batteries()

size_t wlm_power_supply_num_batteries ( struct wlm_power_supply * ps)
static

Returns the tracked amount of batteries.

Parameters
psPointer to the target power supply.
Returns
the tracked amount of batteries.

◆ wlm_power_supply_read()

void wlm_power_supply_read ( struct wlm_power_supply * ps)
static

Reads current state of the power supply (batteries and adapters).

Parameters
ps

◆ wlm_read_buffer()

bool wlm_read_buffer ( char * v,
size_t v_size,
const char * fmt_ptr,
... )
static

Reads content of file named by the formatted path into the buffer.

Parameters
vOutput buffer.
v_sizeLength boundary for v.
fmt_ptrFormat string for the path.
...Arguments for the format string.
Returns
True if read safely, otherwise false.

◆ wlm_read_uint64()

bool wlm_read_uint64 ( uint64_t * u64_ptr,
const char * fmt_ptr,
... )
static

Reads content of file named by the formatted path and parses it as uint64.

Parameters
u64_ptrOutput pointer for the parsed integer.
fmt_ptrFormat string for the path.
...Arguments for the format string.
Returns
True on successful parse, otherwise false.

◆ wlm_vread_buffer()

bool wlm_vread_buffer ( char * v,
size_t v_size,
const char * fmt_ptr,
va_list ap )
static

Reads content of file named by the formatted path into a buffer.

Parameters
vOutput buffer.
v_sizeLength boundary for v.
fmt_ptrFormat string for the path.
apVariadic arguments list.
Returns
True if read safely, otherwise false.