Repository navigation
Expand file tree
/
Copy pathnixieclock-app.C
More file actions
283 lines (258 loc) · 9.39 KB
/
Copy pathnixieclock-app.C
File metadata and controls
283 lines (258 loc) · 9.39 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
282
283
/************************************************************************
nixieclock - a nixie desktop timepiece
Copyright (C) 2000,2020 Greg Ercolano, erco@seriss.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
To report bugs, create an 'issue' on the project's github page:
https://github.com/erco77/nixieclock
*************************************************************************/
#include <stdio.h> // stderr
#include <unistd.h> // fork()
#include <locale.h> // setlocale()
#include <FL/Fl.H> // Fl::error()
#include <FL/fl_ask.H> // Fl::error()
#include "NixieClock.H"
#include "version.H"
// GLOBALS
NixieClock *G_nixieclock = 0; // main window
int G_nofork = 0; // set if we shouldn't fork
void HelpAndExit() {
Fl::error(
"Nixieclock -- Version " VERSION "\n"
"(C) Copyright Greg Ercolano 2000, 2020. (GPL V3 LICENSE)\n"
"This program uses FLTK, a GUI library available from fltk.org\n"
"\n"
"USAGE\n"
" nixieclock [options]\n"
"\n"
"OPTIONS\n"
" -m # month oriented date (default, eg. 07:57 Sat Feb 07)\n"
" -d # digit date mode (eg. 07:57 07-30-01)\n"
" -D # digit date mode (eg. 07:57 07-30-2001)\n"
" -s <timestr> # use strftime(3) date format\n"
" -t # test pattern for alphanumeric display\n"
" -n # no background mode (don't fork)\n"
" -geometry <pos> # sets geometry (x windows style positions)\n"
" -c <epochtime> # counts down to epochtime\n"
" -N # no save: disables saving the current settings to ~/.nixieclock\n"
// NOT YET
// " -r cmd [args] # runs command, each line of output updates in clock\n"
" -a # above all windows (not supported on all platforms)\n"
" -b # enable window borders\n"
" -nb # disable window borders (default)\n"
" -v # enables verbose mode/debugging\n"
" -h # help\n"
"\n"
"FILES\n"
" Previous settings are saved in ~/.nixieclock\n"
"\n"
"BUGS\n"
" To report bugs, create an 'issue' on the project's github page:\n"
" https://github.com/erco77/nixieclock\n"
"\n"
"EXAMPLES\n"
" nixieclock -- (uses last set date format. Default: -m)\n"
" nixieclock -m -- 07:57 SAT FEB 07 (default)\n"
" nixieclock -d -- 07:57 07-30-01\n"
#ifdef _WIN32
" nixieclock -s %%c -- Locale specific: 01/01/00 23:59:59\n"
#else
" nixieclock -s %%c -- Locale specific: MON JAN 01 23:59:59 2000\n"
#endif
" nixieclock -s \"%%I:%%M %%p %%m/%%d/%%y\" -- 12:59 PM 12/31/00\n"
" nixieclock -s \"%%H:%%M:%%S\" -- 23:59:59 (counts seconds)\n"
" nixieclock -s \"%%H:%%M:%%S %%a %%b %%d\" -- 23:59:59 SAT DEC 31\n"
" nixieclock -s \"%%a %%b %%m %%H:%%M:%%S %%Y\" -- Unix format; MON JAN 01 23:59:59 2000\n"
" nixieclock -s \"%%a %%b %%m %%H:%%M:%%S %%Y %%Z\" -- Above + timezone; MON JAN 01 23:59:59 2000 PDT\n"
" nixieclock -geometry -0-0 -- position lower right\n"
" nixieclock -geometry -5-5 -- position 5 pixels lower right\n"
" nixieclock -geometry +0-0 -- position lower left\n"
" nixieclock -N -c `perl -e 'print time()+3600;'` -s \"BEER: %%H:%%M:%%S\"\n"
" -- 'beer o'clock' countdown timer\n"
" Sets 'countdown timer' to 1hr from now\n"
" (3600 secs=1hr)\n"
// NOT YET
// " nixieclock -r 'vmstat 3' -geom 1280x40 -- vmstat appears in clock\n"
"\n");
exit(1);
}
#define ISIT(val) if ( strcmp(argv[t], val) == 0 )
// Handle parsing our app's own arguments
int parse_args(int argc, char **argv, int &t) {
int parsed_words = 0;
ISIT("-nb") {
++t;
G_nixieclock->SetBorder(0);
parsed_words++;
} else ISIT("-b") {
++t;
G_nixieclock->SetBorder(1);
parsed_words++;
} else ISIT("-n") {
++t;
G_nofork = 1;
parsed_words++;
} else ISIT("-N") {
++t;
G_nixieclock->SetNoSave(1);
parsed_words++;
} else ISIT("-a") {
++t;
G_nixieclock->SetAbove(1);
parsed_words++;
} else ISIT("-s") {
if ( ++t >= argc ) {
fprintf(stderr, "-s: missing argument\n");
exit(1);
}
G_nixieclock->SetTimeString(argv[t]);
G_nixieclock->SetTickSpeed(0.25); // fast update (assume user specified time that includes seconds)
++t;
parsed_words += 2;
} else ISIT("-c") {
if ( ++t >= argc ) {
fprintf(stderr, "-c: missing argument (expected a time(2) value)\n");
exit(1);
}
unsigned long ulval = 0;
if ( sscanf(argv[t], "%lu", &ulval) != 1 ) {
fprintf(stderr, "-c: unexpected non-integer '%s'\n", argv[t]);
exit(1);
}
G_nixieclock->SetCountdown((time_t)ulval);
++t;
parsed_words += 2;
} else ISIT("-h") {
// 23:59
++t;
G_nixieclock->SetTimeString("%H:%M");
G_nixieclock->SetTickSpeed(29.0);
parsed_words++;
} else ISIT("-d") {
// 23:59 12/31/99
++t;
G_nixieclock->SetTimeString("%H:%M %m/%d/%y");
G_nixieclock->SetTickSpeed(29.0);
parsed_words++;
} else ISIT("-D") {
// 23:59 12/31/1999
++t;
G_nixieclock->SetTimeString("%H:%M %m/%d/%Y");
G_nixieclock->SetTickSpeed(29.0);
parsed_words++;
} else ISIT("-m") {
// 23:59 Sat Dec 31
++t;
G_nixieclock->SetTimeString("%H:%M %a %b %d");
G_nixieclock->SetTickSpeed(29.0);
parsed_words++;
} else ISIT("-t") {
++t;
G_nixieclock->SetTimeString("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%%^&*()_+`[]{}\\|;':\",./<>?");
G_nixieclock->SetTickSpeed(30.0);
parsed_words++;
} else ISIT("-v") {
++t;
G_nixieclock->SetVerbose(1);
parsed_words++;
} else ISIT("-geometry") {
// Check for, but don't parse this one.. let FLTK handle it
G_nixieclock->SetGeometryForce(1);
//parsed_words++;
}
return(parsed_words);
}
// MAIN
int main(int argc, char **argv) {
setlocale(LC_ALL, ""); // multilanguage support
// BANNER
fprintf(stderr, "\nnixieclock -- Version %s\n"
"(C) Copyright 2011,2020 Greg Ercolano (erco@seriss.com).\n"
"This code is GPL V3.\n\n", VERSION);
// PARSE HELP
for ( int t=1; t<argc; t++ ) {
// HELP
if ( strcmp(argv[t], "-h") == 0 ||
strcmp(argv[t], "-help") == 0 ||
strcmp(argv[t], "--help") == 0 ) {
HelpAndExit();
}
}
// MAKE MAIN WINDOW
// Arbitrary size/position for init;
// SetTimeString() will determine actual size/position.
//
G_nixieclock = new NixieClock(Fl::w()-30,Fl::h()-30,25,25,"nixieclock");
G_nixieclock->ForceUpperCase(1);
G_nixieclock->SetUpdateSpeed(0.50);
G_nixieclock->SetBorder(0);
// Load previously saved settings
// Then command line arguments can alter from there..
//
if ( G_nixieclock->LoadSettings() < 0 ) {
fprintf(stderr, "%s: couldn't load previous settings: %s\n",
argv[0], G_nixieclock->GetErrmsg());
fl_alert("%s: couldn't load previous settings: %s", argv[0], G_nixieclock->GetErrmsg());
}
// PARSE ARGUMENTS
// Handles both our own args and FLTK's.
// Our arguments are handled by the parse_args() function above.
//
int i = 1;
if ( Fl::args(argc, argv, i, parse_args) < argc ) {
fprintf(stderr, "%s: unknown option '%s'\n", argv[0], argv[i]);
exit(1);
}
#ifdef __APPLE__
// Do not fork on mac; we loose mach ports bindings (or whatever)
G_nofork = 1;
#else
if ( G_nofork == -1 ) G_nofork = 0;
#endif
#ifndef _WIN32
#ifndef DARWIN
// UNIX: RUN IN THE BACKGROUND
if ( ! G_nofork ) {
switch ( fork() ) {
case -1: perror("fork"); exit(1); // ERROR
case 0: break; // CHILD
default: exit(0); // PARENT DISCONNECTS
}
}
#endif /*DARWIN*/
#endif /*_WIN32*/
// SET VISUAL MODE
//
// FL_RGB prevents color map eating under X windows.
//
// FL_DOUBLE|FL_INDEX enables hardware double buffering,
// but looks bad on Indy; digit backgrounds are gray.
// Fltk does a good job emulating double buffering
// in Fl_Double_Window w/out flickering, so leave off.
//
Fl::visual(FL_RGB);
//// BAD FOR WINDOWS (causes clock to appear in upperleft until "touched")
//// G_nixieclock->free_position();
G_nixieclock->show(argc, argv);
// HACK: Need to call this again /after/ show
// for it to work on win32..
if ( G_nixieclock->GetAbove() ) {
G_nixieclock->SetAbove(1);
}
if ( G_nixieclock->SaveSettings() ) { // save as new settings
fprintf(stderr, "%s: couldn't save previous settings: %s\n",
argv[0], G_nixieclock->GetErrmsg());
fl_alert("%s: couldn't save previous settings: %s",
argv[0], G_nixieclock->GetErrmsg());
}
return(Fl::run());
}