mce-conf.c 16.1 KB
Newer Older
1 2 3 4 5
/**
 * @file mce-conf.c
 * Configuration option handling for MCE
 * <p>
 * Copyright © 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
6
 * Copyright (C) 2013-2019 Jolla Ltd.
7 8
 * <p>
 * @author David Weinehall <david.weinehall@nokia.com>
9 10
 * @author Santtu Lakkala <ext-santtu.1.lakkala@nokia.com>
 * @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * mce is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * mce 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mce.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mce-conf.h"
26 27 28

#include "mce.h"
#include "mce-log.h"
29
#include "modules/led.h"
30

31 32 33
#include <string.h>
#include <glob.h>

34 35 36
/** Pointer to the keyfile structure where config values are read from */
static gpointer keyfile = NULL;

37 38 39 40 41 42
/** Internal helper for insuring valid keyfile pointer is available
 *
 * @param keyfilepointer custom key file, or NULL to use the default one
 *
 * @returns non-null keyfile pointer, or aborts
 */
43
static gpointer mce_conf_get_keyfile(void)
44
{
45
	if( !keyfile ) {
46 47 48 49 50 51 52 53 54 55
		/* Earlier it was possible to have mce running with NULL
		 * keyfile. Now the only reasons that might happen are:
		 *   1) mce_conf_init() was not called yet
		 *   2) mce_conf_init() has failed
		 *   3) mce_conf_exit() has already been called
		 * i.e. critical logic errors somewhere */
		mce_log(LL_CRIT, "mce config subsystem used without "
			"properly initializing it");
		mce_abort();
	}
56
	return keyfile;
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
/** Check if configuration group is available
 *
 * @param group The configuration group
 *
 * @return TRUE if group is available, FALSE otherwise
 */
gboolean mce_conf_has_group(const gchar *group)
{
	gpointer keyfileptr = mce_conf_get_keyfile();
	return g_key_file_has_group(keyfileptr, group);
}

/** Check if configuration key is available
 *
 * @param group The configuration group
 * @param key The configuration key
 *
 * @return TRUE if key is available, FALSE otherwise
 */
gboolean mce_conf_has_key(const gchar *group, const gchar *key)
{
	gpointer keyfileptr = mce_conf_get_keyfile();
	GError *error = NULL;
	gboolean res = g_key_file_has_key(keyfileptr, group, key, &error);
	g_clear_error(&error);
	return res;
}

87 88 89 90 91 92 93 94 95 96
/**
 * Get a boolean configuration value
 *
 * @param group The configuration group to get the value from
 * @param key The configuration key to get the value of
 * @param defaultval The default value to use if the key isn't set
 * @param keyfileptr A keyfile pointer, or NULL to use the default keyfile
 * @return The configuration value on success, the default value on failure
 */
gboolean mce_conf_get_bool(const gchar *group, const gchar *key,
97
			   const gboolean defaultval)
98 99 100 101
{
	gboolean tmp = FALSE;
	GError *error = NULL;

102
	gpointer keyfileptr = mce_conf_get_keyfile();
103 104 105 106

	tmp = g_key_file_get_boolean(keyfileptr, group, key, &error);

	if (error != NULL) {
107
		mce_log(LL_DEBUG,
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
			"Could not get config key %s/%s; %s; "
			"defaulting to `%d'",
			group, key, error->message, defaultval);
		tmp = defaultval;
	}

	g_clear_error(&error);

	return tmp;
}

/**
 * Get an integer configuration value
 *
 * @param group The configuration group to get the value from
 * @param key The configuration key to get the value of
 * @param defaultval The default value to use if the key isn't set
 * @param keyfileptr A keyfile pointer, or NULL to use the default keyfile
 * @return The configuration value on success, the default value on failure
 */
gint mce_conf_get_int(const gchar *group, const gchar *key,
129
		      const gint defaultval)
130 131 132 133
{
	gint tmp = -1;
	GError *error = NULL;

134
	gpointer keyfileptr = mce_conf_get_keyfile();
135 136 137 138

	tmp = g_key_file_get_integer(keyfileptr, group, key, &error);

	if (error != NULL) {
139
		mce_log(LL_DEBUG,
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
			"Could not get config key %s/%s; %s; "
			"defaulting to `%d'",
			group, key, error->message, defaultval);
		tmp = defaultval;
	}

	g_clear_error(&error);

	return tmp;
}

/**
 * Get an integer list configuration value
 *
 * @param group The configuration group to get the value from
 * @param key The configuration key to get the value of
spiiroin's avatar
spiiroin committed
156
 * @param length The length of the list, or NULL if not needed
157 158 159 160
 * @param keyfileptr A keyfile pointer, or NULL to use the default keyfile
 * @return The configuration value on success, NULL on failure
 */
gint *mce_conf_get_int_list(const gchar *group, const gchar *key,
161
			    gsize *length)
162 163 164 165
{
	gint *tmp = NULL;
	GError *error = NULL;

166
	gpointer keyfileptr = mce_conf_get_keyfile();
167 168 169 170 171

	tmp = g_key_file_get_integer_list(keyfileptr, group, key,
					  length, &error);

	if (error != NULL) {
172
		mce_log(LL_DEBUG,
173 174
			"Could not get config key %s/%s; %s",
			group, key, error->message);
spiiroin's avatar
spiiroin committed
175 176
		if( length )
			*length = 0;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	}

	g_clear_error(&error);

	return tmp;
}

/**
 * Get a string configuration value
 *
 * @param group The configuration group to get the value from
 * @param key The configuration key to get the value of
 * @param defaultval The default value to use if the key isn't set
 * @param keyfileptr A keyfile pointer, or NULL to use the default keyfile
 * @return The configuration value on success, the default value on failure
 */
gchar *mce_conf_get_string(const gchar *group, const gchar *key,
194
			   const gchar *defaultval)
195 196 197 198
{
	gchar *tmp = NULL;
	GError *error = NULL;

199
	gpointer keyfileptr = mce_conf_get_keyfile();
200 201 202 203

	tmp = g_key_file_get_string(keyfileptr, group, key, &error);

	if (error != NULL) {
204
		mce_log(LL_DEBUG,
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
			"Could not get config key %s/%s; %s; %s%s%s",
			group, key, error->message,
			defaultval ? "defaulting to `" : "no default set",
			defaultval ? defaultval : "",
			defaultval ? "'" : "");

		if (defaultval != NULL)
			tmp = g_strdup(defaultval);
	}

	g_clear_error(&error);

	return tmp;
}

/**
 * Get a string list configuration value
 *
 * @param group The configuration group to get the value from
 * @param key The configuration key to get the value of
spiiroin's avatar
spiiroin committed
225
 * @param length The length of the list, or NULL if not needed
226 227 228 229
 * @param keyfileptr A keyfile pointer, or NULL to use the default keyfile
 * @return The configuration value on success, NULL on failure
 */
gchar **mce_conf_get_string_list(const gchar *group, const gchar *key,
230
				 gsize *length)
231 232 233 234
{
	gchar **tmp = NULL;
	GError *error = NULL;

235
	gpointer keyfileptr = mce_conf_get_keyfile();
236 237 238 239 240

	tmp = g_key_file_get_string_list(keyfileptr, group, key,
					 length, &error);

	if (error != NULL) {
241
		mce_log(LL_DEBUG,
242 243
			"Could not get config key %s/%s; %s",
			group, key, error->message);
spiiroin's avatar
spiiroin committed
244 245
		if( length )
			*length = 0;
246 247 248 249 250 251 252
	}

	g_clear_error(&error);

	return tmp;
}

253
gchar **mce_conf_get_keys(const gchar *group, gsize *length)
254 255 256 257
{
	gchar **tmp = NULL;
	GError *error = NULL;

258
	gpointer keyfileptr = mce_conf_get_keyfile();
259 260 261 262 263 264 265

	tmp = g_key_file_get_keys(keyfileptr, group, length, &error);

	if (error != NULL) {
		mce_log(LL_WARN,
			"Could not get config keys %s; %s",
			group, error->message);
spiiroin's avatar
spiiroin committed
266 267
		if( length )
			*length = 0;
268 269 270 271 272 273 274
	}

	g_clear_error(&error);

	return tmp;
}

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
/** Copy key value key value from one keyfile to another
 *
 * @param dest keyfile to modify
 * @param srce keyfile to copy from
 * @param grp  value group to copy
 * @param key  value key to copy
 */
static void mce_conf_override_key(GKeyFile *dest, GKeyFile *srce,
			       const char *grp, const char *key)
{
	gchar *val = g_key_file_get_value(srce, grp, key, 0);
	if( val ) {
		//mce_log(LL_NOTICE, "[%s] %s = %s", grp, key, val);
		g_key_file_set_value(dest, grp, key, val);
		g_free(val);
	}
}

/** Augment key value with data from another file
 *
 * @param dest keyfile to modify
 * @param srce keyfile to add from
 * @param grp  value group to add
 * @param key  value key to add
 */
static void mce_conf_append_key(GKeyFile *dest, GKeyFile *srce,
			       const char *grp, const char *key)
{
	gchar *val = g_key_file_get_value(srce, grp, key, 0);

	if( val ) {
		gchar *old = g_key_file_get_value(dest, grp, key, 0);
		gchar *tmp = 0;

		if( old && *old ) {
310
			tmp = g_strconcat(val, ";", old, NULL);
311 312
		}

313
		//mce_log(LL_NOTICE, "[%s] %s = %s", grp, key, tmp ?: val);
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
		g_key_file_set_value(dest, grp, key, tmp ?: val);

		g_free(tmp);
		g_free(old);
		g_free(val);
	}
}

/** Merge value from one keyfile to another
 *
 * Existing values will be overridden, except for values
 * in group [evdev] that are appended to existing data.
 *
 * @param dest keyfile to modify
 * @param srce keyfile to merge from
 * @param grp  value group to merge
 * @param key  value key to merge
 */
static void mce_conf_merge_key(GKeyFile *dest, GKeyFile *srce,
			       const char *grp, const char *key)
{
335 336 337 338 339
	/* groups/keys to append instead of overriding */
	static const struct {
		const gchar *grp;
		const gchar *key; // NULL == every key in the group
	} lut[] = {
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
		{
			.grp = "evdev",
		},
		{
			.grp = "modules/display",
		},
		{
			.grp = MCE_CONF_LED_GROUP,
			.key = MCE_CONF_LED_PATTERNS_REQUIRED,
		},
		{
			.grp = MCE_CONF_LED_GROUP,
			.key = MCE_CONF_LED_PATTERNS_DISABLED,
		},
		{
			.grp = NULL,
		}
357 358 359 360 361 362 363 364 365 366 367 368 369
	};

	for( size_t i = 0; ; ++i ) {
		if( lut[i].grp == NULL ) {
			mce_conf_override_key(dest, srce, grp, key);
			break;
		}
		if( strcmp(lut[i].grp, grp) )
			continue;
		if( !lut[i].key || !strcmp(lut[i].key, key) ) {
			mce_conf_append_key(dest, srce, grp, key);
			break;
		}
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
	}
}

/** Merge group of values from one keyfile to another
 *
 * @param dest keyfile to modify
 * @param srce keyfile to merge from
 * @param grp  value group to merge
 */
static void mce_conf_merge_group(GKeyFile *dest, GKeyFile *srce,
				 const char *grp)
{
	gchar **key = g_key_file_get_keys(srce, grp, 0, 0);
	if( key ) {
		for( size_t k = 0; key[k]; ++k )
			mce_conf_merge_key(dest, srce, grp, key[k]);
		g_strfreev(key);
	}
}

/** Merge all groups and values from one keyfile to another
 *
 * @param dest keyfile to modify
 * @param srce keyfile to merge from
 */
static void mce_conf_merge_file(GKeyFile *dest, GKeyFile *srce)
{
	gchar **grp = g_key_file_get_groups(srce, 0);

	if( grp ) {
		for( size_t g = 0; grp[g]; ++g )
			mce_conf_merge_group(dest, srce, grp[g]);
		g_strfreev(grp);
	}
}

/** Callback function for logging errors within glob()
 *
 * @param path path to file/dir where error occurred
 * @param err  errno that occurred
 *
 * @return 0 (= do not stop glob)
 */
static int mce_conf_glob_error_cb(const char *path, int err)
{
415 416
	mce_log(LL_WARN, "%s: glob: %s", path, g_strerror(err));
	return 0;
417 418 419 420 421 422
}

/** Process config data from /etc/mce/mce.d/xxx.ini files
 */
static GKeyFile *mce_conf_read_ini_files(void)
{
423 424
	static const char pattern[] = MCE_CONF_DIR"/[0-9][0-9]*.ini";

425 426 427 428 429
	GKeyFile *ini = g_key_file_new();
	glob_t    gb;

	memset(&gb, 0, sizeof gb);

430
	if( glob(pattern, 0, mce_conf_glob_error_cb, &gb) != 0 ) {
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
		mce_log(LL_WARN, "no mce configuration ini-files found");
		goto EXIT;
	}

	for( size_t i = 0; i < gb.gl_pathc; ++i ) {
		const char *path = gb.gl_pathv[i];
		GError     *err  = 0;
		GKeyFile   *tmp  = g_key_file_new();

		if( !g_key_file_load_from_file(tmp, path, 0, &err) ) {
			mce_log(LL_WARN, "%s: can't load: %s", path,
				err->message ?: "unknown");
		}
		else {
			mce_log(LL_NOTICE, "processing %s ...", path);
			mce_conf_merge_file(ini, tmp);
		}
		g_clear_error(&err);
		g_key_file_free(tmp);
	}

EXIT:
	globfree(&gb);

	return ini;
}

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
/* XXX:
 * We should probably use
 * /dev/input/keypad
 * /dev/input/gpio-keys
 * /dev/input/pwrbutton
 * /dev/input/ts
 * and add whitelist entries for misc devices instead
 */

/**
 * List of drivers that provide touchscreen events
 * XXX: If this is made case insensitive,
 *      we could search for "* touchscreen" instead
 */
static const gchar *const touch_builtin[] = {
	/** Input layer name for the Atmel mXT touchscreen */
	"Atmel mXT Touchscreen",

	/** Input layer name for the Atmel QT602240 touchscreen */
	"Atmel QT602240 Touchscreen",

	/** TSC2005 touchscreen */
	"TSC2005 touchscreen",

	/** TSC2301 touchscreen */
	"TSC2301 touchscreen",

	/** ADS784x touchscreen */
	"ADS784x touchscreen",

	/** No more entries */
	NULL
};

/**
 * List of drivers that provide keyboard events
 */
static const gchar *const keybd_builtin[] = {
	/** Input layer name for the TWL4030 keyboard/keypad */
	"TWL4030 Keypad",

	/** Legacy input layer name for the TWL4030 keyboard/keypad */
	"omap_twl4030keypad",

	/** Generic input layer name for keyboard/keypad */
	"Internal keyboard",

	/** Input layer name for the LM8323 keypad */
	"LM8323 keypad",

	/** Generic input layer name for keypad */
	"Internal keypad",

	/** Input layer name for the TSC2301 keypad */
	"TSC2301 keypad",

	/** Legacy generic input layer name for keypad */
	"omap-keypad",

	/** Input layer name for standard PC keyboards */
	"AT Translated Set 2 keyboard",

	/** Input layer name for the power button in various MeeGo devices */
	"msic_power_btn",

	/** Input layer name for the TWL4030 power button */
	"twl4030_pwrbutton",

	/** Input layer name for the Triton 2 power button */
	"triton2-pwrbutton",

	/** Input layer name for the Retu powerbutton */
	"retu-pwrbutton",

	/** Input layer name for the PC Power button */
	"Power Button",

	/** Input layer name for the PC Sleep button */
	"Sleep Button",

	/** Input layer name for the Thinkpad extra buttons */
	"Thinkpad Extra Buttons",

	/** Input layer name for ACPI virtual keyboard */
	"ACPI Virtual Keyboard Device",

	/** Input layer name for GPIO-keys */
	"gpio-keys",

	/** Input layer name for DFL-61/TWL4030 jack sense */
	"dfl61-twl4030 Jack",

	/** Legacy input layer name for TWL4030 jack sense */
	"rx71-twl4030 Jack",

	/** Input layer name for PC Lid switch */
	"Lid Switch",

	/** No more entries */
	NULL
};

/**
 * List of drivers that we should not monitor
 */
static const gchar *const black_builtin[] = {
	/** Input layer name for the AMI305 magnetometer */
	"ami305 magnetometer",

	/** Input layer name for the ST LIS3LV02DL accelerometer */
	"ST LIS3LV02DL Accelerometer",

	/** Input layer name for the ST LIS302DL accelerometer */
	"ST LIS302DL Accelerometer",

	/** Input layer name for the TWL4030 vibrator */
	"twl4030:vibrator",

	/** Input layer name for AV accessory */
	"AV Accessory",

	/** Input layer name for the video bus */
	"Video Bus",

	/** Input layer name for the PC speaker */
	"PC Speaker",

	/** Input layer name for the Intel HDA headphone */
	"HDA Intel Headphone",

	/** Input layer name for the Intel HDA microphone */
	"HDA Intel Mic",

	/** Input layer name for the UVC 17ef:4807 webcam in thinkpad X301 */
	"UVC Camera (17ef:4807)",

	/** Input layer name for the UVC 17ef:480c webcam in thinkpad X201si */
	"UVC Camera (17ef:480c)",

	/** No more entries */
	NULL
};

/** List of touchscreen event devices obtained from ini files */
static gchar **touch_cached = NULL;

/** List of keyboard event devices obtained from ini files */
static gchar **keybd_cached = NULL;

/** List of blacklisted event devices obtained from ini files */
static gchar **black_cached = NULL;

610 611 612 613 614 615 616 617 618
/**
 * Init function for the mce-conf component
 *
 * @return TRUE on success, FALSE on failure
 */
gboolean mce_conf_init(void)
{
	gboolean status = FALSE;

619
	if( !(keyfile = mce_conf_read_ini_files()) )
620 621
		goto EXIT;

622 623 624 625 626 627 628 629 630 631 632 633 634 635
	touch_cached = g_key_file_get_string_list(keyfile,
						  "evdev",
						  "touch",
						  0, 0);

	keybd_cached = g_key_file_get_string_list(keyfile,
						  "evdev",
						  "keybd",
						  0, 0);

	black_cached = g_key_file_get_string_list(keyfile,
						  "evdev",
						  "black",
						  0, 0);
636 637 638 639 640 641 642 643 644 645 646
	status = TRUE;

EXIT:
	return status;
}

/**
 * Exit function for the mce-conf component
 */
void mce_conf_exit(void)
{
647 648 649 650
	g_strfreev(touch_cached), touch_cached = 0;
	g_strfreev(keybd_cached), keybd_cached = 0;
	g_strfreev(black_cached), black_cached = 0;

651
	if( keyfile ) g_key_file_free(keyfile), keyfile = 0;
652 653 654

	return;
}
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669

const gchar * const *mce_conf_get_touchscreen_event_drivers(void)
{
	return (const gchar*const*)touch_cached ?: touch_builtin;
}

const gchar * const *mce_conf_get_keyboard_event_drivers(void)
{
	return (const gchar*const*)keybd_cached ?: keybd_builtin;
}

const gchar * const *mce_conf_get_blacklisted_event_drivers(void)
{
	return (const gchar*const*)black_cached ?: black_builtin;
}