File: ConfDlg.c

package info (click to toggle)
pcsxr 1.9.94-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 10,572 kB
  • sloc: ansic: 117,276; objc: 8,159; cpp: 1,140; makefile: 315; asm: 145; pascal: 30; sh: 20
file content (989 lines) | stat: -rw-r--r-- 32,974 bytes parent folder | download | duplicates (4)
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
/*  Pcsx - Pc Psx Emulator
 *  Copyright (C) 1999-2002  Pcsx Team
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <dlfcn.h>
#include <sys/stat.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <signal.h>
#include <sys/time.h>
#include <regex.h>
#include "Linux.h"
#include "ConfDlg.h"

#include "../libpcsxcore/plugins.h"

static void OnBiosPath_Changed(GtkWidget *wdg, gpointer data);
static void OnConf_Clicked(GtkDialog *dialog, gint arg1, gpointer user_data);
static void OnPluginPath_Changed(GtkWidget *wdg, gpointer data);
static void OnConfConf_Pad1About(GtkWidget *widget, gpointer user_data);
static void OnConfConf_Pad2About(GtkWidget *widget, gpointer user_data);
static void OnConfConf_Pad1Conf(GtkWidget *widget, gpointer user_data);
static void OnConfConf_Pad2Conf(GtkWidget *widget, gpointer user_data);
static void OnNet_Conf(GtkWidget *widget, gpointer user_data);
static void OnNet_About(GtkWidget *widget, gpointer user_data);
static void on_configure_plugin(GtkWidget *widget, gpointer user_data);
static void on_about_plugin(GtkWidget *widget, gpointer user_data);
static void UpdatePluginsBIOS_UpdateGUI();
static void FindNetPlugin();

PSEgetLibType		PSE_getLibType = NULL;
PSEgetLibVersion	PSE_getLibVersion = NULL;
PSEgetLibName		PSE_getLibName = NULL;

static GtkBuilder *builder;
GtkWidget *ConfDlg = NULL;
GtkWidget *NetDlg = NULL;
GtkWidget *controlwidget = NULL;

PluginConf GpuConfS;
PluginConf SpuConfS;
PluginConf CdrConfS;
PluginConf Pad1ConfS;
PluginConf Pad2ConfS;
PluginConf NetConfS;
#ifdef ENABLE_SIO1API
PluginConf Sio1ConfS;
#endif
PluginConf BiosConfS;

#define FindComboText(combo, list, conf) \
	if (strlen(conf) > 0) { \
		int i; \
		for (i = 2; i < 255; i += 2) { \
			if (!strcmp(conf, list[i - 2])) { \
				gtk_combo_box_set_active(GTK_COMBO_BOX(combo), i / 2 - 1); \
				break; \
			} \
		} \
	}

#define GetComboText(combo, list, conf) \
	{ \
		int row; \
		row = gtk_combo_box_get_active(GTK_COMBO_BOX(combo)); \
		strcpy(conf, (char *)list[row * 2]); \
	}

void ConfigurePlugins() {
	if (!UseGui) {
		/* How do we get here if we're not running the GUI? */
		/* Ryan: we're going to imagine that someday, there will be a way
		 * to configure plugins from the commandline */
		printf("ERROR: Plugins cannot be configured without the GUI.");
		return;
	}

	GtkWidget *widget;

	gchar *path;

	UpdatePluginsBIOS();

	builder = gtk_builder_new();
	if (!gtk_builder_add_from_file(builder, PACKAGE_DATA_DIR "pcsxr.ui", NULL)) {
		g_warning("Error: interface could not be loaded!");
		return;
	}

	UpdatePluginsBIOS_UpdateGUI(builder);

	ConfDlg = GTK_WIDGET(gtk_builder_get_object(builder, "ConfDlg"));
	
	gtk_window_set_title(GTK_WINDOW(ConfDlg), _("Configure PCSXR"));
	gtk_widget_show (ConfDlg);

	/* Set the paths in the file choosers to be based on the saved configurations */
	widget = GTK_WIDGET(gtk_builder_get_object(builder, "GtkFileChooser_Bios"));
	gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), Config.BiosDir);

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "GtkFileChooser_Plugin"));
	gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), Config.PluginsDir);

	if (strlen(Config.PluginsDir) == 0) {
		if((path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget))) != NULL) {
			strcpy(Config.PluginsDir, path);
			g_free(path);
		}
	}

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_ConfGpu"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(on_configure_plugin), GINT_TO_POINTER(PSE_LT_GPU), NULL, G_CONNECT_AFTER);

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_ConfSpu"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(on_configure_plugin), GINT_TO_POINTER(PSE_LT_SPU), NULL, G_CONNECT_AFTER);

	/* ADB TODO Does pad 1 and 2 need to be different? */
	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_ConfPad1"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
						  G_CALLBACK(OnConfConf_Pad1Conf), builder, NULL, G_CONNECT_AFTER);

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_ConfPad2"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(OnConfConf_Pad2Conf), builder, NULL, G_CONNECT_AFTER);

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_ConfCdr"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(on_configure_plugin), GINT_TO_POINTER(PSE_LT_CDR), NULL, G_CONNECT_AFTER);
#ifdef ENABLE_SIO1API
	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_ConfSio1"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(on_configure_plugin), (gpointer) PSE_LT_SIO1, NULL, G_CONNECT_AFTER);
