summaryrefslogtreecommitdiff
path: root/src/liberti.c
blob: 2bcc1756c9a54b12cf2f87a7d2ee9f4a5259348b (plain)
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
/*
 *  LiberTI - TI-like calculator designed for LibreCalc
 *  Copyright (C) 2015-2017 Delwink, LLC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, version 3 only.
 *
 *  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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <gsl/gsl_errno.h>
#include <SDL.h>
#include <SDL_image.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wordexp.h>

#include "font.h"
#include "log.h"
#include "skin.h"
#include "tibchar.h"
#include "tibfunction.h"
#include "tibvar.h"

#define VERSION_STRING "0.0.0"
#define PROG "liberti"

#define USAGE_INFO "USAGE: " PROG " [options]\n\n\
OPTIONS:\n\
\t-d\tPrints extra activity while running\n\
\t-h\tPrints this help message and exits\n\
\t-v\tPrints version info and exits\n"

#define VERSION_INFO "LiberTI " VERSION_STRING "\n\
Copyright (C) 2015-2017 Delwink, LLC\n\
License AGPLv3: GNU AGPL version 3 only <http://gnu.org/licenses/agpl.html>.\n\
This is libre software: you are free to change and redistribute it.\n\
There is NO WARRANTY, to the extent permitted by law.\n\n\
Written by David McMackins II."

#ifdef RUN_IN_PLACE
#define USER_DIR "."
#else
#define USER_DIR "~/.liberti"
#endif

#define USER_STATE_PATH "state.conf"

static Uint32
timer(Uint32 interval, void *data)
{
	SDL_Event event;
	SDL_UserEvent user;

	user.type = SDL_USEREVENT;
	user.code = 0;
	user.data1 = data;

	event.type = SDL_USEREVENT;
	event.user = user;

	SDL_PushEvent(&event);
	return interval;
}

static char *
get_conf_path(const char *path)
{
	wordexp_t w;
	int rc = wordexp(USER_DIR, &w, 0);
	if (rc)
		return NULL;

	size_t len = strlen(w.we_wordv[0]) + strlen(path) + 2;
	char *out = malloc(len * sizeof(char));
	if (out)
		snprintf(out, len, "%s/%s", w.we_wordv[0], path);

	wordfree(&w);
	return out;
}

int
main(int argc, char *argv[])
{
	bool user_dir_exists = false, state_file_exists = false;
	bool state_init = false;
	int rc = 0;
	SDL_TimerID timer_id = 0;
	SDL_Window *window = NULL;
	Skin *skin = NULL;

	char *skin_path = NULL, *state_path = NULL;
	Uint32 window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;

	if (argc > 1)
	{
		int c;
		while ((c = getopt(argc, argv, ":dfhs:v")) != -1)
		{
			switch (c)
			{
			case 'd':
				debug_mode = true;
				break;

			case 'f':
				window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
				break;

			case 'h':
				puts(USAGE_INFO);
				return 0;

			case 's':
				skin_path = optarg;
				break;

			case 'v':
				puts(VERSION_INFO);
				return 0;

			case ':':
				fprintf(stderr,
					PROG ": option -%c requires an argument\n",
					optopt);
				// SPILLS OVER!

			case '?':
				return 1;
			}
		}
	}

	gsl_set_error_handler_off();

	rc = SDL_Init(SDL_INIT_TIMER);
	if (rc)
	{
		critical("Could not initialize SDL timer: %s", SDL_GetError());
		goto end;
	}

	rc = SDL_VideoInit(NULL);
	if (rc)
	{
		critical("Could not initialize SDL video: %s", SDL_GetError());
		goto end;
	}

	rc = IMG_Init(IMG_INIT_PNG);
	if (rc != IMG_INIT_PNG)
	{
		critical("Could not initialize PNG image library: %s",
			IMG_GetError());
		rc = 1;
		goto end;
	}

	rc = font_init();
	if (rc)
		goto end;

	rc = tib_keyword_init();
	if (rc)
	{
		critical("Could not initialize TI-BASIC keyword lookup tree: Error %d",
			rc);
		goto end;
	}

	rc = tib_var_init();
	if (rc)
	{
		critical("Could not initialize default variables: Error %d",
			rc);
		goto end;
	}

	rc = tib_registry_init();
	if (rc)
	{
		critical("Could not initialize function registry: Error %d",
			rc);
		goto end;
	}

	SDL_DisplayMode display_mode;
	rc = SDL_GetCurrentDisplayMode(0, &display_mode);
	if (rc)
	{
		critical("Could not get screen information: %s",
			SDL_GetError());
		goto end;
	}

	debug("Screen resolution: %dx%d", display_mode.w, display_mode.h);

	char *user_dir_path = get_conf_path("");
	if (user_dir_path)
	{
		rc = mkdir(user_dir_path, 0755);
		free(user_dir_path);
		if (rc && EEXIST != errno)
		{
			warn("Failed to create config directory: %s",
				strerror(errno));
		}
		else
		{
			user_dir_exists = true;

			state_path = get_conf_path(USER_STATE_PATH);
			if (state_path)
			{
				rc = access(state_path, F_OK);
				if (rc)
				{
					debug("Can't open state file: %s",
						strerror(errno));
					info("Loading empty state");
				}
				else
				{
					state_file_exists = true;
					info("Loading state from %s",
						state_path);
				}
			}
		}
	}
	else
	{
		warn("Could not determine config path");
	}

	struct state state;
	rc = load_state(&state, state_file_exists ? state_path : NULL);
	if (rc)
	{
		critical("Could not initialize calculator state. Error %d",
			rc);
		goto end;
	}

	state_init = true;

	timer_id = SDL_AddTimer(500, timer, &state);
	if (!timer_id)
	{
		critical("Could not add blink timer: %s", SDL_GetError());
		goto end;
	}

	skin = open_skin(skin_path, &state, (struct point2d) { 96, 64 });
	if (!skin)
	{
		critical("Could not load skin");
		goto end;
	}

	window = SDL_CreateWindow("LiberTI " VERSION_STRING,
				SDL_WINDOWPOS_UNDEFINED,
				SDL_WINDOWPOS_UNDEFINED,
				skin->size.x, skin->size.y, window_flags);
	if (!window)
	{
		critical("Could not create window: %s", SDL_GetError());
		goto end;
	}

	for (;;)
	{
		SDL_Surface *frame = Skin_get_frame(skin);
		if (frame)
		{
			SDL_Surface *screen = SDL_GetWindowSurface(window);
			SDL_BlitScaled(frame, NULL, screen, NULL);
			SDL_FreeSurface(frame);
			SDL_UpdateWindowSurface(window);
		}

		SDL_Event event;
		SDL_WaitEvent(NULL);

		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
			case SDL_KEYDOWN:
				rc = Skin_input(skin, &event.key);
				if (rc)
					error("Error %d processing key entry",
						rc);
				break;

			case SDL_USEREVENT:
				switch (event.user.code)
				{
				case 0:
					{
						struct state *state =
							event.user.data1;
						state->blink_state =
							!state->blink_state;
					}
					break;

				default:
					break;
				}
				break;

			case SDL_QUIT:
				info("Got SDL quit event");
				goto end;

			default:
				break;
			}
		}
	}

 end:
	if (timer_id)
		SDL_RemoveTimer(timer_id);

	SDL_Quit();
	SDL_VideoQuit();
	IMG_Quit();

	font_free();
	tib_keyword_free();
	tib_registry_free();
	tib_var_free();

	if (state_init)
	{
		if (user_dir_exists && state_path)
		{
			errno = save_state(&state, state_path);
			if (errno)
				warn("Failed to save state");
		}

		state_destroy(&state);
	}

	if (state_path)
		free(state_path);

	if (skin)
		free_skin(skin);

	if (window)
		SDL_DestroyWindow(window);

	return !!rc;
}