forked from da-luce/astroterm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
613 lines (524 loc) · 18.2 KB
/
Copy pathmain.c
File metadata and controls
613 lines (524 loc) · 18.2 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include "city.h"
#include "core.h"
#include "core_position.h"
#include "core_render.h"
#include "data/keplerian_elements.h"
#include "macros.h"
#include "parse_BSC5.h"
#include "stopwatch.h"
#include "term.h"
#include "version.h"
// Embedded data generated during build
#include "bsc5.h"
#include "bsc5_constellations.h"
#include "bsc5_names.h"
// Third party libraries
#ifdef HAVE_ARGTABLE3
#include <argtable3.h>
#elif defined(HAVE_ARGTABLE2)
#include <argtable2.h>
#else
#error "Neither argtable2 nor argtable3 is available. Please install one of them."
#endif
#include <curses.h>
#include <locale.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
static void catch_winch(int sig);
static void resize_ncurses(void);
static void resize_meta(WINDOW *win);
static void resize_main(WINDOW *win, const struct Conf *config);
static void parse_options(int argc, char *argv[], struct Conf *config);
static void convert_options(struct Conf *config);
static const char *get_timezone(const struct tm *local_time);
static void render_metadata(WINDOW *win, const struct Conf *config);
// Track if we need to resize the curses window
static volatile bool perform_resize = false;
#ifdef _WIN32
// Track console size on windows
static COORD winsize;
#endif
// Track current simulation time (UTC)
// Default to current time in dt_string_utc is NULL
static double julian_date = 0.0;
static double julian_date_start = 0.0; // Note of when we started
int main(int argc, char *argv[])
{
// Default config
struct Conf config = {
.longitude = 0.0,
.latitude = 0.0,
.dt_string_utc = NULL,
.threshold = 5.0f,
.label_thresh = 0.25f,
.fps = 24,
.speed = 1.0f,
.aspect_ratio = 0.0,
.quit_on_any = false,
.unicode = false,
.color = false,
.grid = false,
.constell = false,
.metadata = false,
};
// Parse command line args and convert to internal representations
parse_options(argc, argv, &config);
convert_options(&config);
// Time for each frame in microseconds
unsigned long dt = (unsigned long)(1.0 / config.fps * 1.0E6);
// Initialize data structs
unsigned int num_stars, num_const;
struct Entry *BSC5_entries = NULL;
struct StarName *name_table = NULL;
struct Constell *constell_table = NULL;
struct Star *star_table = NULL;
struct Planet *planet_table = NULL;
struct Moon moon_object;
int *num_by_mag = NULL;
// Track success of functions
bool s = true;
// Generated BSC5 data during build in bsc5_xxx.h:
//
// uint8_t bsc5_xxx[];
// size_t bsc5_xxx_len;
s = s && parse_entries(bsc5, bsc5_len, &BSC5_entries, &num_stars);
s = s && generate_name_table(bsc5_names, bsc5_names_len, &name_table, num_stars);
s = s && generate_constell_table(bsc5_constellations, bsc5_constellations_len, &constell_table, &num_const);
s = s && generate_star_table(&star_table, BSC5_entries, name_table, num_stars);
s = s && generate_planet_table(&planet_table, planet_elements, planet_rates, planet_extras);
s = s && generate_moon_object(&moon_object, &moon_elements, &moon_rates);
s = s && star_numbers_by_magnitude(&num_by_mag, star_table, num_stars);
if (!s)
{
// At least one of the above functions failed, exit
exit(EXIT_FAILURE);
}
// This memory is no longer needed
free(BSC5_entries);
// Terminal/System settings
setlocale(LC_ALL, ""); // Required for unicode rendering
#ifndef _WIN32
signal(SIGWINCH, catch_winch); // Capture window resizes
#endif
tzset(); // Initialize timezone information
// Ncurses initialization
ncurses_init(config.color);
// Main (projection) window
WINDOW *main_win = newwin(0, 0, 0, 0);
resize_main(main_win, &config);
// Metadata window
WINDOW *metadata_win = newwin(0, 0, 0, 0); // Position at top left
if (config.metadata)
{
resize_meta(metadata_win);
}
// Render loop
while (true)
{
struct SwTimestamp frame_begin;
sw_gettime(&frame_begin);
#ifdef _WIN32
// Use this function to catch console resizes on Windows
perform_resize = check_console_window_resize_event(&winsize);
#endif
if (perform_resize)
{
resize_ncurses();
resize_main(main_win, &config);
if (config.metadata)
{
resize_meta(metadata_win);
}
doupdate();
perform_resize = false;
}
else
{
werase(metadata_win);
werase(main_win);
}
// Update object positions
update_star_positions(star_table, num_stars, julian_date, config.latitude, config.longitude);
update_planet_positions(planet_table, julian_date, config.latitude, config.longitude);
update_moon_position(&moon_object, julian_date, config.latitude, config.longitude);
update_moon_phase(&moon_object, julian_date, config.latitude);
// Render objects
render_stars_stereo(main_win, &config, star_table, num_stars, num_by_mag);
if (config.constell)
{
render_constells(main_win, &config, &constell_table, num_const, star_table);
}
render_planets_stereo(main_win, &config, planet_table);
render_moon_stereo(main_win, &config, moon_object);
if (config.grid)
{
render_azimuthal_grid(main_win, &config);
}
else
{
render_cardinal_directions(main_win, &config);
}
// Render metadata
if (config.metadata)
{
render_metadata(metadata_win, &config);
}
// Exit if ESC or q is pressed
int ch = getch();
if (ch != ERR && (ch == 27 || ch == 'q' || config.quit_on_any))
{
break;
}
// Use double buffering to avoid flickering while updating
wnoutrefresh(main_win);
if (config.metadata)
{
wnoutrefresh(metadata_win);
}
doupdate();
// TODO: this timing scheme *should* minimize any drift or divergence
// between simulation time and realtime. Check this to make sure.
// Increment "simulation" time
const double microsec_per_day = 24.0 * 60.0 * 60.0 * 1.0E6;
julian_date += (double)dt / microsec_per_day * config.speed;
// Determine time it took to update positions and render to screen
struct SwTimestamp frame_end;
sw_gettime(&frame_end);
unsigned long long frame_time;
sw_timediff_usec(frame_end, frame_begin, &frame_time);
// If updating the frame took less time than the time between frames,
// wait the rest of the time
if (frame_time < dt)
{
sw_sleep(dt - frame_time);
}
}
// Clean up
ncurses_kill();
free_constells(constell_table, num_const);
free_stars(star_table, num_stars);
free_planets(planet_table, NUM_PLANETS);
free_moon_object(moon_object);
free_star_names(name_table, num_stars);
return EXIT_SUCCESS;
}
void print_city_name_quoted(const CityData *city, void *unused)
{
printf("\"%s\"\n", city->city_name);
}
void parse_options(int argc, char *argv[], struct Conf *config)
{
#define INCLUDE_ARG_DEFINITION_DBL0(token, short_name, long_name, datatype, glossary) \
struct arg_dbl *token = arg_dbl0(short_name, long_name, datatype, glossary);
#define INCLUDE_ARG_DEFINITION_STR0(token, short_name, long_name, datatype, glossary) \
struct arg_str *token = arg_str0(short_name, long_name, datatype, glossary);
#define INCLUDE_ARG_DEFINITION_LIT0(token, short_name, long_name, glossary) \
struct arg_lit *token = arg_lit0(short_name, long_name, glossary);
#define INCLUDE_ARG_DEFINITION_INT0(token, short_name, long_name, datatype, glossary) \
struct arg_int *token = arg_int0(short_name, long_name, datatype, glossary);
#include "arg_definitions.h"
struct arg_end *end = arg_end(20);
void *argtable[] = {latitude_arg, longitude_arg, datetime_arg, threshold_arg, label_arg, fps_arg,
speed_arg, color_arg, constell_arg, grid_arg, unicode_arg, quit_arg,
meta_arg, ratio_arg, help_arg, completions_arg,
city_arg, version_arg, end};
int nerrors = arg_parse(argc, argv, argtable);
if (help_arg->count > 0)
{
printf("View stars, planets, and more, right in your terminal! ✨🪐\n\n");
printf("Usage: astroterm [OPTION]...\n\n");
arg_print_glossary_gnu(stdout, argtable);
exit(EXIT_SUCCESS);
}
if (completions_arg->count > 0)
{
// Print bash completions
printf("# Bash completions for astroterm\n");
printf("ASTROTERM_OPTIONS=(\n");
#define INCLUDE_ARG_DEFINITION_DBL0(token, short_name, long_name, datatype, glossary) \
printf(" -%s\n", short_name); printf(" --%s\n", long_name);
#define INCLUDE_ARG_DEFINITION_STR0(token, short_name, long_name, datatype, glossary) \
printf(" -%s\n", short_name); printf(" --%s\n", long_name);
#define INCLUDE_ARG_DEFINITION_LIT0(token, short_name, long_name, glossary) \
printf(" -%s\n", short_name); printf(" --%s\n", long_name);
#define INCLUDE_ARG_DEFINITION_INT0(token, short_name, long_name, datatype, glossary) \
printf(" -%s\n", short_name); printf(" --%s\n", long_name);
#include "arg_definitions.h"
printf(")\n\n");
printf("ASTROTERM_CITIES=(\n");
iter_cities(&print_city_name_quoted, NULL);
printf(")\n");
printf("_astroterm_completions() {\n");
printf(" local word=\"${COMP_WORDS[COMP_CWORD]}\"\n");
printf(" local prev=\"${COMP_WORDS[COMP_CWORD-1]}\"\n");
printf(" case \"$prev\" in\n");
printf(" -i|--city)\n");
printf(" readarray -t _cities < <(IFS=: compgen -W \"$( printf '%%q:' \"${ASTROTERM_CITIES[@]}\" )\" -- \"${word}\")\n");
printf(" COMPREPLY=()\n");
printf(" for city in \"${_cities[@]}\"; do\n");
printf(" printf -v city_esc '%%q ' \"$city\"\n");
printf(" COMPREPLY+=(\"$city_esc\")\n");
printf(" done\n");
printf(" ;;\n");
printf(" *)\n");
printf(" COMPREPLY=( $(compgen -W \"${ASTROTERM_OPTIONS[*]}\" -- \"${word}\") )\n");
printf(" ;;\n");
printf(" esac\n");
printf("}\n");
printf("complete -o nospace -F _astroterm_completions astroterm\n");
exit(EXIT_SUCCESS);
}
if (nerrors > 0)
{
arg_print_errors(stderr, end, argv[0]);
printf("Try '--help' for more information.\n");
exit(EXIT_FAILURE);
}
if (version_arg->count > 0)
{
printf("%s %s\n", PROJ_NAME, PROJ_VERSION);
exit(EXIT_SUCCESS);
}
if (latitude_arg->count > 0)
{
config->latitude = latitude_arg->dval[0];
if (config->latitude < -90 || config->latitude > 90)
{
fprintf(stderr, "ERROR: Latitude out of range [-90°, 90°]\n");
exit(EXIT_FAILURE);
}
}
if (longitude_arg->count > 0)
{
config->longitude = longitude_arg->dval[0];
if (config->longitude < -180 || config->longitude > 180)
{
fprintf(stderr, "ERROR: Longitude out of range [-180°, 180°]\n");
exit(EXIT_FAILURE);
}
}
if (datetime_arg->count > 0)
{
config->dt_string_utc = datetime_arg->sval[0];
}
if (threshold_arg->count > 0)
{
config->threshold = (float)threshold_arg->dval[0];
}
if (label_arg->count > 0)
{
config->label_thresh = (float)label_arg->dval[0];
}
if (fps_arg->count > 0)
{
config->fps = fps_arg->ival[0];
if (config->fps < 1)
{
fprintf(stderr, "ERROR: FPS must be greater than or equal to 1\n");
exit(EXIT_FAILURE);
}
}
if (speed_arg->count > 0)
{
config->speed = (float)speed_arg->dval[0];
}
if (color_arg->count > 0)
{
config->color = true;
}
if (constell_arg->count > 0)
{
config->constell = true;
}
if (meta_arg->count > 0)
{
config->metadata = true;
}
if (grid_arg->count > 0)
{
config->grid = true;
}
if (unicode_arg->count > 0)
{
config->unicode = true;
}
if (quit_arg->count > 0)
{
config->quit_on_any = true;
}
if (ratio_arg->count > 0)
{
config->aspect_ratio = ratio_arg->dval[0];
}
if (city_arg->count > 0)
{
const char *city_name = city_arg->sval[0];
CityData *city = get_city(city_name);
if (!city)
{
fprintf(stderr, "ERROR: Could not find city \"%s\"\n", city_name);
exit(EXIT_FAILURE);
}
config->latitude = city->latitude;
config->longitude = city->longitude;
free_city(city);
}
// Free Argtable resources
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
}
void convert_options(struct Conf *config)
{
// Convert longitude and latitude to radians
config->longitude *= M_PI / 180.0;
config->latitude *= M_PI / 180.0;
// Convert Gregorian calendar date to Julian date
if (config->dt_string_utc == NULL)
{
// Set julian date to current time
julian_date_start = current_julian_date();
julian_date = julian_date_start;
}
else
{
struct tm datetime;
bool parse_success = string_to_time(config->dt_string_utc, &datetime);
if (!parse_success)
{
printf("ERROR: Unable to parse datetime string '%s'\nDatetimes "
"must be in form <yyyy-mm-ddThh:mm:ss>\n",
config->dt_string_utc);
exit(EXIT_FAILURE);
}
julian_date_start = datetime_to_julian_date(&datetime);
julian_date = julian_date_start;
}
return;
}
void catch_winch(int sig)
{
(void)sig;
perform_resize = true;
}
void resize_ncurses(void)
{
// Resize ncurses internal terminal
int y;
int x;
term_size(&y, &x);
#ifdef _WIN32
resize_term(winsize.Y, winsize.X);
#else
resize_term(y, x);
#endif
}
void resize_main(WINDOW *win, const struct Conf *config)
{
// Clear the window before resizing
werase(win);
#ifndef _WIN32
wnoutrefresh(win);
#endif
// Check cell ratio
float aspect;
if (config->aspect_ratio)
{
aspect = config->aspect_ratio;
}
else
{
aspect = get_cell_aspect_ratio();
}
// Resize/position application window
win_resize_square(win, aspect);
win_position_center(win);
#ifdef _WIN32
wnoutrefresh(win);
#endif
}
void resize_meta(WINDOW *win)
{
// Clear the window before resizing
werase(win);
#ifndef _WIN32
wnoutrefresh(win);
#endif
const int meta_lines = 6; // Allows for 6 rows
const int meta_cols = 45; // Set to allow enough room for longest line (elapsed time)
wresize(win, MIN(LINES, meta_lines), MIN(COLS, meta_cols));
#ifdef _WIN32
wnoutrefresh(win);
#endif
}
const char *get_timezone(const struct tm *local_time)
{
#ifdef _WIN32
// Windows-specific code
TIME_ZONE_INFORMATION tz_info;
GetTimeZoneInformation(&tz_info);
static char tzbuf[8];
char sign = tz_info.Bias > 0 ? '-' : '+';
long hours = labs(tz_info.Bias) / 60;
long minutes = labs(tz_info.Bias) % 60;
snprintf(tzbuf, sizeof(tzbuf), "%c%02ld:%02ld", sign, hours, minutes);
return tzbuf;
#else
// Unix-like systems (Linux/macOS) code
extern char *tzname[2]; // tzname[0] is standard, tzname[1] is DST
return local_time->tm_isdst > 0 ? tzname[1] : tzname[0];
#endif
}
void render_metadata(WINDOW *win, const struct Conf *config)
{
// Gregorian Date (local time)
// Convert sim julian date (UTC) to local time
const double JULIAN_DATE_EPOCH = 2440587.5;
time_t utc_time = (time_t)((julian_date - JULIAN_DATE_EPOCH) * 86400);
const struct tm *local_time = localtime(&utc_time);
if (local_time == NULL)
{
// Default to UTC if conversion fails
local_time = gmtime(&utc_time);
}
int year = local_time->tm_year + 1900; // tm_year is years since 1900
int month = local_time->tm_mon + 1; // tm_mon is months since January (0-11)
int day = local_time->tm_mday; // Day of the month
int hour = local_time->tm_hour; // Hour (0-23)
int minute = local_time->tm_min; // Minute (0-59)
const char *timezone = get_timezone(local_time);
mvwprintw(win, 0, 0, "Date (%s): \t%02d-%02d-%04d %02d:%02d", timezone, day, month, year, hour, minute);
// Zodiac
const char *zodiac_name = get_zodiac_sign(month, day);
const char *zodiac_symbol = get_zodiac_symbol(month, day);
if (config->unicode)
{
mvwprintw(win, 1, 0, "Zodiac: \t%s %s", zodiac_name, zodiac_symbol);
}
else
{
mvwprintw(win, 1, 0, "Zodiac: \t%s", zodiac_name);
}
// Lunar phase
double age = calc_moon_age(julian_date);
enum MoonPhase phase = moon_age_to_phase(age);
const char *lunar_phase = get_moon_phase_name(phase);
mvwprintw(win, 2, 0, "Lunar Phase: \t%s", lunar_phase);
// Lat and Lon (convert back to degrees)
int deg, min;
double sec;
decimal_to_dms(config->latitude * 180 / M_PI, °, &min, &sec);
mvwprintw(win, 3, 0, "Latitude: \t%d° %d' %.2f\"", deg, min, sec);
// Longitude
decimal_to_dms(config->longitude * 180 / M_PI, °, &min, &sec);
mvwprintw(win, 4, 0, "Longitude: \t%d° %d' %.2f\"", deg, min, sec);
// Elapsed time
int eyears, edays, ehours, emins, esecs;
elapsed_time_to_components(julian_date - julian_date_start, &eyears, &edays, &ehours, &emins, &esecs);
const char *year_label = (eyears == 1) ? " year" : "years";
const char *day_label = (edays == 1) ? " day" : "days";
// Display elapsed time with proper labels
mvwprintw(win, 5, 0, "Elapsed Time: \t%03d %s, %03d %s, %02d:%02d:%02d", eyears, year_label, edays, day_label, ehours,
emins, esecs);
return;
}