about summary refs log tree commit diff stats
path: root/src/config/theme.c
blob: dd19dc579c2dc8b44324b08a8188f73dfe614524 (plain) (blame)
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
/*
 * theme.c
 * vim: expandtab:ts=4:sts=4:sw=4
 *
 * Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
 * Copyright (C) 2019 - 2021 Michael Vetter <jubalh@iodoru.org>
 *
 * This file is part of Profanity.
 *
 * Profanity 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 3 of the License, or
 * (at your option) any later version.
 *
 * Profanity 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 Profanity.  If not, see <https://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>

#include <glib.h>

#ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
#elif HAVE_CURSES_H
#include <curses.h>
#endif

#include "common.h"
#include "log.h"
#include "config/files.h"
#include "config/theme.h"
#include "config/preferences.h"
#include "config/color.h"

static GString* theme_loc;
static GKeyFile* theme;
static GHashTable* bold_items;
static GHashTable* defaults;

static void _load_preferences(void);
static void _theme_list_dir(const gchar* const dir, GSList** result);
static GString* _theme_find(const char* const theme_name);
static gboolean _theme_load_file(const char* const theme_name);

void
theme_init(const char* const theme_name)
{
    if (!_theme_load_file(theme_name)) {
        log_error("Loading theme %s failed.", theme_name);

        if (!_theme_load_file("default")) {
            log_error("Theme initialisation failed.");
        }
    }

    defaults = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);

    // Set default colors
    g_hash_table_insert(defaults, strdup("main.text"), strdup("default"));
    g_hash_table_insert(defaults, strdup("main.text.history"), strdup("default"));
    g_hash_table_insert(defaults, strdup("main.text.me"), strdup("default"));
    g_hash_table_insert(defaults, strdup("main.text.them"), strdup("default"));
    g_hash_table_insert(defaults, strdup("main.splash"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("main.help.header"), strdup("default"));
    g_hash_table_insert(defaults, strdup("main.trackbar"), strdup("default"));
    g_hash_table_insert(defaults, strdup("error"), strdup("red"));
    g_hash_table_insert(defaults, strdup("incoming"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("mention"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("trigger"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("input.text"), strdup("default"));
    g_hash_table_insert(defaults, strdup("main.time"), strdup("default"));
    g_hash_table_insert(defaults, strdup("titlebar.text"), strdup("white"));
    g_hash_table_insert(defaults, strdup("titlebar.brackets"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("titlebar.unencrypted"), strdup("red"));
    g_hash_table_insert(defaults, strdup("titlebar.encrypted"), strdup("white"));
    g_hash_table_insert(defaults, strdup("titlebar.untrusted"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("titlebar.trusted"), strdup("white"));
    g_hash_table_insert(defaults, strdup("titlebar.online"), strdup("white"));
    g_hash_table_insert(defaults, strdup("titlebar.offline"), strdup("white"));
    g_hash_table_insert(defaults, strdup("titlebar.away"), strdup("white"));
    g_hash_table_insert(defaults, strdup("titlebar.chat"), strdup("white"));
    g_hash_table_insert(defaults, strdup("titlebar.dnd"), strdup("white"));
    g_hash_table_insert(defaults, strdup("titlebar.xa"), strdup("white"));
    g_hash_table_insert(defaults, strdup("titlebar.scrolled"), strdup("default"));
    g_hash_table_insert(defaults, strdup("statusbar.text"), strdup("white"));
    g_hash_table_insert(defaults, strdup("statusbar.brackets"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("statusbar.active"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("statusbar.current"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("statusbar.new"), strdup("white"));
    g_hash_table_insert(defaults, strdup("statusbar.time"), strdup("white"));
    g_hash_table_insert(defaults, strdup("me"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("them"), strdup("green"));
    g_hash_table_insert(defaults, strdup("receipt.sent"), strdup("red"));
    g_hash_table_insert(defaults, strdup("roominfo"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("roommention"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("roommention.term"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("roomtrigger"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("roomtrigger.term"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("online"), strdup("green"));
    g_hash_table_insert(defaults, strdup("offline"), strdup("red"));
    g_hash_table_insert(defaults, strdup("away"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("chat"), strdup("green"));
    g_hash_table_insert(defaults, strdup("dnd"), strdup("red"));
    g_hash_table_insert(defaults, strdup("xa"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("typing"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("gone"), strdup("red"));
    g_hash_table_insert(defaults, strdup("subscribed"), strdup("green"));
    g_hash_table_insert(defaults, strdup("unsubscribed"), strdup("red"));
    g_hash_table_insert(defaults, strdup("otr.started.trusted"), strdup("green"));
    g_hash_table_insert(defaults, strdup("otr.started.untrusted"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("otr.ended"), strdup("red"));
    g_hash_table_insert(defaults, strdup("otr.trusted"), strdup("green"));
    g_hash_table_insert(defaults, strdup("otr.untrusted"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("roster.header"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("roster.online"), strdup("green"));
    g_hash_table_insert(defaults, strdup("roster.offline"), strdup("red"));
    g_hash_table_insert(defaults, strdup("roster.chat"), strdup("green"));
    g_hash_table_insert(defaults, strdup("roster.away"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("roster.dnd"), strdup("red"));
    g_hash_table_insert(defaults, strdup("roster.xa"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("roster.online.active"), strdup("green"));
    g_hash_table_insert(defaults, strdup("roster.offline.active"), strdup("red"));
    g_hash_table_insert(defaults, strdup("roster.chat.active"), strdup("green"));
    g_hash_table_insert(defaults, strdup("roster.away.active"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("roster.dnd.active"), strdup("red"));
    g_hash_table_insert(defaults, strdup("roster.xa.active"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("roster.online.unread"), strdup("green"));
    g_hash_table_insert(defaults, strdup("roster.offline.unread"), strdup("red"));
    g_hash_table_insert(defaults, strdup("roster.chat.unread"), strdup("green"));
    g_hash_table_insert(defaults, strdup("roster.away.unread"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("roster.dnd.unread"), strdup("red"));
    g_hash_table_insert(defaults, strdup("roster.xa.unread"), strdup("cyan"));
    g_hash_table_insert(defaults, strdup("roster.room"), strdup("green"));
    g_hash_table_insert(defaults, strdup("roster.room.unread"), strdup("green"));
    g_hash_table_insert(defaults, strdup("roster.room.trigger"), strdup("green"));
    g_hash_table_insert(defaults, strdup("roster.room.mention"), strdup("green"));
    g_hash_table_insert(defaults, strdup("occupants.header"), strdup("yellow"));
    g_hash_table_insert(defaults, strdup("untrusted"), strdup("red"));
    g_hash_table_insert(defaults, strdup("cmd.wins.unread"), strdup("default"));

    //_load_preferences();
}

gboolean
theme_exists(const char* const theme_name)
{
    if (g_strcmp0(theme_name, "default") == 0) {
        return TRUE;
    }

    GString* new_theme_file = _theme_find(theme_name);
    if (new_theme_file == NULL) {
        return FALSE;
    }

    g_string_free(new_theme_file, TRUE);
    return TRUE;
}

gboolean
theme_load(const char* const theme_name, gboolean load_theme_prefs)
{
    color_pair_cache_reset();

    if (_theme_load_file(theme_name)) {
        if (load_theme_prefs) {
            _load_preferences();
        }
        return TRUE;
    } else {
        return FALSE;
    }
}

static gboolean
_theme_load_file(const char* const theme_name)
{
    // use default theme
    if (theme_name == NULL || strcmp(theme_name, "default") == 0) {
        if (theme) {
            g_key_file_free(theme);
        }
        theme = g_key_file_new();

        // load theme from file
    } else {
        GString* new_theme_file = _theme_find(theme_name);
        if (new_theme_file == NULL) {
            log_info("Theme does not exist \"%s\"", theme_name);
            return FALSE;
        }

        if (theme_loc) {
            g_string_free(theme_loc, TRUE);
        }
        theme_loc = new_theme_file;
        log_info("Loading theme \"%s\"", theme_name);
        if (theme) {
            g_key_file_free(theme);
        }
        theme = g_key_file_new();
        g_key_file_load_from_file(theme, theme_loc->str, G_KEY_FILE_KEEP_COMMENTS,
                                  NULL);
    }

    return TRUE;
}

GSList*
theme_list(void)
{
    GSList* result = NULL;
    gchar* themes_dir = files_get_config_path(DIR_THEMES);

    _theme_list_dir(themes_dir, &result);
    g_free(themes_dir);

#ifdef THEMES_PATH
    _theme_list_dir(THEMES_PATH, &result);
#endif

    return result;
}

void
theme_close(void)
{
    if (theme) {
        g_key_file_free(theme);
        theme = NULL;
    }
    if (theme_loc) {
        g_string_free(theme_loc, TRUE);
        theme_loc = NULL;
    }
    if (bold_items) {
        g_hash_table_destroy(bold_items);
        bold_items = NULL;
    }
    if (defaults) {
        g_hash_table_destroy(defaults);
        defaults = NULL;
    }
}

void
theme_init_colours(void)
{
    assume_default_colors(-1, -1);
    color_pair_cache_reset();
}

static void
_set_string_preference(char* prefstr, preference_t pref)
{
    if (g_key_file_has_key(theme, "ui", prefstr, NULL)) {
        gchar* val = g_key_file_get_string(theme, "ui", prefstr, NULL);
        prefs_set_string(pref, val);
        g_free(val);
    }
}

static void
_set_boolean_preference(char* prefstr, preference_t pref)
{
    if (g_key_file_has_key(theme, "ui", prefstr, NULL)) {
        gboolean val = g_key_file_get_boolean(theme, "ui", prefstr, NULL);
        prefs_set_boolean(pref, val);
    }
}

static void
_load_preferences(void)
{
    // load booleans from theme and set them to prefs
    _set_boolean_preference("beep", PREF_BEEP);
    _set_boolean_preference("flash", PREF_FLASH);
    _set_boolean_preference("splash", PREF_SPLASH);
    _set_boolean_preference("wrap", PREF_WRAP);
    _set_boolean_preference("resource.title", PREF_RESOURCE_TITLE);
    _set_boolean_preference("resource.message", PREF_RESOURCE_MESSAGE);
    _set_boolean_preference("occupants", PREF_OCCUPANTS);
    _set_boolean_preference("occupants.jid", PREF_OCCUPANTS_JID);
    _set_boolean_preference("occupants.offline", PREF_OCCUPANTS_OFFLINE);
    _set_boolean_preference("occupants.wrap", PREF_OCCUPANTS_WRAP);
    _set_boolean_preference("roster", PREF_ROSTER);
    _set_boolean_preference("roster.offline", PREF_ROSTER_OFFLINE);
    _set_boolean_preference("roster.resource", PREF_ROSTER_RESOURCE);
    _set_boolean_preference("roster.resource.join", PREF_ROSTER_RESOURCE_JOIN);
    _set_boolean_preference("roster.presence", PREF_ROSTER_PRESENCE);
    _set_boolean_preference("roster.status", PREF_ROSTER_STATUS);
    _set_boolean_preference("roster.empty", PREF_ROSTER_EMPTY);
    _set_boolean_preference("roster.wrap", PREF_ROSTER_WRAP);
    _set_boolean_preference("roster.count.zero", PREF_ROSTER_COUNT_ZERO);
    _set_boolean_preference("roster.priority", PREF_ROSTER_PRIORITY);
    _set_boolean_preference("roster.contacts", PREF_ROSTER_CONTACTS);
    _set_boolean_preference("roster.unsubscribed", PREF_ROSTER_UNSUBSCRIBED);
    _set_boolean_preference("roster.rooms", PREF_ROSTER_ROOMS);
    _set_boolean_preference("roster.rooms.server", PREF_ROSTER_ROOMS_SERVER);
    _set_boolean_preference("privileges", PREF_MUC_PRIVILEGES);
    _set_boolean_preference("presence", PREF_PRESENCE);
    _set_boolean_preference("intype", PREF_INTYPE);
    _set_boolean_preference("enc.warn", PREF_ENC_WARN);
    _set_boolean_preference("titlebar.muc.title.name", PREF_TITLEBAR_MUC_TITLE_NAME);
    _set_boolean_preference("titlebar.muc.title.jid", PREF_TITLEBAR_MUC_TITLE_JID);
    _set_boolean_preference("tls.show", PREF_TLS_SHOW);
    _set_boolean_preference("statusbar.show.name", PREF_STATUSBAR_SHOW_NAME);
    _set_boolean_preference("statusbar.show.number", PREF_STATUSBAR_SHOW_NUMBER);

    // load strings from theme and set them to prefs
    _set_string_preference("time.console", PREF_TIME_CONSOLE);
    _set_string_preference("time.chat", PREF_TIME_CHAT);
    _set_string_preference("time.muc", PREF_TIME_MUC);
    _set_string_preference("time.config", PREF_TIME_CONFIG);
    _set_string_preference("time.private", PREF_TIME_PRIVATE);
    _set_string_preference("time.xmlconsole", PREF_TIME_XMLCONSOLE);
    _set_string_preference("time.statusbar", PREF_TIME_STATUSBAR);
    _set_string_preference("time.lastactivity", PREF_TIME_LASTACTIVITY);
    _set_string_preference("statuses.console", PREF_STATUSES_CONSOLE);
    _set_string_preference("statuses.chat", PREF_STATUSES_CHAT);
    _set_string_preference("statuses.muc", PREF_STATUSES_MUC);
    _set_string_preference("console.muc", PREF_CONSOLE_MUC);
    _set_string_preference("console.private", PREF_CONSOLE_PRIVATE);
    _set_string_preference("console.chat", PREF_CONSOLE_CHAT);
    _set_string_preference("roster.by", PREF_ROSTER_BY);
    _set_string_preference("roster.order", PREF_ROSTER_ORDER);
    _set_string_preference("roster.unread", PREF_ROSTER_UNREAD);
    _set_string_preference("roster.rooms.order", PREF_ROSTER_ROOMS_ORDER);
    _set_string_preference("roster.rooms.unread", PREF_ROSTER_ROOMS_UNREAD);
    _set_string_preference("roster.rooms.pos", PREF_ROSTER_ROOMS_POS);
    _set_string_preference("roster.rooms.by", PREF_ROSTER_ROOMS_BY);
    _set_string_preference("roster.private", PREF_ROSTER_PRIVATE);
    _set_string_preference("roster.count", PREF_ROSTER_COUNT);
    _set_string_preference("roster.rooms.use.name", PREF_ROSTER_ROOMS_USE_AS_NAME);
    _set_string_preference("statusbar.self", PREF_STATUSBAR_SELF);
    _set_string_preference("statusbar.chat", PREF_STATUSBAR_CHAT);
    _set_string_preference("statusbar.room", PREF_STATUSBAR_ROOM);

    // load ints from theme and set them to prefs
    // with custom set functions
    if (g_key_file_has_key(theme, "ui", "statusbar.tabs", NULL)) {
        gint tabs_size = g_key_file_get_integer(theme, "ui", "statusbar.tabs", NULL);
        prefs_set_statusbartabs(tabs_size);
    }

    if (g_key_file_has_key(theme, "ui", "statusbar.tablen", NULL)) {
        gint tab_len = g_key_file_get_integer(theme, "ui", "statusbar.tablen", NULL);
        prefs_set_statusbartablen(tab_len);
    }

    if (g_key_file_has_key(theme, "ui", "occupants.size", NULL)) {
        gint occupants_size = g_key_file_get_integer(theme, "ui", "occupants.size", NULL);
        prefs_set_occupants_size(occupants_size);
    }

    if (g_key_file_has_key(theme, "ui", "occupants.indent", NULL)) {
        gint occupants_indent = g_key_file_get_integer(theme, "ui", "occupants.indent", NULL);
        prefs_set_occupants_indent(occupants_indent);
    }

    if (g_key_file_has_key(theme, "ui", "roster.size", NULL)) {
        gint roster_size = g_key_file_get_integer(theme, "ui", "roster.size", NULL);
        prefs_set_roster_size(roster_size);
    }

    if (g_key_file_has_key(theme, "ui", "roster.contact.indent", NULL)) {
        gint contact_indent = g_key_file_get_integer(theme, "ui", "roster.contact.indent", NULL);
        prefs_set_roster_contact_indent(contact_indent);
    }

    if (g_key_file_has_key(theme, "ui", "roster.resource.indent", NULL)) {
        gint resource_indent = g_key_file_get_integer(theme, "ui", "roster.resource.indent", NULL);
        prefs_set_roster_resource_indent(resource_indent);
    }

    if (g_key_file_has_key(theme, "ui", "roster.presence.indent", NULL)) {
        gint presence_indent = g_key_file_get_integer(theme, "ui", "roster.presence.indent", NULL);
        prefs_set_roster_presence_indent(presence_indent);
    }

    // load chars from theme and set them to prefs
    // with custom set functions
    if (g_key_file_has_key(theme, "ui", "occupants.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "occupants.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_occupants_char(ch[0]);
            g_free(ch);
        }
    }

    if (g_key_file_has_key(theme, "ui", "occupants.header.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "occupants.header.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_occupants_header_char(ch[0]);
            g_free(ch);
        }
    }

    if (g_key_file_has_key(theme, "ui", "roster.header.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "roster.header.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_header_char(ch[0]);
            g_free(ch);
        }
    }

    if (g_key_file_has_key(theme, "ui", "roster.contact.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "roster.contact.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_contact_char(ch[0]);
            g_free(ch);
        }
    }

    if (g_key_file_has_key(theme, "ui", "roster.resource.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "roster.resource.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_resource_char(ch[0]);
            g_free(ch);
        }
    } else {
        prefs_clear_roster_resource_char();
    }

    if (g_key_file_has_key(theme, "ui", "roster.rooms.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "roster.rooms.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_room_char(ch[0]);
            g_free(ch);
        }
    }

    if (g_key_file_has_key(theme, "ui", "roster.rooms.private.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "roster.rooms.private.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_room_private_char(ch[0]);
            g_free(ch);
        }
    }

    if (g_key_file_has_key(theme, "ui", "roster.private.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "roster.private.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_private_char(ch[0]);
            g_free(ch);
        }
    }

    if (g_key_file_has_key(theme, "ui", "otr.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "otr.char", NULL);
        if (ch && g_utf8_strlen(ch, 4) == 1) {
            prefs_set_otr_char(ch);
            g_free(ch);
        }
    }

    if (g_key_file_has_key(theme, "ui", "pgp.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "pgp.char", NULL);
        if (ch && g_utf8_strlen(ch, 4) == 1) {
            prefs_set_pgp_char(ch);
            g_free(ch);
        }
    }

    if (g_key_file_has_key(theme, "ui", "omemo.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "omemo.char", NULL);
        if (ch && g_utf8_strlen(ch, 4) == 1) {
            prefs_set_omemo_char(ch);
            g_free(ch);
        }
    }

    if (g_key_file_has_key(theme, "ui", "correction.char", NULL)) {
        gchar* ch = g_key_file_get_string(theme, "ui", "correction.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_correction_char(ch[0]);
            g_free(ch);
        }
    }

    // load window positions
    if (g_key_file_has_key(theme, "ui", "titlebar.position", NULL) && g_key_file_has_key(theme, "ui", "mainwin.position", NULL) && g_key_file_has_key(theme, "ui", "statusbar.position", NULL) && g_key_file_has_key(theme, "ui", "inputwin.position", NULL)) {

        int titlebar_pos = g_key_file_get_integer(theme, "ui", "titlebar.position", NULL);
        int mainwin_pos = g_key_file_get_integer(theme, "ui", "mainwin.position", NULL);
        int statusbar_pos = g_key_file_get_integer(theme, "ui", "statusbar.position", NULL);
        int inputwin_pos = g_key_file_get_integer(theme, "ui", "inputwin.position", NULL);

        ProfWinPlacement* placement = prefs_create_profwin_placement(titlebar_pos, mainwin_pos, statusbar_pos, inputwin_pos);

        prefs_save_win_placement(placement);
        prefs_free_win_placement(placement);
    }
}

static void
_theme_list_dir(const gchar* const dir, GSList** result)
{
    GDir* themes = g_dir_open(dir, 0, NULL);
    if (themes) {
        const gchar* theme = g_dir_read_name(themes);
        while (theme) {
            *result = g_slist_append(*result, strdup(theme));
            theme = g_dir_read_name(themes);
        }
        g_dir_close(themes);
    }
}

static GString*
_theme_find(const char* const theme_name)
{
    GString* path = NULL;
    gchar* themes_dir = files_get_config_path(DIR_THEMES);

    if (themes_dir) {
        path = g_string_new(themes_dir);
        g_free(themes_dir);
        g_string_append(path, "/");
        g_string_append(path, theme_name);
        if (!g_file_test(path->str, G_FILE_TEST_EXISTS)) {
            g_string_free(path, true);
            path = NULL;
        }
    }

#ifdef THEMES_PATH
    if (path == NULL) {
        path = g_string_new(THEMES_PATH);
        g_string_append(path, "/");
        g_string_append(path, theme_name);
        if (!g_file_test(path->str, G_FILE_TEST_EXISTS)) {
            g_string_free(path, true);
            path = NULL;
        }
    }
#endif /* THEMES_PATH */

    return path;
}

theme_item_t
theme_roster_unread_presence_attrs(const char* const presence)
{
    if (g_strcmp0(presence, "online") == 0) {
        return THEME_ROSTER_ONLINE_UNREAD;
    } else if (g_strcmp0(presence, "away") == 0) {
        return THEME_ROSTER_AWAY_UNREAD;
    } else if (g_strcmp0(presence, "chat") == 0) {
        return THEME_ROSTER_CHAT_UNREAD;
    } else if (g_strcmp0(presence, "dnd") == 0) {
        return THEME_ROSTER_DND_UNREAD;
    } else if (g_strcmp0(presence, "xa") == 0) {
        return THEME_ROSTER_XA_UNREAD;
    } else {
        return THEME_ROSTER_OFFLINE_UNREAD;
    }
}

theme_item_t
theme_roster_active_presence_attrs(const char* const presence)
{
    if (g_strcmp0(presence, "online") == 0) {
        return THEME_ROSTER_ONLINE_ACTIVE;
    } else if (g_strcmp0(presence, "away") == 0) {
        return THEME_ROSTER_AWAY_ACTIVE;
    } else if (g_strcmp0(presence, "chat") == 0) {
        return THEME_ROSTER_CHAT_ACTIVE;
    } else if (g_strcmp0(presence, "dnd") == 0) {
        return THEME_ROSTER_DND_ACTIVE;
    } else if (g_strcmp0(presence, "xa") == 0) {
        return THEME_ROSTER_XA_ACTIVE;
    } else {
        return THEME_ROSTER_OFFLINE_ACTIVE;
    }
}

theme_item_t
theme_roster_presence_attrs(const char* const presence)
{
    if (g_strcmp0(presence, "online") == 0) {
        return THEME_ROSTER_ONLINE;
    } else if (g_strcmp0(presence, "away") == 0) {
        return THEME_ROSTER_AWAY;
    } else if (g_strcmp0(presence, "chat") == 0) {
        return THEME_ROSTER_CHAT;
    } else if (g_strcmp0(presence, "dnd") == 0) {
        return THEME_ROSTER_DND;
    } else if (g_strcmp0(presence, "xa") == 0) {
        return THEME_ROSTER_XA;
    } else {
        return THEME_ROSTER_OFFLINE;
    }
}

theme_item_t
theme_main_presence_attrs(const char* const presence)
{
    if (g_strcmp0(presence, "online") == 0) {
        return THEME_ONLINE;
    } else if (g_strcmp0(presence, "away") == 0) {
        return THEME_AWAY;
    } else if (g_strcmp0(presence, "chat") == 0) {
        return THEME_CHAT;
    } else if (g_strcmp0(presence, "dnd") == 0) {
        return THEME_DND;
    } else if (g_strcmp0(presence, "xa") == 0) {
        return THEME_XA;
    } else {
        return THEME_OFFLINE;
    }
}

static void
_theme_prep_bgnd(char* setting, char* def, GString* lookup_str)
{
    gchar* val = g_key_file_get_string(theme, "colours", setting, NULL);
    if (!val) {
        g_string_append(lookup_str, def);
    } else {
        if (g_str_has_prefix(val, "bold_")) {
            g_string_append(lookup_str, &val[5]);
        } else {
            g_string_append(lookup_str, val);
        }
    }
    g_free(val);
}