#else
	widget = GTK_WIDGET(gtk_builder_get_object(builder, "label18"));
	gtk_widget_set_sensitive(widget, FALSE);
	widget = GTK_WIDGET(gtk_builder_get_object(builder, "GtkCombo_Sio1"));
	gtk_widget_set_sensitive(widget, FALSE);
	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_ConfSio1"));
	gtk_widget_set_sensitive(widget, FALSE);
	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_AboutSio1"));
	gtk_widget_set_sensitive(widget, FALSE);
#endif
	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_AboutGpu"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(on_about_plugin), GINT_TO_POINTER(PSE_LT_GPU), NULL, G_CONNECT_AFTER);

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_AboutSpu"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(on_about_plugin), GINT_TO_POINTER(PSE_LT_SPU), NULL, G_CONNECT_AFTER);

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_AboutPad1"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(OnConfConf_Pad1About), builder, NULL, G_CONNECT_AFTER);

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_AboutPad2"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(OnConfConf_Pad2About), builder, NULL, G_CONNECT_AFTER);

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_AboutCdr"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(on_about_plugin), GINT_TO_POINTER(PSE_LT_CDR), NULL, G_CONNECT_AFTER);
#ifdef ENABLE_SIO1API
	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_AboutSio1"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(on_about_plugin), (gpointer) PSE_LT_SIO1, NULL, G_CONNECT_AFTER);
#endif
	widget = GTK_WIDGET(gtk_builder_get_object(builder, "GtkFileChooser_Bios"));
	g_signal_connect_data(G_OBJECT(widget), "current_folder_changed",
			G_CALLBACK(OnBiosPath_Changed), builder, NULL, G_CONNECT_AFTER);

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "GtkFileChooser_Plugin"));
	g_signal_connect_data(G_OBJECT(widget), "current_folder_changed",
			G_CALLBACK(OnPluginPath_Changed), builder, NULL, G_CONNECT_AFTER);

	g_signal_connect_data(G_OBJECT(ConfDlg), "response",
			G_CALLBACK(OnConf_Clicked), builder, (GClosureNotify)g_object_unref, G_CONNECT_AFTER);
}

void OnNet_Clicked(GtkDialog *dialog, gint arg1, gpointer user_data) {
	GetComboText(NetConfS.Combo, NetConfS.plist, Config.Net);
	SaveConfig();
	gtk_widget_destroy(GTK_WIDGET(dialog));
	NetDlg = NULL;
}

void OnConf_Net() {
	GtkWidget *widget;

	if (NetDlg != NULL) {
		gtk_window_present (GTK_WINDOW (NetDlg));
		return;
	}

	builder = gtk_builder_new();
	if (!gtk_builder_add_from_file(builder, PACKAGE_DATA_DIR "pcsxr.ui", NULL)) {
		g_warning("Error: interface could not be loaded!");
		return;
	}

	NetDlg = GTK_WIDGET(gtk_builder_get_object(builder, "NetDlg"));
	
	gtk_widget_show (NetDlg);

	FindNetPlugin(builder);

	/* Setup a handler for when Close or Cancel is clicked */
	g_signal_connect_data(G_OBJECT(NetDlg), "response",
			G_CALLBACK(OnNet_Clicked), builder, (GClosureNotify)g_object_unref, G_CONNECT_AFTER);

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_ConfNet"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(OnNet_Conf), builder, NULL, G_CONNECT_AFTER);

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_AboutNet"));
	g_signal_connect_data(G_OBJECT(widget), "clicked",
			G_CALLBACK(OnNet_About), builder, NULL, G_CONNECT_AFTER);
}

void OnConf_Graphics() {
	void *drv;
	GPUconfigure conf;
	char Plugin[MAXPATHLEN];

	sprintf(Plugin, "%s/%s", Config.PluginsDir, Config.Gpu);
	drv = SysLoadLibrary(Plugin);
	if (drv == NULL) { printf("Error with file %s\n", Plugin); return; }

	while (gtk_events_pending()) gtk_main_iteration();

	conf = (GPUconfigure)SysLoadSym(drv, "GPUconfigure");
	if (conf != NULL) {
		conf();
	}
	else
		SysInfoMessage (_("No configuration required"), _("This plugin doesn't need to be configured."));

	SysCloseLibrary(drv);
}

