Skip to content

Instantly share code, notes, and snippets.

@ryonsherman
Created September 16, 2012 19:38
Show Gist options
  • Save ryonsherman/3734048 to your computer and use it in GitHub Desktop.
Save ryonsherman/3734048 to your computer and use it in GitHub Desktop.
Auto-throttle CGMiner intensity on xscreensaver activiation.
#!/usr/bin/env php
<?php
$address = isset($argv[2]) ? $argv[2] : '127.0.0.1';
$port = isset($argv[3]) ? $argv[3] : 4028;
$command = isset($argv[1]) ? $argv[1] : 'summary';
if (!$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) return;
if (!socket_connect($socket, $address, $port)) return socket_close($socket);
socket_write($socket, $command, strlen($command));
$response = "";
while (true) {
$buffer = socket_read($socket, 1);
if ($buffer === false or $buffer === '' or $buffer === "\0") break;
$response .= $buffer;
}
socket_close($socket);
echo $response;
?>
#!/usr/bin/env perl
sub get_gpu_count {
my $response = `bash -c './api-command.php gpucount'`;
@response = split(/\|/, $response);
@response = split(/,/, @response[1]);
@response = split(/=/, @response[1]);
$response = @response[1];
return $response;
}
sub set_intensity {
for ($gpu = 0; $gpu < get_gpu_count(); $gpu++) {
`bash -c './api-command.php "gpuintensity|$gpu,$_[0]"'`;
}
}
my $active = 0;
open(IN, "xscreensaver-command -watch |");
while (<IN>) {
if (m/^(BLANK|LOCK)/) {
if (!$active) {
set_intensity(9);
$active = 1;
}
} elsif (m/^UNBLANK/) {
set_intensity(5);
$active = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment