Source

Open PS2 Loader / src / themes.c

   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
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
#include "include/usbld.h"
#include "include/themes.h"
#include "include/util.h"
#include "include/gui.h"
#include "include/renderman.h"
#include "include/textures.h"
#include "include/ioman.h"
#include "include/fntsys.h"
#include "include/lang.h"

#define MENU_POS_V 50
#define HINT_HEIGHT 32
#define DECORATOR_SIZE 20

theme_t* gTheme;

static int screenWidth;
static int screenHeight;
static int guiThemeID = 0;

static int nThemes = 0;
static theme_file_t themes[THM_MAX_FILES];
static char **guiThemesNames = NULL;

#define TYPE_ATTRIBUTE_TEXT		0
#define TYPE_STATIC_TEXT		1
#define TYPE_ATTRIBUTE_IMAGE	2
#define TYPE_GAME_IMAGE			3
#define TYPE_STATIC_IMAGE		4
#define TYPE_BACKGROUND			5
#define TYPE_MENU_ICON			6
#define TYPE_MENU_TEXT			7
#define	TYPE_ITEMS_LIST			8
#define	TYPE_ITEM_ICON			9
#define	TYPE_ITEM_COVER			10
#define TYPE_ITEM_TEXT			11
#define TYPE_HINT_TEXT			12
#define TYPE_INFO_HINT_TEXT		13
#define TYPE_LOADING_ICON		14

#define DISPLAY_ALWAYS		0
#define DISPLAY_DEFINED		1
#define DISPLAY_NEVER		2

#define SIZING_NONE			-1
#define SIZING_CLIP			0
#define SIZING_WRAP			1

static char *elementsType[] = {
	"AttributeText",
	"StaticText",
	"AttributeImage",
	"GameImage",
	"StaticImage",
	"Background",
	"MenuIcon",
	"MenuText",
	"ItemsList",
	"ItemIcon",
	"ItemCover",
	"ItemText",
	"HintText",
	"InfoHintText",
	"LoadingIcon",
};

// Common functions for Text ////////////////////////////////////////////////////////////////////////////////////////////////

static void endMutableText(theme_element_t* elem) {
	mutable_text_t* mutableText = (mutable_text_t*) elem->extended;
	if (mutableText) {
		if (mutableText->value)
			free(mutableText->value);

		if (mutableText->alias)
			free(mutableText->alias);

		free(mutableText);
	}

	free(elem);
}

static mutable_text_t* initMutableText(char* themePath, config_set_t* themeConfig, theme_t* theme, char* name, int type, struct theme_element* elem,
		char* value, char* alias, int displayMode, int sizingMode) {

	mutable_text_t* mutableText = (mutable_text_t*) malloc(sizeof(mutable_text_t));
	mutableText->currentConfigId = 0;
	mutableText->currentValue = NULL;
	mutableText->alias = NULL;

	char elemProp[64];

	snprintf(elemProp, 64, "%s_display", name);
	configGetInt(themeConfig, elemProp, &displayMode);
	mutableText->displayMode = displayMode;

	int length = strlen(value) + 1;
	mutableText->value = (char*) malloc(length * sizeof(char));
	memcpy(mutableText->value, value, length);

	snprintf(elemProp, 64, "%s_wrap", name);
	if (configGetInt(themeConfig, elemProp, &sizingMode)) {
		if (sizingMode > 0)
			mutableText->sizingMode = SIZING_WRAP;
	}

	if ((elem->width != DIM_UNDEF) || (elem->height != DIM_UNDEF)) {
		if (sizingMode == SIZING_NONE)
			sizingMode = SIZING_CLIP;

		if (elem->width == DIM_UNDEF)
			elem->width = screenWidth;

		if (elem->height == DIM_UNDEF)
			elem->height = screenHeight;
	} else
		sizingMode = SIZING_NONE;
	mutableText->sizingMode = sizingMode;

	if (type == TYPE_ATTRIBUTE_TEXT) {
		snprintf(elemProp, 64, "%s_title", name);
		configGetStr(themeConfig, elemProp, &alias);
		if (!alias) {
			if (value[0] == '#')
				alias = &value[1];
			else
				alias = value;
		}
		length = strlen(alias) + 1 + 3;
		mutableText->alias = (char*) malloc(length * sizeof(char));
		if (mutableText->sizingMode == SIZING_WRAP)
			snprintf(mutableText->alias, length, "%s :\n", alias);
		else
			snprintf(mutableText->alias, length, "%s : ", alias);
	} else {
		if (mutableText->sizingMode == SIZING_WRAP)
			fntFitString(elem->font, mutableText->value, elem->width);
	}

	return mutableText;
}

// StaticText ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

static void drawStaticText(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	mutable_text_t* mutableText = (mutable_text_t*) elem->extended;
	if (mutableText->sizingMode == SIZING_NONE)
		fntRenderString(elem->font, elem->posX, elem->posY, elem->aligned, 0, 0, mutableText->value, elem->color);
	else
		fntRenderString(elem->font, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, mutableText->value, elem->color);
}

static void initStaticText(char* themePath, config_set_t* themeConfig, theme_t* theme, theme_element_t* elem, char* name,
		char* value) {

	char elemProp[64];

	snprintf(elemProp, 64, "%s_value", name);
	configGetStr(themeConfig, elemProp, &value);
	if (value) {
		elem->extended = initMutableText(themePath, themeConfig, theme, name, TYPE_STATIC_TEXT, elem, value, NULL, DISPLAY_ALWAYS, SIZING_NONE);
		elem->endElem = &endMutableText;
		elem->drawElem = &drawStaticText;
	} else
		LOG("THEMES StaticText %s: NO value, elem disabled !!\n", name);
}

// AttributeText ////////////////////////////////////////////////////////////////////////////////////////////////////////////

static void drawAttributeText(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	mutable_text_t* mutableText = (mutable_text_t*) elem->extended;
	if (config) {
		if (mutableText->currentConfigId != config->uid) {
			// force refresh
			mutableText->currentConfigId = config->uid;
			mutableText->currentValue = NULL;
			if (configGetStr(config, mutableText->value, &mutableText->currentValue)) {
				if (mutableText->sizingMode == SIZING_WRAP)
					fntFitString(elem->font, mutableText->currentValue, elem->width);
			}
		}
		if (mutableText->currentValue) {
			if (mutableText->displayMode == DISPLAY_NEVER) {
				if (mutableText->sizingMode == SIZING_NONE)
					fntRenderString(elem->font, elem->posX, elem->posY, elem->aligned, 0, 0, mutableText->currentValue, elem->color);
				else
					fntRenderString(elem->font, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, mutableText->currentValue, elem->color);
			} else {
				char result[300];
				snprintf(result, 300, "%s%s", mutableText->alias, mutableText->currentValue);
				if (mutableText->sizingMode == SIZING_NONE)
					fntRenderString(elem->font, elem->posX, elem->posY, elem->aligned, 0, 0, result, elem->color);
				else
					fntRenderString(elem->font, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, result, elem->color);
			}
			return;
		}
	}
	if (mutableText->displayMode == DISPLAY_ALWAYS) {
		if (mutableText->sizingMode == SIZING_NONE)
			fntRenderString(elem->font, elem->posX, elem->posY, elem->aligned, 0, 0, mutableText->alias, elem->color);
		else
			fntRenderString(elem->font, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, mutableText->alias, elem->color);
	}
}