void OnConf_Sound() {
	void *drv;
	SPUconfigure conf;
	char Plugin[MAXPATHLEN];

	sprintf(Plugin, "%s/%s", Config.PluginsDir, Config.Spu);
	drv = SysLoadLibrary(Plugin);
	if (drv == NULL) { printf("Error with file %s\n", Plugin); return; }

	while (gtk_events_pending()) gtk_main_iteration();

	conf = (GPUconfigure)SysLoadSym(drv, "SPUconfigure");
	if (conf != NULL) {
		conf();
	}
	else
		SysInfoMessage (_("No configuration required"), _("This plugin doesn't need to be configured."));

	SysCloseLibrary(drv);
}

void OnConf_CdRom() {
	void *drv;
	CDRconfigure conf;
	char Plugin[MAXPATHLEN];

	sprintf(Plugin, "%s/%s", Config.PluginsDir, Config.Cdr);
	drv = SysLoadLibrary(Plugin);
	if (drv == NULL) { printf("Error with file %s\n", Plugin); return; }

	while (gtk_events_pending()) gtk_main_iteration();

	conf = (GPUconfigure)SysLoadSym(drv, "CDRconfigure");
	if (conf != NULL) {
		conf();
	}
	else
		SysInfoMessage (_("No configuration required"), _("This plugin doesn't need to be configured."));

	SysCloseLibrary(drv);
}
#ifdef ENABLE_SIO1API
void OnConf_Sio1() {
	void *drv;
	SIO1configure conf;
	char Plugin[MAXPATHLEN];

	sprintf(Plugin, "%s/%s", Config.PluginsDir, Config.Sio1);
	drv = SysLoadLibrary(Plugin);
	if (drv == NULL) { printf("Error with file %s\n", Plugin); return; }

	while (gtk_events_pending()) gtk_main_iteration();

	conf = (SIO1configure)SysLoadSym(drv, "SIO1configure");
	if (conf != NULL) {
		conf();
	}
	else
		SysInfoMessage (_("No configuration required"), _("This plugin doesn't need to be configured."));

	SysCloseLibrary(drv);
}
#endif
void OnConf_Pad() {
	void *drv;
	PADconfigure conf;
	char Plugin[MAXPATHLEN];

	sprintf(Plugin, "%s/%s", Config.PluginsDir, Config.Pad1);
	drv = SysLoadLibrary(Plugin);
	if (drv == NULL) { printf("Error with file %s\n", Plugin); return; }

	while (gtk_events_pending()) gtk_main_iteration();

	conf = (PADconfigure)SysLoadSym(drv, "PADconfigure");
	if (conf != NULL) {
		conf();
	}
	else
		SysInfoMessage (_("No configuration required"), _("This plugin doesn't need to be configured."));

	SysCloseLibrary(drv);

	if (strcmp(Config.Pad1, Config.Pad2) != 0) {
		sprintf(Plugin, "%s/%s", Config.PluginsDir, Config.Pad2);
		drv = SysLoadLibrary(Plugin);
		if (drv == NULL) { printf("Error with file %s\n", Plugin); return; }

		while (gtk_events_pending()) gtk_main_iteration();

		conf = (PADconfigure)SysLoadSym(drv, "PADconfigure");
		if (conf != NULL) {
			conf();
		}

		SysCloseLibrary(drv);
	}
}

static int all_config_set() {
	int retval;

	if ((strlen(Config.Gpu) != 0) &&
		(strlen(Config.Spu) != 0) &&
		(strlen(Config.Cdr) != 0) &&
#ifdef ENABLE_SIO1API
		(strlen(Config.Sio1) != 0) &&
#endif
		(strlen(Config.Pad1) != 0) &&
		(strlen(Config.Pad2) != 0))
		retval = TRUE;
	else
		retval = FALSE;

	return retval;
}

/* TODO Check whether configuration is required when we choose the plugin, and set the state of the
    button appropriately. New gtk tooltip API should allow us to put a tooltip explanation for
    disabled widgets */
/* TODO If combo screen hasn't been opened and the user chooses the menu config option, confs.Combo will be null and cause a segfault */
#define ConfPlugin(src, confs, plugin, name, parent)  { \
	void *drv; \
	src conf; \
	gchar *filename; \
 \
	GetComboText(confs.Combo, confs.plist, plugin); \
	if (strlen(plugin) > 0) { \
		filename = g_build_filename (getenv("HOME"), PLUGINS_DIR, plugin, NULL); \
		/*printf("Configuring plugin %s\n", filename);*/ \
		drv = SysLoadLibrary(filename); \
		if (drv == NULL) {printf("Error with file %s\n", filename);return; } \
\
		while (gtk_events_pending()) gtk_main_iteration(); \
			conf = (src) SysLoadSym(drv, name); \
		if (conf) { \
			conf(); \
		} else \
			SysInfoMessage (_("No configuration required"), _("This plugin doesn't need to be configured.")); \
		SysCloseLibrary(drv); \
		g_free (filename); \
	} else SysInfoMessage (name, _("Please select a plugin.")); \
}

