#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "IOPMPrivate.h" using namespace std; //----------------------------------,, //=global level preprocess defs======// #define APP_NAME "ResDaemon" // #define VERSION 0.1 // #define AUTHOR "nawcom" // #define CONTACT "nawcom@nawcom.com" // //----------------------------------'' //-------------------------,, //=global level enumerators=// enum mode_type // { // background = 0, // verbose, // help // }; // //-------------------------'' //---------------------------------------------------------,, //=global level postprocess constants=======================// const int SYSTEM_ERROR = 1; // const int ARG_ERROR = 2; // const mode_type DEFAULT_MODE = background; // const float DEFAULT_SLEEP = 0.2; // char * DEFAULT_PATH = "/private/tmp/ResDaemon.log"; // char * TEMP_LOG = "/private/tmp/resolution"; // //---------------------------------------------------------'' //------------------------------------------------------------------------------,, //=Deez be mah functions yo!=====================================================// int check_args(mode_type mode, float sleep_seconds, char* log_location); // // ^-- verify that arguments are legit values // void do_main_loop(mode_type mode, float sleep_seconds); // // ^-- main daemon function // int open_log(char* log_location); // // ^-- verify log location is readable/writable // string get_time(string old); // // ^-- attach current date/time to the beginning of a string inside () // int check_binaries(char* log_location, ifstream& write_log, ofstream& log_file); // // ^-- verify that these assholes aren't using fucked up versions of cscreen// void update_cscreen(); // // ^-- launch cscreen, update log // void get_res(int& width, int& height, int& color); // // ^-- grab current resolution from log // void write_to_log(string str, int time, int in_or_out) // // string itostr(int i); // // ^-- convert integer to string // void sleepwake(); // // ^-- sleep and then wake up that fucking screen yo // string home(char * s); // void error(); // // ^-- print out command line info // //------------------------------------------------------------------------------'' string home(char * s) { string strhome= ""; string::iterator it; if (s[0] == '~') { system("echo $HOME > /private/tmp/HOME"); //FILE * read; ifstream infile; //fseek (read , 0 , SEEK_END); //long lsize = ftell (read); //rewind (read); //char *buffer = (char*) malloc (sizeof(char)*lsize); //read = fopen ("/private/tmp/HOME","w+"); infile.open("/private/tmp/HOME"); getline(infile, strhome); //fscanf(read, "%s", strhome); //strhome = fread (buffer,1,lsize,read); infile.close(); //free(read); //fscan //free(buffer); // system("rm /private/tmp/HOME"); string ss = string(s); it=ss.begin(); ss.erase(it); strhome.append(ss); //ss.replace(0,1,strhome); return strhome; } return s; } string itostr(int i) { string s = ""; if (i == 0) { s = "0"; return s; } if (i < 0) { s += '-'; i = -i; } int count = log10(i); while (count >= 0) { s += ('0' + i/pow(10.0, count)); i -= static_cast(i/pow(10.0,count)) * static_cast(pow(10.0,count)); count--; } return s; } void write_to_log(string str, int time, int in_or_out) { string old_str; if (time > 0) { str = get_time(str); } if (in_or_out > 0) { old_str = str; str = "echo '"; str.append(old_str); str.append("' >> "); str.append(DEFAULT_PATH); const char * run_str = str.c_str(); system(run_str); } else { cout << str << '\n'; } return; } void get_res(int& width, int& height, int& color) { int temp; ifstream file; file.open(TEMP_LOG); file >> temp >> temp >> width >> height >> color; file.close(); return; } int check_binaries(char* log_location, ifstream& write_log, ofstream& log_file) { string temp; string cscreen_check, cscreen_md5 = "b1ed80be7b150bedaea9d417df6bd476"; string results; size_t cscreen_char_count; size_t results_char_count; write_log.open(log_location); string cscreen_write = "/sbin/md5 /usr/bin/cscreen > "; cscreen_write.append(log_location); cout << "\nResDaemon v." << VERSION << " by nawcom\n"; cout << setfill('=') << setw(27) << "\n\n"; const char * cscreen_write_final = cscreen_write.c_str(); system(cscreen_write_final); getline(write_log, cscreen_check); cscreen_char_count = cscreen_check.size(); cscreen_check = cscreen_check.substr(int(cscreen_char_count)-32, 32); cout << "cscreen MD5: " << cscreen_check << endl; cout << "Correct MD5: " << cscreen_md5 << endl << endl; if (cscreen_check != cscreen_md5) { write_to_log("CHECKSUM BAD! Quiting..",0,0); write_to_log("CHECKSUM BAD! Quiting..",0,1); return SYSTEM_ERROR; } write_to_log("Checksum Good. Starting up daemon..",1,0); write_to_log("Checksum Good. Starting up daemon..",1,1); return 0; } void sleepwake() { kern_return_t kernel_retval; CFTypeRef reg_object; io_registry_entry_t reg_entry; reg_entry = IORegistryEntryFromPath(kIOMasterPortDefault, kIOServicePlane ":/IOResources/IODisplayWrangler"); reg_object = CFRetain(kCFBooleanTrue); SInt32 count_val = 1000 * strtol("1", 0, 0); reg_object = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &count_val); kernel_retval = IORegistryEntrySetCFProperty(reg_entry, CFSTR("IORequestIdle"), reg_object); write_to_log("Clap OFF! \*CLAPCLAPCLAP\*",0,0); write_to_log("Clap OFF! \*CLAPCLAPCLAP\*",0,1); count_val = 1000 * strtol("0", 0, 0); reg_object = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &count_val); usleep(100000); kernel_retval = IORegistryEntrySetCFProperty(reg_entry, CFSTR("IORequestIdle"), reg_object); write_to_log("Clap ON! \*CLAPCLAPCLAP\*",0,0); write_to_log("Clap ON! \*CLAPCLAPCLAP\*",0,1); CFRelease(reg_object); IOObjectRelease(reg_entry); usleep(1000000); CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)55, true); CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)55, false); return; } void update_cscreen() { string command = "/usr/bin/cscreen -l > "; command.append(TEMP_LOG); const char * first_command = command.c_str(); system(first_command); return; } string get_time(string old) { string::iterator it; string string_time; time_t current_time; time (¤t_time); string_time = "("; string_time.append(ctime(¤t_time)); int time_count = string_time.size(); it=string_time.begin()+time_count; string_time.erase (it-1, it); string_time.append(") "); string_time.append(old); return string_time; } int open_log(char* log_location) { cout << "\nLog file location: " << log_location; FILE * file; cout << '.'; file = fopen(log_location, "w+"); cout << '.'; if (!file) { cout << '.'; cout << " BAD!\n\nFile access failed!!\n"; return SYSTEM_ERROR; } cout << '.'; cout << " GOOD!\n"; fclose(file); return 0; } int check_args(mode_type mode, float sleep_seconds, char* log_location) { if (mode != help) { if (sleep_seconds < DEFAULT_SLEEP) { cout << " must be greater or equal to " << DEFAULT_SLEEP << ". Try again.\n"; return ARG_ERROR; } else if ((static_cast(mode) < static_cast(DEFAULT_MODE)) || (static_cast(mode) > static_cast(help))) { //cout << int(mode) << '\n'; cout << " must be -[b]ackground, -[v]erbose, or -[h]elp\n"; return ARG_ERROR; } else if ((open_log(log_location)) == SYSTEM_ERROR) { cout << "\nYou don't have write access to that log location.\n" << "Pick a different location or leave it blank for\n" << "the default (${HOME}/Library/Logs/ResDaemon.log)\n"; return ARG_ERROR; } return 0; } return 1; } void error() { cout << "\nResDaemon [-b(ackground)|-v(erbose)|-h(elp)] [-s ${sleep seconds > 0.1}] [-f ${log file location}]\n\n"; return; } void do_main_loop(mode_type mode, float sleep_seconds) { int new_width, new_height, new_color; int width, height, color; int done = 0; int last_set_status = -1; update_cscreen(); get_res(width, height, color); string current_val; string start_val = "Initial resolution: "; start_val.append(itostr(width)); start_val.append("x"); start_val.append(itostr(height)); start_val.append("@"); start_val.append(itostr(color)); if (mode == verbose) { write_to_log(start_val, 1, 0); } write_to_log(start_val,1 ,1); while ( done == 0 ) //{ //do { update_cscreen(); usleep(1000000 * sleep_seconds); get_res(new_width, new_height, new_color); if ((new_width != width) || (new_height != height) || (new_color != color)) { sleepwake(); width = new_width; height = new_height; color = new_color; system("killall Finder"); current_val = "New resolution: "; current_val.append(itostr(width)); current_val.append("x"); current_val.append(itostr(height)); current_val.append("@"); current_val.append(itostr(color)); write_to_log(current_val, 1, 0); write_to_log(current_val, 1, 1); } else { if (mode == verbose) { current_val = "Current resolution: "; current_val.append(itostr(width)); current_val.append("x"); current_val.append(itostr(height)); current_val.append("@"); current_val.append(itostr(color)); write_to_log(current_val, 1, 0); write_to_log(current_val, 1, 1); } } } return; } int main (int argc, char * const argv[]) { ofstream logfile; ifstream writelog; mode_type mode = DEFAULT_MODE; char* old_log_location = DEFAULT_PATH; float sleep_seconds = DEFAULT_SLEEP; int error_c = 0; char current_arg[32]; for (int i = 1; i < argc; i++) { strcpy(current_arg, argv[i]); if (current_arg[0] == '-') { if (current_arg[1] == 'v') { mode = verbose; continue; } if (current_arg[1] == 'b') { mode = background; continue; } if (current_arg[1] == 'h') { mode = help; break; } if (current_arg[1] == 's') { i++; sleep_seconds = atof(argv[i]); continue; } if (current_arg[1] == 'f') { i++; old_log_location = argv[i]; continue; } } } /**** taking a constant reference char array, altering its values, and returning it to another constant reference char array is like sucking Bill Gates' little white cock: */ string tmp_l = string(old_log_location); const char * t = tmp_l.c_str(); char * log_location; strcpy(log_location,t); /*...it sure ain't fun, but someone's got to do it. ****/ error_c = check_args(mode, sleep_seconds, log_location); if (error_c == 0) { error_c = check_binaries(log_location, writelog, logfile); if (error_c == 0) { do_main_loop(mode, sleep_seconds); return 0; } } error(); return 1; }