static void initAttributeText(char* themePath, config_set_t* themeConfig, theme_t* theme, theme_element_t* elem, char* name,
		char* attribute) {

	char elemProp[64];

	snprintf(elemProp, 64, "%s_attribute", name);
	configGetStr(themeConfig, elemProp, &attribute);
	if (attribute) {
		elem->extended = initMutableText(themePath, themeConfig, theme, name, TYPE_ATTRIBUTE_TEXT, elem, attribute, NULL, DISPLAY_ALWAYS, SIZING_NONE);
		elem->endElem = &endMutableText;
		elem->drawElem = &drawAttributeText;
	} else
		LOG("THEMES AttributeText %s: NO attribute, elem disabled !!\n", name);
}

// Common functions for Image ///////////////////////////////////////////////////////////////////////////////////////////////

static void findDuplicate(theme_element_t* first, char* cachePattern, char* defaultTexture, char* overlayTexture, mutable_image_t* target) {
	theme_element_t* elem = first;
	while (elem) {
		if ((elem->type == TYPE_STATIC_IMAGE) || (elem->type == TYPE_ATTRIBUTE_IMAGE) || (elem->type == TYPE_GAME_IMAGE) || (elem->type == TYPE_BACKGROUND)) {
			mutable_image_t* source = (mutable_image_t*) elem->extended;

			if (cachePattern && source->cache && !strcmp(cachePattern, source->cache->suffix)) {
				target->cache = source->cache;
				target->cacheLinked = 1;
				LOG("THEMES Re-using a cache for pattern %s\n", cachePattern);
			}

			if (defaultTexture && source->defaultTexture && !strcmp(defaultTexture, source->defaultTexture->name)) {
				target->defaultTexture = source->defaultTexture;
				target->defaultTextureLinked = 1;
				LOG("THEMES Re-using the default texture for %s\n", defaultTexture);
			}

			if (overlayTexture && source->overlayTexture && !strcmp(overlayTexture, source->overlayTexture->name)) {
				target->overlayTexture = source->overlayTexture;
				target->overlayTextureLinked = 1;
				LOG("THEMES Re-using the overlay texture for %s\n", overlayTexture);
			}
		}

		elem = elem->next;
	}
}

static void freeImageTexture(image_texture_t* texture) {
	if (texture) {
		if (texture->source.Mem)
			free(texture->source.Mem);

		free(texture->name);

		free(texture);
	}
}

static image_texture_t* initImageTexture(char* themePath, config_set_t* themeConfig, char* name, char* imgName, int isOverlay) {
	image_texture_t* texture = (image_texture_t*) malloc(sizeof(image_texture_t));
	texPrepare(&texture->source, isOverlay ? GS_PSM_CT32 : GS_PSM_CT24);
	texture->name = NULL;

	char path[255];
	snprintf(path, 255, "%s%s", themePath, imgName);
	if (texDiscoverLoad(&texture->source, path, -1, isOverlay ? GS_PSM_CT32 : GS_PSM_CT24) >= 0) {
		int length = strlen(imgName) + 1;
		texture->name = (char*) malloc(length * sizeof(char));
		memcpy(texture->name, imgName, length);

		if (isOverlay) {
			int intValue;
			char elemProp[64];
			snprintf(elemProp, 64, "%s_overlay_ulx", name);
			if (configGetInt(themeConfig, elemProp, &intValue))
				texture->upperLeft_x = intValue;
			snprintf(elemProp, 64, "%s_overlay_uly", name);
			if (configGetInt(themeConfig, elemProp, &intValue))
				texture->upperLeft_y = intValue;
			snprintf(elemProp, 64, "%s_overlay_urx", name);
			if (configGetInt(themeConfig, elemProp, &intValue))
				texture->upperRight_x = intValue;
			snprintf(elemProp, 64, "%s_overlay_ury", name);
			if (configGetInt(themeConfig, elemProp, &intValue))
				texture->upperRight_y = intValue;
			snprintf(elemProp, 64, "%s_overlay_llx", name);
			if (configGetInt(themeConfig, elemProp, &intValue))
				texture->lowerLeft_x = intValue;
			snprintf(elemProp, 64, "%s_overlay_lly", name);
			if (configGetInt(themeConfig, elemProp, &intValue))
				texture->lowerLeft_y = intValue;
			snprintf(elemProp, 64, "%s_overlay_lrx", name);
			if (configGetInt(themeConfig, elemProp, &intValue))
				texture->lowerRight_x = intValue;
			snprintf(elemProp, 64, "%s_overlay_lry", name);
			if (configGetInt(themeConfig, elemProp, &intValue))
				texture->lowerRight_y = intValue;
		}
	} else {
		freeImageTexture(texture);
		texture = NULL;
	}

	return texture;
}

static void endMutableImage(struct theme_element* elem) {
	mutable_image_t* mutableImage = (mutable_image_t*) elem->extended;
	if (mutableImage) {
		if (mutableImage->cache && !mutableImage->cacheLinked)
			cacheDestroyCache(mutableImage->cache);

		if (mutableImage->defaultTexture && !mutableImage->defaultTextureLinked)
			freeImageTexture(mutableImage->defaultTexture);

		if (mutableImage->overlayTexture && !mutableImage->overlayTextureLinked)
			freeImageTexture(mutableImage->overlayTexture);

		free(mutableImage);
	}

	free(elem);
}

