Remove unnecessary background services and polling loops to improve battery life
This commit is contained in:
1 parent
726500085a
commit
68cf9c515b
17 files changed
+159
-22
No files matched your search
@@ -10,8 +10,13 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <dirent.h>
|
||||
#include <dlfcn.h>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <sys/stat.h>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -233,6 +238,67 @@ void writeStatusIndicator(const std::string& badge) {
|
||||
std::fprintf(fp, "%s\n", badge.c_str());
|
||||
std::fclose(fp);
|
||||
}
|
||||
|
||||
DIR* dir = ::opendir(statusDir.c_str());
|
||||
if (!dir) {
|
||||
return;
|
||||
}
|
||||
std::vector<std::string> names;
|
||||
while (struct dirent* ent = ::readdir(dir)) {
|
||||
if (ent->d_name[0] == '.') {
|
||||
continue;
|
||||
}
|
||||
names.emplace_back(ent->d_name);
|
||||
}
|
||||
::closedir(dir);
|
||||
std::sort(names.begin(), names.end());
|
||||
|
||||
std::string statusLine;
|
||||
char lineBuf[256];
|
||||
for (const auto& name : names) {
|
||||
std::string fpath = statusDir + "/" + name;
|
||||
FILE* fp = std::fopen(fpath.c_str(), "r");
|
||||
if (!fp) {
|
||||
continue;
|
||||
}
|
||||
if (std::fgets(lineBuf, sizeof(lineBuf), fp)) {
|
||||
size_t len = std::strlen(lineBuf);
|
||||
while (len > 0 && (lineBuf[len - 1] == '\n' || lineBuf[len - 1] == '\r')) {
|
||||
lineBuf[--len] = '\0';
|
||||
}
|
||||
if (len > 0) {
|
||||
if (!statusLine.empty()) {
|
||||
statusLine.push_back(' ');
|
||||
}
|
||||
statusLine.append(lineBuf, len);
|
||||
}
|
||||
}
|
||||
std::fclose(fp);
|
||||
}
|
||||
|
||||
if (statusLine.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
using OpenDisplayFn = void* (*)(const char*);
|
||||
using DefaultRootWindowFn = unsigned long (*)(void*);
|
||||
using StoreNameFn = int (*)(void*, unsigned long, const char*);
|
||||
using CloseDisplayFn = int (*)(void*);
|
||||
|
||||
static void* x11Handle = ::dlopen("libX11.so.6", RTLD_LAZY | RTLD_LOCAL);
|
||||
static auto fnOpenDisplay = x11Handle ? reinterpret_cast<OpenDisplayFn>(::dlsym(x11Handle, "XOpenDisplay")) : nullptr;
|
||||
static auto fnDefaultRoot = x11Handle ? reinterpret_cast<DefaultRootWindowFn>(::dlsym(x11Handle, "XDefaultRootWindow")) : nullptr;
|
||||
static auto fnStoreName = x11Handle ? reinterpret_cast<StoreNameFn>(::dlsym(x11Handle, "XStoreName")) : nullptr;
|
||||
static auto fnCloseDisplay = x11Handle ? reinterpret_cast<CloseDisplayFn>(::dlsym(x11Handle, "XCloseDisplay")) : nullptr;
|
||||
|
||||
if (fnOpenDisplay && fnDefaultRoot && fnStoreName && fnCloseDisplay) {
|
||||
const char* disp = std::getenv("DISPLAY");
|
||||
void* x11Dpy = fnOpenDisplay((disp && disp[0]) ? disp : ":0");
|
||||
if (x11Dpy) {
|
||||
fnStoreName(x11Dpy, fnDefaultRoot(x11Dpy), statusLine.c_str());
|
||||
fnCloseDisplay(x11Dpy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in new issue
Block a user