Skip to content

Commit c0c5793

Browse files
committed
Daemon: Update daemon to check and parse Applist.JSON,
Sync Commit with latest CI Build (Stable)
1 parent 9d4d91e commit c0c5793

7 files changed

Lines changed: 301 additions & 44 deletions

File tree

jni/include/AZenith.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <time.h>
1616
#include <unistd.h>
1717

18+
1819
#define TASK_INTERVAL_SEC (12 * 60 * 60)
1920
#define LOOP_INTERVAL_MS 700
2021
#define LOOP_INTERVAL_SEC 2
@@ -33,11 +34,13 @@
3334
#define LOG_FILE_PRELOAD "/data/adb/.config/AZenith/preload/AZenithPR.log"
3435
#define PROFILE_MODE "/data/adb/.config/AZenith/API/current_profile"
3536
#define GAME_INFO "/data/adb/.config/AZenith/API/gameinfo"
36-
#define GAMELIST "/data/adb/.config/AZenith/gamelist/gamelist.txt"
37+
#define GAMELIST "/data/adb/.config/AZenith/gamelist/azenithApplist.json"
3738
#define MODULE_PROP "/data/adb/modules/AZenith/module.prop"
3839
#define MODULE_UPDATE "/data/adb/modules/AZenith/update"
3940
#define MODULE_VERSION ".placeholder"
40-
41+
#define IS_TRUE(v) ((v) && strcmp((v), "true") == 0)
42+
#define IS_FALSE(v) ((v) && strcmp((v), "false") == 0)
43+
#define IS_DEFAULT(v) (!(v) || strcmp((v), "default") == 0)
4144
#define MY_PATH \
4245
"PATH=/system/bin:/system/xbin:/data/adb/ap/bin:/data/adb/ksu/bin:/data/adb/magisk:/debug_ramdisk:/sbin:/sbin/su:/su/bin:/su/" \
4346
"xbin:/data/data/com.termux/files/usr/bin"
@@ -50,6 +53,14 @@
5053
#define IS_LOW_POWER(state) (strcmp(state, "true") == 0 || strcmp(state, "1") == 0)
5154

5255
// Basic C knowledge: enum starts with 0
56+
typedef struct {
57+
char perf_lite_mode[16];
58+
char dnd_on_gaming[16];
59+
char app_priority[16];
60+
char game_preload[16];
61+
char refresh_rate[16];
62+
char renderer[16];
63+
} GameOptions;
5364

