summaryrefslogtreecommitdiff
path: root/settings-backend/kconfig_backend.cpp
blob: 8e900be23ada9699cf6888bf337f560798543db6 (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
#include <kconfig.h>
#include <ksimpleconfig.h>
#include <kdebug.h>
#include <kinstance.h>
#include <stdlib.h>

/*
 *  Aquamarine the KDE window decorator
 *
 *  Copyright (c) 2006 Dennis Kasprzyk <onestone@beryl-project.org>
 *
 *  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.
 *
 */

extern "C" {
#include <beryl-settings-backend.h>
}

static KInstance *instance = NULL;

const static char *edgeName[] = {
    "Left",
    "Right",
    "Top",
    "Bottom",
    "TopLeft",
    "TopRight",
    "BottomLeft",
    "BottomRight"
};

void read_setting(BerylSettingsContext * c, BerylSetting * setting)
{
	KSimpleConfig *cfg = (KSimpleConfig *)c->backend_private_ptr;

	QString key(setting->name);
	QString group(setting->parent->name);
	if (setting->is_screen)
		group += "_screen0";
	cfg->setGroup(group);

	if (setting->type != BERYL_SETTING_TYPE_BINDING && !cfg->hasKey(key))
		return;

	setting->is_default=FALSE;
	switch (setting->type)
	{
		case BERYL_SETTING_TYPE_STRING:
			setting->value.value.as_string =
					g_strdup(cfg->readEntry(key,setting->value.value.as_string).ascii());
			break;
		case BERYL_SETTING_TYPE_FLOAT:
			{
				double val = cfg->readDoubleNumEntry(key);
				// stupid rounding issue
				val = MAX(val,setting->info.for_float.min);
				val = MIN(val,setting->info.for_float.max);
				beryl_setting_value_set_float(&setting->value,&val);
			}
			break;
		case BERYL_SETTING_TYPE_INT:
			{
				int val = cfg->readNumEntry(key);
				beryl_setting_value_set_int(&setting->value,&val);
			}
			break;
		case BERYL_SETTING_TYPE_BOOL:
			{

				Bool val = (cfg->readBoolEntry(key))?TRUE:FALSE;
				beryl_setting_value_set_bool(&setting->value,&val);
			}
			break;
		case BERYL_SETTING_TYPE_COLOR:
			{
				QValueList<int> list = cfg->readIntListEntry(key);
				BerylSettingColorValue color;
				color.color.red = list[0];
				color.color.green = list[1];
				color.color.blue = list[2];
				color.color.alpha = list[3];
				beryl_setting_value_set_color(&setting->value,&color);
			}
			break;
		case BERYL_SETTING_TYPE_LIST:
			{
				switch(setting->info.for_list.list_of_type)
				{
					case BERYL_SETTING_TYPE_BOOL:
						{
							QValueList<int> list = cfg->readIntListEntry(key);

							beryl_setting_list_clear(setting);

							QValueList<int>::iterator it;
							for (it = list.begin(); it != list.end(); it++)
							{
								BerylSettingValue * value = beryl_setting_list_append(setting);
								Bool val = ((*it))?TRUE:FALSE;
                            	beryl_setting_value_set_bool(value,&val);
							}
						}
						break;
					case BERYL_SETTING_TYPE_INT:
						{
							QValueList<int> list = cfg->readIntListEntry(key);

							beryl_setting_list_clear(setting);

							QValueList<int>::iterator it;
							for (it = list.begin(); it != list.end(); it++)
							{
								BerylSettingValue * value = beryl_setting_list_append(setting);
                            	beryl_setting_value_set_int(value,&(*it));
							}
						}
						break;
					case BERYL_SETTING_TYPE_STRING:
						{
							QStringList list = cfg->readListEntry(key,';');

							beryl_setting_list_clear(setting);

							QStringList::iterator it;
							for (it = list.begin(); it != list.end(); it++)
							{
								BerylSettingValue * value = beryl_setting_list_append(setting);
                            	value->value.as_string = g_strdup((*it).ascii());
								value->parent->is_default=FALSE;
							}
						}
						break;
					case BERYL_SETTING_TYPE_FLOAT:
						{
							QStringList list = cfg->readListEntry(key,';');

							beryl_setting_list_clear(setting);

							QStringList::iterator it;
							for (it = list.begin(); it != list.end(); it++)
							{
								BerylSettingValue * value = beryl_setting_list_append(setting);
								double val = (*it).toDouble();
								beryl_setting_value_set_float(value,&val);
							}
						}
						break;
					case BERYL_SETTING_TYPE_COLOR:
						{
							QStringList list = cfg->readListEntry(key,';');

							beryl_setting_list_clear(setting);

							QStringList::iterator it;
							for (it = list.begin(); it != list.end(); it++)
							{
								BerylSettingValue * value = beryl_setting_list_append(setting);
								QStringList colorstr = QStringList::split(",",(*it));

								BerylSettingColorValue color;
								color.color.red = colorstr[0].toInt();
								color.color.green = colorstr[1].toInt();
								color.color.blue = colorstr[2].toInt();
								color.color.alpha = colorstr[3].toInt();
								beryl_setting_value_set_color(value,&color);
							}
						}
						break;
					case BERYL_SETTING_TYPE_BINDING:
						{
							QStringList list = cfg->readListEntry(key,';');

							beryl_setting_list_clear(setting);

							QStringList::iterator it;
							for (it = list.begin(); it != list.end(); it++)
							{
								BerylSettingValue * value = beryl_setting_list_append(setting);
								QStringList binding = QStringList::split(",",(*it));

								int val;
								Bool bval;

								val = binding[0].toInt();
								beryl_setting_value_set_buttonmods(value,&val);
								val = binding[1].toInt();
								beryl_setting_value_set_button(value,&val);
								val = binding[2].toInt();
								beryl_setting_value_set_keymods(value,&val);
								val = binding[3].toInt();
								beryl_setting_value_set_keysym(value,&val);
								val = binding[4].toInt();
								beryl_setting_value_set_edgemask(value,&val);
								bval = (binding[5].toInt())?TRUE:FALSE;
								beryl_setting_value_set_bell(value,&bval);
								bval = (binding[6].toInt())?TRUE:FALSE;
								beryl_setting_value_set_button_enabled(value,&bval);
								bval = (binding[7].toInt())?TRUE:FALSE;
								beryl_setting_value_set_key_enabled(value,&bval);
							}
						}
						break;
					default:
						break;
				}
			}
			break;
		case BERYL_SETTING_TYPE_BINDING:
			{
				setting->is_default=TRUE;
				if (cfg->hasKey(key + "_key"))
				{
					setting->is_default=FALSE;
					Bool bval = (cfg->readBoolEntry(key + "_key_enabled"))?TRUE:FALSE;
					char * sval = strdup(cfg->readEntry(key + "_key").ascii());

					int keysym = 0;
					int keymods = 0;
					gchar * spos;
					keymods = beryl_settings_get_mods_and_endptr(sval,&spos);
					if (spos && *spos)
					{
						keysym=XStringToKeysym(spos);
					}
					beryl_setting_value_set_keysym(&setting->value,&keysym);
					beryl_setting_value_set_keymods(&setting->value,&keymods);
					beryl_setting_value_set_key_enabled(&setting->value,&bval);
					delete sval;
				}
				if (cfg->hasKey(key + "_button"))
				{
					setting->is_default=FALSE;
					Bool bval = (cfg->readBoolEntry(key + "_button_enabled"))?TRUE:FALSE;
					char * sval = strdup(cfg->readEntry(key + "_button").ascii());

					int button = 0;
					int buttonmods = 0;
					char * spos;
					buttonmods = beryl_settings_get_mods_and_endptr(sval,&spos);
					if (spos && *spos)
					{
						spos=strcasestr(spos,"Button");
						if (spos && *spos)
						{
							spos+=strlen("Button");
							button=atoi(spos);
						}
					}
					beryl_setting_value_set_button(&setting->value,&button);
					beryl_setting_value_set_buttonmods(&setting->value,&buttonmods);
					beryl_setting_value_set_button_enabled(&setting->value,&bval);
					delete sval;
				}
				if (cfg->hasKey(key + "_edge"))
				{
					setting->is_default=FALSE;
					QString sval = cfg->readEntry(key + "_edge");
					int int_val = 0;

					for (int i = 0; i < 8; i++)
					{
						if (sval == edgeName[i])
						{
							int_val=1<<i;
						}
					}
						beryl_setting_value_set_edgemask(&setting->value,&int_val);
				}
				if (cfg->hasKey(key + "_bell"))
				{
					setting->is_default=FALSE;
					Bool bval = (cfg->readBoolEntry(key + "_bell"))?TRUE:FALSE;
					beryl_setting_value_set_bell(&setting->value,&bval);
				}
			}
			break;
		default:
			kdDebug () << "Not supported setting type : " << setting->type << endl;
			setting->is_default=TRUE;
			break;
	}
}

static void copy_bool(BerylSettingValue * value, QValueList<int> *list)
{
	list->append((value->value.as_bool)?1:0);
}

static void copy_int(BerylSettingValue * value, QValueList<int> *list)
{
	list->append(value->value.as_int);
}

static void copy_float(BerylSettingValue * value, QStringList *list)
{
	list->append(QString::number(value->value.as_float));
}

static void copy_binding(BerylSettingValue * value, QStringList *list)
{
	QString binding;
	binding += QString::number(value->value.as_binding.button_mod_mask);
	binding += ",";
	binding += QString::number(value->value.as_binding.button);
	binding += ",";
	binding += QString::number(value->value.as_binding.key_mod_mask);
	binding += ",";
	binding += QString::number(value->value.as_binding.keysym);
	binding += ",";
	binding += QString::number(value->value.as_binding.edge_mask);
	binding += ",";
	binding += QString::number((value->value.as_binding.on_bell)?1:0);
	binding += ",";
	binding += QString::number((value->value.as_binding.enabled.value.button)?1:0);
	binding += ",";
	binding += QString::number((value->value.as_binding.enabled.value.key)?1:0);
	list->append(binding);
}

static void copy_string(BerylSettingValue * value, QStringList *list)
{
	list->append(value->value.as_string);
}

static void copy_color(BerylSettingValue * value, QStringList *list)
{
	QString color;
	color += QString::number(value->value.as_color.color.red);
	color += ",";
	color += QString::number(value->value.as_color.color.green);
	color += ",";
	color += QString::number(value->value.as_color.color.blue);
	color += ",";
	color += QString::number(value->value.as_color.color.alpha);
	list->append(color);
}

void write_setting(BerylSettingsContext * c, BerylSetting * setting)
{
	KSimpleConfig *cfg = (KSimpleConfig *)c->backend_private_ptr;

	QString key(setting->name);
	QString group(setting->parent->name);
	if (setting->is_screen)
		group += "_screen0";
	cfg->setGroup(group);

	if (setting->is_default)
	{
		if (cfg->hasKey(key))
			cfg->deleteEntry(key);
		return;
	}

	switch (setting->type)
	{
		case BERYL_SETTING_TYPE_STRING:
			cfg->writeEntry(key,setting->value.value.as_string);
			break;
		case BERYL_SETTING_TYPE_FLOAT:
			cfg->writeEntry(key,setting->value.value.as_float);
			break;
		case BERYL_SETTING_TYPE_INT:
			cfg->writeEntry(key,setting->value.value.as_int);
			break;
		case BERYL_SETTING_TYPE_BOOL:
			cfg->writeEntry(key,bool(setting->value.value.as_bool));
			break;
		case BERYL_SETTING_TYPE_COLOR:
			{
				QValueList<int> list;
				list.append(setting->value.value.as_color.color.red);
				list.append(setting->value.value.as_color.color.green);
				list.append(setting->value.value.as_color.color.blue);
				list.append(setting->value.value.as_color.color.alpha);
				cfg->writeEntry(key,list);
			}
			break;
		case BERYL_SETTING_TYPE_LIST:
			{
				switch(setting->info.for_list.list_of_type)
				{
					case BERYL_SETTING_TYPE_BOOL:
						{
							QValueList<int> list;
							g_slist_foreach(setting->value.value.as_list,(GFunc)copy_bool,&list);
							cfg->writeEntry(key,list,';');
						}
						break;
					case BERYL_SETTING_TYPE_INT:
						{
							QValueList<int> list;
							g_slist_foreach(setting->value.value.as_list,(GFunc)copy_int,&list);
							cfg->writeEntry(key,list,';');
						}
						break;
					case BERYL_SETTING_TYPE_STRING:
						{
							QStringList list;
							g_slist_foreach(setting->value.value.as_list,(GFunc)copy_string,&list);
							cfg->writeEntry(key,list,';');
						}
						break;
					case BERYL_SETTING_TYPE_FLOAT:
						{
							QStringList list;
							g_slist_foreach(setting->value.value.as_list,(GFunc)copy_float,&list);
							cfg->writeEntry(key,list,';');
						}
						break;
					case BERYL_SETTING_TYPE_COLOR:
						{
							QStringList list;
							g_slist_foreach(setting->value.value.as_list,(GFunc)copy_color,&list);
							cfg->writeEntry(key,list,';');
						}
						break;
					case BERYL_SETTING_TYPE_BINDING:
						{
							QStringList list;
							g_slist_foreach(setting->value.value.as_list,(GFunc)copy_binding,&list);
							cfg->writeEntry(key,list,';');
						}
						break;
					default:
						break;
				}
			}
			break;
		case BERYL_SETTING_TYPE_BINDING:
			{
				gboolean bool_val;
				if (beryl_setting_get_can_set_key(setting,&bool_val)
						&&bool_val)
				{
					if (beryl_setting_value_get_key_enabled(
								&setting->value,&bool_val)&&bool_val)
						cfg->writeEntry(key + "_key_enabled",true);
					else
						cfg->writeEntry(key + "_key_enabled",false);

					gint ire;
					QString binding;
					QString keysym;

					if (beryl_setting_value_get_keymods(&setting->value,&ire))
						binding += beryl_settings_mods_to_string(ire);

					if (beryl_setting_value_get_keysym(&setting->value,&ire))
						keysym = XKeysymToString(ire);
					if (keysym.isEmpty()) keysym = "None";

					binding += keysym;
					cfg->writeEntry(key + "_key",binding);
				}
				if (beryl_setting_get_can_set_button(setting,&bool_val)
						&&bool_val)
				{
					if (beryl_setting_value_get_button_enabled(
								&setting->value,&bool_val)&&bool_val)
						cfg->writeEntry(key + "_button_enabled",true);
					else
						cfg->writeEntry(key + "_button_enabled",false);


					gint ire;
					QString binding;
					QString button = "Any";

					if (beryl_setting_value_get_buttonmods(&setting->value,&ire))
						binding += beryl_settings_mods_to_string(ire);

					if (beryl_setting_value_get_button(&setting->value,&ire))
						button = "Button" + QString::number(ire);

					binding += button;
					cfg->writeEntry(key + "_button",binding);
				}
				if (beryl_setting_get_can_set_edgemask(setting,&bool_val)
						&&bool_val)
				{
					int ema;
					QString edge = "None";

					if (beryl_setting_value_get_edgemask(&setting->value,&ema))
					{
						int i;
						for (i=0;i<8;i++)
							if (ema & 1<<i)
								edge = edgeName[i];
					}
					cfg->writeEntry(key + "_edge",edge);
				}
				if (beryl_setting_get_can_set_bell(setting,&bool_val)
						&&bool_val)
				{
					if (beryl_setting_value_get_bell(&setting->value,&bool_val))
						cfg->writeEntry(key + "_bell",bool(bool_val));
				}
			}
			break;
		default:
			kdDebug () << "Not supported setting type : " << setting->type << endl;
			break;
	}
}

gboolean read_init(BerylSettingsContext * c)
{
	if (!instance)
		instance = new KInstance("beryl-kconfig");

	KSimpleConfig *cfg = new KSimpleConfig("berylrc",true);
	c->backend_private_ptr=(void *)cfg;
	return TRUE;
}

void read_done(BerylSettingsContext * c)
{
	KSimpleConfig *cfg = (KSimpleConfig *)c->backend_private_ptr;
	c->backend_private_ptr=NULL;
	delete cfg;
}

gboolean write_init(BerylSettingsContext * c)
{
	if (!instance)
		instance = new KInstance("beryl-kconfig");

	KSimpleConfig *cfg = new KSimpleConfig("berylrc");
	c->backend_private_ptr=(void *)cfg;
	return TRUE;
}

void write_done(BerylSettingsContext * c)
{
	KSimpleConfig *cfg = (KSimpleConfig *)c->backend_private_ptr;
	c->backend_private_ptr=NULL;
	cfg->sync();
	delete cfg;
}