summaryrefslogtreecommitdiff
path: root/python/berylsettings.pyx
blob: 28f5ec6a1904caf82eea38e0270c3b10b3a1cc23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
# 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.
#
# Authors: Quinn Storm (quinn@beryl-project.org)
# Copyright (C) 2007 Quinn Storm


#libberylsettings header
#ifndef LIB_BERYL_SETTINGS
#define LIB_BERYL_SETTINGS

#include <glib.h>
#include <beryl.h>
# as noted, all values in the structs are in the current locale, not C
# libberylsettings transparently handles translation back to C for
#		the options of type STRING and LIST[STRING] that have a list
#		of allowed values set.

#cdef struct _BerylSettingsContext BerylSettingsContext
#cdef struct _BerylSetting BerylSetting

#cdef gboolean (*BerylSettingsContextReadInitFunc) (BerylSettingsContext * context)
#cdef void (*BerylSettingsContextReadDoneFunc) (BerylSettingsContext * context)
#cdef gboolean (*BerylSettingsContextWriteInitFunc) (BerylSettingsContext * context)
#cdef void (*BerylSettingsContextWriteDoneFunc) (BerylSettingsContext * context)
#cdef void (*BerylSettingsContextReadSettingFunc)
#	(BerylSettingsContext * context, BerylSetting * setting)
#cdef void (*BerylSettingsContextWriteSettingFunc)
#	(BerylSettingsContext * context, BerylSetting * setting)

cdef struct GSList:
	void * data
	GSList * next

#THIS MAY NOT WORK RIGHT ON ALL ARCH
#TODO - test!
ctypedef int gboolean
ctypedef int gint
ctypedef char gchar
ctypedef unsigned int guint
ctypedef double gdouble
ctypedef void * gpointer

cdef enum BerylSettingConflictType:
	BERYL_SETTING_CONFLICT_TYPE_KEY,
	BERYL_SETTING_CONFLICT_TYPE_BUTTON,
	BERYL_SETTING_CONFLICT_TYPE_EDGE,
	BERYL_SETTING_CONFLICT_TYPE_ANY

cdef enum BerylSettingType:
	BERYL_SETTING_TYPE_BOOL,
	BERYL_SETTING_TYPE_INT,
	BERYL_SETTING_TYPE_FLOAT,
	BERYL_SETTING_TYPE_STRING,
	BERYL_SETTING_TYPE_COLOR,
	BERYL_SETTING_TYPE_BINDING,
	BERYL_SETTING_TYPE_LIST,
	BERYL_SETTING_TYPE_COUNT

cdef extern struct BerylSettingsBackend
cdef extern struct BerylSettingsContext
cdef extern struct BerylSettingsPluginCategory
cdef extern struct BerylSettingsSubGroup
cdef extern struct BerylSettingsGroup
cdef extern struct BerylSettingsPlugin
cdef extern struct BerylSettingValue
cdef extern struct BerylSetting

cdef struct BerylSettingConflict:
	BerylSettingConflictType type
	GSList * list


cdef struct BerylSettingColorValueColor:
	gint red
	gint green
	gint blue
	gint alpha

cdef struct BerylSettingColorValueArray:
	gint array[4]

cdef union BerylSettingColorValue:
	BerylSettingColorValueColor color
	BerylSettingColorValueArray array


cdef extern BerylSettingsContext * beryl_settings_context_new()

cdef extern gboolean beryl_settings_context_set_backend(BerylSettingsContext * context,
	gchar * backend)

cdef extern void beryl_settings_context_destroy(BerylSettingsContext * context)

cdef extern GSList * beryl_settings_plugin_get_groups(BerylSettingsPlugin * plugin)

cdef extern BerylSettingsGroup * beryl_settings_plugin_find_group(BerylSettingsPlugin * plugin,
		gchar * group)

cdef extern gchar * beryl_settings_group_get_name(BerylSettingsGroup * group)

cdef extern GSList * beryl_settings_group_get_subgroups(BerylSettingsGroup * group)

cdef extern BerylSettingsSubGroup * beryl_settings_group_find_subgroup(BerylSettingsGroup * group,
		gchar * subgroup)

cdef extern gchar * beryl_settings_subgroup_get_name(BerylSettingsSubGroup * group)

cdef extern GSList * beryl_settings_subgroup_get_settings(BerylSettingsSubGroup * subGroup)

cdef extern BerylSetting * beryl_settings_subgroup_find_setting(BerylSettingsSubGroup * subGroup,
		gchar * setting)

cdef extern gboolean beryl_setting_has_hint(BerylSetting * setting, gchar * hint)

cdef extern BerylSettingsPlugin * beryl_settings_context_find_plugin(
						BerylSettingsContext * context, gchar * name)

cdef extern BerylSetting * beryl_settings_plugin_find_setting(
						BerylSettingsPlugin * plugin, gchar * name, gboolean is_screen)

cdef extern void beryl_settings_context_read(BerylSettingsContext * context)

cdef extern void beryl_settings_context_write(BerylSettingsContext * context)

cdef extern BerylSettingsContext * beryl_settings_context_comp_new()

cdef extern gboolean beryl_settings_send_reload_signal()

cdef extern void beryl_setting_reset_to_default(BerylSetting * setting)

cdef extern void beryl_setting_list_clear(BerylSetting * setting)

