-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinclock.c
More file actions
281 lines (237 loc) · 5.9 KB
/
binclock.c
File metadata and controls
281 lines (237 loc) · 5.9 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright 2011, 2012 John Anthony and licensed under the GPLv3
* Copyright 2013 Cyphar (added interactive mode)
* See README.md and LICENSE files for more information
*/
#include <assert.h>
#include <getopt.h>
#include <ncurses.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define LENGTH(x) (( sizeof(x) / sizeof(x[0]) ))
/* Enums */
typedef enum Colour {
COLOUR_WHITE,
COLOUR_RED,
COLOUR_GREEN,
COLOUR_YELLOW,
COLOUR_BLUE,
COLOUR_MAGENTA,
COLOUR_CYAN,
COLOUR_BLACK,
COLOUR_LAST
} Colour;
/* Typedefs */
typedef struct ColourSet {
Colour hour;
Colour min;
Colour sec;
} ColourSet;
typedef struct Theme {
char left;
char right;
char off;
char on;
} Theme;
typedef struct Config {
Theme theme;
int themeindex;
ColourSet colourset;
int colourindex;
} Config;
typedef struct State {
int cols;
int rows;
int draw_col;
int draw_row;
} State;
/* Globals */
static bool RUNNING;
static Theme themes[] = {
{'[', ']', ' ', '*'},
{'<', '>', '0', '1'},
{'(', ')', ' ', '+'}
};
static ColourSet sets[] = {
{COLOUR_RED, COLOUR_GREEN, COLOUR_YELLOW},
{COLOUR_BLUE, COLOUR_MAGENTA, COLOUR_CYAN},
{COLOUR_RED, COLOUR_WHITE, COLOUR_BLUE}
};
/* Function Predecs */
static void draw_time(Config *conf, State *s);
static void draw_line(int x, int y, Theme *t, Colour c, int value);
static Config* handle_args(int argc, char **argv);
static void handle_sigterm(int sig);
static State* init(Config *conf);
static void mainloop(Config *conf, State *s);
static void refresh_position(State *s);
static void usage_and_exit(int status);
/* Functions */
static void
draw_time(Config *conf, State *s) {
struct tm *timeset;
time_t unixtime;
unixtime = time(NULL);
timeset = localtime(&unixtime);
refresh_position(s);
draw_line(s->draw_col, s->draw_row + 0, &conf->theme,
conf->colourset.hour, timeset->tm_hour);
draw_line(s->draw_col, s->draw_row + 2, &conf->theme,
conf->colourset.min, timeset->tm_min);
draw_line(s->draw_col, s->draw_row + 4, &conf->theme,
conf->colourset.sec, timeset->tm_sec);
}
static void
draw_line(int col, int row, Theme *t, Colour c, int value) {
static const int binlength = 8;
static const int mask = 1 << 7;
char onoff;
wmove(stdscr, row, col);
for (int i = 0; i < binlength; ++i, value = value << 1) {
if (value & mask)
onoff = t->on;
else
onoff = t->off;
wprintw(stdscr, "%c", t->left);
color_set(c, NULL);
wprintw(stdscr, "%c", onoff);
color_set(0, NULL);
wprintw(stdscr, "%c ", t->right);
}
}
static Config*
handle_args(int argc, char **argv) {
Config *conf;
int i;
int tmp;
conf = malloc(sizeof(Config));
assert(conf);
/* Initialise defaults */
conf->theme = themes[0];
conf->themeindex = 0;
conf->colourset = sets[0];
conf->colourindex = 0;
/* Actual parsing */
while( (i = getopt(argc, argv, "ht:s:")) != -1 ) {
switch(i) {
case 'h':
free(conf);
usage_and_exit(EXIT_SUCCESS);
break;
case 't':
tmp = atoi(optarg);
assert(tmp >= 0 && tmp < LENGTH(themes));
conf->theme = themes[tmp];
conf->themeindex = tmp;
break;
case 's':
tmp = atoi(optarg);
assert(tmp >= 0 && tmp < LENGTH(sets));
conf->colourset = sets[tmp];
conf->colourindex = tmp;
break;
case '?':
default:
if (strchr("t", optopt)){
fprintf(stderr, "Option -%c requires an argument\n",
optopt);
}
else
fprintf(stderr, "Unknown option: -%c\n", i);
exit(EXIT_FAILURE);
}
}
return conf;
}
static void
handle_sigterm(int sig) {
RUNNING = false;
}
static State*
init(Config *conf) {
State *s;
assert( initscr() );
clear();
start_color();
raw();
noecho();
nonl();
keypad(stdscr, true);
cbreak();
curs_set(0);
signal(SIGTERM, handle_sigterm);
s = malloc(sizeof(State));
assert(s);
refresh_position(s);
/* Colours */
for (int i = 1; i < COLOUR_LAST; ++i)
init_pair(i, i, COLOR_BLACK);
/* TODO: Colour handling */
return s;
}
static void
mainloop(Config *conf, State *s) {
timeout(1000);
while (true) {
refresh();
draw_time(conf, s);
refresh();
switch(getch()) {
case 'Q':
case 'q':
return;
case 'T':
case 't':
++conf->themeindex;
conf->themeindex %= LENGTH(themes);
conf->theme = themes[conf->themeindex];
break;
case 'C':
case 'c':
++conf->colourindex;
conf->colourindex %= LENGTH(sets);
conf->colourset = sets[conf->colourindex];
break;
default:
break;
}
}
}
static void
refresh_position(State *s) {
static int output_length = 31;
static int v_offset = 2;
int oldcols, oldrows;
oldrows = s->rows;
oldcols = s->cols;
getmaxyx(stdscr, s->rows, s->cols);
if (oldcols == s->cols && oldrows == s->rows)
return;
erase();
s->draw_col = s->cols / 2 - output_length / 2;
s->draw_row = s->rows / 2 - v_offset;
}
static void
usage_and_exit(int status) {
fprintf(stderr,
"Please see the README or man page for usage instructions.\n\
\n\
:: man binclock\n\
:: cat README.md\n");
exit(status);
}
int main(int argc, char **argv) {
Config *conf;
State *s;
conf = handle_args(argc, argv);
s = init(conf);
mainloop(conf, s);
endwin();
free(conf);
free(s);
return EXIT_SUCCESS;
}