static mutable_image_t* initMutableImage(char* themePath, config_set_t* themeConfig, theme_t* theme, char* name, int type,
		char* cachePattern, int cacheCount, char* defaultTexture, char* overlayTexture) {

	mutable_image_t* mutableImage = (mutable_image_t*) malloc(sizeof(mutable_image_t));
	mutableImage->currentUid = -1;
	mutableImage->currentConfigId = 0;
	mutableImage->currentValue = NULL;
	mutableImage->cache = NULL;
	mutableImage->cacheLinked = 0;
	mutableImage->defaultTexture = NULL;
	mutableImage->defaultTextureLinked = 0;
	mutableImage->overlayTexture = NULL;
	mutableImage->overlayTextureLinked = 0;

	char elemProp[64];

	if (type == TYPE_ATTRIBUTE_IMAGE) {
		snprintf(elemProp, 64, "%s_attribute", name);
		configGetStr(themeConfig, elemProp, &cachePattern);
		LOG("THEMES MutableImage %s: type: %s using cache pattern: %s\n", name, elementsType[type], cachePattern);
	} else if ((type == TYPE_GAME_IMAGE) || type == (TYPE_BACKGROUND)) {
		snprintf(elemProp, 64, "%s_pattern", name);
		configGetStr(themeConfig, elemProp, &cachePattern);
		snprintf(elemProp, 64, "%s_count", name);
		configGetInt(themeConfig, elemProp, &cacheCount);
		LOG("THEMES MutableImage %s: type: %s using cache pattern: %s count: %d\n", name, elementsType[type], cachePattern, cacheCount);
	}

	snprintf(elemProp, 64, "%s_default", name);
	configGetStr(themeConfig, elemProp, &defaultTexture);

	if (type != TYPE_BACKGROUND) {
		snprintf(elemProp, 64, "%s_overlay", name);
		configGetStr(themeConfig, elemProp, &overlayTexture);
	}

	findDuplicate(theme->mainElems.first, cachePattern, defaultTexture, overlayTexture, mutableImage);
	findDuplicate(theme->infoElems.first, cachePattern, defaultTexture, overlayTexture, mutableImage);

	if (cachePattern && !mutableImage->cache) {
		if (type == TYPE_ATTRIBUTE_IMAGE)
			mutableImage->cache = cacheInitCache(-1, themePath, 0, cachePattern, 1);
		else
			mutableImage->cache = cacheInitCache(theme->gameCacheCount++, "ART", 1, cachePattern, cacheCount);
	}

	if (defaultTexture && !mutableImage->defaultTexture)
		mutableImage->defaultTexture = initImageTexture(themePath, themeConfig, name, defaultTexture, 0);

	if (overlayTexture && !mutableImage->overlayTexture)
		mutableImage->overlayTexture = initImageTexture(themePath, themeConfig, name, overlayTexture, 1);

	return mutableImage;
}

// StaticImage //////////////////////////////////////////////////////////////////////////////////////////////////////////////

static void drawStaticImage(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	mutable_image_t* staticImage = (mutable_image_t*) elem->extended;
	if (staticImage->overlayTexture) {
		rmDrawOverlayPixmap(&staticImage->overlayTexture->source, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, elem->scaled, gDefaultCol,
				&staticImage->defaultTexture->source, staticImage->overlayTexture->upperLeft_x, staticImage->overlayTexture->upperLeft_y, staticImage->overlayTexture->upperRight_x, staticImage->overlayTexture->upperRight_y,
				staticImage->overlayTexture->lowerLeft_x, staticImage->overlayTexture->lowerLeft_y, staticImage->overlayTexture->lowerRight_x, staticImage->overlayTexture->lowerRight_y);
	} else
		rmDrawPixmap(&staticImage->defaultTexture->source, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, elem->scaled, gDefaultCol);
}

static void initStaticImage(char* themePath, config_set_t* themeConfig, theme_t* theme, theme_element_t* elem, char* name,
		char* imageName) {

	mutable_image_t* mutableImage = initMutableImage(themePath, themeConfig, theme, name, elem->type, NULL, 0, imageName, NULL);
	elem->extended = mutableImage;
	elem->endElem = &endMutableImage;

	if (mutableImage->defaultTexture)
		elem->drawElem = &drawStaticImage;
	else
		LOG("THEMES StaticImage %s: NO image name, elem disabled !!\n", name);
}

// GameImage ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

static GSTEXTURE* getGameImageTexture(image_cache_t* cache, void* support, struct submenu_item* item) {
	if (gEnableArt) {
		item_list_t * list = (item_list_t *) support;
		char* startup = list->itemGetStartup(item->id);
		return cacheGetTexture(cache, list, &item->cache_id[cache->userId], &item->cache_uid[cache->userId], startup);
	}

	return NULL;
}

static void drawGameImage(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	mutable_image_t* gameImage = (mutable_image_t*) elem->extended;
	if (item) {
		GSTEXTURE* texture = getGameImageTexture(gameImage->cache, menu->item->userdata, &item->item);
		if (!texture || !texture->Mem) {
			if (gameImage->defaultTexture)
				texture = &gameImage->defaultTexture->source;
			else {
				if (elem->type == TYPE_BACKGROUND)
					guiDrawBGPlasma();
				return;
			}
		}

		if (gameImage->overlayTexture) {
			rmDrawOverlayPixmap(&gameImage->overlayTexture->source, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, elem->scaled, gDefaultCol,
				texture, gameImage->overlayTexture->upperLeft_x, gameImage->overlayTexture->upperLeft_y, gameImage->overlayTexture->upperRight_x, gameImage->overlayTexture->upperRight_y,
				gameImage->overlayTexture->lowerLeft_x, gameImage->overlayTexture->lowerLeft_y, gameImage->overlayTexture->lowerRight_x, gameImage->overlayTexture->lowerRight_y);
		} else
			rmDrawPixmap(texture, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, elem->scaled, gDefaultCol);

	} else if (elem->type == TYPE_BACKGROUND) {
		if (gameImage->defaultTexture)
			rmDrawPixmap(&gameImage->defaultTexture->source, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, elem->scaled, gDefaultCol);
		else
			guiDrawBGPlasma();
	}
}

static void initGameImage(char* themePath, config_set_t* themeConfig, theme_t* theme, theme_element_t* elem, char* name,
		char* pattern, int count, char* texture, char* overlay) {

	mutable_image_t* mutableImage = initMutableImage(themePath, themeConfig, theme, name, elem->type, pattern, count, texture, overlay);
	elem->extended = mutableImage;
	elem->endElem = &endMutableImage;

	if (mutableImage->cache)
		elem->drawElem = &drawGameImage;
	else
		LOG("THEMES GameImage %s: NO pattern, elem disabled !!\n", name);
}

// AttributeImage ///////////////////////////////////////////////////////////////////////////////////////////////////////////