cdef extern BerylSettingValue * beryl_setting_list_append(BerylSetting * setting)

cdef extern gint beryl_setting_list_length(BerylSetting * setting)

cdef extern gint beryl_setting_list_value_index(BerylSettingValue * value)

cdef extern void beryl_setting_list_value_swap_with(BerylSettingValue * value, guint n)

cdef extern void beryl_setting_list_value_move_before(BerylSettingValue * value,
				guint n)

cdef extern void beryl_setting_list_value_remove(BerylSettingValue * value)

cdef extern BerylSettingValue * beryl_setting_get_primary_value(BerylSetting * setting)

#these are value get functions
cdef extern gboolean beryl_setting_value_get_int(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_get_float(BerylSettingValue * value, gdouble * data)
cdef extern gboolean beryl_setting_value_get_bool(BerylSettingValue * value, gboolean * data)
cdef extern gboolean beryl_setting_value_get_string(BerylSettingValue * value, gchar ** data)
cdef extern gboolean beryl_setting_value_get_keysym(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_get_keymods(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_get_button(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_get_buttonmods(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_get_bell(BerylSettingValue * value, gboolean * data)
cdef extern gboolean beryl_setting_value_get_edgemask(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_get_key_enabled(BerylSettingValue * value, gboolean * data)
cdef extern gboolean beryl_setting_value_get_button_enabled(BerylSettingValue * value, gboolean * data)
cdef extern gboolean beryl_setting_value_get_color(BerylSettingValue * value, BerylSettingColorValue * data)

#returns list of BerylSettingValue's that is the list value
cdef extern gboolean beryl_setting_value_get_value_list(BerylSettingValue * value, GSList ** data)

#these are value set functions
cdef extern gboolean beryl_setting_value_set_int(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_set_float(BerylSettingValue * value, gdouble * data)
cdef extern gboolean beryl_setting_value_set_bool(BerylSettingValue * value, gboolean * data)
cdef extern gboolean beryl_setting_value_set_string(BerylSettingValue * value, gchar ** data)
cdef extern gboolean beryl_setting_value_set_keysym(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_set_keymods(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_set_button(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_set_buttonmods(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_set_bell(BerylSettingValue * value, gboolean * data)
cdef extern gboolean beryl_setting_value_set_edgemask(BerylSettingValue * value, gint * data)
cdef extern gboolean beryl_setting_value_set_key_enabled(BerylSettingValue * value, gboolean * data)
cdef extern gboolean beryl_setting_value_set_button_enabled(BerylSettingValue * value, gboolean * data)
cdef extern gboolean beryl_setting_value_set_color(BerylSettingValue * value, BerylSettingColorValue * data)

#these are info get functions
cdef extern gboolean beryl_setting_get_can_set_key(BerylSetting * value, gboolean * data)
cdef extern gboolean beryl_setting_get_can_set_button(BerylSetting * value, gboolean * data)
cdef extern gboolean beryl_setting_get_can_set_edgemask(BerylSetting * value, gboolean * data)
cdef extern gboolean beryl_setting_get_can_set_bell(BerylSetting * value, gboolean * data)
cdef extern gboolean beryl_setting_get_allowed_strings(BerylSetting * value, GSList ** data)
cdef extern gboolean beryl_setting_get_raw_allowed_strings(BerylSetting * value, GSList ** data)
cdef extern gboolean beryl_setting_get_int_min(BerylSetting * value, gint * data)
cdef extern gboolean beryl_setting_get_int_max(BerylSetting * value, gint * data)
cdef extern gboolean beryl_setting_get_float_min(BerylSetting * value, gdouble * data)
cdef extern gboolean beryl_setting_get_float_max(BerylSetting * value, gdouble * data)
cdef extern gboolean beryl_setting_get_float_precision(BerylSetting * value, gdouble * data)
cdef extern gboolean beryl_setting_get_list_type(BerylSetting * value, BerylSettingType * data)

#these are info functions
cdef extern BerylSettingType beryl_setting_get_type(BerylSetting * setting)
cdef extern gchar * beryl_setting_get_short_desc(BerylSetting * setting)
cdef extern gchar * beryl_setting_get_long_desc(BerylSetting * setting)
cdef extern gchar * beryl_setting_get_group(BerylSetting * setting)
cdef extern gchar * beryl_setting_get_subgroup(BerylSetting * setting)
cdef extern gchar * beryl_setting_get_name(BerylSetting * setting)
cdef extern gchar * beryl_settings_plugin_get_short_desc(BerylSettingsPlugin * plugin)
cdef extern gchar * beryl_settings_plugin_get_long_desc(BerylSettingsPlugin * plugin)
cdef extern gchar * beryl_settings_plugin_get_name(BerylSettingsPlugin * plugin)

#context functions
cdef extern GSList * beryl_settings_context_get_plugins(BerylSettingsContext * context)
cdef extern GSList * beryl_settings_plugin_get_settings(BerylSettingsPlugin * plugin)

#privates
cdef extern gpointer beryl_settings_context_get_private(BerylSettingsContext * context)
cdef extern gpointer beryl_settings_plugin_get_private(BerylSettingsPlugin * plugin)
cdef extern gpointer beryl_setting_get_private(BerylSetting * setting)
cdef extern void beryl_settings_context_set_private(BerylSettingsContext * context, gpointer private_ptr)
cdef extern void beryl_settings_plugin_set_private(BerylSettingsPlugin * plugin, gpointer private_ptr)
cdef extern void beryl_setting_set_private(BerylSetting * setting, gpointer private_ptr)

#utility functions
cdef extern unsigned int beryl_settings_get_mods_and_endptr(gchar * src, gchar ** ret)
cdef extern gchar * beryl_settings_mods_to_string(unsigned int mods)

cdef extern int beryl_settings_get_plugin_category_count()
cdef extern BerylSettingsPluginCategory * beryl_settings_context_get_nth_plugin_category(BerylSettingsContext * context, int i)
cdef extern gchar * beryl_settings_plugin_category_get_name(BerylSettingsPluginCategory * category)
cdef extern gchar * beryl_settings_plugin_category_get_short_desc(BerylSettingsPluginCategory * category)
cdef extern gchar * beryl_settings_plugin_category_get_long_desc(BerylSettingsPluginCategory * category)
cdef extern GSList * beryl_settings_plugin_category_get_plugins(BerylSettingsPluginCategory * category)

cdef extern GSList * beryl_settings_plugin_get_provides(BerylSettingsPlugin * plugin)
cdef extern GSList * beryl_settings_plugin_get_requires(BerylSettingsPlugin * plugin)
cdef extern GSList * beryl_settings_plugin_get_load_before(BerylSettingsPlugin * plugin)
cdef extern GSList * beryl_settings_plugin_get_load_after(BerylSettingsPlugin * plugin)

cdef extern gchar * beryl_settings_group_get_desc(BerylSettingsGroup * group)
cdef extern gchar * beryl_settings_subgroup_get_desc(BerylSettingsSubGroup * subGroup)

cdef extern void beryl_settings_context_set_profile(BerylSettingsContext * context, gchar * profile)
cdef extern gchar * beryl_settings_context_get_profile(BerylSettingsContext * context)

cdef extern void beryl_settings_context_set_de_integration_enabled(BerylSettingsContext * context,
		gboolean enable_integration)
cdef extern gboolean beryl_settings_context_get_de_integration_enabled(BerylSettingsContext * context)

cdef extern gboolean beryl_settings_context_get_setting_is_read_only(BerylSettingsContext * context,
		BerylSetting * setting)
cdef extern gboolean beryl_settings_context_get_setting_is_de_integrated(BerylSettingsContext * context,
		BerylSetting * setting)

cdef extern gchar * beryl_settings_context_get_backend(BerylSettingsContext * context)

cdef extern gboolean beryl_setting_get_is_advanced(BerylSetting * setting)

cdef extern gboolean beryl_settings_context_import_from_file(BerylSettingsContext * context,
		gchar * filename, gboolean overwrite)
cdef extern gboolean beryl_settings_context_export_to_file(BerylSettingsContext * context,
		gchar * filename)

cdef extern GSList * beryl_settings_context_get_existing_profiles(BerylSettingsContext * context)
cdef extern void beryl_settings_free_profile_list(GSList * list)

cdef extern BerylSettingsPlugin * beryl_setting_get_plugin(BerylSetting * setting)
cdef extern BerylSetting * beryl_settings_context_find_first_edge_owner(BerylSettingsContext * context, gint edgemask)

cdef extern BerylSettingConflictType beryl_setting_conflict_get_type(BerylSettingConflict * conflict)
cdef extern GSList * beryl_setting_conflict_get_settings(BerylSettingConflict * conflict)
cdef extern GSList * beryl_settings_context_find_conflicts(BerylSettingsContext * context, BerylSettingConflictType type)
cdef extern GSList * beryl_settings_context_find_conflicts_for_setting(BerylSettingsContext * context, BerylSetting * setting, BerylSettingConflictType type)
cdef extern void beryl_settings_free_conflict_list(GSList * list)

cdef extern GSList * beryl_settings_context_get_active_plugins(BerylSettingsContext * context)
cdef extern gboolean beryl_settings_plugin_enable(BerylSettingsPlugin * plugin)
cdef extern gboolean beryl_settings_plugin_disable(BerylSettingsPlugin * plugin)
cdef extern gboolean beryl_settings_plugin_get_is_enabled(BerylSettingsPlugin * plugin)

cdef extern GSList * beryl_settings_get_backends()
cdef extern gchar * beryl_settings_backend_get_name(BerylSettingsBackend * backend)
cdef extern gchar * beryl_settings_backend_get_short_desc(BerylSettingsBackend * backend)
cdef extern gboolean beryl_settings_backend_get_supports_integration(BerylSettingsBackend * backend)
#endif

#glib
cdef extern void g_slist_free(GSList * l)

#X11
cdef extern char * XKeysymToString (int keysym)
cdef extern int XStringToKeysym (gchar * string)

cdef plugin_get_name(BerylSettingsPlugin * plugin):
	cdef gchar * s
	s=beryl_settings_plugin_get_name(plugin)
	if s==NULL:
		p = '_'
	else:
		p = s
	return p

def keytostring(keysym):
	cdef char * s
	s=XKeysymToString(keysym)
	if (s==NULL):
		return "None"
	return s

def modstostring(mods):
	cdef char * s
	s=beryl_settings_mods_to_string(mods)
	if (s==NULL):
		return ""
	return s

def buttontostring(button):
	if (button==0):
		return "None"
	return "Button%d"%button

def edgelisttomask(edge):
	edges=(
			(1,"Left"),
			(2,"Right"),
			(4,"Top"),
			(8,"Bottom"),
			(16,"TopLeft"),
			(32,"TopRight"),
			(64,"BottomLeft"),
			(128,"BottomRight"))
	ret=0
	for each in edge:
		for ev in edges:
			if (str(each).lower()==ev[1].lower()):
				ret=ret|ev[0]
	return ret

def edgetolist(edge):
	edges=(
			(1,"Left"),
			(2,"Right"),
			(4,"Top"),
			(8,"Bottom"),
			(16,"TopLeft"),
			(32,"TopRight"),
			(64,"BottomLeft"),
			(128,"BottomRight"))
	ret=[]
	for p in edges:
		if (edge & p[0]):
			ret = ret + [ p[1]]
	return ret

def send_reload():
	beryl_settings_send_reload_signal()

class PluginSetEnabledException(Exception):
	def __init__(self,plugin,type):
		self.plugin=str(plugin)
		self.type=str(type)
	def __str__(self):
		return repr('Could not %s %s, dependencies not met.'%(self.type,self.plugin))

class PluginEnableException(PluginSetEnabledException):
	def __init__(self,plugin):
		PluginSetEnabledException.__init__(self,plugin,"enable")

class PluginDisableException(PluginSetEnabledException):
	def __init__(self,plugin):
		PluginSetEnabledException.__init__(self,plugin,"disable")

class NotFoundException(Exception):
	def __init__(self,type,value,owner):
		self.value=str(value)
		self.type=str(type)
		self.owner=str(owner)
	def __str__(self):
		return repr('Could not find %s named "%s" in "%s"'%(self.type,self.value,self.owner))

class SettingNotFoundException(NotFoundException):
	def __init__(self,value,owner):
		NotFoundException.__init__(self,"setting",value,owner)

class GroupNotFoundException(NotFoundException):
	def __init__(self,value,owner):
		NotFoundException.__init__(self,"group",value,owner)

class PluginNotFoundException(NotFoundException):
	def __init__(self,value,owner):
		NotFoundException.__init__(self,"plugin",value,owner)

class SubGroupNotFoundException(NotFoundException):
	def __init__(self,value,owner):
		NotFoundException.__init__(self,"subgroup",value,owner)

def Backends():
	cdef GSList * l
	cdef BerylSettingsBackend * b
	l = beryl_settings_get_backends()
	ret=[]
	if (l == NULL):
		return ret
	while(l!=NULL):
		b=<BerylSettingsBackend *>l.data
		ret=ret+[Backend(beryl_settings_backend_get_name(b))]
		l=l.next
	return ret

cdef class Backend:
	cdef BerylSettingsBackend * myBackend
	
	def __new__(self,nam):
		cdef GSList * l
		cdef BerylSettingsBackend * b
		l = beryl_settings_get_backends()
		while(l!=NULL):
			b = <BerylSettingsBackend *>l.data
			if (beryl_settings_backend_get_name(b)==nam):
				self.myBackend=b
				break
			l=l.next
	
	property Name:
		def __get__(self):
			return beryl_settings_backend_get_name(self.myBackend)
	
	property ShortDesc:
		def __get__(self):
			return beryl_settings_backend_get_short_desc(self.myBackend)
	
	property IntegrationSupport:
		def __get__(self):
			val = beryl_settings_backend_get_supports_integration(self.myBackend)
			if (val):
				return True
			return False

	def __str__(self):
		return "%s backend"%self.Name

cdef class Context:
	cdef BerylSettingsContext * myContext
	cdef char * backend

	def __new__(self):
		self.myContext=beryl_settings_context_new()
		self.backend=beryl_settings_context_get_backend(self.myContext)

	def __dealloc__(self):
		beryl_settings_context_destroy(self.myContext)

	property Backend:
		def __get__(self):
			return beryl_settings_context_get_backend(self.myContext)

		def __set__(self,backend):
			beryl_settings_context_set_backend(self.myContext,backend)

	property Categories:
		def __get__(self):
			cdef int i
			cdef gchar * n
			cdef BerylSettingsPluginCategory * pc
			i=beryl_settings_get_plugin_category_count()
			ret=[]
			for p in range(0,i):
				ret=ret+[Category(self,p)]
			return ret

	property Plugins:
		def __get__(self):
			cdef GSList * l
			l = beryl_settings_context_get_plugins(self.myContext)
			ret=[]
			while (l!=NULL):
				snam=plugin_get_name(<BerylSettingsPlugin *>l.data)
				ret = ret + [Plugin(self,snam)]
				l=l.next
			return ret

	property Profile:
		def __get__(self):
			cdef gchar * s
			s=beryl_settings_context_get_profile(self.myContext)
			if (s==NULL):
				return None
			else:
				return s
		def __set__(self,val):
			cdef gchar * s
			if val == None:
				s = NULL
			else:
				s=val
			beryl_settings_context_set_profile(self.myContext,s)

	property Profiles:
		def __get__(self):
			cdef GSList * l
			cdef gchar * s
			l=beryl_settings_context_get_existing_profiles(self.myContext)
			ret=[]
			while(l!=NULL):
				s = <gchar *>l.data
				ret=ret+[s]
				l=l.next
			beryl_settings_free_profile_list(l)
			return ret

	property Integrated:
		def __get__(self):
			if beryl_settings_context_get_de_integration_enabled(self.myContext):
				return True
			else:
				return False

		def __set__(self,isIntegrated):
			cdef gboolean b
			if (isIntegrated):
				b=True
			else:
				b=False
			beryl_settings_context_set_de_integration_enabled(self.myContext,b)

	property ActivePlugins:
		def __get__(self):
			cdef GSList * l
			cdef GSList * m
			ret=[]
			l = beryl_settings_context_get_active_plugins(self.myContext)
			m = l
			while(m!=NULL):
				s=plugin_get_name(<BerylSettingsPlugin *>m.data)
				ret=ret+[s]
				m=m.next
			g_slist_free(l)
			return ret

	def EdgeOwner(self,edge):
		cdef BerylSettingsPlugin * p
		cdef BerylSetting * s
		cdef gint edm
		edm=edge
		s=beryl_settings_context_find_first_edge_owner(self.myContext,edm)
		if (s!=NULL):
			p=beryl_setting_get_plugin(s)
			return Setting(self.Plugin(plugin_get_name(p)),beryl_setting_get_name(s))
		else:
			return None

	def Conflicts(self,t=BERYL_SETTING_CONFLICT_TYPE_ANY):
		cdef GSList * l
		cdef GSList * m
		cdef GSList * n
		cdef BerylSettingConflictType type
		type=t
		cdef BerylSettingConflict * c
		cdef BerylSetting * s
		cdef BerylSettingsPlugin * p
		l=beryl_settings_context_find_conflicts(self.myContext,type)
		m=l
		ret=[]
		while(m!=NULL):
			oret=[]
			c=<BerylSettingConflict *>m.data
			rtype=beryl_setting_conflict_get_type(c)
			n=beryl_setting_conflict_get_settings(c)
			while(n!=NULL):
				s=<BerylSetting *>n.data
				p=beryl_setting_get_plugin(s)
				oret=oret+[Setting(self.Plugin(plugin_get_name(p)),beryl_setting_get_name(s))]
				n=n.next
			ret = ret + [(rtype,oret)]
			m=m.next
		beryl_settings_free_conflict_list(l)
		return ret

	def Read(self):
		beryl_settings_context_read(self.myContext)

	def Write(self):
		beryl_settings_context_write(self.myContext)

	def Import(self,filename,overwrite=False):
		cdef gboolean ow
		cdef gchar * s
		s=filename
		if (overwrite):
			ow=True
		else:
			ow=False
		return beryl_settings_context_import_from_file(self.myContext,s,ow)

	def Export(self,filename):
		cdef gchar * s
		s=filename
		beryl_settings_context_export_to_file(self.myContext,s)

	def Plugin(self,name):
		return Plugin(self,name)

	def __str__(self):
		return self.backend+" context"

cdef class Category:
	cdef BerylSettingsPluginCategory * myCategory
	cdef Context pContext

	def __new__(self,Context context,index):
		self.myCategory=beryl_settings_context_get_nth_plugin_category(context.myContext,index)
		self.pContext=context

	property Name:
		def __get__(self):
			return beryl_settings_plugin_category_get_name(self.myCategory)

	property ShortDesc:
		def __get__(self):
			return beryl_settings_plugin_category_get_short_desc(self.myCategory)

	property LongDesc:
		def __get__(self):
			return beryl_settings_plugin_category_get_long_desc(self.myCategory)

	property Plugins:
		def __get__(self):
			cdef GSList * l
			l = beryl_settings_plugin_category_get_plugins(self.myCategory)
			ret=[]
			while (l!=NULL):
				snam=plugin_get_name(<BerylSettingsPlugin *>l.data)
				ret = ret + [Plugin(self.pContext,snam)]
				l=l.next
			return ret

	def __str__(self):
		return self.Name + " category"

cdef class Plugin:
	cdef BerylSettingsPlugin * myPlugin
	cdef Context pContext

	cdef int is_general

	def __new__(self,Context context,plugin):
		self.pContext=context
		if (plugin=="_"):
			self.myPlugin=beryl_settings_context_find_plugin(context.myContext,NULL)
			self.is_general=1
		else:
			self.myPlugin=beryl_settings_context_find_plugin(context.myContext,plugin)
			self.is_general=0
		if (self.myPlugin==NULL):
			raise PluginNotFoundException,(plugin,context)

	property Enabled:
		def __get__(self):
			if (self.is_general):
				return True
			else:
				if beryl_settings_plugin_get_is_enabled(self.myPlugin):
					return True
				else:
					return False
		def __set__(self,val):
			if (self.is_general):
				return
			else:
				if val:
					if not beryl_settings_plugin_enable(self.myPlugin):
						beryl_settings_plugin_disable(self.myPlugin)
						raise PluginEnableException,(self)
				else:
					if not beryl_settings_plugin_disable(self.myPlugin):
						beryl_settings_plugin_enable(self.myPlugin)
						raise PluginDisableException,(self)

	property Name:
		def __get__(self):
			return plugin_get_name(self.myPlugin)

	property ShortDesc:
		def __get__(self):
			if (self.is_general==0):
				return beryl_settings_plugin_get_short_desc(self.myPlugin)
			else:
				return "General Options"

	property LongDesc:
		def __get__(self):
			if (self.is_general==0):
				return beryl_settings_plugin_get_long_desc(self.myPlugin)
			else:
				return "Lots of General Options"

	property Settings:
		def __get__(self):
			cdef GSList * l
			l = beryl_settings_plugin_get_settings(self.myPlugin)
			ret=[]
			while (l!=NULL):
				if not str(beryl_setting_get_name(<BerylSetting *>l.data))=='____plugin_enabled':
					ret = ret + [Setting(self,beryl_setting_get_name(<BerylSetting *>l.data))]
				l=l.next
			return ret

	property Groups:
		def __get__(self):
			cdef GSList * l
			l = beryl_settings_plugin_get_groups(self.myPlugin)
			ret=[]
			while (l!=NULL):
				ret = ret + [Group(self,beryl_settings_group_get_name(<BerylSettingsGroup *>l.data))]
				l=l.next
			return ret

	property Provides:
		def __get__(self):
			cdef GSList * l
			l = beryl_settings_plugin_get_provides(self.myPlugin)
			ret=[]
			while (l!=NULL):
				st=<gchar *>l.data
				ret=ret+[st]
				l=l.next
			return ret

	property Requires:
		def __get__(self):
			cdef GSList * l
			l = beryl_settings_plugin_get_requires(self.myPlugin)
			ret=[]
			while (l!=NULL):
				st=<gchar *>l.data
				ret=ret+[st]
				l=l.next
			return ret

	def Group(self,group):
		return Group(self,group)

	def Setting(self,setting):
		return Setting(self,setting)

	def __str__(self):
		return self.Name+" plugin"

cdef class Group:
	cdef BerylSettingsGroup * myGroup

	cdef Plugin pPlugin

	def __new__(self,Plugin plugin,group):
		self.myGroup=beryl_settings_plugin_find_group(plugin.myPlugin,group)
		if (self.myGroup==NULL):
			raise GroupNotFoundException,(group,plugin)
		self.pPlugin=plugin

	property Plugin:
		def __get__(self):
			return self.pPlugin

	property Name:
		def __get__(self):
			return beryl_settings_group_get_name(self.myGroup)

	property Desc:
		def __get__(self):
			cdef gchar * c
			c=beryl_settings_group_get_desc(self.myGroup)
			if (c==NULL):
				return None
			else:
				return c

	property SubGroups:
		def __get__(self):
			cdef GSList * l
			l = beryl_settings_group_get_subgroups(self.myGroup)
			ret=[]
			while (l!=NULL):
				ret = ret + [SubGroup(self,beryl_settings_subgroup_get_name(<BerylSettingsSubGroup *>l.data))]
				l=l.next
			return ret

	def SubGroup(self,subgroup):
		return SubGroup(self,subgroup)

	def __str__(self):
		return self.Name+" group"

cdef class SubGroup:
	cdef BerylSettingsSubGroup * mySubGroup

	cdef Group pGroup
	cdef Plugin pPlugin

	def __new__(self,Group group,subgroup):
		self.mySubGroup=beryl_settings_group_find_subgroup(group.myGroup,subgroup)
		if self.mySubGroup==NULL:
			raise SubGroupNotFoundException,(subgroup,group)
		self.pPlugin=group.pPlugin
		self.pGroup=group

	property Name:
		def __get__(self):
			return beryl_settings_subgroup_get_name(self.mySubGroup)

	property Desc:
		def __get__(self):
			cdef gchar * c
			c=beryl_settings_subgroup_get_desc(self.mySubGroup)
			if (c==NULL):
				return None
			else:
				return c

	property Settings:
		def __get__(self):
			cdef GSList * l
			l = beryl_settings_subgroup_get_settings(self.mySubGroup)
			ret = []
			while (l!=NULL):
				if not str(beryl_setting_get_name(<BerylSetting *>l.data))=='____plugin_enabled':
					ret = ret + [Setting(self.pPlugin,beryl_setting_get_name(<BerylSetting *>l.data))]
				l=l.next
			return ret

	def Setting(self,setting):
		cdef BerylSetting * s
		s = beryl_settings_subgroup_find_setting(self.mySubGroup,setting)
		if (s==NULL):
			raise SettingNotFoundException,(setting,self)
		return Setting(self.pPlugin,beryl_setting_get_name(s))

	def __str__(self):
		return self.Name+" subgroup"

cdef class Setting:
	cdef BerylSetting * mySetting

	cdef Plugin pPlugin

	def __new__(self,Plugin plugin,setting):
		self.mySetting=beryl_settings_plugin_find_setting(plugin.myPlugin,setting,0)
		if (self.mySetting==NULL):
			self.mySetting=beryl_settings_plugin_find_setting(plugin.myPlugin,setting,1)
		if (self.mySetting==NULL):
			raise SettingNotFoundException,(setting,plugin)
		self.pPlugin = plugin

	property Plugin:
		def __get__(self):
			return self.pPlugin

	property Name:
		def __get__(self):
			return beryl_setting_get_name(self.mySetting)

	property ShortDesc:
		def __get__(self):
			return beryl_setting_get_short_desc(self.mySetting)

	property LongDesc:
		def __get__(self):
			return beryl_setting_get_long_desc(self.mySetting)

	property Group:
		def __get__(self):
			return beryl_setting_get_group(self.mySetting)

	property SubGroup:
		def __get__(self):
			return beryl_setting_get_subgroup(self.mySetting)

	property Advanced:
		def __get__(self):
			if beryl_setting_get_is_advanced(self.mySetting):
				return True
			else:
				return False

	property Integrated:
		def __get__(self):
			if beryl_settings_context_get_setting_is_de_integrated(self.pPlugin.pContext.myContext,
					self.mySetting):
				return True
			else:
				return False

	property ReadOnly:
		def __get__(self):
			if beryl_settings_context_get_setting_is_read_only(self.pPlugin.pContext.myContext,
					self.mySetting):
				return True
			else:
				return False

	def ResetToDefault(self):
		beryl_setting_reset_to_default(self.mySetting)
		return self.Value

	def HasHint(self,hint):
		return beryl_setting_has_hint(self.mySetting,hint)

	def Conflicts(self,t=BERYL_SETTING_CONFLICT_TYPE_ANY):
		cdef GSList * l
		cdef GSList * m
		cdef GSList * n
		cdef gchar * pnam
		cdef BerylSettingConflictType type
		type=t
		cdef BerylSettingConflict * c
		cdef BerylSetting * s
		cdef BerylSettingsPlugin * p
		l=beryl_settings_context_find_conflicts_for_setting(self.pPlugin.pContext.myContext,self.mySetting,type)
		m=l
		ret=[]
		while(m!=NULL):
			oret=[]
			c=<BerylSettingConflict *>m.data
			rtype=beryl_setting_conflict_get_type(c)
			n=beryl_setting_conflict_get_settings(c)
			while(n!=NULL):
				s=<BerylSetting *>n.data
				p=beryl_setting_get_plugin(s)
				oret=oret+[Setting(self.pPlugin.pContext.Plugin(plugin_get_name(p)),beryl_setting_get_name(s))]
				n=n.next
			ret = ret + [(rtype,oret)]
			m=m.next
		beryl_settings_free_conflict_list(l)
		return ret

	def __str__(self):
		return self.Name+" setting"

	property Restrictions:
		def __get__(self):
			cdef BerylSettingType t
			cdef GSList * l
			cdef gint i
			cdef gdouble d
			cdef gboolean b
			t=beryl_setting_get_type(self.mySetting)
			if (t==BERYL_SETTING_TYPE_LIST):
				beryl_setting_get_list_type(self.mySetting,&t)
			if (t==BERYL_SETTING_TYPE_STRING):
				beryl_setting_get_allowed_strings(self.mySetting,&l)
				ret=[]
				while(l!=NULL):
					st=<gchar *>l.data
					ret=ret+[st]
					l=l.next
				return ret
			if (t==BERYL_SETTING_TYPE_INT):
				beryl_setting_get_int_min(self.mySetting,&i)
				imin=i
				beryl_setting_get_int_max(self.mySetting,&i)
				imax=i
				return (imin,imax)
			if (t==BERYL_SETTING_TYPE_FLOAT):
				beryl_setting_get_float_min(self.mySetting,&d)
				fmin=d
				beryl_setting_get_float_max(self.mySetting,&d)
				fmax=d
				beryl_setting_get_float_precision(self.mySetting,&d)
				fprec=d
				return(fmin,fmax,fprec)
			if (t==BERYL_SETTING_TYPE_BINDING):
				beryl_setting_get_can_set_key(self.mySetting,&b)
				csk=b
				beryl_setting_get_can_set_button(self.mySetting,&b)
				csm=b
				beryl_setting_get_can_set_edgemask(self.mySetting,&b)
				cse=b
				beryl_setting_get_can_set_bell(self.mySetting,&b)
				csb=b
				return(csk,csm,csb,cse)
			return None

	property Type:
		def __get__(self):
			cdef BerylSettingType t
			t=beryl_setting_get_type(self.mySetting)
			lret=""
			ret=""
			if (t==BERYL_SETTING_TYPE_LIST):
				beryl_setting_get_list_type(self.mySetting,&t)
				lret="List"
			if (t==BERYL_SETTING_TYPE_STRING):
				ret= "String"
			if (t==BERYL_SETTING_TYPE_BINDING):
				ret= "Binding"
			if (t==BERYL_SETTING_TYPE_COLOR):
				ret= "Color"
			if (t==BERYL_SETTING_TYPE_INT):
				ret= "Int"
			if (t==BERYL_SETTING_TYPE_FLOAT):
				ret= "Float"
			if (t==BERYL_SETTING_TYPE_BOOL):
				ret= "Bool"
			if (ret==""):
				return "Unknown"
			if (lret==""):
				return ret
			else:
				return "List of %s"%ret

	property Value:
		def __set__(self,value):
			cdef BerylSettingType t
			cdef BerylSettingValue * v
			cdef char * s
			cdef int i
			cdef double d
			cdef gboolean b
			cdef BerylSettingColorValue c
			cdef GSList * l
			t = beryl_setting_get_type(self.mySetting)
			v = beryl_setting_get_primary_value(self.mySetting)
			is_list=False
			if (t==BERYL_SETTING_TYPE_LIST):
				is_list=True
				beryl_setting_get_list_type(self.mySetting,&t)
				beryl_setting_list_clear(self.mySetting)
				val=value
			else:
				val=[value]
			for value in val:
				if is_list:
					v=beryl_setting_list_append(self.mySetting)
				if (t==BERYL_SETTING_TYPE_STRING):
					s=value
					beryl_setting_value_set_string(v,&s)
				if (t==BERYL_SETTING_TYPE_INT):
					i=value
					beryl_setting_value_set_int(v,&i)
				if (t==BERYL_SETTING_TYPE_FLOAT):
					d=value
					beryl_setting_value_set_float(v,&d)
				if (t==BERYL_SETTING_TYPE_BOOL):
					if (value):
						b=1
					else:
						b=0
					beryl_setting_value_set_bool(v,&b)
				if (t==BERYL_SETTING_TYPE_COLOR):
					c.color.red=value[0]
					c.color.green=value[1]
					c.color.blue=value[2]
					c.color.alpha=value[3]
					beryl_setting_value_set_color(v,&c)
				if (t==BERYL_SETTING_TYPE_BINDING):
					#(keystr,buttonstr,bellbool,edgelist)
					if self.Restrictions[0]:
						keystr=value[0]
						ke=0
						keysym=0
						mods=0
						if (str(keystr).lower()!="disabled" and keystr!=""):
							mods=beryl_settings_get_mods_and_endptr(keystr,&s)
							keysym=XStringToKeysym(s)
							ke=1
						i=keysym
						beryl_setting_value_set_keysym(v,&i)
						i=mods
						beryl_setting_value_set_keymods(v,&i)
						b=ke
						beryl_setting_value_set_key_enabled(v,&b)
					if self.Restrictions[1]:
						buttonstr=value[1]
						bu=0
						button=0
						mods=0
						if (str(buttonstr).lower()!="disabled" and buttonstr!=""):
							mods=beryl_settings_get_mods_and_endptr(buttonstr,&s)
							try:
								button=int(s[6:])
							except:
								button=0
							bu=1
						i=button
						beryl_setting_value_set_button(v,&i)
						i=mods
						beryl_setting_value_set_buttonmods(v,&i)
						b=bu
						beryl_setting_value_set_button_enabled(v,&b)
					if self.Restrictions[2]:
						if value[2]:
							bel=1
						else:
							bel=0
						b=bel
						beryl_setting_value_set_bell(v,&b)
					if self.Restrictions[3]:
						edg=edgelisttomask(value[3])
						i=edg
						beryl_setting_value_set_edgemask(v,&i)

		def __get__(self):
			cdef BerylSettingType t
			cdef BerylSettingValue * v
			cdef char * s
			cdef int i
			cdef double d
			cdef gboolean b
			cdef BerylSettingColorValue c
			cdef GSList * l
			t = beryl_setting_get_type(self.mySetting)
			v = beryl_setting_get_primary_value(self.mySetting)
			is_list=False
			rem=1
			if (t==BERYL_SETTING_TYPE_LIST):
				is_list=True
				beryl_setting_get_list_type(self.mySetting,&t)
				beryl_setting_value_get_value_list(v,&l)
				rem=0
			ret=[]
			while((rem!=0) or (is_list and l!=NULL)):
				if is_list:
					v=<BerylSettingValue *>l.data
				else:
					rem=rem-1
				if (t==BERYL_SETTING_TYPE_STRING):
					if beryl_setting_value_get_string(v,&s):
						ret = ret + [s]
					else:
						if len(self.Restrictions):
							ret = ret + [self.Restrictions[0]]
						else:
							ret = ret + ['']
				if (t==BERYL_SETTING_TYPE_INT):
					if beryl_setting_value_get_int(v,&i):
						ret = ret + [i]
				if (t==BERYL_SETTING_TYPE_FLOAT):
					if beryl_setting_value_get_float(v,&d):
						ret = ret + [d]
				if (t==BERYL_SETTING_TYPE_BOOL):
					if beryl_setting_value_get_bool(v,&b):
						if b:
							ret = ret + [True]
						else:
							ret = ret + [False]
				if (t==BERYL_SETTING_TYPE_COLOR):
					if beryl_setting_value_get_color(v,&c):
						ret = ret + [(c.color.red,c.color.green,c.color.blue,c.color.alpha)]
				if (t==BERYL_SETTING_TYPE_BINDING):
					beryl_setting_value_get_keysym(v,&i)
					kys=keytostring(i)
					beryl_setting_value_get_keymods(v,&i)
					kym=modstostring(i)
					beryl_setting_value_get_button(v,&i)
					bus=buttontostring(i)
					beryl_setting_value_get_buttonmods(v,&i)
					bum=modstostring(i)
					beryl_setting_value_get_bell(v,&b)
					if (b):
						bes=True
					else:
						bes=False
					beryl_setting_value_get_edgemask(v,&i)
					eds=edgetolist(i)
					beryl_setting_value_get_key_enabled(v,&b)
					if (b):
						kss=kym+kys
					else:
						kss="Disabled"
					beryl_setting_value_get_button_enabled(v,&b)
					if (b):
						bss=bum+bus
					else:
						bss="Disabled"
					ret = ret + [(kss,bss,bes,eds)]
				if is_list:
					l=l.next
			if is_list:
				return ret
			else:
				return ret[0]