summaryrefslogtreecommitdiff
path: root/src/direct-access.c
blob: bfeaa84163610a357e220a2cb55d3855395c297d (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
/*
 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <Ecore.h>
#include <dd-display.h>
#include <dd-led.h>
#include <aul.h>
#include <vconf.h>
#include <Ecore_X.h>
#include <Ecore_Input.h>
#include <utilX.h>
#include <feedback.h>
#include<Elementary.h>
#ifndef FEATURE_LITE
#include <tts.h>
#endif

#include "starter.h"
#include "util.h"
#include "direct-access.h"

#define _(str) dgettext("starter", str)

#define DBUS_REPLY_TIMEOUT (120 * 1000)
#define DEVICED_BUS_NAME        "org.tizen.system.deviced"
#define DEVICED_OBJECT_PATH     "/Org/Tizen/System/DeviceD"
#define DEVICED_INTERFACE_NAME  DEVICED_BUS_NAME

#define ALWAYS_ASK_BUS_NAME       "org.tizen.system.popup"
#define ALWAYS_ASK_OBJECT_PATH    "/Org/Tizen/System/Popup/System"
#define ALWAYS_ASK_INTERFACE_NAME ALWAYS_ASK_BUS_NAME".System"
#define METHOD_ALWAYS_ASK_NAME    "AccessibilityPopupLaunch"
#define ALWAYS_ASK_PARAM_1        "_SYSPOPUP_CONTENT_"
#define ALWAYS_ASK_PARAM_2        "accessibility"

#ifndef FEATURE_LITE
static tts_h tts;
#endif
static int tts_status;

static int _append_variant(DBusMessageIter *iter, const char *sig, char *param[])
{
	char *ch;
	int i;
	int int_type;
	uint64_t int64_type;

	if (!sig || !param)
		return 0;

	for (ch = (char*)sig, i = 0; *ch != '\0'; ++i, ++ch) {
		switch (*ch) {
		case 'i':
			int_type = atoi(param[i]);
			dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &int_type);
			break;
		case 'u':
			int_type = atoi(param[i]);
			dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &int_type);
			break;
		case 't':
			int64_type = atoi(param[i]);
			dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT64, &int64_type);
			break;
		case 's':
			dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &param[i]);
			break;
		default:
			return -EINVAL;
		}
	}

	return 0;
}


DBusMessage *invoke_dbus_method_sync(const char *dest, const char *path,
		const char *interface, const char *method,
		const char *sig, char *param[])
{
	DBusConnection *conn;
	DBusMessage *msg;
	DBusMessageIter iter;
	DBusMessage *reply;
	DBusError err;
	int r;

	conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
	if (!conn) {
		_E("dbus_bus_get error");
		return NULL;
	}

	msg = dbus_message_new_method_call(dest, path, interface, method);
	if (!msg) {
		_E("dbus_message_new_method_call(%s:%s-%s)", path, interface, method);
		return NULL;
	}

	dbus_message_iter_init_append(msg, &iter);
	r = _append_variant(&iter, sig, param);
	if (r < 0) {
		_E("append_variant error(%d)", r);
		return NULL;
	}

	dbus_error_init(&err);

	reply = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
	if (!reply) {
		_E("dbus_connection_send error(No reply)");
	}

	if (dbus_error_is_set(&err)) {
		_E("dbus_connection_send error(%s:%s)", err.name, err.message);
		reply = NULL;
	}

	dbus_message_unref(msg);
	dbus_error_free(&err);

	return reply;
}


DBusMessage *invoke_dbus_method(const char *dest, const char *path,
		const char *interface, const char *method,
		const char *sig, char *param[])
{
	DBusConnection *conn;
	DBusMessage *msg;
	DBusMessageIter iter;
	DBusMessage *reply;
	DBusError err;
	int r;

	conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
	if (!conn) {
		_E("dbus_bus_get error");
		return NULL;
	}

	msg = dbus_message_new_method_call(dest, path, interface, method);
	if (!msg) {
		_E("dbus_message_new_method_call(%s:%s-%s)", path, interface, method);
		return NULL;
	}

	dbus_message_iter_init_append(msg, &iter);
	r = _append_variant(&iter, sig, param);
	if (r < 0) {
		_E("append_variant error(%d)", r);
		return NULL;
	}

	r = dbus_connection_send(conn, msg, NULL);
	dbus_message_unref(msg);
	if (r != TRUE) {
		_E("dbus_connection_send error(%s:%s:%s-%s)", dest, path, interface, method);
		return -ECOMM;
	}
	_D("dbus_connection_send, ret=%d", r);
	return NULL;
}








static void _set_assistive_light(void)
{
	int max, state, ret;

	max = led_get_max_brightness();
	if (max < 0)
		max = 1;

	state = led_get_brightness();
	if (state > 0) {
		ret = led_set_brightness_with_noti(0, true);
		if (ret == 0)
			vconf_set_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TORCH_LIGHT, 0);
	} else {
		ret = led_set_brightness_with_noti(max, true);
		if (ret == 0)
			vconf_set_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TORCH_LIGHT, 1);
	}

	if(feedback_initialize() == FEEDBACK_ERROR_NONE)
	{
		_D("make vibration effect");
		feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_TAP);
		feedback_deinitialize();
	}
}



#define LIVE_SETTING_APP "com.samsung.live-setting-app"
static int _launch_live_setting_app(void)
{
	bundle *b;
	int ret;

	b = bundle_create();
	if (!b) {
		_E("Failed to create bundle");
		return -ENOMEM;
	}

	ret = bundle_add(b, "popup", "zoom");
	if (ret < 0) {
		_E("Failed to add parameters to bundle");
		goto out;
	}

	ret = aul_launch_app(LIVE_SETTING_APP, b);
	if (ret < 0)
		_E("Failed to launch app(%s)", LIVE_SETTING_APP);

out:
	bundle_free(b);
	return ret;
}

static bool _is_aircommand_on(void)
{
	int state;
	if (vconf_get_bool(VCONFKEY_AIRCOMMAND_ENABLED, &state) == 0
			&& state == 1)
		return true;
	return false;
}

#define PROP_ZOOM      "_E_ACC_ENABLE_ZOOM_UI_"
static void _set_zoom(void)
{
	int ret;
	unsigned int value;
	Ecore_X_Window rootWin;
	Ecore_X_Atom atomZoomUI;

	rootWin = ecore_x_window_root_first_get();
	atomZoomUI = ecore_x_atom_get(PROP_ZOOM);

	ret = ecore_x_window_prop_card32_get(rootWin, atomZoomUI, &value, 1);
	if (ret == 1 && value == 1)
		value = 0;
	else
		value = 1;

	ecore_x_window_prop_card32_set(rootWin, atomZoomUI, &value, 1);
	ecore_x_flush();

	vconf_set_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_SCREEN_ZOOM, value);
}



#define PROP_HIGH_CONTRAST "_E_ACC_ENABLE_HIGH_CONTRAST_"
static void _set_contrast(void)
{
	int ret;
	unsigned int value;
	Ecore_X_Window rootWin;
	Ecore_X_Atom atomHighContrast;

	rootWin = ecore_x_window_root_first_get();
	atomHighContrast = ecore_x_atom_get(PROP_HIGH_CONTRAST);

	ret = ecore_x_window_prop_card32_get(rootWin, atomHighContrast, &value, 1);
	if (ret == 1 && value == 1)
		value = 0;
	else
		value = 1;

	ecore_x_window_prop_card32_set(rootWin, atomHighContrast, &value, 1);
	ecore_x_flush();

	vconf_set_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_HIGH_CONTRAST, value);
}
#ifndef FEATURE_LITE
void utterance_completed_callback(tts_h tts, int utt_id, void* user_data){
	_D("");
	int ret = 0;
	int u_id = 0;
	ret = tts_stop(tts);
	if(ret != TTS_ERROR_NONE){
		_E("fail to stop(%d)", ret);
		return;
	}
	ret = tts_unprepare(tts);
	if(ret != TTS_ERROR_NONE){
		_E("fail to unprepare(%d)", ret);
		return;
	}
	if(tts){
		ret = tts_destroy(tts);
		tts = NULL;
	}
	return;
}

void _tts_state_changed_cb(tts_h tts, tts_state_e previous, tts_state_e current, void* data){
	int ret = 0;
	int u_id = 0;
	bindtextdomain("starter", "/usr/share/locale");
	appcore_set_i18n();
	if(TTS_STATE_CREATED == previous && current == TTS_STATE_READY){
		if(tts_status){
			ret = tts_add_text(tts, _("IDS_TPLATFORM_BODY_SCREEN_READER_ENABLED_T_TTS"), NULL, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &u_id);
			if(ret != TTS_ERROR_NONE){
				_E("fail to add text(%d)", ret);
				return;
			}
		}
		else{
			ret = tts_add_text(tts, _("IDS_TPLATFORM_BODY_SCREEN_READER_DISABLED_T_TTS"), NULL, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &u_id);
			if(ret != TTS_ERROR_NONE){
				_E("fail to add text(%d)", ret);
				return;
			}
		}
		ret = tts_play(tts);
		if(ret != TTS_ERROR_NONE){
			_E("fail to play(%d)", ret);
			return;
		}
	}
}

static void _starter_tts_play(void){
	int ret = 0;
	int u_id = 0;

	ret = tts_create(&tts);
	if(ret != TTS_ERROR_NONE){
		_E("fail to get handle(%d)", ret);
		return;
	}
	ret = tts_set_state_changed_cb(tts, _tts_state_changed_cb, NULL);
	if(ret != TTS_ERROR_NONE){
		_E("fail to set state changed cb(%d)", ret);
		return;
	}
	ret = tts_set_mode(tts, TTS_MODE_NOTIFICATION);
	if(ret != TTS_ERROR_NONE){
		_E("fail to set mode(%d)", ret);
		return;
	}
	ret = tts_set_utterance_completed_cb(tts, utterance_completed_callback, NULL);
	if(ret != TTS_ERROR_NONE){
		_E("fail to prepare(%d)", ret);
		return;
	}
	ret = tts_prepare(tts);
	if(ret != TTS_ERROR_NONE){
		_E("fail to prepare(%d)", ret);
		return;
	}
	return;
}
#endif
static int _set_accessibility_tts(void)
{
	if (vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &tts_status) < 0) {
		_E("FAIL: vconf_get_bool()");
		return -1;
	}

	if (tts_status == FALSE)
		tts_status = TRUE;
	else
		tts_status = FALSE;
	if (vconf_set_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, tts_status) < 0) {
		_E("FAIL: vconf_set_bool()");
		return -1;
	}
#ifndef FEATURE_LITE
	_starter_tts_play();
#endif
	return 0;
}



static int _launch_always_ask(void)
{
	DBusError err;
	DBusMessage *msg;
	int ret, ret_val;
	char *arr[2];
	char val[32];

	arr[0] = ALWAYS_ASK_PARAM_1;
	arr[1] = ALWAYS_ASK_PARAM_2;

	msg = invoke_dbus_method(ALWAYS_ASK_BUS_NAME, ALWAYS_ASK_OBJECT_PATH, ALWAYS_ASK_INTERFACE_NAME,
			METHOD_ALWAYS_ASK_NAME, "ss", arr);
	if (!msg)
		return -EBADMSG;

	dbus_error_init(&err);

	ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
	if (!ret) {
		_E("no message : [%s:%s]", err.name, err.message);
		ret_val = -EBADMSG;
	}

	dbus_message_unref(msg);
	dbus_error_free(&err);

	_D("%s-%s : %d", ALWAYS_ASK_INTERFACE_NAME, METHOD_ALWAYS_ASK_NAME, ret_val);
	return ret_val;
}



int launch_direct_access(int access_val)
{
	int ret = 0;
	_D("launch_direct_access [%d]", access_val);

	/* Direct Access Operation */
	switch (access_val) {
		case SETTING_POWERKEY_SHORTCUT_ALWAYS_ASK:
			if (!_launch_always_ask())
				ret = -1;
			break;
		case SETTING_POWERKEY_SHORTCUT_SCREEN_READER_TTS:
			if (!_set_accessibility_tts())
				ret = -1;
			break;
		case SETTING_POWERKEY_SHORTCUT_NEGATIVE_COLOURS:
			_set_contrast();
			break;
		case SETTING_POWERKEY_SHORTCUT_ZOOM:
			if (_is_aircommand_on()) {
				ret = -1;
				if (_launch_live_setting_app() < 0)
					_E("Failed to launch (%s)", LIVE_SETTING_APP);
				return ret;
			}
			_set_zoom();
			break;
		case SETTING_POWERKEY_SHORTCUT_ASSISTIVE_LIGHT:
			_set_assistive_light();
			break;
		case SETTING_POWERKEY_SHORTCUT_SHOT_READER:

			break;
		default:
			ret = -1;
			break;
	}
	return ret;
}