static void on_configure_plugin(GtkWidget *widget, gpointer user_data) {
	gint plugin_type = GPOINTER_TO_INT(user_data);

	while (gtk_events_pending())
		gtk_main_iteration();

	switch (plugin_type) {
		case PSE_LT_GPU:
			ConfPlugin(GPUconfigure, GpuConfS, Config.Gpu, "GPUconfigure", ConfDlg);
			break;
		case PSE_LT_SPU:
			ConfPlugin(SPUconfigure, SpuConfS, Config.Spu, "SPUconfigure", ConfDlg);
			break;
		case PSE_LT_CDR:
			ConfPlugin(CDRconfigure, CdrConfS, Config.Cdr, "CDRconfigure", ConfDlg);
			break;
#ifdef ENABLE_SIO1API
		case PSE_LT_SIO1:
			ConfPlugin(SIO1configure, Sio1ConfS, Config.Sio1, "SIO1configure", ConfDlg);
			break;
#endif
	}
}

static void on_about_plugin(GtkWidget *widget, gpointer user_data) {
	gint plugin_type = GPOINTER_TO_INT(user_data);

	while (gtk_events_pending())
		gtk_main_iteration();

	switch (plugin_type) {
		case PSE_LT_GPU:
			ConfPlugin(GPUconfigure, GpuConfS, Config.Gpu, "GPUabout", ConfDlg);
			break;
		case PSE_LT_SPU:
			ConfPlugin(SPUconfigure, SpuConfS, Config.Spu, "SPUabout", ConfDlg);
			break;
		case PSE_LT_CDR:
			ConfPlugin(CDRconfigure, CdrConfS, Config.Cdr, "CDRabout", ConfDlg);
			break;
#ifdef ENABLE_SIO1API
		case PSE_LT_SIO1:
			ConfPlugin(SIO1configure, Sio1ConfS, Config.Sio1, "SIO1about", ConfDlg);
			break;
#endif
	}
}

static void OnConfConf_Pad1About(GtkWidget *widget, gpointer user_data) {
	ConfPlugin(PADabout, Pad1ConfS, Config.Pad1, "PADabout", ConfDlg);
}

static void OnConfConf_Pad2About(GtkWidget *widget, gpointer user_data) {
	ConfPlugin(PADabout, Pad2ConfS, Config.Pad2, "PADabout", ConfDlg);
}

static void OnConfConf_Pad1Conf(GtkWidget *widget, gpointer user_data) {
	ConfPlugin(PADabout, Pad1ConfS, Config.Pad1, "PADconfigure", ConfDlg);
}

static void OnConfConf_Pad2Conf(GtkWidget *widget, gpointer user_data) {
	ConfPlugin(PADabout, Pad2ConfS, Config.Pad2, "PADconfigure", ConfDlg);
}

static void OnNet_Conf(GtkWidget *widget, gpointer user_data) {
	ConfPlugin(NETconfigure, NetConfS, Config.Net, "NETconfigure", NetDlg);
}

static void OnNet_About(GtkWidget *widget, gpointer user_data) {
	ConfPlugin(NETabout, NetConfS, Config.Net, "NETabout", NetDlg);
}

static void OnPluginPath_Changed(GtkWidget *wdg, gpointer data) {
	gchar *path;

	path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (wdg));
	GSList * l = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER (wdg));
	//printf(("%s and %s\n"), path, l->data);

    if (l) path = l->data;
	strcpy(Config.PluginsDir, path);

    UpdatePluginsBIOS();
	UpdatePluginsBIOS_UpdateGUI(data);

	g_free(path);
}

static void OnBiosPath_Changed(GtkWidget *wdg, gpointer data) {
	gchar *foldername;

	foldername = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (wdg));
	GSList * l = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER (wdg));
	//printf(("%s and %s\n"), foldername, l->data);

	if (l) foldername = l->data;
	strcpy(Config.BiosDir, foldername);

	UpdatePluginsBIOS();
	UpdatePluginsBIOS_UpdateGUI(data);

	g_free(foldername);
}