5465
typedef enum : char {
5566
LOG_DEBUG,
@@ -104,6 +115,7 @@ bool return_false(void);
104115
void runthermalcore(void);
105116
void check_module_version(void);
106117
void runtask(void);
118+
int get_current_refresh_rate(void);
107119

108120
// Shell and Command execution
109121
char* execute_command(const char* format, ...);
@@ -136,9 +148,11 @@ MLBBState handle_mlbb(const char* gamestart);
136148
// Profiler
137149
extern bool (*get_screenstate)(void);
138150
extern bool (*get_low_power_state)(void);
139-
char* get_gamestart(void);
151+
char* get_gamestart(GameOptions* options);
140152
bool get_screenstate_normal(void);
141153
bool get_low_power_state_normal(void);
142154
void run_profiler(const int profile);
155+
char* skip_space(char* p);
156+
void extract_string_value(char* dest, const char* start, size_t max_len);
143157

144158
#endif

jni/main.c

Lines changed: 123 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <libgen.h>
1919
char* gamestart = NULL;
2020
pid_t game_pid = 0;
21+
GameOptions opts;
2122

2223
int main(int argc, char* argv[]) {
2324

@@ -89,7 +90,9 @@ int main(int argc, char* argv[]) {
8990
bool need_profile_checkup = false;
9091
MLBBState mlbb_is_running = MLBB_NOT_RUNNING;
9192
static bool is_initialize_complete = false;
92-
ProfileMode cur_mode = PERFCOMMON;
93+
ProfileMode cur_mode = PERFCOMMON;
94+
static bool dnd_enabled = false;
95+
static int saved_refresh_rate = -1;
9396

9497
log_zenith(LOG_INFO, "Daemon started as PID %d", getpid());
9598
setspid();
@@ -170,17 +173,17 @@ int main(int argc, char* argv[]) {
170173

171174
// Only fetch gamestart when user not in-game
172175
// prevent overhead from dumpsys commands.
173-
if (!gamestart) {
174-
gamestart = get_gamestart();
176+
if (!gamestart) {
177+
gamestart = get_gamestart(&opts);
175178
} else if (game_pid != 0 && kill(game_pid, 0) == -1) [[clang::unlikely]] {
176179
log_zenith(LOG_INFO, "Game %s exited, resetting profile...", gamestart);
177180
game_pid = 0;
178181
free(gamestart);
179-
gamestart = get_gamestart();
182+
gamestart = get_gamestart(&opts);
180183
// Force profile recheck to make sure new game session get boosted
181184
need_profile_checkup = true;
182185
}
183-
186+
184187
if (gamestart)
185188
mlbb_is_running = handle_mlbb(gamestart);
186189

@@ -198,18 +201,90 @@ int main(int argc, char* argv[]) {
198201
gamestart = NULL;
199202
continue;
200203
}
201-
204+
202205
cur_mode = PERFORMANCE_PROFILE;
203206
need_profile_checkup = false;
204207
log_zenith(LOG_INFO, "Applying performance profile for %s", gamestart);
205-
toast("Applying Performance Profile");
206-
set_priority(game_pid);
208+
toast("Applying Performance Profile");
209+
210+
if (IS_TRUE(opts.perf_lite_mode)) {
211+
systemv("setprop persist.sys.azenithconf.litemode 1");
212+
} else if (IS_FALSE(opts.perf_lite_mode)) {
213+
systemv("setprop persist.sys.azenithconf.litemode 0");
214+
} else {
215+
char lite_prop[PROP_VALUE_MAX] = {0};
216+
__system_property_get("persist.sys.azenithconf.cpulimit", lite_prop);
217+
if (strcmp(lite_prop, "1") == 0) {
218+
systemv("setprop persist.sys.azenithconf.litemode 1");
219+
} else {
220+
systemv("setprop persist.sys.azenithconf.litemode 0");
221+
}
222+
}
223+
224+
if (strcmp(opts.renderer, "vulkan") == 0) {
225+
systemv("sys.azenith-utilityconf setrender skiavk");
226+
} else if (strcmp(opts.renderer, "skiagl") == 0) {
227+
systemv("sys.azenith-utilityconf setrender skiagl");
228+
} else {
229+
// Do Nothing
230+
}
231+
232+
if (IS_TRUE(opts.app_priority)) {
233+
set_priority(game_pid);
234+
} else if (IS_FALSE(opts.app_priority)) {
235+
// do nothing
236+
} else {
237+
char val[PROP_VALUE_MAX] = {0};
238+
if (__system_property_get("persist.sys.azenithconf.iosched", val) > 0) {
239+
if (val[0] == '1') {
240+
set_priority(game_pid);
241+
}
242+
}
243+
}
244+
245+
if (IS_TRUE(opts.dnd_on_gaming)) {
246+
systemv("sys.azenith-utilityconf enableDND");
247+
dnd_enabled = true;
248+
} else if (IS_FALSE(opts.dnd_on_gaming)) {
249+
// do nothing
250+
} else {
251+
char dnd_state[PROP_VALUE_MAX] = {0};
252+
__system_property_get("persist.sys.azenithconf.dnd", dnd_state);
253+
if (strcmp(dnd_state, "1") == 0) {
254+
systemv("sys.azenith-utilityconf enableDND");
255+
dnd_enabled = true;
256+
}
257+
}
258+
259+
if (!IS_DEFAULT(opts.refresh_rate)) {
260+
int rr = atoi(opts.refresh_rate);
261+
262+
if (rr >= 60 && rr <= 144) {
263+
264+
if (saved_refresh_rate < 0) {
265+
saved_refresh_rate = get_current_refresh_rate();
266+
log_zenith(LOG_INFO, "Saved refresh rate: %d", saved_refresh_rate);
267+
}
268+
269+
systemv("sys.azenith-utilityconf setrefreshrates %d", rr);
270+
}
271+
272+
} else {
273+
// do nothing
274+
}
275+
207276
run_profiler(PERFORMANCE_PROFILE);
208-
209-
char preload_active[PROP_VALUE_MAX] = {0};
210-
__system_property_get("persist.sys.azenithconf.APreload", preload_active);
211-
if (strcmp(preload_active, "1") == 0) {
277+
278+
if (IS_TRUE(opts.game_preload)) {
212279
GamePreload(gamestart);
280+
} else if (IS_FALSE(opts.game_preload)) {
281+
// do nothing
282+
} else {
283+
char preload_active[PROP_VALUE_MAX] = {0};
284+
__system_property_get("persist.sys.azenithconf.APreload", preload_active);
285+
if (strcmp(preload_active, "1") == 0) {
286+
GamePreload(gamestart);
287+
}
213288
}
214289
} else if (is_initialize_complete && get_low_power_state()) {
215290
// Bail out if we already on powersave profile
@@ -220,8 +295,24 @@ int main(int argc, char* argv[]) {
220295
need_profile_checkup = false;
221296
log_zenith(LOG_INFO, "Applying ECO Mode");
222297
toast("Applying Eco Mode");
223-
run_profiler(ECO_MODE);
224-
298+
char renderer[PROP_VALUE_MAX] = {0};
299+
__system_property_get("persist.sys.azenithconf.renderer", renderer);
300+
if (strcmp(renderer, "vulkan") == 0) {
301+
systemv("sys.azenith-utilityconf setrender skiavk");
302+
303+
} else {
304+
systemv("sys.azenith-utilityconf setrender skiagl");
305+
306+
}
307+
if (saved_refresh_rate > 0) {
308+
systemv("sys.azenith-utilityconf setrefreshrates %d", saved_refresh_rate);
309+
saved_refresh_rate = -1;
310+
}
311+
if (dnd_enabled) {
312+
systemv("sys.azenith-utilityconf disableDND");
313+
dnd_enabled = false;
314+
}
315+
run_profiler(ECO_MODE);
225316
} else {
226317
// Bail out if we already on normal profile
227318
if (cur_mode == BALANCED_PROFILE)
@@ -230,7 +321,24 @@ int main(int argc, char* argv[]) {
230321
cur_mode = BALANCED_PROFILE;
231322
need_profile_checkup = false;
232323
log_zenith(LOG_INFO, "Applying Balanced profile");
233-
toast("Applying Balanced profile");
324+
toast("Applying Balanced profile");
325+
char renderer[PROP_VALUE_MAX] = {0};
326+
__system_property_get("persist.sys.azenithconf.renderer", renderer);
327+
if (strcmp(renderer, "vulkan") == 0) {
328+
systemv("sys.azenith-utilityconf setrender skiavk");
329+
330+
} else {
331+
systemv("sys.azenith-utilityconf setrender skiagl");
332+
333+
}
334+
if (saved_refresh_rate > 0) {
335+
systemv("sys.azenith-utilityconf setrefreshrates %d", saved_refresh_rate);
336+
saved_refresh_rate = -1;
337+
}
338+
if (dnd_enabled) {
339+
systemv("sys.azenith-utilityconf disableDND");
340+
dnd_enabled = false;
341+
}
234342
if (!is_initialize_complete) {
235343
notify("AZenith is running successfully");
236344
is_initialize_complete = true;

jni/src/AZenith_profiler.c

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,77 @@ void run_profiler(const int profile) {
5252
* listed in Gamelist.
5353
* Note : Caller is responsible for freeing the returned string.
5454
***********************************************************************************/
55-
char* get_gamestart(void) {
55+
char* get_gamestart(GameOptions* options) {
5656
char* pkg = get_visible_package();
57-
if (!pkg)
58-
return NULL;
57+
if (!pkg) return NULL;
5958

6059
FILE* fp = fopen(GAMELIST, "r");
6160
if (!fp) {
6261
free(pkg);
6362
return NULL;
6463
}
6564

66-
char buf[32768];
67-
size_t n = fread(buf, 1, sizeof(buf) - 1, fp);
65+
fseek(fp, 0, SEEK_END);
66+
long size = ftell(fp);
67+
fseek(fp, 0, SEEK_SET);
68+
69+
if (size <= 0) {
70+
fclose(fp);
71+
free(pkg);
72+
return NULL;
73+
}
74+
75+
char* buf = malloc(size + 1);
76+
if (!buf) {
77+
fclose(fp);
78+
free(pkg);
79+
return NULL;
80+
}
81+
82+
if (fread(buf, 1, size, fp) != (size_t)size) {
83+
fclose(fp);
84+
free(buf);
85+
free(pkg);
86+
return NULL;
87+
}
6888
fclose(fp);
89+
buf[size] = '\0';
6990

70-
if (n == 0) {
91+
// Find package key in JSON
92+
char* entry = strstr(buf, pkg);
93+
if (!entry) {
94+
free(buf);
7195
free(pkg);
7296
return NULL;
7397
}
7498

75-
buf[n] = '\0';
99+
// Fill options
100+
if (options) {
101+
char* p;
76102

77-
char* token = strtok(buf, "|");
78-
while (token) {
79-
if (strcmp(token, pkg) == 0) {
80-
return pkg;
81-
}
82-
token = strtok(NULL, "|");
103+
p = strstr(entry, "\"perf_lite_mode\":");
104+
extract_string_value(options->perf_lite_mode, p, sizeof(options->perf_lite_mode));
105+
106+
p = strstr(entry, "\"dnd_on_gaming\":");
107+
extract_string_value(options->dnd_on_gaming, p, sizeof(options->dnd_on_gaming));
108+
109+
p = strstr(entry, "\"app_priority\":");
110+
extract_string_value(options->app_priority, p, sizeof(options->app_priority));
111+
112+
p = strstr(entry, "\"game_preload\":");
113+
extract_string_value(options->game_preload, p, sizeof(options->game_preload));
114+
115+
p = strstr(entry, "\"refresh_rate\":");
116+
extract_string_value(options->refresh_rate, p, sizeof(options->refresh_rate));
117+
118+
p = strstr(entry, "\"renderer\":");
119+
extract_string_value(options->renderer, p, sizeof(options->renderer));
83120
}
84-
121+
122+
free(buf);
123+
char* ret_pkg = strdup(pkg);
85124
free(pkg);
86-
return NULL;
125+
return ret_pkg;
87126
}
88127

89128
/***********************************************************************************

0 commit comments

Comments
 (0)