/* return value needs to be freed */
char*
theme_get_bkgnd(void)
{
    char* val = g_key_file_get_string(theme, "colours", "bkgnd", NULL);
    return val;
}

/* gets the foreground color from the theme. or uses the one defined in 'defaults' */
static void
_theme_prep_fgnd(char* setting, GString* lookup_str, gboolean* bold)
{
    gchar* val = g_key_file_get_string(theme, "colours", setting, NULL);
    if (!val) {
        char* def = g_hash_table_lookup(defaults, setting);
        g_string_append(lookup_str, def);
    } else {
        if (g_str_has_prefix(val, "bold_")) {
            g_string_append(lookup_str, &val[5]);
            *bold = TRUE;
        } else {
            g_string_append(lookup_str, val);
            *bold = FALSE;
        }
    }
    g_free(val);
}

char*
theme_get_string(char* str)
{
    char* res = g_key_file_get_string(theme, "colours", str, NULL);
    if (!res) {
        return strdup(g_hash_table_lookup(defaults, str));
    } else {
        return res;
    }
}

void
theme_free_string(char* str)
{
    if (str) {
        g_free(str);
    }
}

int
theme_hash_attrs(const char* str)
{
    color_profile profile = COLOR_PROFILE_DEFAULT;

    char* color_pref = prefs_get_string(PREF_COLOR_NICK);
    if (strcmp(color_pref, "redgreen") == 0) {
        profile = COLOR_PROFILE_REDGREEN_BLINDNESS;
    } else if (strcmp(color_pref, "blue") == 0) {
        profile = COLOR_PROFILE_BLUE_BLINDNESS;
    }
    g_free(color_pref);

    return COLOR_PAIR(color_pair_cache_hash_str(str, profile));
}