static void drawAttributeImage(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	mutable_image_t* attributeImage = (mutable_image_t*) elem->extended;
	if (config) {
		if (attributeImage->currentConfigId != config->uid) {
			// force refresh
			attributeImage->currentUid = -1;
			attributeImage->currentConfigId = config->uid;
			attributeImage->currentValue = NULL;
			configGetStr(config, attributeImage->cache->suffix, &attributeImage->currentValue);
		}
		if (attributeImage->currentValue) {
			int posZ = 0;
			GSTEXTURE* texture = cacheGetTexture(attributeImage->cache, menu->item->userdata, &posZ, &attributeImage->currentUid, attributeImage->currentValue);
			if (texture && texture->Mem) {
				if (attributeImage->overlayTexture) {
					rmDrawOverlayPixmap(&attributeImage->overlayTexture->source, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, elem->scaled, gDefaultCol,
						texture, attributeImage->overlayTexture->upperLeft_x, attributeImage->overlayTexture->upperLeft_y, attributeImage->overlayTexture->upperRight_x, attributeImage->overlayTexture->upperRight_y,
						attributeImage->overlayTexture->lowerLeft_x, attributeImage->overlayTexture->lowerLeft_y, attributeImage->overlayTexture->lowerRight_x, attributeImage->overlayTexture->lowerRight_y);
				} else
					rmDrawPixmap(texture, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, elem->scaled, gDefaultCol);

				return;
			}
		}
	}
	if (attributeImage->defaultTexture)
		rmDrawPixmap(&attributeImage->defaultTexture->source, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, elem->scaled, gDefaultCol);
}

static void initAttributeImage(char* themePath, config_set_t* themeConfig, theme_t* theme, theme_element_t* elem, char* name) {
	mutable_image_t* mutableImage = initMutableImage(themePath, themeConfig, theme, name, elem->type, NULL, 1, NULL, NULL);
	elem->extended = mutableImage;
	elem->endElem = &endMutableImage;

	if (mutableImage->cache)
		elem->drawElem = &drawAttributeImage;
	else
		LOG("THEMES AttributeImage %s: NO attribute, elem disabled !!\n", name);
}

// BasicElement /////////////////////////////////////////////////////////////////////////////////////////////////////////////

static void endBasic(theme_element_t* elem) {
	if (elem->extended)
		free(elem->extended);

	free(elem);
}

static theme_element_t* initBasic(char* themePath, config_set_t* themeConfig, theme_t* theme,
		char* name,	int type, int x, int y, short aligned, int w, int h, short scaled, u64 color, int font) {

	int intValue;
	unsigned char charColor[3];
	char* temp;
	char elemProp[64];

	theme_element_t* elem = (theme_element_t*) malloc(sizeof(theme_element_t));

	elem->type = type;
	elem->extended = NULL;
	elem->drawElem = NULL;
	elem->endElem = &endBasic;
	elem->next = NULL;

	snprintf(elemProp, 64, "%s_x", name);
	if (configGetStr(themeConfig, elemProp, &temp)) {
		if (!strncmp(temp, "POS_MID", 7))
			x = screenWidth >> 1;
		else
			x = atoi(temp);
	}
	if (x < 0)
		elem->posX = screenWidth + x;
	else
		elem->posX = x;

	snprintf(elemProp, 64, "%s_y", name);
	if (configGetStr(themeConfig, elemProp, &temp)) {
		if (!strncmp(temp, "POS_MID", 7))
			y = screenHeight >> 1;
		else
			y = atoi(temp);
	}
	if (y < 0)
		elem->posY = ceil((screenHeight + y) * theme->usedHeight / screenHeight);
	else
		elem->posY = y;

	snprintf(elemProp, 64, "%s_width", name);
	if (configGetStr(themeConfig, elemProp, &temp)) {
		if (!strncmp(temp, "DIM_INF", 7))
			elem->width = screenWidth;
		else
			elem->width = atoi(temp);
	} else
		elem->width = w;

	snprintf(elemProp, 64, "%s_height", name);
	if (configGetStr(themeConfig, elemProp, &temp)) {
		if (!strncmp(temp, "DIM_INF", 7))
			elem->height = screenHeight;
		else
			elem->height = atoi(temp);
	} else
		elem->height = h;

	snprintf(elemProp, 64, "%s_aligned", name);
	if (configGetInt(themeConfig, elemProp, &intValue))
		elem->aligned = intValue;
	else
		elem->aligned = aligned;

	snprintf(elemProp, 64, "%s_scaled", name);
	if (configGetInt(themeConfig, elemProp, &intValue))
		elem->scaled = intValue;
	else
		elem->scaled = scaled;

	snprintf(elemProp, 64, "%s_color", name);
	if (configGetColor(themeConfig, elemProp, charColor))
		elem->color = GS_SETREG_RGBA(charColor[0], charColor[1], charColor[2], 0xff);
	else
		elem->color = color;

	snprintf(elemProp, 64, "%s_font", name);
	if (configGetInt(themeConfig, elemProp, &intValue))
		font = intValue;
	if (font >= 0 && font < THM_MAX_FONTS)
		elem->font = theme->fonts[font];
	else
		elem->font = FNT_DEFAULT;

	return elem;
}

// Internal elements ////////////////////////////////////////////////////////////////////////////////////////////////////////

static void drawBackground(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	guiDrawBGPlasma();
}

static void initBackground(char* themePath, config_set_t* themeConfig, theme_t* theme, theme_element_t* elem, char* name,
		char* pattern, int count, char* texture) {

	mutable_image_t* mutableImage = initMutableImage(themePath, themeConfig, theme, name, elem->type, pattern, count, texture, NULL);
	elem->extended = mutableImage;
	elem->endElem = &endMutableImage;

	if (mutableImage->cache)
		elem->drawElem = &drawGameImage;
	else if (mutableImage->defaultTexture)
		elem->drawElem = &drawStaticImage;
	else
		elem->drawElem = &drawBackground;
}

static void drawMenuIcon(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	GSTEXTURE* menuIconTex = thmGetTexture(menu->item->icon_id);
	if (menuIconTex && menuIconTex->Mem)
		rmDrawPixmap(menuIconTex, elem->posX, elem->posY, elem->aligned, elem->width, elem->height, elem->scaled, gDefaultCol);
}

static void drawMenuText(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	GSTEXTURE* leftIconTex = NULL, *rightIconTex = NULL;
	if (menu->prev)
		leftIconTex = thmGetTexture(LEFT_ICON);
	if (menu->next)
		rightIconTex = thmGetTexture(RIGHT_ICON);

	if (elem->aligned) {
		int offset = elem->width >> 1;
		if (leftIconTex && leftIconTex->Mem)
			rmDrawPixmap(leftIconTex, elem->posX - offset, elem->posY, elem->aligned, leftIconTex->Width, leftIconTex->Height, elem->scaled, gDefaultCol);
		if (rightIconTex && rightIconTex->Mem)
			rmDrawPixmap(rightIconTex, elem->posX + offset, elem->posY, elem->aligned, rightIconTex->Width, rightIconTex->Height, elem->scaled, gDefaultCol);
	}
	else {
		if (leftIconTex && leftIconTex->Mem)
			rmDrawPixmap(leftIconTex, elem->posX - leftIconTex->Width, elem->posY, elem->aligned, leftIconTex->Width, leftIconTex->Height, elem->scaled, gDefaultCol);
		if (rightIconTex && rightIconTex->Mem)
			rmDrawPixmap(rightIconTex, elem->posX + elem->width, elem->posY, elem->aligned, rightIconTex->Width, rightIconTex->Height, elem->scaled, gDefaultCol);
	}
	fntRenderString(elem->font, elem->posX, elem->posY, elem->aligned, 0, 0, menuItemGetText(menu->item), elem->color);
}

