Search code examples
ccross-platformvulkanbattery

Is there a cross-platform way to determine if a device is powered via PSU or external battery in C?


I'm trying to wisely choose presentation modes for my Vulkan C program depending on the device and need a cross-platform script to determine if:

  • the device running the program is PC or Laptop/Phone
  • an uninterruptible PSU is present or the device is powered by internal battery

Both solutions are targeted to check for MAILBOX mode if battery is not present, otherwise check for RELAXED FIFO, so any of two solutions will fit my needs.

I know I can use macros to check if the device is Android or Apple with __ANDROID__ and __APPLE__, and also get battery status with WinAPI GetSystemPowerStatus(), but I don't wanna stick to some platforms writting application on Vulkan...

Is there a cross-platform way to check for presence of either Power Supply or Laptop/Phone battery?


Solution

  • There's a cross-platform way to check for battery status with SDL/SDL2/SDL3 with SDL_GetPowerInfo() in <SDL/SDL_power.h>. Then you can compare the status with SDL_PowerState enumerations, for instance:

    SDL_PowerState systemPowerState = SDL_GetPowerInfo(NULL, NULL);
    if (systemPowerState == SDL_POWERSTATE_NO_BATTERY)
        // check for mailbox
    else
        // check for relaxed_fifo