Commit | Line | Data |
---|---|---|
21c4e167 H |
1 | /* strobe.c (<~Harvie 2oo9) |
2 | * Dream Machine implementation for GNU/Linux | |
3 | * usage ./strobe [frequency] | |
4 | */ | |
5 | ||
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #include <unistd.h> | |
9 | #include <signal.h> | |
10 | ||
11 | void signal_handler(int signo) { | |
12 | if(signo == SIGTERM || signo == SIGINT || signo == SIGQUIT) { | |
13 | fputs("\033[0m" "\033[2J" "\033[0;0H", stdout); fflush(stdout); //restore default terminal settings | |
14 | exit(0); | |
15 | } | |
16 | } | |
17 | ||
18 | int main(int argc, char *argv[]) { | |
19 | //Settings | |
20 | char bg_black[] = "\033[40m\033[1;37m"; //set black background | |
21 | char bg_white[] = "\033[47m\033[1;30m"; //set white background | |
22 | char cls[] = "\033[2J" "\033[0;0H"; //clear screen and return to begining of it | |
23 | char format_str[] = "%s%s" //"\033[40m\033[1;37m" | |
24 | "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)"; | |
25 | double frequency = 10; //Hz | |
26 | ||
27 | //Compute intervals | |
28 | int cache_size = 0; | |
29 | if(argc > 1) { | |
30 | frequency = strtod(argv[1], NULL); //get frequency from argv[1] (if present) | |
31 | cache_size += sizeof(argv[1]); | |
32 | } | |
33 | useconds_t half_interval = (1000000/frequency)/2; //uSeconds | |
34 | ||
35 | //Pre-cache output strings | |
36 | cache_size += sizeof(bg_black) + sizeof(cls) + sizeof(format_str) + 5; | |
37 | char state_a[cache_size]; | |
38 | char state_b[cache_size]; | |
39 | ||
40 | sprintf(state_a, format_str, bg_black, cls, frequency); | |
41 | sprintf(state_b, format_str, bg_white, cls, frequency); | |
42 | ||
43 | //Register signals | |
44 | signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); signal(SIGQUIT, signal_handler); | |
45 | ||
46 | //Main loop | |
47 | while(1) { | |
48 | fputs(state_a, stdout); | |
49 | fflush(stdout); | |
50 | usleep(half_interval); | |
51 | fputs(state_b, stdout); | |
52 | fflush(stdout); | |
53 | usleep(half_interval); | |
54 | } | |
55 | } |