Minor code cleanup, wrote a homebrew formula for it
This commit is contained in:
parent
2f57bbddef
commit
68f8c94466
9
Makefile
9
Makefile
|
@ -41,12 +41,9 @@ tags: $(SRC)
|
||||||
@echo [ctags]
|
@echo [ctags]
|
||||||
@ctags $(SRC)
|
@ctags $(SRC)
|
||||||
|
|
||||||
# root ownership and suid bit is set to enable non-root execution
|
|
||||||
#install: kbbutil
|
install: kbbutil
|
||||||
# @sudo rm -f $(PREFIX)/bin/kbbutil
|
@mv kbbutil $(PREFIX)/bin/kbbutil
|
||||||
# @cp -a kbbutil $(PREFIX)/bin/kbbutil
|
|
||||||
# @sudo chown root:root $(PREFIX)/bin/kbbutil
|
|
||||||
# @sudo chmod 4755 $(PREFIX)/bin/kbbutil
|
|
||||||
|
|
||||||
depend:
|
depend:
|
||||||
makedepend -Y $(SRC)
|
makedepend -Y $(SRC)
|
||||||
|
|
45
kbbutil.c
45
kbbutil.c
|
@ -10,16 +10,16 @@
|
||||||
|
|
||||||
// variable defs {{{
|
// variable defs {{{
|
||||||
io_connect_t io;
|
io_connect_t io;
|
||||||
uint64_t wtf=0;
|
uint64_t wtf=0; // I don't know what this is used for
|
||||||
//}}}
|
//}}}
|
||||||
|
|
||||||
int getKBBrightness(void) {//{{{
|
int getKBBrightness(void) {//{{{
|
||||||
kern_return_t kr;
|
kern_return_t kr;
|
||||||
uint32_t c_out=1,c_in=1;
|
uint32_t c_out=1, c_in=1;
|
||||||
uint64_t cbr;
|
uint64_t cbr;
|
||||||
kr = IOConnectCallScalarMethod(io,kGetLEDBrightnessID,&wtf,c_in,&cbr,&c_out);
|
kr = IOConnectCallScalarMethod(io,kGetLEDBrightnessID,&wtf,c_in,&cbr,&c_out);
|
||||||
if(kr==kIOReturnBusy) {fprintf(stderr,"AppleLMUController IO is busy?\n");return -1;}
|
if(kr==kIOReturnBusy) { fprintf(stderr,"AppleLMUController IO is busy?\n"); return -1; }
|
||||||
if(kr!=KERN_SUCCESS) {mach_error("IOKit error: ",kr);exit(kr);}
|
if(kr!=KERN_SUCCESS) { mach_error("IOKit error: ",kr); exit(kr); }
|
||||||
return (int)cbr;
|
return (int)cbr;
|
||||||
}//}}}
|
}//}}}
|
||||||
bool setKBBrightness(int nbr) {//{{{
|
bool setKBBrightness(int nbr) {//{{{
|
||||||
|
@ -27,8 +27,8 @@ bool setKBBrightness(int nbr) {//{{{
|
||||||
uint32_t c_out=1,c_in=2;
|
uint32_t c_out=1,c_in=2;
|
||||||
uint64_t cbr,sbr[2]={wtf,nbr};
|
uint64_t cbr,sbr[2]={wtf,nbr};
|
||||||
kr = IOConnectCallScalarMethod(io,kSetLEDBrightnessID,sbr,c_in,&cbr,&c_out);
|
kr = IOConnectCallScalarMethod(io,kSetLEDBrightnessID,sbr,c_in,&cbr,&c_out);
|
||||||
if(kr==kIOReturnBusy) {fprintf(stderr,"AppleLMUController IO is busy?\n");return false;}
|
if(kr==kIOReturnBusy) { fprintf(stderr,"AppleLMUController IO is busy?\n"); return false; }
|
||||||
if(kr!=KERN_SUCCESS) {mach_error("IOKit error: ",kr);exit(kr);}
|
if(kr!=KERN_SUCCESS) { mach_error("IOKit error: ",kr); exit(kr); }
|
||||||
return true;
|
return true;
|
||||||
}//}}}
|
}//}}}
|
||||||
|
|
||||||
|
@ -36,12 +36,35 @@ int main(int argc, char *argv []) {//{{{
|
||||||
kern_return_t kr;
|
kern_return_t kr;
|
||||||
io_service_t is;
|
io_service_t is;
|
||||||
is = IOServiceGetMatchingService(kIOMasterPortDefault,IOServiceMatching("AppleLMUController"));
|
is = IOServiceGetMatchingService(kIOMasterPortDefault,IOServiceMatching("AppleLMUController"));
|
||||||
if(!is) {fprintf(stderr,"Failed to into AppleLMUController\n");exit(1);}
|
if(!is) { fprintf(stderr,"Failed to get AppleLMUController service\n"); exit(1); }
|
||||||
kr = IOServiceOpen(is,mach_task_self(),0,&io);
|
kr = IOServiceOpen(is,mach_task_self(),0,&io);
|
||||||
IOObjectRelease(is);
|
IOObjectRelease(is);
|
||||||
if(kr!=KERN_SUCCESS) {mach_error("IOKit error: ",kr);exit(kr);}
|
if(kr!=KERN_SUCCESS) { mach_error("IOKit error: ",kr); exit(kr); }
|
||||||
if(argc==1||(argc==2&&(strcmp(argv[1],"-n")==0||strcmp(argv[1],"--numeric-only")==0))) printf(argc==2? "%i" : "Current keyboard brightness: %i\n",getKBBrightness());
|
//big argparsing block i intend to clean up later, but for now i've just formatted it more readably
|
||||||
else if(argc==2&&((atoi(argv[1])==0&&strcmp(argv[1],"0")==0)||(atoi(argv[1])>0&&atoi(argv[1])<=4091))) setKBBrightness(atoi(argv[1]));
|
if
|
||||||
else {fprintf(stderr,"Invalid argument. kbbutil must be run either with no arguments, with -n or --numeric-only, or with one argument which is a number between 0 and 4091.\n");exit(1);}
|
(argc==1 ||
|
||||||
|
(argc==2 &&
|
||||||
|
(strcmp(argv[1],"-n")==0 ||
|
||||||
|
strcmp(argv[1],"--numeric-only")==0)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
printf(argc==2? "%i\n" : "Current keyboard brightness: %i\n",getKBBrightness());
|
||||||
|
else if
|
||||||
|
(argc==2 && (
|
||||||
|
(atoi(argv[1])==0 && strcmp(argv[1],"0")==0) ||
|
||||||
|
(atoi(argv[1])> 0 && atoi(argv[1])<=4091)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
if(!setKBBrightness(atoi(argv[1]))) {
|
||||||
|
fprintf(stderr,"Error setting brightness to %i.\n",atoi(argv[1]));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fprintf(stderr,"Usage:\n\
|
||||||
|
- kbbutil : Returns current brightness\n\
|
||||||
|
- kbbutil [-n|--numeric-only] : Returns only integer brightness\n\
|
||||||
|
- kbbutil [0-4091] : Set brightness\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}//}}}
|
}//}}}
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
// prototypes {{{
|
// prototypes {{{
|
||||||
int getKBBrightness(void);
|
int getKBBrightness(void);
|
||||||
bool setKBBrightness(int);
|
bool setKBBrightness(int);
|
||||||
|
int main(int, char* []);
|
||||||
enum {
|
enum {
|
||||||
kGetSensorReadingID = 0,
|
kGetSensorReadingID = 0,
|
||||||
kGetLEDBrightnessID = 1,
|
kGetLEDBrightnessID = 1,
|
||||||
|
|
Loading…
Reference in New Issue