void OnConf_Clicked(GtkDialog *dialog, gint arg1, gpointer user_data) {
	GetComboText(GpuConfS.Combo, GpuConfS.plist, Config.Gpu);
	GetComboText(SpuConfS.Combo, SpuConfS.plist, Config.Spu);
	GetComboText(CdrConfS.Combo, CdrConfS.plist, Config.Cdr);
#ifdef ENABLE_SIO1API
	GetComboText(Sio1ConfS.Combo, Sio1ConfS.plist, Config.Sio1);
#endif
	GetComboText(Pad1ConfS.Combo, Pad1ConfS.plist, Config.Pad1);
	GetComboText(Pad2ConfS.Combo, Pad2ConfS.plist, Config.Pad2);
	GetComboText(BiosConfS.Combo, BiosConfS.plist, Config.Bios);

	SaveConfig();

	gtk_widget_destroy(ConfDlg);
	ConfDlg = NULL;
}

#define ComboAddPlugin(type) { \
	type##ConfS.plugins += 2; \
	strcpy(type##ConfS.plist[type##ConfS.plugins - 1], name); \
	strcpy(type##ConfS.plist[type##ConfS.plugins - 2], ent->d_name); \
	type##ConfS.glist = g_list_append(type##ConfS.glist, type##ConfS.plist[type##ConfS.plugins-1]); \
}

void populate_combo_box(GtkWidget *widget, GList *list) {
	GtkListStore *store;
	GtkCellRenderer *renderer;
	store = gtk_list_store_new(1, G_TYPE_STRING);

	// Clear existing data from combo box
	gtk_cell_layout_clear(GTK_CELL_LAYOUT(widget));

	renderer = gtk_cell_renderer_text_new();
	gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widget), renderer, FALSE);
	gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(widget), renderer, "text", 0);

	while (list != NULL) {
		GtkTreeIter iter;
		gtk_list_store_append(store, &iter);
		gtk_list_store_set(store, &iter, 0, (char *)list->data, -1);
		list = list->next;
	}

	gtk_combo_box_set_model(GTK_COMBO_BOX(widget), GTK_TREE_MODEL(store));
}