static void drawItemsList(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	if (item) {
		items_list_t* itemsList = (items_list_t*) elem->extended;

		int posX = elem->posX, posY = elem->posY;
		if (elem->aligned) {
			posX -= elem->width >> 1;
			posY -= elem->height >> 1;
		}

		submenu_list_t *ps  = menu->item->pagestart;
		int others = 0;
		u64 color;
		while (ps && (others++ < itemsList->displayedItems)) {
			if (ps == item)
				color = gTheme->selTextColor;
			else
				color = elem->color;

			if (itemsList->decoratorImage) {
				GSTEXTURE* itemIconTex = getGameImageTexture(itemsList->decoratorImage->cache, menu->item->userdata, &ps->item);
				if (itemIconTex && itemIconTex->Mem)
					rmDrawPixmap(itemIconTex, posX, posY, ALIGN_NONE, DECORATOR_SIZE, DECORATOR_SIZE, elem->scaled, gDefaultCol);
				else {
					if (itemsList->decoratorImage->defaultTexture)
						rmDrawPixmap(&itemsList->decoratorImage->defaultTexture->source, posX, posY, ALIGN_NONE, DECORATOR_SIZE, DECORATOR_SIZE, elem->scaled, gDefaultCol);
				}
				fntRenderString(elem->font, posX + DECORATOR_SIZE, posY, ALIGN_NONE, elem->width, elem->height, submenuItemGetText(&ps->item), color);
			} else
				fntRenderString(elem->font, posX, posY, ALIGN_NONE, elem->width, elem->height, submenuItemGetText(&ps->item), color);

			posY += MENU_ITEM_HEIGHT;
			ps = ps->next;
		}
	}
}

static void initItemsList(char* themePath, config_set_t* themeConfig, theme_t* theme, theme_element_t* elem, char* name, char* decorator) {
	char elemProp[64];

	items_list_t* itemsList = (items_list_t*) malloc(sizeof(items_list_t));

	if (elem->width == DIM_UNDEF)
		elem->width = screenWidth;

	if (elem->height == DIM_UNDEF)
		elem->height = theme->usedHeight - (MENU_POS_V + HINT_HEIGHT);

	itemsList->displayedItems = elem->height / MENU_ITEM_HEIGHT;
	LOG("THEMES ItemsList %s: displaying %d elems, item height: %d\n", name, itemsList->displayedItems, elem->height);

	itemsList->decorator = NULL;
	snprintf(elemProp, 64, "%s_decorator", name);
	configGetStr(themeConfig, elemProp, &decorator);
	if (decorator)
		itemsList->decorator = decorator; // Will be used later (thmValidate)

	itemsList->decoratorImage = NULL;

	elem->extended = itemsList;
	// elem->endElem = &endBasic; does the job

	elem->drawElem = &drawItemsList;
}

static void drawItemText(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	if (item) {
		item_list_t *support = menu->item->userdata;
		fntRenderString(elem->font, elem->posX, elem->posY, elem->aligned, 0, 0, support->itemGetStartup(item->item.id), elem->color);
	}
}

static void drawHintText(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	menu_hint_item_t* hint = menu->item->hints;
	if (hint) {
		int x = elem->posX;

		for (; hint; hint = hint->next) {
			x = guiDrawIconAndText(hint->icon_id, hint->text_id, elem->font, x, elem->posY, elem->color);
			x += elem->width;
		}
	}
}