/* returns the colours (fgnd and bknd) for a certain attribute ie main.text */
int
theme_attrs(theme_item_t attrs)
{
    int result = 0;

    GString* lookup_str = g_string_new("");
    gboolean bold = FALSE;

    // get foreground colour
    switch (attrs) {
    case THEME_TEXT:
        _theme_prep_fgnd("main.text", lookup_str, &bold);
        break;
    case THEME_TEXT_HISTORY:
        _theme_prep_fgnd("main.text.history", lookup_str, &bold);
        break;
    case THEME_TEXT_ME:
        _theme_prep_fgnd("main.text.me", lookup_str, &bold);
        break;
    case THEME_TEXT_THEM:
        _theme_prep_fgnd("main.text.them", lookup_str, &bold);
        break;
    case THEME_SPLASH:
        _theme_prep_fgnd("main.splash", lookup_str, &bold);
        break;
    case THEME_TRACKBAR:
        _theme_prep_fgnd("main.trackbar", lookup_str, &bold);
        break;
    case THEME_HELP_HEADER:
        _theme_prep_fgnd("main.help.header", lookup_str, &bold);
        break;
    case THEME_ERROR:
        _theme_prep_fgnd("error", lookup_str, &bold);
        break;
    case THEME_INCOMING:
        _theme_prep_fgnd("incoming", lookup_str, &bold);
        break;
    case THEME_MENTION:
        _theme_prep_fgnd("mention", lookup_str, &bold);
        break;
    case THEME_TRIGGER:
        _theme_prep_fgnd("trigger", lookup_str, &bold);
        break;
    case THEME_INPUT_TEXT:
        _theme_prep_fgnd("input.text", lookup_str, &bold);
        break;
    case THEME_TIME:
        _theme_prep_fgnd("main.time", lookup_str, &bold);
        break;
    case THEME_TITLE_TEXT:
        _theme_prep_fgnd("titlebar.text", lookup_str, &bold);
        break;
    case THEME_TITLE_BRACKET:
        _theme_prep_fgnd("titlebar.brackets", lookup_str, &bold);
        break;
    case THEME_TITLE_SCROLLED:
        _theme_prep_fgnd("titlebar.scrolled", lookup_str, &bold);
        break;
    case THEME_TITLE_UNENCRYPTED:
        _theme_prep_fgnd("titlebar.unencrypted", lookup_str, &bold);
        break;
    case THEME_TITLE_ENCRYPTED:
        _theme_prep_fgnd("titlebar.encrypted", lookup_str, &bold);
        break;
    case THEME_TITLE_UNTRUSTED:
        _theme_prep_fgnd("titlebar.untrusted", lookup_str, &bold);
        break;
    case THEME_TITLE_TRUSTED:
        _theme_prep_fgnd("titlebar.trusted", lookup_str, &bold);
        break;
    case THEME_TITLE_ONLINE:
        _theme_prep_fgnd("titlebar.online", lookup_str, &bold);
        break;
    case THEME_TITLE_OFFLINE:
        _theme_prep_fgnd("titlebar.offline", lookup_str, &bold);
        break;
    case THEME_TITLE_AWAY:
        _theme_prep_fgnd("titlebar.away", lookup_str, &bold);
        break;
    case THEME_TITLE_CHAT:
        _theme_prep_fgnd("titlebar.chat", lookup_str, &bold);
        break;
    case THEME_TITLE_DND:
        _theme_prep_fgnd("titlebar.dnd", lookup_str, &bold);
        break;
    case THEME_TITLE_XA:
        _theme_prep_fgnd("titlebar.xa", lookup_str, &bold);
        break;
    case THEME_STATUS_TEXT:
        _theme_prep_fgnd("statusbar.text", lookup_str, &bold);
        break;
    case THEME_STATUS_BRACKET:
        _theme_prep_fgnd("statusbar.brackets", lookup_str, &bold);
        break;
    case THEME_STATUS_ACTIVE:
        _theme_prep_fgnd("statusbar.active", lookup_str, &bold);
        break;
    case THEME_STATUS_CURRENT:
        _theme_prep_fgnd("statusbar.current", lookup_str, &bold);
        break;
    case THEME_STATUS_NEW:
        _theme_prep_fgnd("statusbar.new", lookup_str, &bold);
        break;
    case THEME_STATUS_TIME:
        _theme_prep_fgnd("statusbar.time", lookup_str, &bold);
        break;
    case THEME_ME:
        _theme_prep_fgnd("me", lookup_str, &bold);
        break;
    case THEME_THEM:
        _theme_prep_fgnd("them", lookup_str, &bold);
        break;
    case THEME_RECEIPT_SENT:
        _theme_prep_fgnd("receipt.sent", lookup_str, &bold);
        break;
    case THEME_ROOMINFO:
        _theme_prep_fgnd("roominfo", lookup_str, &bold);
        break;
    case THEME_ROOMMENTION:
        _theme_prep_fgnd("roommention", lookup_str, &bold);
        break;
    case THEME_ROOMMENTION_TERM:
        _theme_prep_fgnd("roommention.term", lookup_str, &bold);
        break;
    case THEME_ROOMTRIGGER:
        _theme_prep_fgnd("roomtrigger", lookup_str, &bold);
        break;
    case THEME_ROOMTRIGGER_TERM:
        _theme_prep_fgnd("roomtrigger.term", lookup_str, &bold);
        break;
    case THEME_ONLINE:
        _theme_prep_fgnd("online", lookup_str, &bold);
        break;
    case THEME_OFFLINE:
        _theme_prep_fgnd("offline", lookup_str, &bold);
        break;
    case THEME_AWAY:
        _theme_prep_fgnd("away", lookup_str, &bold);
        break;
    case THEME_CHAT:
        _theme_prep_fgnd("chat", lookup_str, &bold);
        break;
    case THEME_DND:
        _theme_prep_fgnd("dnd", lookup_str, &bold);
        break;
    case THEME_XA:
        _theme_prep_fgnd("xa", lookup_str, &bold);
        break;
    case THEME_TYPING:
        _theme_prep_fgnd("typing", lookup_str, &bold);
        break;
    case THEME_GONE:
        _theme_prep_fgnd("gone", lookup_str, &bold);
        break;
    case THEME_SUBSCRIBED:
        _theme_prep_fgnd("subscribed", lookup_str, &bold);
        break;
    case THEME_UNSUBSCRIBED:
        _theme_prep_fgnd("unsubscribed", lookup_str, &bold);
        break;
    case THEME_OTR_STARTED_TRUSTED:
        _theme_prep_fgnd("otr.started.trusted", lookup_str, &bold);
        break;
    case THEME_OTR_STARTED_UNTRUSTED:
        _theme_prep_fgnd("otr.started.untrusted", lookup_str, &bold);
        break;
    case THEME_OTR_ENDED:
        _theme_prep_fgnd("otr.ended", lookup_str, &bold);
        break;
    case THEME_OTR_TRUSTED:
        _theme_prep_fgnd("otr.trusted", lookup_str, &bold);
        break;
    case THEME_OTR_UNTRUSTED:
        _theme_prep_fgnd("otr.untrusted", lookup_str, &bold);
        break;
    case THEME_ROSTER_HEADER:
        _theme_prep_fgnd("roster.header", lookup_str, &bold);
        break;
    case THEME_ROSTER_ONLINE:
        _theme_prep_fgnd("roster.online", lookup_str, &bold);
        break;
    case THEME_ROSTER_OFFLINE:
        _theme_prep_fgnd("roster.offline", lookup_str, &bold);
        break;
    case THEME_ROSTER_CHAT:
        _theme_prep_fgnd("roster.chat", lookup_str, &bold);
        break;
    case THEME_ROSTER_AWAY:
        _theme_prep_fgnd("roster.away", lookup_str, &bold);
        break;
    case THEME_ROSTER_DND:
        _theme_prep_fgnd("roster.dnd", lookup_str, &bold);
        break;
    case THEME_ROSTER_XA:
        _theme_prep_fgnd("roster.xa", lookup_str, &bold);
        break;
    case THEME_ROSTER_ONLINE_ACTIVE:
        _theme_prep_fgnd("roster.online.active", lookup_str, &bold);
        break;
    case THEME_ROSTER_OFFLINE_ACTIVE:
        _theme_prep_fgnd("roster.offline.active", lookup_str, &bold);
        break;
    case THEME_ROSTER_CHAT_ACTIVE:
        _theme_prep_fgnd("roster.chat.active", lookup_str, &bold);
        break;
    case THEME_ROSTER_AWAY_ACTIVE:
        _theme_prep_fgnd("roster.away.active", lookup_str, &bold);
        break;
    case THEME_ROSTER_DND_ACTIVE:
        _theme_prep_fgnd("roster.dnd.active", lookup_str, &bold);
        break;
    case THEME_ROSTER_XA_ACTIVE:
        _theme_prep_fgnd("roster.xa.active", lookup_str, &bold);
        break;
    case THEME_ROSTER_ONLINE_UNREAD:
        _theme_prep_fgnd("roster.online.unread", lookup_str, &bold);
        break;
    case THEME_ROSTER_OFFLINE_UNREAD:
        _theme_prep_fgnd("roster.offline.unread", lookup_str, &bold);
        break;
    case THEME_ROSTER_CHAT_UNREAD:
        _theme_prep_fgnd("roster.chat.unread", lookup_str, &bold);
        break;
    case THEME_ROSTER_AWAY_UNREAD:
        _theme_prep_fgnd("roster.away.unread", lookup_str, &bold);
        break;
    case THEME_ROSTER_DND_UNREAD:
        _theme_prep_fgnd("roster.dnd.unread", lookup_str, &bold);
        break;
    case THEME_ROSTER_XA_UNREAD:
        _theme_prep_fgnd("roster.xa.unread", lookup_str, &bold);
        break;
    case THEME_ROSTER_ROOM:
        _theme_prep_fgnd("roster.room", lookup_str, &bold);
        break;
    case THEME_ROSTER_ROOM_UNREAD:
        _theme_prep_fgnd("roster.room.unread", lookup_str, &bold);
        break;
    case THEME_ROSTER_ROOM_TRIGGER:
        _theme_prep_fgnd("roster.room.trigger", lookup_str, &bold);
        break;
    case THEME_ROSTER_ROOM_MENTION:
        _theme_prep_fgnd("roster.room.mention", lookup_str, &bold);
        break;
    case THEME_OCCUPANTS_HEADER:
        _theme_prep_fgnd("occupants.header", lookup_str, &bold);
        break;
    case THEME_UNTRUSTED:
        _theme_prep_fgnd("untrusted", lookup_str, &bold);
        break;
    case THEME_CMD_WINS_UNREAD:
        _theme_prep_fgnd("cmd.wins.unread", lookup_str, &bold);
        break;
    case THEME_WHITE:
        g_string_append(lookup_str, "white");
        bold = FALSE;
        break;
    case THEME_WHITE_BOLD:
        g_string_append(lookup_str, "white");
        bold = TRUE;
        break;
    case THEME_GREEN:
        g_string_append(lookup_str, "green");
        bold = FALSE;
        break;
    case THEME_GREEN_BOLD:
        g_string_append(lookup_str, "green");
        bold = TRUE;
        break;
    case THEME_RED:
        g_string_append(lookup_str, "red");
        bold = FALSE;
        break;
    case THEME_RED_BOLD:
        g_string_append(lookup_str, "red");
        bold = TRUE;
        break;
    case THEME_YELLOW:
        g_string_append(lookup_str, "yellow");
        bold = FALSE;
        break;
    case THEME_YELLOW_BOLD:
        g_string_append(lookup_str, "yellow");
        bold = TRUE;
        break;
    case THEME_BLUE:
        g_string_append(lookup_str, "blue");
        bold = FALSE;
        break;
    case THEME_BLUE_BOLD:
        g_string_append(lookup_str, "blue");
        bold = TRUE;
        break;
    case THEME_CYAN:
        g_string_append(lookup_str, "cyan");
        bold = FALSE;
        break;
    case THEME_CYAN_BOLD:
        g_string_append(lookup_str, "cyan");
        bold = TRUE;
        break;
    case THEME_BLACK:
        g_string_append(lookup_str, "black");
        bold = FALSE;
        break;
    case THEME_BLACK_BOLD:
        g_string_append(lookup_str, "black");
        bold = TRUE;
        break;
    case THEME_MAGENTA:
        g_string_append(lookup_str, "magenta");
        bold = FALSE;
        break;
    case THEME_MAGENTA_BOLD:
        g_string_append(lookup_str, "magenta");
        bold = TRUE;
        break;
    default:
        g_string_append(lookup_str, "default");
        bold = FALSE;
        break;
    }

    g_string_append(lookup_str, "_");

    // append background str
    switch (attrs) {
    case THEME_TITLE_TEXT:
    case THEME_TITLE_BRACKET:
    case THEME_TITLE_SCROLLED:
    case THEME_TITLE_UNENCRYPTED:
    case THEME_TITLE_ENCRYPTED:
    case THEME_TITLE_UNTRUSTED:
    case THEME_TITLE_TRUSTED:
    case THEME_TITLE_ONLINE:
    case THEME_TITLE_OFFLINE:
    case THEME_TITLE_AWAY:
    case THEME_TITLE_CHAT:
    case THEME_TITLE_DND:
    case THEME_TITLE_XA:
        _theme_prep_bgnd("titlebar", "blue", lookup_str);
        break;
    case THEME_STATUS_TEXT:
    case THEME_STATUS_BRACKET:
    case THEME_STATUS_ACTIVE:
    case THEME_STATUS_CURRENT:
    case THEME_STATUS_NEW:
    case THEME_STATUS_TIME:
        _theme_prep_bgnd("statusbar", "blue", lookup_str);
        break;
    default:
        _theme_prep_bgnd("bkgnd", "default", lookup_str);
        break;
    }

    // lookup colour pair
    result = color_pair_cache_get(lookup_str->str);
    if (result < 0) {
        log_error("Unable to load colour theme");
        result = 0;
    }

    g_string_free(lookup_str, TRUE);

    if (bold) {
        return COLOR_PAIR(result) | A_BOLD;
    } else {
        return COLOR_PAIR(result);
    }
}
w"> <- print screen, [move: ] m:address:move, quit:boolean, error:boolean <- read-move buffered-stdin-in, screen break-if quit, +quit:label buffered-stdin-in <- clear buffered-stdin-in # cleanup after error. todo: test this? loop-if error } board <- make-move board, m screen <- clear-screen screen loop } +quit ] ## a board is an array of files, a file is an array of characters (squares) def new-board initial-position:address:array:character -> board:address:array:address:array:character [ local-scope load-ingredients # assert(length(initial-position) == 64) len:number <- length *initial-position correct-length?:boolean <- equal len, 64 assert correct-length?, [chessboard had incorrect size] # board is an array of pointers to files; file is an array of characters board <- new {(address array character): type}, 8 col:number <- copy 0 { done?:boolean <- equal col, 8 break-if done? file:address:array:character <- new-file initial-position, col *board <- put-index *board, col, file col <- add col, 1 loop } ] def new-file position:address:array:character, index:number -> result:address:array:character [ local-scope load-ingredients index <- multiply index, 8 result <- new character:type, 8 row:number <- copy 0 { done?:boolean <- equal row, 8 break-if done? square:character <- index *position, index *result <- put-index *result, row, square row <- add row, 1 index <- add index, 1 loop } ] def print-board screen:address:screen, board:address:array:address:array:character -> screen:address:screen [ local-scope load-ingredients row:number <- copy 7 # start printing from the top of the board space:character <- copy 32/space # print each row { done?:boolean <- lesser-than row, 0 break-if done? # print rank number as a legend rank:number <- add row, 1 print-integer screen, rank print screen, [ | ] # print each square in the row col:number <- copy 0 { done?:boolean <- equal col:number, 8 break-if done?:boolean f:address:array:character <- index *board, col c:character <- index *f, row print screen, c print screen, space col <- add col, 1 loop } row <- subtract row, 1 cursor-to-next-line screen loop } # print file letters as legend print screen, [ +----------------] cursor-to-next-line screen print screen, [ a b c d e f g h] cursor-to-next-line screen ] def initial-position -> board:address:array:address:array:character [ local-scope # layout in memory (in raster order): # R P _ _ _ _ p r # N P _ _ _ _ p n # B P _ _ _ _ p b # Q P _ _ _ _ p q # K P _ _ _ _ p k # B P _ _ _ _ p B # N P _ _ _ _ p n # R P _ _ _ _ p r initial-position:address:array:character <- new-array 82/R, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 114/r, 78/N, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 110/n, 66/B, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 98/b, 81/Q, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 113/q, 75/K, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 107/k, 66/B, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 98/b, 78/N, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 110/n, 82/R, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 114/r #? 82/R, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 114/r, #? 78/N, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 110/n, #? 66/B, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 98/b, #? 81/Q, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 113/q, #? 75/K, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 107/k, #? 66/B, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 98/b, #? 78/N, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 110/n, #? 82/R, 80/P, 32/blank, 32/blank, 32/blank, 32/blank, 112/p, 114/r board <- new-board initial-position ] scenario printing-the-board [ assume-screen 30/width, 12/height run [ local-scope board:address:array:address:array:character <- initial-position screen:address:screen <- print-board screen:address:screen, board ] screen-should-contain [ # 012345678901234567890123456789 .8 | r n b q k b n r . .7 | p p p p p p p p . .6 | . .5 | . .4 | . .3 | . .2 | P P P P P P P P . .1 | R N B Q K B N R . . +---------------- . . a b c d e f g h . . . . . ] ] ## data structure: move container move [ # valid range: 0-7 from-file:number from-rank:number to-file:number to-rank:number ] # prints only error messages to screen def read-move stdin:address:source:character, screen:address:screen -> result:address:move, quit?:boolean, error?:boolean, stdin:address:source:character, screen:address:screen [ local-scope load-ingredients from-file:number, quit?:boolean, error?:boolean <- read-file stdin, screen return-if quit?, 0/dummy return-if error?, 0/dummy # construct the move object result:address:move <- new move:type *result <- put *result, from-file:offset, from-file from-rank:number, quit?, error? <- read-rank stdin, screen return-if quit?, 0/dummy return-if error?, 0/dummy *result <- put *result, from-rank:offset, from-rank error? <- expect-from-channel stdin, 45/dash, screen return-if error?, 0/dummy, 0/quit to-file:number, quit?, error? <- read-file stdin, screen return-if quit?:boolean, 0/dummy return-if error?:boolean, 0/dummy *result <- put *result, to-file:offset, to-file to-rank:number, quit?, error? <- read-rank stdin, screen return-if quit?, 0/dummy return-if error?, 0/dummy *result <- put *result, to-rank:offset, to-rank error? <- expect-from-channel stdin, 10/newline, screen return-if error?, 0/dummy, 0/quit ] # valid values for file: 0-7 def read-file stdin:address:source:character, screen:address:screen -> file:number, quit:boolean, error:boolean, stdin:address:source:character, screen:address:screen [ local-scope load-ingredients c:character, eof?:boolean, stdin <- read stdin return-if eof?, 0/dummy, 1/quit, 0/error { q-pressed?:boolean <- equal c, 81/Q break-unless q-pressed? return 0/dummy, 1/quit, 0/error } { q-pressed? <- equal c, 113/q break-unless q-pressed? return 0/dummy, 1/quit, 0/error } { empty-fake-keyboard?:boolean <- equal c, 0/eof break-unless empty-fake-keyboard? return 0/dummy, 1/quit, 0/error } { newline?:boolean <- equal c, 10/newline break-unless newline? print screen, [that's not enough] return 0/dummy, 0/quit, 1/error } file:number <- subtract c, 97/a # 'a' <= file <= 'h' { above-min:boolean <- greater-or-equal file, 0 break-if above-min print screen, [file too low: ] print screen, c cursor-to-next-line screen return 0/dummy, 0/quit, 1/error } { below-max:boolean <- lesser-than file, 8 break-if below-max print screen, [file too high: ] print screen, c return 0/dummy, 0/quit, 1/error } return file, 0/quit, 0/error ] # valid values for rank: 0-7 def read-rank stdin:address:source:character, screen:address:screen -> rank:number, quit?:boolean, error?:boolean, stdin:address:source:character, screen:address:screen [ local-scope load-ingredients c:character, eof?:boolean, stdin <- read stdin return-if eof?, 0/dummy, 1/quit, 0/error { q-pressed?:boolean <- equal c, 8/Q break-unless q-pressed? return 0/dummy, 1/quit, 0/error } { q-pressed? <- equal c, 113/q break-unless q-pressed? return 0/dummy, 1/quit, 0/error } { newline?:boolean <- equal c, 10 # newline break-unless newline? print screen, [that's not enough] return 0/dummy, 0/quit, 1/error } rank:number <- subtract c, 49/'1' # assert'1' <= rank <= '8' { above-min:boolean <- greater-or-equal rank, 0 break-if above-min print screen, [rank too low: ] print screen, c return 0/dummy, 0/quit, 1/error } { below-max:boolean <- lesser-or-equal rank, 7 break-if below-max print screen, [rank too high: ] print screen, c return 0/dummy, 0/quit, 1/error } return rank, 0/quit, 0/error ] # read a character from the given channel and check that it's what we expect # return true on error def expect-from-channel stdin:address:source:character, expected:character, screen:address:screen -> result:boolean, stdin:address:source:character, screen:address:screen [ local-scope load-ingredients c:character, eof?:boolean, stdin <- read stdin return-if eof? 1/true { match?:boolean <- equal c, expected break-if match? print screen, [expected character not found] } result <- not match? ] scenario read-move-blocking [ assume-screen 20/width, 2/height run [ local-scope source:address:source:character, sink:address:sink:character <- new-channel 2/capacity read-move-routine:number/routine <- start-running read-move, source, screen:address:screen # 'read-move' is waiting for input wait-for-routine-to-block read-move-routine read-move-state:number <- routine-state read-move-routine waiting?:boolean <- equal read-move-state, 3/waiting assert waiting?, [ F read-move-blocking: routine failed to pause after coming up (before any keys were pressed)] # press 'a' sink <- write sink, 97/a restart read-move-routine # 'read-move' still waiting for input wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine waiting? <- equal read-move-state, 3/waiting assert waiting?, [ F read-move-blocking: routine failed to pause after rank 'a'] # press '2' sink <- write sink, 50/'2' restart read-move-routine # 'read-move' still waiting for input wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine waiting? <- equal read-move-state, 3/waiting assert waiting?, [ F read-move-blocking: routine failed to pause after file 'a2'] # press '-' sink <- write sink, 45/'-' restart read-move-routine # 'read-move' still waiting for input wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine waiting? <- equal read-move-state, 3/waiting assert waiting?, [ F read-move-blocking: routine failed to pause after hyphen 'a2-'] # press 'a' sink <- write sink, 97/a restart read-move-routine # 'read-move' still waiting for input wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine waiting? <- equal read-move-state, 3/waiting assert waiting?, [ F read-move-blocking: routine failed to pause after rank 'a2-a'] # press '4' sink <- write sink, 52/'4' restart read-move-routine # 'read-move' still waiting for input wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine waiting? <- equal read-move-state, 3/waiting assert waiting?, [ F read-move-blocking: routine failed to pause after file 'a2-a4'] # press 'newline' sink <- write sink, 10 # newline restart read-move-routine # 'read-move' now completes wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine completed?:boolean <- equal read-move-state, 1/completed assert completed?, [ F read-move-blocking: routine failed to terminate on newline] trace 1, [test], [reached end] ] trace-should-contain [ test: reached end ] ] scenario read-move-quit [ assume-screen 20/width, 2/height run [ local-scope source:address:source:character, sink:address:sink:character <- new-channel 2/capacity read-move-routine:number <- start-running read-move, source, screen:address:screen # 'read-move' is waiting for input wait-for-routine-to-block read-move-routine read-move-state:number <- routine-state read-move-routine waiting?:boolean <- equal read-move-state, 3/waiting assert waiting?, [ F read-move-quit: routine failed to pause after coming up (before any keys were pressed)] # press 'q' sink <- write sink, 113/q restart read-move-routine # 'read-move' completes wait-for-routine-to-block read-move-routine read-move-state <- routine-state read-move-routine completed?:boolean <- equal read-move-state, 1/completed assert completed?, [ F read-move-quit: routine failed to terminate on 'q'] trace 1, [test], [reached end] ] trace-should-contain [ test: reached end ] ] scenario read-move-illegal-file [ assume-screen 20/width, 2/height run [ local-scope source:address:source:character, sink:address:sink:character <- new-channel 2/capacity read-move-routine:number <- start-running read-move, source, screen:address:screen # 'read-move' is waiting for input wait-for-routine-to-block read-move-routine read-move-state:number <- routine-state read-move-routine waiting?:boolean <- equal read-move-state, 3/waiting assert waiting?, [ F read-move-file: routine failed to pause after coming up (before any keys were pressed)] sink <- write sink, 50/'2' restart read-move-routine wait-for-routine-to-block read-move-routine ] screen-should-contain [ .file too low: 2 . . . ] ] scenario read-move-illegal-rank [ assume-screen 20/width, 2/height run [ local-scope source:address:source:character, sink:address:sink:character <- new-channel 2/capacity read-move-routine:number <- start-running read-move, source, screen:address:screen # 'read-move' is waiting for input wait-for-routine-to-block read-move-routine read-move-state:number <- routine-state read-move-routine waiting?:boolean <- equal read-move-state, 3/waiting assert waiting?, [ F read-move-file: routine failed to pause after coming up (before any keys were pressed)] sink <- write sink, 97/a sink <- write sink, 97/a restart read-move-routine wait-for-routine-to-block read-move-routine ] screen-should-contain [ .rank too high: a . . . ] ] scenario read-move-empty [ assume-screen 20/width, 2/height run [ local-scope source:address:source:character, sink:address:sink:character <- new-channel 2/capacity read-move-routine:number <- start-running read-move, source, screen:address:screen # 'read-move' is waiting for input wait-for-routine-to-block read-move-routine read-move-state:number <- routine-state read-move-routine waiting?:boolean <- equal read-move-state, 3/waiting assert waiting?, [ F read-move-file: routine failed to pause after coming up (before any keys were pressed)] sink <- write sink, 10/newline sink <- write sink, 97/a restart read-move-routine wait-for-routine-to-block read-move-routine ] screen-should-contain [ .that's not enough . . . ] ] def make-move board:address:array:address:array:character, m:address:move -> board:address:array:address:array:character [ local-scope load-ingredients from-file:number <- get *m, from-file:offset from-rank:number <- get *m, from-rank:offset to-file:number <- get *m, to-file:offset to-rank:number <- get *m, to-rank:offset from-f:address:array:character <- index *board, from-file to-f:address:array:character <- index *board, to-file src:character/square <- index *from-f, from-rank *to-f <- put-index *to-f, to-rank, src *from-f <- put-index *from-f, from-rank, 32/space ] scenario making-a-move [ assume-screen 30/width, 12/height run [ local-scope board:address:array:address:array:character <- initial-position move:address:move <- new move:type *move <- merge 6/g, 1/'2', 6/g, 3/'4' board <- make-move board, move screen:address:screen <- print-board screen:address:screen, board ] screen-should-contain [ # 012345678901234567890123456789 .8 | r n b q k b n r . .7 | p p p p p p p p . .6 | . .5 | . .4 | P . .3 | . .2 | P P P P P P P . .1 | R N B Q K B N R . . +---------------- . . a b c d e f g h . . . ] ]