Mercurial > sci
comparison scid/theme.c @ 33:1d0ddf9e6efd
misc: general documentation
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 04 Aug 2022 16:47:10 +0200 |
parents | 081e1c258e64 |
children | 752bb1cd2dd8 |
comparison
equal
deleted
inserted
replaced
32:081e1c258e64 | 33:1d0ddf9e6efd |
---|---|
24 #include <duktape.h> | 24 #include <duktape.h> |
25 #include <jansson.h> | 25 #include <jansson.h> |
26 #include <mustache.h> | 26 #include <mustache.h> |
27 | 27 |
28 #include "log.h" | 28 #include "log.h" |
29 #include "scid.h" | |
30 #include "theme.h" | 29 #include "theme.h" |
31 #include "util.h" | 30 #include "util.h" |
32 | 31 |
33 #define SIGNATURE DUK_HIDDEN_SYMBOL("File") | 32 #define SIGNATURE DUK_HIDDEN_SYMBOL("File") |
34 | 33 |
35 static struct { | 34 static duk_context *context; |
36 duk_context *ctx; | 35 static char base[PATH_MAX]; |
37 } theme; | |
38 | 36 |
39 /* {{{ mustache support */ | 37 /* {{{ mustache support */ |
40 | 38 |
41 static void | 39 static void |
42 mch_parse_error(int code, const char *msg, unsigned int line, unsigned int col, void *data) | 40 mch_parse_error(int code, const char *msg, unsigned int line, unsigned int col, void *data) |
287 { | 285 { |
288 char *out = NULL, *dump; | 286 char *out = NULL, *dump; |
289 size_t outsz = 0; | 287 size_t outsz = 0; |
290 FILE *fp; | 288 FILE *fp; |
291 | 289 |
292 duk_get_global_string(theme.ctx, function); | 290 duk_get_global_string(context, function); |
293 | 291 |
294 if (duk_is_callable(theme.ctx, -1)) { | 292 if (duk_is_callable(context, -1)) { |
295 fp = util_open_memstream(&out, &outsz); | 293 fp = util_open_memstream(&out, &outsz); |
296 dump = util_json_dump(json); | 294 dump = util_json_dump(json); |
297 | 295 |
298 duk_push_pointer(theme.ctx, fp); | 296 duk_push_pointer(context, fp); |
299 duk_push_string(theme.ctx, dump); | 297 duk_push_string(context, dump); |
300 duk_json_decode(theme.ctx, -1); | 298 duk_json_decode(context, -1); |
301 | 299 |
302 if (duk_pcall(theme.ctx, 2) != 0) | 300 if (duk_pcall(context, 2) != 0) |
303 log_warn("theme: %s", duk_safe_to_string(theme.ctx, -1)); | 301 log_warn("theme: %s", duk_safe_to_string(context, -1)); |
304 | 302 |
305 duk_pop(theme.ctx); | 303 duk_pop(context); |
306 fclose(fp); | 304 fclose(fp); |
307 free(dump); | 305 free(dump); |
308 } else | 306 } else |
309 duk_pop(theme.ctx); | 307 duk_pop(context); |
310 | 308 |
311 if (!out) | 309 if (!out) |
312 out = util_strdup(""); | 310 out = util_strdup(""); |
313 | 311 |
314 return out; | 312 return out; |
320 assert(directory); | 318 assert(directory); |
321 | 319 |
322 const char *path; | 320 const char *path; |
323 char *data; | 321 char *data; |
324 | 322 |
325 theme.ctx = duk_create_heap_default(); | 323 util_strlcpy(base, directory, sizeof (base)); |
324 context = duk_create_heap_default(); | |
326 path = theme_path("theme.js"); | 325 path = theme_path("theme.js"); |
327 | 326 |
328 if (!(data = util_read(path))) | 327 if (!(data = util_read(path))) |
329 log_warn("theme: %s: %s", path, strerror(errno)); | 328 log_warn("theme: %s: %s", path, strerror(errno)); |
330 else { | 329 else { |
331 if (duk_peval_string(theme.ctx, data) != 0) | 330 if (duk_peval_string(context, data) != 0) |
332 log_warn("theme: %s", duk_safe_to_string(theme.ctx, -1)); | 331 log_warn("theme: %s", duk_safe_to_string(context, -1)); |
333 | 332 |
334 duk_pop(theme.ctx); | 333 duk_pop(context); |
335 duk_push_object(theme.ctx); | 334 duk_push_object(context); |
336 duk_put_function_list(theme.ctx, -1, functions); | 335 duk_put_function_list(context, -1, functions); |
337 duk_put_global_string(theme.ctx, "Scid"); | 336 duk_put_global_string(context, "Scid"); |
338 free(data); | 337 free(data); |
339 } | 338 } |
340 } | 339 } |
341 | 340 |
342 const char * | 341 const char * |
345 assert(filename); | 344 assert(filename); |
346 | 345 |
347 /* Build path to the template file. */ | 346 /* Build path to the template file. */ |
348 static _Thread_local char path[PATH_MAX]; | 347 static _Thread_local char path[PATH_MAX]; |
349 | 348 |
350 snprintf(path, sizeof (path), "%s/%s", scid.themedir, filename); | 349 snprintf(path, sizeof (path), "%s/%s", base, filename); |
351 | 350 |
352 return path; | 351 return path; |
353 } | 352 } |
354 | 353 |
355 char * | 354 char * |
375 } | 374 } |
376 | 375 |
377 void | 376 void |
378 theme_free(void) | 377 theme_free(void) |
379 { | 378 { |
380 duk_destroy_heap(theme.ctx); | 379 duk_destroy_heap(context); |
381 } | 380 } |