Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 73 lines (64 sloc) 2.158 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/* strobe.c (<~Harvie 2oo9)
* Dream Machine implementation for GNU/Linux
* usage ./strobe [frequency]
*/

//#define __WIN32__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#ifndef __WIN32__
#include <fcntl.h>
#else
#include <windows.h>
#endif

void signal_handler(int signo) {
if(signo == SIGTERM || signo == SIGINT || signo == SIGQUIT) {
fputs("\033[0m" "\033[2J" "\033[0;0H", stdout); fflush(stdout); //restore default terminal settings
exit(0);
}
}

int main(int argc, char *argv[]) {
//Settings
char bg_black[] = "\033[40m\033[1;37m"; //set black background
char bg_white[] = "\033[47m\033[1;30m"; //set white background
char cls[] = "\033[2J" "\033[0;0H"; //clear screen and return to begining of it
char format_str[] = "%s%s" //"\033[40m\033[1;37m"
"Warning: this software can cause seizures. Use it on your own risk only! Don't forget to close your eyes! Running at %g Hz (Press ^C to exit)";
double frequency = 10; //Hz

//Compute intervals
int cache_size = 0;
if(argc > 1) {
frequency = strtod(argv[1], NULL); //get frequency from argv[1] (if present)
cache_size += sizeof(argv[1]);
}
useconds_t half_interval = (1000000/frequency)/2; //uSeconds

//Pre-cache output strings
cache_size += sizeof(bg_black) + sizeof(cls) + sizeof(format_str) + 5;
char state_a[cache_size];
char state_b[cache_size];

sprintf(state_a, format_str, bg_black, cls, frequency);
sprintf(state_b, format_str, bg_white, cls, frequency);

//Register signals
signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); signal(SIGQUIT, signal_handler);

//Main loop
while(1) {
fputs(state_a, stdout);
fflush(stdout);
usleep(half_interval);
/*
#ifndef __WIN32__
int fflags = fcntl(fileno(stdin), F_GETFL, 0);
fcntl(fileno(stdin), F_SETFL, fflags | O_NONBLOCK);
while(getchar()) puts(".");
fcntl(fileno(stdin), F_SETFL, fflags);
#else
//WARNING! Somehow implement FILE_FLAG_OVERLAPPED & FILE_FLAG_NO_BUFFERING support on windows
#endif
*/
if(argc > 2) getchar(); //interactive strobe
fputs(state_b, stdout);
fflush(stdout);
usleep(half_interval);
}
}
Something went wrong with that request. Please try again.