Atlas - power.c
Home / ext / SDL / examples / misc / 01-power Lines: 1 | Size: 5822 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 * This example code reports power status (plugged in, battery level, etc). 3 * 4 * This code is public domain. Feel free to use it for any purpose! 5 */ 6 7#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ 8#include <SDL3/SDL.h> 9#include <SDL3/SDL_main.h> 10 11/* We will use this renderer to draw into this window every frame. */ 12static SDL_Window *window = NULL; 13static SDL_Renderer *renderer = NULL; 14 15/* This function runs once at startup. */ 16SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 17{ 18 SDL_SetAppMetadata("Example Misc Power", "1.0", "com.example.misc-power"); 19 20 if (!SDL_Init(SDL_INIT_VIDEO)) { 21 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); 22 return SDL_APP_FAILURE; 23 } 24 25 if (!SDL_CreateWindowAndRenderer("examples/misc/power", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 26 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 27 return SDL_APP_FAILURE; 28 } 29 SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); 30 31 return SDL_APP_CONTINUE; /* carry on with the program! */ 32} 33 34/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 35SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 36{ 37 if (event->type == SDL_EVENT_QUIT) { 38 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 39 } 40 return SDL_APP_CONTINUE; /* carry on with the program! */ 41} 42 43/* This function runs once per frame, and is the heart of the program. */ 44SDL_AppResult SDL_AppIterate(void *appstate) 45{ 46 const SDL_FRect frame = { 100, 200, 440, 80 }; /* the percentage bar dimensions. */ 47 48 /* Query for battery info */ 49 int seconds = 0, percent = 0; 50 const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent); 51 52 /* We set up different drawing details for each power state, then 53 run it all through the same drawing code. */ 54 int clearr = 0, clearg = 0, clearb = 0; /* clear window to this color. */ 55 int textr = 255, textg = 255, textb = 255; /* draw messages in this color. */ 56 int framer = 255, frameg = 255, frameb = 255; /* draw a percentage bar frame in this color. */ 57 int barr = 0, barg = 0, barb = 0; /* draw a percentage bar in this color. */ 58 const char *msg = NULL; 59 const char *msg2 = NULL; 60 61 switch (state) { 62 case SDL_POWERSTATE_ERROR: 63 msg2 = "ERROR GETTING POWER STATE"; 64 msg = SDL_GetError(); 65 clearr = 255; /* red background */ 66 break; 67 68 default: /* in case this does something unexpected later, treat it as unknown. */ 69 case SDL_POWERSTATE_UNKNOWN: 70 msg = "Power state is unknown."; 71 clearr = clearb = clearg = 50; /* grey background */ 72 break; 73 74 case SDL_POWERSTATE_ON_BATTERY: 75 msg = "Running on battery."; 76 barr = 255; /* draw in red */ 77 break; 78 79 case SDL_POWERSTATE_NO_BATTERY: 80 msg = "Plugged in, no battery available."; 81 clearg = 50; /* green background */ 82 break; 83 84 case SDL_POWERSTATE_CHARGING: 85 msg = "Charging."; 86 barb = barg = 255; /* draw in cyan */ 87 break; 88 89 case SDL_POWERSTATE_CHARGED: 90 msg = "Charged."; 91 barg = 255; /* draw in green */ 92 break; 93 } 94 95 SDL_SetRenderDrawColor(renderer, clearr, clearg, clearb, 255); 96 SDL_RenderClear(renderer); 97 98 if (percent >= 0) { 99 float x, y; 100 SDL_FRect pctrect; 101 char remainstr[64]; 102 char msgbuf[128]; 103 104 SDL_copyp(&pctrect, &frame); 105 pctrect.w *= percent / 100.0f; 106 107 if (seconds < 0) { 108 SDL_strlcpy(remainstr, "unknown time", sizeof (remainstr)); 109 } else { 110 int hours, minutes; 111 hours = seconds / (60 * 60); 112 seconds -= hours * (60 * 60); 113 minutes = seconds / 60; 114 seconds -= minutes * 60; 115 SDL_snprintf(remainstr, sizeof (remainstr), "%02d:%02d:%02d", hours, minutes, seconds); 116 } 117 118 SDL_snprintf(msgbuf, sizeof (msgbuf), "Battery: %3d percent, %s remaining", percent, remainstr); 119 x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msgbuf))) / 2.0f); 120 y = frame.y + frame.h + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE; 121 122 SDL_SetRenderDrawColor(renderer, barr, barg, barb, 255); /* draw percent bar. */ 123 SDL_RenderFillRect(renderer, &pctrect); 124 SDL_SetRenderDrawColor(renderer, framer, frameg, frameb, 255); /* draw frame on top of bar. */ 125 SDL_RenderRect(renderer, &frame); 126 SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255); 127 SDL_RenderDebugText(renderer, x, y, msgbuf); /* draw text about battery level */ 128 } 129 130 if (msg) { 131 const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f); 132 const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2); 133 SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255); 134 SDL_RenderDebugText(renderer, x, y, msg); 135 } 136 137 if (msg2) { 138 const float x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg2))) / 2.0f); 139 const float y = frame.y - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 4); 140 SDL_SetRenderDrawColor(renderer, textr, textg, textb, 255); 141 SDL_RenderDebugText(renderer, x, y, msg2); 142 } 143 144 /* put the new rendering on the screen. */ 145 SDL_RenderPresent(renderer); 146 147 return SDL_APP_CONTINUE; /* carry on with the program! */ 148} 149 150/* This function runs once at shutdown. */ 151void SDL_AppQuit(void *appstate, SDL_AppResult result) 152{ 153 /* SDL will clean up the window/renderer for us. */ 154} 155 156[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.