Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
Moe-hacker committed Feb 21, 2024
1 parent ce18b95 commit 1c90a65
Show file tree
Hide file tree
Showing 11 changed files with 945 additions and 0 deletions.
120 changes: 120 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# SPDX-License-Identifier: GPL-2.0
#
# clang-format configuration file. Intended for clang-format >= 11.
#
# For more information, see:
#
# Documentation/process/clang-format.rst
# https://clang.llvm.org/docs/ClangFormat.html
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
#
---
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false
ColumnLimit: 114514
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false


ForEachMacros:
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentGotoLabels: false
IndentPPDirectives: None
IndentWidth: 8
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 8
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true

# Taken from git's rules
PenaltyBreakAssignment: 10
PenaltyBreakBeforeFirstCallParameter: 30
PenaltyBreakComment: 10
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 10
PenaltyExcessCharacter: 100
PenaltyReturnTypeOnItsOwnLine: 60

PointerAlignment: Right
ReflowComments: true
SortIncludes: false
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatementsExceptForEachMacros
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp03
TabWidth: 8
UseTab: Always
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
brctl.zip
brctl
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
all:
$(CC) -std=gnu99 -c -o libk2v.o src/k2v.c
ar -r libk2v.a libk2v.o
rm libk2v.o
$(CC) -fPIE -static -Wl,--gc-sections -fstack-protector-all -fstack-clash-protection -mshstk -D_FORTIFY_SOURCE=3 -Wno-unused-result -O2 -std=gnu99 -Wno-gnu-zero-variadic-macro-arguments -L. -lk2v -o brctl src/brctl.c -z noexecstack -z now
strip brctl
rm libk2v.a
cp brctl module/
cd module&&zip -r ../brctl.zip .
format:
clang-format -i src/*.c
check:
clang-tidy --checks=*,-clang-analyzer-security.insecureAPI.strcpy,-altera-unroll-loops,-cert-err33-c,-concurrency-mt-unsafe,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,-readability-function-cognitive-complexity,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-bugprone-easily-swappable-parameters,-cert-err34-c,-misc-include-cleaner,-readability-identifier-length,-bugprone-signal-handler,-cert-msc54-cpp,-cert-sig30-c,-altera-id-dependent-backward-branch,-cppcoreguidelines-avoid-non-const-global-variables *.c --
33 changes: 33 additions & 0 deletions module/META-INF/com/google/android/update-binary
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/sbin/sh

#################
# Initialization
#################

umask 022

# echo before loading util_functions
ui_print() { echo "$1"; }

require_new_magisk() {
ui_print "*******************************"
ui_print " Please install Magisk v20.4+! "
ui_print "*******************************"
exit 1
}

#########################
# Load util_functions.sh
#########################

OUTFD=$2
ZIPFILE=$3

mount /data 2>/dev/null

[ -f /data/adb/magisk/util_functions.sh ] || require_new_magisk
. /data/adb/magisk/util_functions.sh
[ $MAGISK_VER_CODE -lt 20400 ] && require_new_magisk

install_module
exit 0
1 change: 1 addition & 0 deletions module/META-INF/com/google/android/updater-script
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#MAGISK
16 changes: 16 additions & 0 deletions module/br.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 注意:请将值用半角引号包裹
#
# 请设置为`ls /sys/class/backlight/`的输出内容
panel="panel0-backlight"
# 低亮度级别:0到30%
low_max="30"
# 中亮度级别:30%到60%
mid_max="60"
# 低亮度时,亮度增加到原来的1.5倍
low_inc="1.5"
# 中亮度时,亮度减小到原来的0.7倍
mid_inc="0.7"
# 高亮度时,亮度增加到原来的1.5倍
hig_inc="1.5"
# 每次增加亮度前的时间间隔,用于防止和自动亮度冲突引起的闪屏
sleep_time="1"
6 changes: 6 additions & 0 deletions module/module.prop
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id=brctl
name=brctl
version=1
versionCode=1
author=Moe Hacker
description=Auto brightness control
3 changes: 3 additions & 0 deletions module/service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MODDIR=${0%/*}
chmod 777 /data/adb/modules/brctl/brctl
/data/adb/modules/brctl/brctl &
121 changes: 121 additions & 0 deletions src/brctl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include <linux/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/limits.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <signal.h>
#if __STDC_VERSION__ < 202000L
#define bool _Bool
#define true ((_Bool) + 1u)
#define false ((_Bool) + 0u)
#endif
#include "include/k2v.h"
struct CONFIG {
int low_max;
int mid_max;
float low_inc;
float mid_inc;
float hig_inc;
int sleep_time;
char *panel;
} config = {
.low_max = 0,
.mid_max = 0,
.low_inc = 0,
.mid_inc = 0,
.hig_inc = 0,
.sleep_time = 0,
.panel = NULL,
};
void init_config(void)
{
char *conf_file = "/data/adb/modules/brctl/br.conf";
char *buf = k2v_open_file(conf_file, 4096);
config.panel = key_get_char("panel", buf);
config.low_max = key_get_int("low_max", buf);
config.mid_max = key_get_int("mid_max", buf);
config.low_inc = key_get_float("low_inc", buf);
config.mid_inc = key_get_float("mid_inc", buf);
config.hig_inc = key_get_float("hig_inc", buf);
config.sleep_time = key_get_int("sleep_time", buf);
free(buf);
}
u_int ftoi(char *path)
{
int fd = open(path, O_RDONLY);
if (fd < 0) {
exit(EXIT_FAILURE);
}
char buf[128] = { '\0' };
read(fd, buf, sizeof(buf));
return (u_int)atoi(buf);
}
void set_br(char *brctl_path, u_int br)
{
int fd = open(brctl_path, O_RDWR);
if (fd < 0) {
exit(EXIT_FAILURE);
}
char buf[128] = { '\0' };
sprintf(buf, "%u\n", br);
write(fd, buf, strlen(buf));
close(fd);
}
u_int new_br(u_int cur_br, u_int max_br)
{
u_int ret = cur_br;
int br_per = (int)(((float)cur_br / (float)max_br) * 100);
if (br_per < config.low_max) {
ret = (u_int)((float)cur_br * config.low_inc);
if (ret > max_br) {
ret = max_br;
}
return ret;
}
if (br_per < config.mid_max) {
ret = (u_int)((float)cur_br * config.mid_inc);
if (ret > max_br) {
ret = max_br;
}
return ret;
}
ret = (u_int)((float)cur_br * config.hig_inc);
if (ret > max_br) {
ret = max_br;
}
return ret;
}
void brctl_daemon()
{
init_config();
char brctl_path[PATH_MAX] = { '\0' };
char brmax_path[PATH_MAX] = { '\0' };
sprintf(brctl_path, "/sys/class/backlight/%s/brightness", config.panel);
sprintf(brmax_path, "/sys/class/backlight/%s/max_brightness", config.panel);
u_int max_br = ftoi(brmax_path);
u_int cur_br = ftoi(brctl_path);
sigset_t sigs;
sigemptyset(&sigs);
sigaddset(&sigs, SIGTTIN);
sigaddset(&sigs, SIGTTOU);
sigprocmask(SIG_BLOCK, &sigs, 0);
u_int bk_br = cur_br;
while (true) {
cur_br = ftoi(brctl_path);
if (cur_br != bk_br) {
sleep(config.sleep_time);
cur_br = ftoi(brctl_path);
bk_br = new_br(cur_br, max_br);
set_br(brctl_path, bk_br);
fflush(stdout);
}
sleep(3);
}
}
int main(void)
{
brctl_daemon();
}
57 changes: 57 additions & 0 deletions src/include/k2v.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: MIT
/*
*
* This file is part of libk2v, with ABSOLUTELY NO WARRANTY.
*
* MIT License
*
* Copyright (c) 2024 Moe-hacker
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
// Bool!!!
#if __STDC_VERSION__ < 202000L
#define bool _Bool
#define true ((_Bool) + 1u)
#define false ((_Bool) + 0u)
#endif
// Version info.
#define LIBK2V_MAJOR 0
#define LIBK2V_MINOR 1
// Warning.
extern bool k2v_stop_at_warning;
extern bool k2v_show_warning;
// Functions.
char *key_get_char(const char *key, const char *buf);
int key_get_int(const char *key, const char *buf);
float key_get_float(const char *key, const char *buf);
bool key_get_bool(const char *key, const char *buf);
int key_get_int_array(const char *key, const char *buf, int *array);
int key_get_char_array(const char *key, const char *buf, char *array[]);
int key_get_float_array(const char *key, const char *buf, float *array);
bool have_key(const char *key, const char *buf);
char *k2v_open_file(char *path, size_t bufsize);
void k2v_to_shell(const char *buf);
Loading

0 comments on commit 1c90a65

Please sign in to comment.