#define ConfCreatePConf(name, type) \
	/* Populate the relevant combo widget with the list of plugins. \
	   If no plugins available, disable the combo and its controls. \
	   Note that the Bios plugin has no About/Conf control. */ \
	type##ConfS.Combo = GTK_WIDGET(gtk_builder_get_object(builder, "GtkCombo_" name)); \
	if (type##ConfS.glist != NULL) { \
		populate_combo_box (type##ConfS.Combo, type##ConfS.glist); \
		FindComboText(type##ConfS.Combo, type##ConfS.plist, Config.type); \
		gtk_widget_set_sensitive (type##ConfS.Combo, TRUE); \
		if (g_ascii_strcasecmp (name, "Bios") != 0) { \
			controlwidget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_Conf" name)); \
			gtk_widget_set_sensitive (controlwidget, TRUE); \
			controlwidget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_About" name)); \
			gtk_widget_set_sensitive (controlwidget, TRUE); \
		} \
	} else { \
		if (g_ascii_strcasecmp (name, "Bios") != 0) { \
			gtk_combo_box_set_model(GTK_COMBO_BOX(type##ConfS.Combo), NULL); \
			gtk_widget_set_sensitive (type##ConfS.Combo, FALSE); \
			controlwidget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_Conf" name)); \
			gtk_widget_set_sensitive (controlwidget, FALSE); \
			controlwidget = GTK_WIDGET(gtk_builder_get_object(builder, "btn_About" name)); \
			gtk_widget_set_sensitive (controlwidget, FALSE); \
		} \
	}

int plugin_is_available(gchar *plugin) {
	int retval;
	gchar *pluginfile;
	struct stat stbuf;

	pluginfile = g_strconcat(getenv("HOME"), PLUGINS_DIR, plugin, NULL);

	if (stat(pluginfile, &stbuf) == -1)
		retval = FALSE;
	else
		retval = TRUE;

	g_free(pluginfile);

	return retval;
}

int plugins_configured() {
	// make sure there are choices for all of the plugins!!
	if (all_config_set() == FALSE)
		return FALSE;

	// and make sure they can all be accessed
	// if they can't be, wipe the variable and return FALSE
	if (plugin_is_available (Config.Gpu) == FALSE) { Config.Gpu[0] = '\0'; return FALSE; }
	if (plugin_is_available (Config.Spu) == FALSE) { Config.Spu[0] = '\0'; return FALSE; }
	if (plugin_is_available (Config.Cdr) == FALSE) { Config.Cdr[0] = '\0'; return FALSE; }
#ifdef ENABLE_SIO1API
	if (plugin_is_available (Config.Sio1) == FALSE) { Config.Sio1[0] = '\0'; return FALSE; }
#endif
	if (plugin_is_available (Config.Pad1) == FALSE) { Config.Pad1[0] = '\0'; return FALSE; }
	if (plugin_is_available (Config.Pad2) == FALSE) { Config.Pad2[0] = '\0'; return FALSE; }

	// if everything is happy, return TRUE
	return TRUE;
}

int is_valid_bios_file(gchar *filename) {
	int valid;
	struct stat buf;

	if ((stat(filename, &buf) == -1) || (buf.st_size != (1024*512)))
		valid = FALSE;
	else {
		valid = TRUE;
	}

	return valid;
}

// Add the name of the BIOS file to the drop-down list. This will
// be the filename, not the full path to the file
void add_bios_to_list(gchar *bios_name, gchar *internal_name) {
	BiosConfS.plugins += 2;
	strcpy(BiosConfS.plist[BiosConfS.plugins - 1], bios_name);
	strcpy(BiosConfS.plist[BiosConfS.plugins - 2], internal_name);
	BiosConfS.glist = g_list_append(BiosConfS.glist, BiosConfS.plist[BiosConfS.plugins - 1]);
}

void scan_bios_dir(gchar *dirname) {
	DIR *dir;
	struct dirent *ent;
	gchar *filename;

	dir = opendir(dirname);
	if (dir == NULL) {
		SysMessage(_("Could not open BIOS directory: '%s'\n"), dirname);
		return;
	}

	while ((ent = readdir(dir)) != NULL) {
		filename = g_build_filename(dirname, ent->d_name, NULL);
		if (is_valid_bios_file(filename))
			add_bios_to_list(g_path_get_basename(filename), g_path_get_basename (filename));
		g_free(filename);
	}
	closedir(dir);
}

void UpdatePluginsBIOS() {
	DIR *dir;
	struct dirent *ent;
	void *Handle;
	char name[256];
	gchar *linkname;

	GpuConfS.plugins  = 0;
	SpuConfS.plugins  = 0;
	CdrConfS.plugins  = 0;
#ifdef ENABLE_SIO1API
	Sio1ConfS.plugins = 0;
#endif
	Pad1ConfS.plugins = 0;
	Pad2ConfS.plugins = 0;
	BiosConfS.plugins = 0;
	GpuConfS.glist  = NULL;
	SpuConfS.glist  = NULL;
	CdrConfS.glist  = NULL;
#ifdef ENABLE_SIO1API
	Sio1ConfS.glist  = NULL;
#endif
	Pad1ConfS.glist = NULL;
	Pad2ConfS.glist = NULL;
	BiosConfS.glist = NULL;
	GpuConfS.plist[0][0]  = '\0';
	SpuConfS.plist[0][0]  = '\0';
	CdrConfS.plist[0][0]  = '\0';
#ifdef ENABLE_SIO1API
	Sio1ConfS.plist[0][0]  = '\0';
#endif
	Pad1ConfS.plist[0][0] = '\0';
	Pad2ConfS.plist[0][0] = '\0';
	BiosConfS.plist[0][0] = '\0';

	// Load and get plugin info
	dir = opendir(Config.PluginsDir);
	if (dir == NULL) {
		printf(_("Could not open directory: '%s'\n"), Config.PluginsDir);
		return;
	}
	while ((ent = readdir(dir)) != NULL) {
		long type, v;
		linkname = g_build_filename(Config.PluginsDir, ent->d_name, NULL);

		// only libraries past this point, not config tools
		if (strstr(linkname, ".so") == NULL && strstr(linkname, ".dylib") == NULL)
			continue;

		Handle = dlopen(linkname, RTLD_NOW);
		if (Handle == NULL) {
			printf("%s\n", dlerror());
			g_free(linkname);
			continue;
		}

		PSE_getLibType = (PSEgetLibType)dlsym(Handle, "PSEgetLibType");
		if (PSE_getLibType == NULL) {
			if (strstr(linkname, "gpu") != NULL) type = PSE_LT_GPU;
			else if (strstr(linkname, "cdr") != NULL) type = PSE_LT_CDR;
#ifdef ENABLE_SIO1API
			else if (strstr(linkname, "sio1") != NULL) type = PSE_LT_SIO1;
#endif
			else if (strstr(linkname, "spu") != NULL) type = PSE_LT_SPU;
			else if (strstr(linkname, "pad") != NULL) type = PSE_LT_PAD;
			else { g_free(linkname); continue; }
		}
		else type = PSE_getLibType();

		PSE_getLibName = (PSEgetLibName) dlsym(Handle, "PSEgetLibName");
		if (PSE_getLibName != NULL) {
			sprintf(name, "%s", PSE_getLibName());
			PSE_getLibVersion = (PSEgetLibVersion) dlsym(Handle, "PSEgetLibVersion");
			if (PSE_getLibVersion != NULL) {
				char ver[32];

				v = PSE_getLibVersion();
				sprintf(ver, " %ld.%ld.%ld", v >> 16, (v >> 8) & 0xff, v & 0xff);
				strcat(name, ver);
			}
		}
		else strcpy(name, ent->d_name);

		if (type & PSE_LT_CDR)
			ComboAddPlugin(Cdr);
#ifdef ENABLE_SIO1API
		if (type & PSE_LT_SIO1)
			ComboAddPlugin(Sio1);
#endif
		if (type & PSE_LT_GPU)
			ComboAddPlugin(Gpu);
		if (type & PSE_LT_SPU)
			ComboAddPlugin(Spu);
		if (type & PSE_LT_PAD) {
			PADquery query = (PADquery)dlsym(Handle, "PADquery");
			if (query() & 0x1) {
				ComboAddPlugin(Pad1);
			}
			if (query() & 0x2) {
				ComboAddPlugin(Pad2);
			}
		}
		g_free(linkname);
	}
	closedir(dir);

	scan_bios_dir(Config.BiosDir);

	// The BIOS list always contains the PCSXR internal BIOS
	add_bios_to_list(_("Simulate PSX BIOS"), "HLE");
}

static void UpdatePluginsBIOS_UpdateGUI() {
	// Populate the plugin combo boxes
	ConfCreatePConf("Gpu", Gpu);
	ConfCreatePConf("Spu", Spu);
	ConfCreatePConf("Pad1", Pad1);
	ConfCreatePConf("Pad2", Pad2);
	ConfCreatePConf("Cdr", Cdr);
#ifdef ENABLE_SIO1API
	ConfCreatePConf("Sio1", Sio1);
#endif
	ConfCreatePConf("Bios", Bios);
}

static void FindNetPlugin() {
	DIR *dir;
	struct dirent *ent;
	void *Handle;
	char plugin[MAXPATHLEN],name[MAXPATHLEN];

	NetConfS.plugins  = 0;
	NetConfS.glist = NULL; 

	NetConfS.plugins += 2;
	strcpy(NetConfS.plist[NetConfS.plugins - 1], "Disabled");
	strcpy(NetConfS.plist[NetConfS.plugins - 2], "Disabled");
	NetConfS.glist = g_list_append(NetConfS.glist, NetConfS.plist[NetConfS.plugins - 1]);

	dir = opendir(Config.PluginsDir);
	if (dir == NULL)
		SysMessage(_("Could not open directory: '%s'\n"), Config.PluginsDir);
	else {
		/* ADB TODO Replace the following with a function */
		while ((ent = readdir(dir)) != NULL) {
			long type, v;

			sprintf(plugin, "%s/%s", Config.PluginsDir, ent->d_name);

			if (strstr(plugin, ".so") == NULL && strstr(plugin, ".dylib") == NULL)
				continue;
			Handle = dlopen(plugin, RTLD_NOW);
			if (Handle == NULL) continue;

			PSE_getLibType = (PSEgetLibType) dlsym(Handle, "PSEgetLibType");
			if (PSE_getLibType == NULL) {
				if (strstr(plugin, "net") != NULL) type = PSE_LT_NET;
				else continue;
			}
			else type = PSE_getLibType();

			PSE_getLibName = (PSEgetLibName) dlsym(Handle, "PSEgetLibName");
			if (PSE_getLibName != NULL) {
				sprintf(name, "%s", PSE_getLibName());
				PSE_getLibVersion = (PSEgetLibVersion) dlsym(Handle, "PSEgetLibVersion");
				if (PSE_getLibVersion != NULL) {
					char ver[32];

					v = PSE_getLibVersion();
					sprintf(ver, " %ld.%ld.%ld",v>>16,(v>>8)&0xff,v&0xff);
					strcat(name, ver);
				}
			}
			else strcpy(name, ent->d_name);

			if (type & PSE_LT_NET) {
				ComboAddPlugin(Net);
			}
		}
		closedir(dir);

		ConfCreatePConf("Net", Net);
	}
}

GtkWidget *CpuDlg;
GtkWidget *PsxCombo;
GList *psxglist;
char *psxtypes[] = {
	"NTSC",
	"PAL"
};

// When the auto-detect CPU type is selected, disable the NTSC/PAL selection
static void OnCpu_PsxAutoClicked (GtkWidget *widget, gpointer user_data) {
	GtkWidget *combo;
	combo = GTK_WIDGET(gtk_builder_get_object(builder, "GtkCombo_PsxType"));

	gtk_widget_set_sensitive (combo,
			!(gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))));
}

// When the interpreter core is deselected, disable the debugger checkbox
static void OnCpu_CpuClicked(GtkWidget *widget, gpointer user_data) {
	GtkWidget *check;
	check = GTK_WIDGET(gtk_builder_get_object(builder, "GtkCheckButton_Dbg"));

	// Debugger is only working with interpreter not recompiler, so let's set it
	if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), FALSE);

	gtk_widget_set_sensitive (check,
			gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)));
}

