/
sensor-gestures.c
526 lines (425 loc) · 14.2 KB
/
sensor-gestures.c
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
/**
* @file sensor-gestures.c
*
* Sensor gesture module for the Mode Control Entity
* <p>
* Copyright © 2014 Jolla Ltd.
* <p>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* 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.h"
#include "../mce-log.h"
#include "../mce-setting.h"
#include "../mce-dbus.h"
#include "display.h"
#include <mce/dbus-names.h>
#include <mce/mode-names.h>
#include <gmodule.h>
/* ========================================================================= *
* STATE_DATA
* ========================================================================= */
/** Cached display state */
static display_state_t display_state_curr = MCE_DISPLAY_UNDEF;
/** Cached alarm ui state */
static alarm_ui_state_t alarm_ui_state = MCE_ALARM_UI_INVALID_INT32;
/** Cached call state */
static call_state_t call_state = CALL_STATE_INVALID;
/** Cached raw orientation sensor value */
static orientation_state_t orientation_sensor_actual = MCE_ORIENTATION_UNDEFINED;
/** Cached delayed orientation sensor value */
static orientation_state_t orientation_sensor_effective = MCE_ORIENTATION_UNDEFINED;
/** Timer id for delayed orientation_sensor_effective updating */
static gint orientation_sensor_effective_id = 0;
/** Use of flipover gesture enabled */
static gboolean sg_flipover_gesture_enabled = MCE_DEFAULT_FLIPOVER_GESTURE_ENABLED;
static guint sg_flipover_gesture_enabled_setting_id = 0;
/* ========================================================================= *
* FUNCTIONS
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* FLIPOVER_GESTURE
* ------------------------------------------------------------------------- */
static void sg_send_flipover_signal (const char *sig);
static void sg_detect_flipover_gesture (void);
/* ------------------------------------------------------------------------- *
* DATAPIPE_TRACKING
* ------------------------------------------------------------------------- */
static bool sg_have_alarm_dialog (void);
static bool sg_have_incoming_call (void);
static void sg_call_state_cb (gconstpointer const data);
static void sg_alarm_ui_state_cb (gconstpointer data);
static void sg_display_state_curr_cb (gconstpointer data);
static void sg_orientation_sensor_update(void);
static gboolean sg_orientation_sensor_effective_cb (gpointer data);
static void sg_orientation_sensor_actual_cb (gconstpointer data);
static void sg_datapipe_init (void);
static void sg_datapipe_quit (void);
/* ------------------------------------------------------------------------- *
* DYNAMIC_SETTINGS
* ------------------------------------------------------------------------- */
static void sg_setting_cb (GConfClient *const gcc, const guint id, GConfEntry *const entry, gpointer const data);
static void sg_setting_init (void);
static void sg_setting_quit (void);
/* ------------------------------------------------------------------------- *
* PLUGIN_LOAD_UNLOAD
* ------------------------------------------------------------------------- */
G_MODULE_EXPORT const gchar *g_module_check_init (GModule *module);
G_MODULE_EXPORT void g_module_unload (GModule *module);
/* ========================================================================= *
* FLIPOVER_GESTURE
* ========================================================================= */
/** Helper for sending flipover dbus signal
*
* @param signal name
*/
static void sg_send_flipover_signal(const char *sig)
{
/* Do not send the signals if orientation sensor happens to be
* powered on for some other reasons than flipover detection */
if( !sg_flipover_gesture_enabled )
goto EXIT;
// NOTE: introspection data shared with powerkey.c
const char *arg = MCE_FEEDBACK_EVENT_FLIPOVER;
mce_log(LL_DEVEL, "sending dbus signal: %s %s", sig, arg);
dbus_send(0, MCE_SIGNAL_PATH, MCE_SIGNAL_IF, sig, 0,
DBUS_TYPE_STRING, &arg, DBUS_TYPE_INVALID);
EXIT:
return;
}
/** Detect and broadcast device flipover during alarm
*
* While display is on and alarm active,
* send "flipover" signal over dbus, if
* we first see orientation = face up,
* followed by orientation = face down.
*/
static void sg_detect_flipover_gesture(void)
{
static bool primed = false;
/* Check display state */
if( display_state_curr != MCE_DISPLAY_ON ) {
primed = false;
goto EXIT;
}
/* Check active alarm / incoming call */
if( !sg_have_alarm_dialog() &&
!sg_have_incoming_call() ) {
primed = false;
goto EXIT;
}
/* Check for undefined orientation state */
if( orientation_sensor_actual == MCE_ORIENTATION_UNDEFINED ||
orientation_sensor_effective == MCE_ORIENTATION_UNDEFINED ) {
primed = false;
goto EXIT;
}
/* Check effective orientation state */
if( orientation_sensor_effective == MCE_ORIENTATION_FACE_UP ) {
primed = true;
}
else if( orientation_sensor_effective != MCE_ORIENTATION_FACE_DOWN ) {
// nop
}
else if( primed ) {
primed = false;
if( sg_have_alarm_dialog() )
sg_send_flipover_signal(MCE_ALARM_UI_FEEDBACK_SIG);
if( sg_have_incoming_call() )
sg_send_flipover_signal(MCE_CALL_UI_FEEDBACK_SIG);
}
EXIT:
return;
}
/* ========================================================================= *
* DATAPIPE_TRACKING
* ========================================================================= */
/** Helper for checking if there is an active alarm dialog
*
* @return true if there is alarm, false otherwise
*/
static bool sg_have_alarm_dialog(void)
{
bool res = false;
switch( alarm_ui_state ) {
case MCE_ALARM_UI_RINGING_INT32:
case MCE_ALARM_UI_VISIBLE_INT32:
res = true;
break;
default:
break;
}
return res;
}
/** Helper for checking if there is an incoming call
*
* @return true if there is incoming call, false otherwise
*/
static bool sg_have_incoming_call(void)
{
bool res = false;
switch( call_state ) {
case CALL_STATE_RINGING:
res = true;
break;
default:
break;
}
return res;
}
/** Handle call_state_pipe notifications
*
* @param data The call state stored in a pointer
*/
static void sg_call_state_cb(gconstpointer const data)
{
call_state_t prev = call_state;
call_state = GPOINTER_TO_INT(data);
if( call_state == prev )
goto EXIT;
mce_log(LL_DEBUG, "call: %s -> %s",
call_state_repr(prev),
call_state_repr(call_state));
sg_detect_flipover_gesture();
EXIT:
return;
}
/** Handle alarm_ui_state_pipe notifications
*
* @param data The alarm state stored in a pointer
*/
static void sg_alarm_ui_state_cb(gconstpointer data)
{
alarm_ui_state_t prev = alarm_ui_state;
alarm_ui_state = GPOINTER_TO_INT(data);
if( alarm_ui_state == prev )
goto EXIT;
mce_log(LL_DEBUG, "alarm: %s -> %s",
alarm_state_repr(prev),
alarm_state_repr(alarm_ui_state));
sg_detect_flipover_gesture();
EXIT:
return;
}
/** Handle display_state_curr_pipe notifications
*
* @param data The display state stored in a pointer
*/
static void sg_display_state_curr_cb(gconstpointer data)
{
display_state_t prev = display_state_curr;
display_state_curr = GPOINTER_TO_INT(data);
if( display_state_curr == prev )
goto EXIT;
mce_log(LL_DEBUG, "display: %s -> %s",
display_state_repr(prev),
display_state_repr(display_state_curr));
sg_detect_flipover_gesture();
EXIT:
return;
}
/** Update effective orientation state from raw sensor state
*/
static void sg_orientation_sensor_update(void)
{
orientation_state_t prev = orientation_sensor_effective;
orientation_sensor_effective = orientation_sensor_actual;
if( orientation_sensor_effective == prev )
goto EXIT;
mce_log(LL_DEBUG, "orient.eff: %s -> %s",
orientation_state_repr(prev),
orientation_state_repr(orientation_sensor_effective));
sg_detect_flipover_gesture();
EXIT:
return;
}
/** Handle delayed orientation_sensor_actual_pipe notifications
*
* @param data (unused)
*
* @return FALSE to stop the timer from repeating
*/
static gboolean sg_orientation_sensor_effective_cb(gpointer data)
{
(void)data;
if( !orientation_sensor_effective_id )
goto EXIT;
mce_log(LL_DEBUG, "orient.eff: timer triggered");
orientation_sensor_effective_id = 0;
sg_orientation_sensor_update();
EXIT:
return FALSE;
}
/** Handle orientation_sensor_actual_pipe notifications
*
* @param data The orientation state stored in a pointer
*/
static void sg_orientation_sensor_actual_cb(gconstpointer data)
{
orientation_state_t prev = orientation_sensor_actual;
orientation_sensor_actual = GPOINTER_TO_INT(data);
if( orientation_sensor_actual == prev )
goto EXIT;
mce_log(LL_DEBUG, "orient.raw: %s -> %s",
orientation_state_repr(prev),
orientation_state_repr(orientation_sensor_actual));
/* Unprime if orientation is unknown */
sg_detect_flipover_gesture();
/* When the orientation sensor is stopped and restarted,
* sensord reports initially the last state that was seen
* before the sensor was stopped.
*
* To avoid false positives, the acceptance of face up
* orientation after sensor startup is delayed by bit
* more than what the sensor ramp up is expected to take.
*/
/* Remove existing delay timer */
if( orientation_sensor_effective_id ) {
g_source_remove(orientation_sensor_effective_id);
orientation_sensor_effective_id = 0;
mce_log(LL_DEBUG, "orient.eff: timer canceled");
}
if( prev == MCE_ORIENTATION_UNDEFINED &&
orientation_sensor_actual == MCE_ORIENTATION_FACE_UP ) {
/* Invalidate effective sensor value */
orientation_sensor_effective = MCE_ORIENTATION_UNDEFINED;
/* Schedule re-validation after 1000 ms */
orientation_sensor_effective_id =
g_timeout_add(1000, sg_orientation_sensor_effective_cb, 0);
mce_log(LL_DEBUG, "orient.eff: timer started");
}
else {
/* Update effective sensor value immediately */
sg_orientation_sensor_update();
}
EXIT:
return;
}
/** Array of datapipe handlers */
static datapipe_handler_t sg_datapipe_handlers[] =
{
// input triggers
{
.datapipe = &call_state_pipe,
.input_cb = sg_call_state_cb,
},
// output triggers
{
.datapipe = &orientation_sensor_actual_pipe,
.output_cb = sg_orientation_sensor_actual_cb,
},
{
.datapipe = &display_state_curr_pipe,
.output_cb = sg_display_state_curr_cb,
},
{
.datapipe = &alarm_ui_state_pipe,
.output_cb = sg_alarm_ui_state_cb,
},
// sentinel
{
.datapipe = 0,
}
};
static datapipe_bindings_t sg_datapipe_bindings =
{
.module = "sensor-gestures",
.handlers = sg_datapipe_handlers,
};
/** Append triggers/filters to datapipes
*/
static void sg_datapipe_init(void)
{
mce_datapipe_init_bindings(&sg_datapipe_bindings);
}
/** Remove triggers/filters from datapipes */
static void sg_datapipe_quit(void)
{
mce_datapipe_quit_bindings(&sg_datapipe_bindings);
}
/* ========================================================================= *
* DYNAMIC_SETTINGS
* ========================================================================= */
/**
* GConf callback for display related settings
*
* @param gcc Unused
* @param id Connection ID from gconf_client_notify_add()
* @param entry The modified GConf entry
* @param data Unused
*/
static void sg_setting_cb(GConfClient *const gcc, const guint id,
GConfEntry *const entry, gpointer const data)
{
const GConfValue *gcv = gconf_entry_get_value(entry);
(void)gcc;
(void)data;
/* Key is unset */
if (gcv == NULL) {
mce_log(LL_DEBUG, "GConf Key `%s' has been unset",
gconf_entry_get_key(entry));
goto EXIT;
}
if( id == sg_flipover_gesture_enabled_setting_id ) {
sg_flipover_gesture_enabled = gconf_value_get_bool(gcv);
}
else {
mce_log(LL_WARN, "Spurious GConf value received; confused!");
}
EXIT:
return;
}
/** Get initial setting values and start tracking changes
*/
static void sg_setting_init(void)
{
mce_setting_track_bool(MCE_SETTING_FLIPOVER_GESTURE_ENABLED,
&sg_flipover_gesture_enabled,
MCE_DEFAULT_FLIPOVER_GESTURE_ENABLED,
sg_setting_cb,
&sg_flipover_gesture_enabled_setting_id);
}
/** Stop tracking setting changes */
static void sg_setting_quit(void)
{
mce_setting_notifier_remove(sg_flipover_gesture_enabled_setting_id),
sg_flipover_gesture_enabled_setting_id = 0;
}
/* ========================================================================= *
* PLUGIN_LOAD_UNLOAD
* ========================================================================= */
/** Init function for the sensor-gestures module
*
* @param module (not used)
*
* @return NULL on success, a string with an error message on failure
*/
const gchar *g_module_check_init(GModule *module)
{
(void)module;
sg_setting_init();
sg_datapipe_init();
return NULL;
}
/** Exit function for the sensor-gestures module
*
* @param module (not used)
*/
void g_module_unload(GModule *module)
{
(void)module;
sg_datapipe_quit();
sg_setting_quit();
return;
}