Skip to content

Commit

Permalink
src\utilities: add file_read()
Browse files Browse the repository at this point in the history
from prv PR + cleanup
  • Loading branch information
Apaczer committed Oct 10, 2024
1 parent 8509f72 commit b96c16a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
17 changes: 5 additions & 12 deletions src/platform/miyoo.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#define MIYOO_BATTERY_FILE "/mnt/.batterylow.conf"
#define MIYOO_LID_CONF "/sys/devices/platform/backlight/backlight/backlight/brightness"
#define MIYOO_BATTERY "/sys/class/power_supply/miyoo-battery/voltage_now"
#define MIYOO_USB_STATE "/sys/class/udc/musb-hdrc.1.auto/state"
#define MIYOO_USB_SUSPEND "/sys/devices/platform/usb_phy_generic.0.auto/subsystem/devices/1c13000.usb/musb-hdrc.1.auto/gadget/suspended"
#define MIYOO_OPTIONS_FILE "/mnt/options.cfg"
#define MIYOO_TVOUT_FILE "/mnt/tvout"
#define MIYOO_SND_FILE "/dev/miyoo_snd"
Expand Down Expand Up @@ -262,19 +264,10 @@ class GMenu2X_platform : public GMenu2X {

return val;
}
std::string readFile(const std::string& path) {
std::ifstream file(path);
std::string content;
if (file.is_open()) {
std::getline(file, content);
file.close();
}
return content;
}

bool isUsbConnected() {
std::string state = readFile("/sys/class/udc/musb-hdrc.1.auto/state");
std::string suspended = readFile("/sys/devices/platform/usb_phy_generic.0.auto/subsystem/devices/1c13000.usb/musb-hdrc.1.auto/gadget/suspended");

string state = file_read(MIYOO_USB_STATE);
string suspended = file_read(MIYOO_USB_SUSPEND);
return (state == "configured" && suspended == "0");
}

Expand Down
3 changes: 1 addition & 2 deletions src/powermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ uint32_t PowerManager::doSuspend(uint32_t interval, void *param) {
// PowerManager::instance->gmenu2x->setVolume(0);
PowerManager::instance->resetPowerTimer();
PowerManager::instance->gmenu2x->cls();
if (!PowerManager::instance->gmenu2x->isUsbConnected()) {
if (!PowerManager::instance->gmenu2x->isUsbConnected())
PowerManager::instance->gmenu2x->setCPU(PowerManager::instance->gmenu2x->confInt["cpuMin"]);
}
PowerManager::instance->suspendActive = true;
return interval;
}
Expand Down
13 changes: 13 additions & 0 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include <algorithm>

#include <fstream>

#include "utilities.h"
#include "debug.h"

Expand Down Expand Up @@ -318,6 +320,17 @@ bool file_copy(const string &src, const string &dst) {
return true;
}

string file_read(const string& path) {
if (!file_exists(path)) return 0;
ifstream file(path);
string content;
if (file.is_open()) {
getline(file, content);
file.close();
}
return content;
}

string unique_filename(string path, string ext) {
uint32_t x = 0;
string fname = path + ext;
Expand Down
1 change: 1 addition & 0 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ string real_path(const string &path);
string dir_name(const string &path);
string base_name(string path, bool strip_extension = false);
string file_ext(const string &path, bool tolower = false);
string file_read(const string &path);
string lowercase(string s);
string unique_filename(string path, string ext);
string exe_path();
Expand Down

0 comments on commit b96c16a

Please sign in to comment.