static void drawInfoHintText(struct menu_list* menu, struct submenu_list* item, config_set_t* config, struct theme_element* elem) {
	int x = elem->posX;
	x = guiDrawIconAndText(CROSS_ICON, _STR_RUN, elem->font, x, elem->posY, elem->color);
	x += elem->width;
	x = guiDrawIconAndText(CIRCLE_ICON, _STR_O_BACK, elem->font, x, elem->posY, elem->color);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

static void validateGUIElems(char* themePath, config_set_t* themeConfig, theme_t* theme) {
	// 1. check we have a valid Background elements
	if ( !theme->mainElems.first || (theme->mainElems.first->type != TYPE_BACKGROUND) ) {
		LOG("THEMES No valid background found for main, add default BG_ART\n");
		theme_element_t* backgroundElem = initBasic(themePath, themeConfig, theme, "bg", TYPE_BACKGROUND, 0, 0, ALIGN_NONE, screenWidth, screenHeight, SCALING_NONE, gDefaultCol, FNT_DEFAULT);
		if (themePath)
			initBackground(themePath, themeConfig, theme, backgroundElem, "bg", "BG", 1, "background");
		else
			initBackground(themePath, themeConfig, theme, backgroundElem, "bg", "BG", 1, NULL);
		backgroundElem->next = theme->mainElems.first;
		theme->mainElems.first = backgroundElem;
	}

	if (theme->infoElems.first) {
		if (theme->infoElems.first->type != TYPE_BACKGROUND) {
			LOG("THEMES No valid background found for info, add default BG_ART\n");
			theme_element_t* backgroundElem = initBasic(themePath, themeConfig, theme, "bg", TYPE_BACKGROUND, 0, 0, ALIGN_NONE, screenWidth, screenHeight, SCALING_NONE, gDefaultCol, FNT_DEFAULT);
			if (themePath)
				initBackground(themePath, themeConfig, theme, backgroundElem, "bg", "BG", 1, "background");
			else
				initBackground(themePath, themeConfig, theme, backgroundElem, "bg", "BG", 1, NULL);
			backgroundElem->next = theme->infoElems.first;
			theme->infoElems.first = backgroundElem;
		}
	}

	// 2. check we have a valid ItemsList element, and link its decorator to the target element
	if (theme->itemsList) {
		items_list_t* itemsList = (items_list_t*) theme->itemsList->extended;
		if (itemsList->decorator) {
			// Second pass to find the decorator
			theme_element_t* decoratorElem = theme->mainElems.first;
			while (decoratorElem) {
				if (decoratorElem->type == TYPE_GAME_IMAGE) {
					mutable_image_t* gameImage = (mutable_image_t*) decoratorElem->extended;
					if (!strcmp(itemsList->decorator, gameImage->cache->suffix)) {
						// if user want to cache less than displayed items, then disable itemslist icons, if not would load constantly
						if (gameImage->cache->count >= itemsList->displayedItems)
							itemsList->decoratorImage = gameImage;
						break;
					}
				}

				decoratorElem = decoratorElem->next;
			}
			itemsList->decorator = NULL;
		}
	} else {
		LOG("THEMES No itemsList found, adding a default one\n");
		theme->itemsList = initBasic(themePath, themeConfig, theme, "il", TYPE_ITEMS_LIST, 150, MENU_POS_V, ALIGN_NONE, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, theme->textColor, FNT_DEFAULT);
		initItemsList(themePath, themeConfig, theme, theme->itemsList, "il", NULL);
		theme->itemsList->next = theme->mainElems.first->next; // Position the itemsList as second element (right after the Background)
		theme->mainElems.first->next = theme->itemsList;
	}
}

static int addGUIElem(char* themePath, config_set_t* themeConfig, theme_t* theme, theme_elems_t* elems, char* type, char* name) {

	int enabled = 1;
	char elemProp[64];
	theme_element_t* elem = NULL;

	snprintf(elemProp, 64, "%s_enabled", name);
	configGetInt(themeConfig, elemProp, &enabled);

	if (enabled) {
		snprintf(elemProp, 64, "%s_type", name);
		configGetStr(themeConfig, elemProp, &type);
		if (type) {
			if (!strcmp(elementsType[TYPE_ATTRIBUTE_TEXT], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_ATTRIBUTE_TEXT, 0, 0, ALIGN_CENTER, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, theme->textColor, FNT_DEFAULT);
				initAttributeText(themePath, themeConfig, theme, elem, name, NULL);
			} else if (!strcmp(elementsType[TYPE_STATIC_TEXT], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_STATIC_TEXT, 0, 0, ALIGN_CENTER, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, theme->textColor, FNT_DEFAULT);
				initStaticText(themePath, themeConfig, theme, elem, name, NULL);
			} else if (!strcmp(elementsType[TYPE_ATTRIBUTE_IMAGE], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_ATTRIBUTE_IMAGE, 0, 0, ALIGN_CENTER, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, gDefaultCol, FNT_DEFAULT);
				initAttributeImage(themePath, themeConfig, theme, elem, name);
			} else if (!strcmp(elementsType[TYPE_GAME_IMAGE], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_GAME_IMAGE, 0, 0, ALIGN_CENTER, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, gDefaultCol, FNT_DEFAULT);
				initGameImage(themePath, themeConfig, theme, elem, name, NULL, 1, NULL, NULL);
			} else if (!strcmp(elementsType[TYPE_STATIC_IMAGE], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_STATIC_IMAGE, 0, 0, ALIGN_CENTER, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, gDefaultCol, FNT_DEFAULT);
				initStaticImage(themePath, themeConfig, theme, elem, name, NULL);
			} else if (!strcmp(elementsType[TYPE_BACKGROUND], type)) {
				if (!elems->first) { // Background elem can only be the first one
					elem = initBasic(themePath, themeConfig, theme, name, TYPE_BACKGROUND, 0, 0, ALIGN_NONE, screenWidth, screenHeight, SCALING_NONE, gDefaultCol, FNT_DEFAULT);
					initBackground(themePath, themeConfig, theme, elem, name, NULL, 1, NULL);
				}
			} else if (!strcmp(elementsType[TYPE_MENU_ICON], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_MENU_ICON, 40, 40, ALIGN_CENTER, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, gDefaultCol, FNT_DEFAULT);
				elem->drawElem = &drawMenuIcon;
			} else if (!strcmp(elementsType[TYPE_MENU_TEXT], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_MENU_TEXT, screenWidth >> 1, 20, ALIGN_CENTER, 200, 20, SCALING_RATIO, theme->textColor, FNT_DEFAULT);
				elem->drawElem = &drawMenuText;
			} else if (!strcmp(elementsType[TYPE_ITEMS_LIST], type)) {
				if (!theme->itemsList) {
					elem = initBasic(themePath, themeConfig, theme, name, TYPE_ITEMS_LIST, 150, MENU_POS_V, ALIGN_NONE, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, theme->textColor, FNT_DEFAULT);
					initItemsList(themePath, themeConfig, theme, elem, name, NULL);
					theme->itemsList = elem;
				}
			} else if (!strcmp(elementsType[TYPE_ITEM_ICON], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_GAME_IMAGE, 80, theme->usedHeight >> 1, ALIGN_CENTER, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, gDefaultCol, FNT_DEFAULT);
				initGameImage(themePath, themeConfig, theme, elem, name, "ICO", 20, NULL, NULL);
			} else if (!strcmp(elementsType[TYPE_ITEM_COVER], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_GAME_IMAGE, 520, theme->usedHeight >> 1, ALIGN_CENTER, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, gDefaultCol, FNT_DEFAULT);
				initGameImage(themePath, themeConfig, theme, elem, name, "COV", 10, NULL, NULL);
			} else if (!strcmp(elementsType[TYPE_ITEM_TEXT], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_ITEM_TEXT, 520, 370, ALIGN_CENTER, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, theme->textColor, FNT_DEFAULT);
				elem->drawElem = &drawItemText;
			} else if (!strcmp(elementsType[TYPE_HINT_TEXT], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_HINT_TEXT, 16, -HINT_HEIGHT, ALIGN_NONE, 12, 20, SCALING_RATIO, theme->textColor, FNT_DEFAULT);
				elem->drawElem = &drawHintText;
			} else if (!strcmp(elementsType[TYPE_INFO_HINT_TEXT], type)) {
				elem = initBasic(themePath, themeConfig, theme, name, TYPE_INFO_HINT_TEXT, 16, -HINT_HEIGHT, ALIGN_NONE, 12, 20, SCALING_RATIO, theme->textColor, FNT_DEFAULT);
				elem->drawElem = &drawInfoHintText;
			} else if (!strcmp(elementsType[TYPE_LOADING_ICON], type)) {
				if (!theme->loadingIcon)
					theme->loadingIcon = initBasic(themePath, themeConfig, theme, name, TYPE_LOADING_ICON, -40, -60, ALIGN_CENTER, DIM_UNDEF, DIM_UNDEF, SCALING_RATIO, gDefaultCol, FNT_DEFAULT);
			}

			if (elem) {
				if (!elems->first)
					elems->first = elem;

				if (!elems->last)
					elems->last = elem;
				else {
					elems->last->next = elem;
					elems->last = elem;
				}
			}
		} else
			return 0; // ends the reading of elements
	}

	return 1;
}

static void freeGUIElems(theme_elems_t* elems) {
	theme_element_t* elem = elems->first;
	while (elem) {
		elems->first = elem->next;
		elem->endElem(elem);
		elem = elems->first;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

GSTEXTURE* thmGetTexture(unsigned int id) {
	if (id >= TEXTURES_COUNT)
		return NULL;
	else {
		// see if the texture is valid
		GSTEXTURE* txt = &gTheme->textures[id];
		
		if (txt->Mem)
			return txt;
		else
			return NULL;
	}
}

static void thmFree(theme_t* theme) {
	if (theme) {
		// free elements
		freeGUIElems(&theme->mainElems);
		freeGUIElems(&theme->infoElems);

		// free textures
		GSTEXTURE* texture;
		int id = 0;
		for(; id < TEXTURES_COUNT; id++) {
			texture = &theme->textures[id];
			if (texture->Mem != NULL) {
				free(texture->Mem);
				texture->Mem = NULL;
			}
		}

		// free fonts
		for (id = 0; id < THM_MAX_FONTS; ++id) {
			int fntid = theme->fonts[id];

			if (fntid != FNT_DEFAULT)
				fntRelease(fntid);
		}

		free(theme);
		theme = NULL;
	}
}

static int thmReadEntry(int index, char* path, char* separator, char* name, unsigned int mode) {
	if (FIO_SO_ISDIR(mode) && strstr(name, "thm_")) {
		theme_file_t* currTheme = &themes[nThemes + index];

		int length = strlen(name) - 4 + 1;
		currTheme->name = (char*) malloc(length * sizeof(char));
		memcpy(currTheme->name, name + 4, length);
		currTheme->name[length - 1] = '\0';

		length = strlen(path) + 1 + strlen(name) + 1 + 1;
		currTheme->filePath = (char*) malloc(length * sizeof(char));
		sprintf(currTheme->filePath, "%s%s%s%s", path, separator, name, separator);

		LOG("THEMES Theme found: %s\n", currTheme->filePath);

		index++;
	}
	return index;
}

/* themePath must contains the leading separator (as it is dependent of the device, we can't know here) */
static int thmLoadResource(int texId, char* themePath, short psm, int useDefault) {
	int success = -1;
	GSTEXTURE* texture = &gTheme->textures[texId];

	if (themePath != NULL)
		success = texDiscoverLoad(texture, themePath, texId, psm); // only set success here

	if ((success < 0) && useDefault)
		texPngLoad(texture, NULL, texId, psm);  // we don't mind the result of "default"

	return success;
}

static void thmSetColors(theme_t* theme) {
	memcpy(theme->bgColor, gDefaultBgColor, 3);
	theme->textColor = GS_SETREG_RGBA(gDefaultTextColor[0], gDefaultTextColor[1], gDefaultTextColor[2], 0xff);
	theme->uiTextColor = GS_SETREG_RGBA(gDefaultUITextColor[0], gDefaultUITextColor[1], gDefaultUITextColor[2], 0xff);
	theme->selTextColor = GS_SETREG_RGBA(gDefaultSelTextColor[0], gDefaultSelTextColor[1], gDefaultSelTextColor[2], 0xff);

	theme_element_t* elem = theme->mainElems.first;
	while (elem) {
		elem->color = theme->textColor;
		elem = elem->next;
	}
}

static void thmLoadFonts(config_set_t* themeConfig, const char* themePath, theme_t* theme) {
    int fntID; // theme side font id, not the fntSys handle
	for (fntID = -1; fntID < THM_MAX_FONTS; ++fntID) {
		// does the font by the key exist?
		char fntKey[16];

		// -1 is a placeholder for default font...
		if (fntID >= 0) {
			// Default font handle...
			theme->fonts[fntID] = FNT_DEFAULT;
			snprintf(fntKey, 16, "font%d", fntID);
		} else {
			snprintf(fntKey, 16, "default_font");
		}

		char *fntFile;
		int cfgKeyOK = configGetStr(themeConfig, fntKey, &fntFile);
		if (!cfgKeyOK && (fntID >= 0))
			continue;

		char fullPath[128];

		if (fntID < 0) {
			// replace the default font
			if (cfgKeyOK) {
				snprintf(fullPath, 128, "%s%s", themePath, fntFile);

				int size = -1;
				void* customFont = readFile(fullPath, -1, &size);

				if (customFont)
					fntReplace(FNT_DEFAULT, customFont, size, 1, 0);
			} else {
				// TODO reload the language font file
				fntSetDefault(FNT_DEFAULT);
			}
		} else {
			snprintf(fullPath, 128, "%s%s", themePath, fntFile);
			int fntHandle = fntLoadFile(fullPath);

			// Do we have a valid font? Assign the font handle to the theme font slot
			if (fntHandle != FNT_ERROR)
				theme->fonts[fntID] = fntHandle;
		};
	};
}

static void thmLoad(char* themePath) {
	LOG("THEMES Load theme path=%s\n", themePath);
	theme_t* curT = gTheme;
	theme_t* newT = (theme_t*) malloc(sizeof(theme_t));
	memset(newT, 0, sizeof(theme_t));

	newT->useDefault = 1;
	newT->usedHeight = 480;
	thmSetColors(newT);
	newT->mainElems.first = NULL;
	newT->mainElems.last = NULL;
	newT->infoElems.first = NULL;
	newT->infoElems.last = NULL;
	newT->gameCacheCount = 0;
	newT->itemsList = NULL;
	newT->loadingIcon = NULL;
	newT->loadingIconCount = LOAD7_ICON - LOAD0_ICON + 1;

	config_set_t* themeConfig = NULL;
	if (!themePath) {
		themeConfig = configAlloc(0, NULL, NULL);
		addGUIElem(themePath, themeConfig, newT, &newT->mainElems, elementsType[TYPE_MENU_ICON], "_");
		addGUIElem(themePath, themeConfig, newT, &newT->mainElems, elementsType[TYPE_MENU_TEXT], "_");
		addGUIElem(themePath, themeConfig, newT, &newT->mainElems, elementsType[TYPE_ITEMS_LIST], "_");
		addGUIElem(themePath, themeConfig, newT, &newT->mainElems, elementsType[TYPE_ITEM_ICON], "_");
		addGUIElem(themePath, themeConfig, newT, &newT->mainElems, elementsType[TYPE_ITEM_COVER], "_");
		addGUIElem(themePath, themeConfig, newT, &newT->mainElems, elementsType[TYPE_ITEM_TEXT], "_");
		addGUIElem(themePath, themeConfig, newT, &newT->mainElems, elementsType[TYPE_HINT_TEXT], "_");
		addGUIElem(themePath, themeConfig, newT, &newT->mainElems, elementsType[TYPE_LOADING_ICON], "_");

		// reset the default font to be sure
		fntSetDefault(FNT_DEFAULT);
	} else {
		char path[255];
		snprintf(path, 255, "%sconf_theme.cfg", themePath);
		themeConfig = configAlloc(0, NULL, path);
		configRead(themeConfig); // try to load the theme config file

		int intValue;
		if (configGetInt(themeConfig, "use_default", &intValue))
			newT->useDefault = intValue;

		if (configGetInt(themeConfig, "use_real_height", &intValue)) {
			if (intValue)
				newT->usedHeight = screenHeight;
		}

		configGetColor(themeConfig, "bg_color", newT->bgColor);

		unsigned char color[3];
		if (configGetColor(themeConfig, "text_color", color))
			newT->textColor = GS_SETREG_RGBA(color[0], color[1], color[2], 0xff);

		if (configGetColor(themeConfig, "ui_text_color", color))
			newT->uiTextColor = GS_SETREG_RGBA(color[0], color[1], color[2], 0xff);

		if (configGetColor(themeConfig, "sel_text_color", color))
			newT->selTextColor = GS_SETREG_RGBA(color[0], color[1], color[2], 0xff);

		// before loading the element definitions, we have to have the fonts prepared
		// for that, we load the fonts and a translation table
		thmLoadFonts(themeConfig, themePath, newT);

		int i = 1;
		snprintf(path, 255, "main0");
		while (addGUIElem(themePath, themeConfig, newT, &newT->mainElems, NULL, path))
			snprintf(path, 255, "main%d", i++);

		i = 1;
		snprintf(path, 255, "info0");
		while(addGUIElem(themePath, themeConfig, newT, &newT->infoElems, NULL, path))
			snprintf(path, 255, "info%d", i++);
	}

	validateGUIElems(themePath, themeConfig, newT);
	configFree(themeConfig);

	LOG("THEMES Number of cache: %d\n", newT->gameCacheCount);
	LOG("THEMES Used height: %d\n", newT->usedHeight);

	/// Now swap theme and start loading textures
	if (newT->usedHeight == screenHeight)
		rmResetShiftRatio();
	else
		rmSetShiftRatio((float) screenHeight / newT->usedHeight);
	
	int i;
	// default all to not loaded...
	for (i = 0; i < TEXTURES_COUNT; i++) {
		newT->textures[i].Mem = NULL;
	}

	// LOGO, loaded here to avoid flickering during startup with device in AUTO + theme set
	texPngLoad(&newT->textures[LOGO_PICTURE], NULL, LOGO_PICTURE, GS_PSM_CT24);

	gTheme = newT;
	thmFree(curT);

	// First start with busy icon
	char* path = themePath;
	int customBusy = 0;
	for (i = LOAD0_ICON; i <= LOAD7_ICON; i++) {
		if (thmLoadResource(i, path, GS_PSM_CT32, gTheme->useDefault) >= 0)
			customBusy = 1;
		else {
			if (customBusy)
				break;
			else
				path = NULL;
		}
	}
	gTheme->loadingIconCount = i;

	// Customizable icons
	for (i = USB_ICON; i <= START_ICON; i++)
		thmLoadResource(i, themePath, GS_PSM_CT32, gTheme->useDefault);

	// Not  customizable icons
	for (i = L1_ICON; i <= R2_ICON; i++)
		thmLoadResource(i, NULL, GS_PSM_CT32, 1);
}

static void thmRebuildGuiNames() {
	if (guiThemesNames)
		free(guiThemesNames);

	// build the languages name list
	guiThemesNames = (char**) malloc((nThemes + 2) * sizeof(char**));

	// add default internal
	guiThemesNames[0] = "<OPL>";

	int i = 0;
	for (; i < nThemes; i++) {
		guiThemesNames[i + 1] =  themes[i].name;
	}

	guiThemesNames[nThemes + 1] = NULL;
}

void thmAddElements(char* path, char* separator, int mode) {
	nThemes += listDir(path, separator, THM_MAX_FILES - nThemes, &thmReadEntry);
	thmRebuildGuiNames();

	char* temp;
	if (configGetStr(configGetByType(CONFIG_OPL), "theme", &temp)) {
		LOG("THEMES Trying to set again theme: %s\n", temp);
		if (thmSetGuiValue(thmFindGuiID(temp), 0))
			moduleUpdateMenu(mode, 1);
	}
}

void thmInit() {
	LOG("THEMES Init\n");
	gTheme = NULL;

	thmReloadScreenExtents();

	// initialize default internal
	thmLoad(NULL);

	thmAddElements(gBaseMCDir, "/", -1);
}

void thmReinit(char* path) {
	thmLoad(NULL);
	guiThemeID = 0;

	int i = 0;
	while (i < nThemes) {
		if (strncmp(themes[i].filePath, path, strlen(path)) == 0) {
			LOG("THEMES Remove theme: %s\n", themes[i].filePath);
			nThemes--;
			free(themes[i].name);
			themes[i].name = themes[nThemes].name;
			themes[nThemes].name = NULL;
			free(themes[i].filePath);
			themes[i].filePath = themes[nThemes].filePath;
			themes[nThemes].filePath = NULL;
		} else
			i++;
	}

	thmRebuildGuiNames();
}

void thmReloadScreenExtents() {
	rmGetScreenExtents(&screenWidth, &screenHeight);
}

char* thmGetValue() {
	return guiThemesNames[guiThemeID];
}

int thmSetGuiValue(int themeID, int reload) {
	if (themeID != -1) {
		if (guiThemeID != themeID || reload) {
			if (themeID != 0)
				thmLoad(themes[themeID - 1].filePath);
			else
				thmLoad(NULL);

			guiThemeID = themeID;
			configSetStr(configGetByType(CONFIG_OPL), "theme", thmGetValue());
			return 1;
		}
		else if (guiThemeID == 0)
			thmSetColors(gTheme);
	}
	return 0;
}

int thmGetGuiValue() {
	return guiThemeID;
}

int thmFindGuiID(char* theme) {
	if (theme) {
		int i = 0;
		for (; i < nThemes; i++) {
			if (stricmp(themes[i].name, theme) == 0)
				return i + 1;
		}
	}
	return 0;
}

char **thmGetGuiList() {
	return guiThemesNames;
}

void thmEnd() {
	thmFree(gTheme);

	int i = 0;
	for (; i < nThemes; i++) {
		free(themes[i].name);
		free(themes[i].filePath);
	}

	free(guiThemesNames);
}