void OnCpu_Clicked(GtkDialog *dialog, gint arg1, gpointer user_data) {
	GtkWidget *widget;
	int tmp;
	long t;

	widget = GTK_WIDGET(gtk_builder_get_object(builder, "GtkCombo_PsxType"));

	// If nothing chosen, default to NTSC
	tmp = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
	if (tmp == -1)	
		tmp = PSX_TYPE_NTSC;

	if (!strcmp("NTSC", psxtypes[tmp]))
		Config.PsxType = PSX_TYPE_NTSC;
	else
		Config.PsxType = PSX_TYPE_PAL;

	Config.Xa = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_Xa")));
	Config.SioIrq = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_SioIrq")));
	Config.Mdec = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_Mdec")));
	Config.Cdda = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_CDDA")));
	Config.SlowBoot = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_SlowBoot")));
	Config.PsxAuto = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_PsxAuto")));

	t = Config.Debug;
	Config.Debug = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_Dbg")));
	if (t != Config.Debug) {
		if (Config.Debug) StartDebugger();
		else StopDebugger();
	}

	t = Config.Cpu;
	Config.Cpu = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_Cpu")));
	if (t != Config.Cpu) {
		psxCpu->Shutdown();
#ifdef PSXREC
		if (Config.Cpu == CPU_INTERPRETER) {
			psxCpu = &psxInt;
		}
		else psxCpu = &psxRec;
#else
		psxCpu = &psxInt;
#endif
		if (psxCpu->Init() == -1) {
			SysClose();
			exit(1);
		}
		psxCpu->Reset();
	}

	Config.PsxOut = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_PsxOut")));
	Config.SpuIrq = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_SpuIrq")));
	Config.RCntFix = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_RCntFix")));
	Config.VSyncWA = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_VSyncWA")));
	Config.Widescreen = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_Widescreen")));

	SaveConfig();

	gtk_widget_destroy(CpuDlg);
	CpuDlg = NULL;
}

