1 /* strobe.c (<~Harvie 2oo9)
2 * Dream Machine implementation for GNU/Linux
3 * usage ./strobe [frequency]
17 void signal_handler(int signo
) {
18 if(signo
== SIGTERM
|| signo
== SIGINT
|| signo
== SIGQUIT
) {
19 fputs("\033[0m" "\033[2J" "\033[0;0H", stdout
); fflush(stdout
); //restore default terminal settings
24 int main(int argc
, char *argv
[]) {
26 char bg_black
[] = "\033[40m\033[1;37m"; //set black background
27 char bg_white
[] = "\033[47m\033[1;30m"; //set white background
28 char cls
[] = "\033[2J" "\033[0;0H"; //clear screen and return to begining of it
29 char format_str
[] = "%s%s" //"\033[40m\033[1;37m"
30 "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)";
31 double frequency
= 10; //Hz
36 frequency
= strtod(argv
[1], NULL
); //get frequency from argv[1] (if present)
37 cache_size
+= sizeof(argv
[1]);
39 useconds_t half_interval
= (1000000/frequency
)/2; //uSeconds
41 //Pre-cache output strings
42 cache_size
+= sizeof(bg_black
) + sizeof(cls
) + sizeof(format_str
) + 5;
43 char state_a
[cache_size
];
44 char state_b
[cache_size
];
46 sprintf(state_a
, format_str
, bg_black
, cls
, frequency
);
47 sprintf(state_b
, format_str
, bg_white
, cls
, frequency
);
50 signal(SIGINT
, signal_handler
); signal(SIGTERM
, signal_handler
); signal(SIGQUIT
, signal_handler
);
54 fputs(state_a
, stdout
);
56 usleep(half_interval
);
59 int fflags = fcntl(fileno(stdin), F_GETFL, 0);
60 fcntl(fileno(stdin), F_SETFL, fflags | O_NONBLOCK);
61 while(getchar()) puts(".");
62 fcntl(fileno(stdin), F_SETFL, fflags);
64 //WARNING! Somehow implement FILE_FLAG_OVERLAPPED & FILE_FLAG_NO_BUFFERING support on windows
67 if(argc
> 2) getchar(); //interactive strobe
68 fputs(state_b
, stdout
);
70 usleep(half_interval
);
This page took 0.699614 seconds and 4 git commands to generate.