/* * screensaver calls proxy for MythTV. * Solves the problem of jerky video when remote is in use * by limiting the frequency of calls to gnome-screensaver-command * to no more than one a minute. * * Copyright (c) 2009 by Vitaly Blinov, vitaly@blinnov.com * 12 Jun 2009 * * This software is distributed under GPL v2 and later as is. * No warranties whatsoever. * * * Usage: * 1. execute "which gnome-screensaver-comand" * If output is different from what you find below in * DEF_PROGRAM, modify this macro accordingly. * * 2. build the program: * g++ myth-screensaver-proxy.cpp -o gnome-screensaver-command * * 3. Find what directory original gnome-screensaver-command is in * (/usr/bin/ by default). * * 4. Look at your PATH settings (printenv | grep PATH). * * 5. copy binary buld on step 2 to any of the directories that * come in PATH before original gnome-screensaver-command directory. * */ #include #include #include #include // NOTE - You may want to modify this line if // gnome-screensaver-command is installed in // different directory on your machine #define DEF_PROGRAM "/usr/bin/gnome-screensaver-command" // NOTE - DON'T thouch this please #define TMP_PATH "/tmp/screensaverPoke234235-" #define DEF_MARGIN 60 /** * Returns timestamp file name, which includes user name * */ std::string getFileName() { char text[255]; FILE *pipe; pipe = popen("whoami", "r"); fgets(text, sizeof(text), pipe); pclose(pipe); std::stringstream sStream; sStream << TMP_PATH << text ; return sStream.str(); } /** * Stores current timestamp in a file. * */ void save() { std::cout << "Saving current timestamp " << time(0) << std::endl; std::ofstream ofs(getFileName().c_str(), std::ios_base::trunc); ofs << time(0); } /** * Looks if timestamp file exists. If so, reads timestamp from it * and compares it with current timestamp. If the difference is * bigger than DEF_MARGIN (or file does not exist), returs true; * */ bool allow_poke() { std::ifstream ifs(getFileName().c_str()); if (!ifs.is_open()) { save(); return true; } else { long tt; ifs >> tt; if (abs(time(0) - tt) > DEF_MARGIN) { save(); return true; } std::cout << "Last poke was " << abs(time(0) - tt) << " seconds ago" << std::endl; return false; } } /** * Performs screensaver poke * */ void do_poke(int argc, char* argv[]) { std::stringstream sCmd; sCmd << DEF_PROGRAM << " " ; if (argc > 1) { for (int i=1; i 1) { std::string sString(argv[1]); if (sString == "--poke" || sString=="-p") { if (allow_poke()) { do_poke(argc, argv); return 0; } else { std::cout << "Not forwarding" << std::endl; return 0; } } } do_poke(argc, argv); return 0; }