void OnConf_Cpu() {
	

	builder = gtk_builder_new();
	
	if (!gtk_builder_add_from_file(builder, PACKAGE_DATA_DIR "pcsxr.ui", NULL)) {
		g_warning("Error: interface could not be loaded!");
		return;
	}

	CpuDlg = GTK_WIDGET(gtk_builder_get_object(builder, "CpuDlg"));

	gtk_widget_show (CpuDlg);

	PsxCombo = GTK_WIDGET(gtk_builder_get_object(builder, "GtkCombo_PsxType"));
	gtk_combo_box_set_active(GTK_COMBO_BOX (PsxCombo), Config.PsxType);
	gtk_widget_set_sensitive(GTK_WIDGET (PsxCombo), !Config.PsxAuto);

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_Xa")), Config.Xa);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_SioIrq")), Config.SioIrq);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_Mdec")), Config.Mdec);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_CDDA")), Config.Cdda);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_SlowBoot")), Config.SlowBoot);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_PsxAuto")), Config.PsxAuto);

	g_signal_connect_data(G_OBJECT(gtk_builder_get_object(builder, "GtkCheckButton_PsxAuto")), "toggled",
			G_CALLBACK(OnCpu_PsxAutoClicked), builder, NULL, G_CONNECT_AFTER);

#ifdef PSXREC
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (gtk_builder_get_object(builder, "GtkCheckButton_Cpu")), Config.Cpu);

	g_signal_connect_data(G_OBJECT(gtk_builder_get_object(builder, "GtkCheckButton_Cpu")), "toggled",
			G_CALLBACK(OnCpu_CpuClicked), builder, NULL, G_CONNECT_AFTER);
#else
	Config.Cpu = CPU_INTERPRETER;

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (gtk_builder_get_object(builder, "GtkCheckButton_Cpu")), TRUE);
	gtk_widget_set_sensitive(GTK_WIDGET (gtk_builder_get_object(builder, "GtkCheckButton_Cpu")), FALSE);
#endif

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (gtk_builder_get_object(builder, "GtkCheckButton_Dbg")), Config.Cpu && Config.Debug);
	gtk_widget_set_sensitive(GTK_WIDGET (gtk_builder_get_object(builder, "GtkCheckButton_Dbg")), Config.Cpu);

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_PsxOut")), Config.PsxOut);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_SpuIrq")), Config.SpuIrq);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_RCntFix")), Config.RCntFix);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_VSyncWA")), Config.VSyncWA);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "GtkCheckButton_Widescreen")), Config.Widescreen);

	// Setup a handler for when Close or Cancel is clicked
	g_signal_connect_data(G_OBJECT(CpuDlg), "response",
			G_CALLBACK(OnCpu_Clicked), builder, (GClosureNotify)g_object_unref, G_CONNECT_AFTER);
}