datapipe.c 78.7 KB
Newer Older
1 2 3 4 5
/**
 * @file datapipe.c
 * This file implements the sinmple datapipe framework;
 * this can be used to filter data and to setup data triggers
 * <p>
6 7 8
 * Copyright (c) 2007 - 2008 Nokia Corporation and/or its subsidiary(-ies).
 * Copyright (c) 2014 - 2020 Jolla Ltd.
 * Copyright (c) 2019 - 2020 Open Mobile Platform LLC.
9 10
 * <p>
 * @author David Weinehall <david.weinehall@nokia.com>
11
 * @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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/>.
 */
25 26 27

#include "mce.h"
#include "mce-log.h"
28
#include "mce-lib.h"
29
#include "evdev.h"
30 31

#include <mce/mode-names.h>
32

33 34
#include <linux/input.h>

35 36
#include <stdio.h>

37 38 39 40
/* ========================================================================= *
 * Macros
 * ========================================================================= */

41 42 43 44 45 46 47 48 49 50 51
#define cat2(a,b) a##b
#define cat3(a,b,c) a##b##c

#define datapipe_log(PIPE_, FMT_, ARGS_...)\
     do {\
         if( mce_log_p(LL_DEBUG) || \
             mce_log_p_(LL_DEBUG,__FILE__,PIPE_->dp_name) ) {\
             mce_log_unconditional(LL_DEBUG, __FILE__, __func__,\
                          FMT_ , ## ARGS_);\
         }\
     } while(0)
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/* ========================================================================= *
 * Types
 * ========================================================================= */

struct datapipe_t
{
    const char           *dp_name;             /**< Name of the datapipe */
    GSList               *dp_filters;          /**< The filters */
    GSList               *dp_input_triggers;   /**< Triggers called on indata */
    GSList               *dp_output_triggers;  /**< Triggers called on outdata */
    gconstpointer         dp_cached_data;      /**< Latest cached data */
    gsize                 dp_datasize;         /**< Size of data; NULL == automagic */
    datapipe_filtering_t  dp_read_only;        /**< Datapipe is read only */
    datapipe_cache_t      dp_cache;
    guint                 dp_gc_id;
    guint                 dp_token;
69 70
    const char         *(*dp_value_repr_cb)(gconstpointer value);
    const char         *(*dp_change_repr_cb)(gconstpointer prev, gconstpointer curr);
71 72
};

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#define DATAPIPE_INIT(NAME_,TYPE_,VALUE_,SIZE_,FILTERING_,CACHING_)\
     {\
         .dp_name = #NAME_ "_pipe",\
         .dp_filters = 0,\
         .dp_input_triggers = 0,\
         .dp_output_triggers = 0,\
         .dp_cached_data = GINT_TO_POINTER(VALUE_),\
         .dp_datasize = SIZE_,\
         .dp_read_only = FILTERING_,\
         .dp_cache = CACHING_,\
         .dp_gc_id = 0,\
         .dp_token = 0,\
         .dp_value_repr_cb = cat3(datapipe_hook_,TYPE_,_value),\
         .dp_change_repr_cb = cat3(datapipe_hook_,TYPE_,_change),\
     }

89 90 91 92
/* ========================================================================= *
 * Prototypes
 * ========================================================================= */

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/* ------------------------------------------------------------------------- *
 * DATAPIPE_HOOK
 * ------------------------------------------------------------------------- */

static const char  *datapipe_hook_int_value                (gconstpointer data);
static const char  *datapipe_hook_boolean_value            (gconstpointer data);
static const char  *datapipe_hook_string_value             (gconstpointer data);
static const char  *datapipe_hook_impulse_value            (gconstpointer data);
static const char  *datapipe_hook_input_event_value        (gconstpointer data);
static const char  *datapipe_hook_input_event_ptr_value    (gconstpointer data);
static const char  *datapipe_hook_display_state_value      (gconstpointer data);
static const char  *datapipe_hook_uiexception_type_value   (gconstpointer data);
static const char  *datapipe_hook_lockkey_state_value      (gconstpointer data);
static const char  *datapipe_hook_tristate_value           (gconstpointer data);
static const char  *datapipe_hook_cover_state_value        (gconstpointer data);
static const char  *datapipe_hook_orientation_state_value  (gconstpointer data);
static const char  *datapipe_hook_alarm_ui_state_value     (gconstpointer data);
static const char  *datapipe_hook_system_state_value       (gconstpointer data);
static const char  *datapipe_hook_submode_value            (gconstpointer data);
static const char  *datapipe_hook_submode_change           (gconstpointer prev, gconstpointer curr);
static const char  *datapipe_hook_call_state_value         (gconstpointer data);
static const char  *datapipe_hook_call_type_value          (gconstpointer data);
static const char  *datapipe_hook_tklock_request_value     (gconstpointer data);
static const char  *datapipe_hook_charger_state_value      (gconstpointer data);
117
static const char  *datapipe_hook_charger_type_value       (gconstpointer data);
118
static const char  *datapipe_hook_battery_status_value     (gconstpointer data);
119
static const char  *datapipe_hook_battery_state_value      (gconstpointer data);
120 121 122 123 124 125 126 127
static const char  *datapipe_hook_camera_button_state_value(gconstpointer data);
static const char  *datapipe_hook_audio_route_value        (gconstpointer data);
static const char  *datapipe_hook_usb_cable_state_value    (gconstpointer data);
static const char  *datapipe_hook_thermal_state_value      (gconstpointer data);
static const char  *datapipe_hook_service_state_value      (gconstpointer data);
static const char  *datapipe_hook_devicelock_state_value   (gconstpointer data);
static const char  *datapipe_hook_fpstate_value            (gconstpointer data);

128 129 130 131
/* ------------------------------------------------------------------------- *
 * DATAPIPE
 * ------------------------------------------------------------------------- */

132 133
const char       *datapipe_name                 (const datapipe_t *self);
gconstpointer     datapipe_value                (const datapipe_t *self);
134
void              datapipe_set_value            (datapipe_t *self, gconstpointer data);
135 136 137 138
static void       datapipe_gc                   (datapipe_t *self);
static gboolean   datapipe_gc_cb                (gpointer aptr);
static void       datapipe_schedule_gc          (datapipe_t *self);
static void       datapipe_cancel_gc            (datapipe_t *self);
139
gconstpointer     datapipe_exec_full_real       (datapipe_t *self, gconstpointer indata, const char *file, const char *func);
140 141 142 143 144 145 146
static void       datapipe_add_filter           (datapipe_t *self, gpointer (*filter)(gpointer data));
static void       datapipe_remove_filter        (datapipe_t *self, gpointer (*filter)(gpointer data));
static void       datapipe_add_input_trigger    (datapipe_t *self, void (*trigger)(gconstpointer data));
static void       datapipe_remove_input_trigger (datapipe_t *self, void (*trigger)(gconstpointer data));
static void       datapipe_add_output_trigger   (datapipe_t *self, void (*trigger)(gconstpointer data));
static void       datapipe_remove_output_trigger(datapipe_t *self, void (*trigger)(gconstpointer data));
static void       datapipe_free                 (datapipe_t *self);
147 148 149 150 151

/* ------------------------------------------------------------------------- *
 * MCE_DATAPIPE
 * ------------------------------------------------------------------------- */

152 153 154 155 156
void             mce_datapipe_init               (void);
void             mce_datapipe_quit               (void);
static void      mce_datapipe_install_handlers   (datapipe_handler_t *bindings);
static void      mce_datapipe_remove_handlers    (datapipe_handler_t *bindings);
static void      mce_datapipe_execute_handlers   (datapipe_handler_t *bindings);
157 158 159
static gboolean  mce_datapipe_execute_handlers_cb(gpointer aptr);
void             mce_datapipe_init_bindings      (datapipe_bindings_t *self);
void             mce_datapipe_quit_bindings      (datapipe_bindings_t *self);
160 161
void             mce_datapipe_generate_activity  (void);
void             mce_datapipe_generate_inactivity(void);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

/* ------------------------------------------------------------------------- *
 * SUBMODE
 * ------------------------------------------------------------------------- */

const char  *submode_change_repr(submode_t prev, submode_t curr);
const char  *submode_repr       (submode_t submode);

/* ------------------------------------------------------------------------- *
 * SYSTEM_STATE
 * ------------------------------------------------------------------------- */

const char  *system_state_repr(system_state_t state);

/* ------------------------------------------------------------------------- *
 * DEVICELOCK_STATE
 * ------------------------------------------------------------------------- */

const char  *devicelock_state_repr(devicelock_state_t state);

182 183 184 185 186 187
/* ------------------------------------------------------------------------- *
 * THERMAL_STATE
 * ------------------------------------------------------------------------- */

const char  *thermal_state_repr(thermal_state_t state);

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/* ------------------------------------------------------------------------- *
 * UIEXCEPTION_TYPE
 * ------------------------------------------------------------------------- */

const char  *uiexception_type_repr   (uiexception_type_t type);
const char  *uiexception_type_to_dbus(uiexception_type_t type);

/* ------------------------------------------------------------------------- *
 * SERVICE_STATE
 * ------------------------------------------------------------------------- */

const char  *service_state_repr(service_state_t state);

/* ------------------------------------------------------------------------- *
 * USB_CABLE_STATE
 * ------------------------------------------------------------------------- */

const char  *usb_cable_state_repr   (usb_cable_state_t state);
const char  *usb_cable_state_to_dbus(usb_cable_state_t state);

208 209 210 211 212 213 214
/* ------------------------------------------------------------------------- *
 * CHARGER_TYPE
 * ------------------------------------------------------------------------- */

const char  *charger_type_repr   (charger_type_t type);
const char  *charger_type_to_dbus(charger_type_t type);

215 216 217 218 219 220 221
/* ------------------------------------------------------------------------- *
 * CHARGER_STATE
 * ------------------------------------------------------------------------- */

const char  *charger_state_repr   (charger_state_t state);
const char  *charger_state_to_dbus(charger_state_t state);

222 223 224 225 226 227 228 229 230 231 232 233
/* ------------------------------------------------------------------------- *
 * CAMERA_BUTTON_STATE
 * ------------------------------------------------------------------------- */

const char  *camera_button_state_repr(camera_button_state_t state);

/* ------------------------------------------------------------------------- *
 * AUDIO_ROUTE
 * ------------------------------------------------------------------------- */

const char  *audio_route_repr(audio_route_t state);

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/* ------------------------------------------------------------------------- *
 * TKLOCK_REQUEST
 * ------------------------------------------------------------------------- */

const char  *tklock_request_repr(tklock_request_t state);

/* ------------------------------------------------------------------------- *
 * TKLOCK_STATUS
 * ------------------------------------------------------------------------- */

const char  *tklock_status_repr(int status);

/* ------------------------------------------------------------------------- *
 * BATTERY_STATUS
 * ------------------------------------------------------------------------- */

const char  *battery_status_repr   (battery_status_t state);
const char  *battery_status_to_dbus(battery_status_t state);

253 254 255 256 257 258 259
/* ------------------------------------------------------------------------- *
 * BATTERY_STATE
 * ------------------------------------------------------------------------- */

const char  *battery_state_repr   (battery_state_t state);
const char  *battery_state_to_dbus(battery_state_t state);

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
/* ------------------------------------------------------------------------- *
 * ALARM_STATE
 * ------------------------------------------------------------------------- */

const char  *alarm_state_repr(alarm_ui_state_t state);

/* ------------------------------------------------------------------------- *
 * CALL_STATE
 * ------------------------------------------------------------------------- */

const char    *call_state_to_dbus  (call_state_t state);
call_state_t   call_state_from_dbus(const char *name);
const char    *call_state_repr     (call_state_t state);

/* ------------------------------------------------------------------------- *
 * CALL_TYPE
 * ------------------------------------------------------------------------- */

const char   *call_type_repr (call_type_t type);
call_type_t   call_type_parse(const char *name);

/* ------------------------------------------------------------------------- *
 * COVER_STATE
 * ------------------------------------------------------------------------- */

const char  *cover_state_repr(cover_state_t state);

/* ------------------------------------------------------------------------- *
 * PROXIMITY_STATE
 * ------------------------------------------------------------------------- */

const char  *proximity_state_repr(cover_state_t state);

/* ------------------------------------------------------------------------- *
 * DISPLAY_STATE
 * ------------------------------------------------------------------------- */

const char  *display_state_repr(display_state_t state);

/* ------------------------------------------------------------------------- *
 * ORIENTATION_STATE
 * ------------------------------------------------------------------------- */

const char  *orientation_state_repr(orientation_state_t state);

/* ------------------------------------------------------------------------- *
 * KEY_STATE
 * ------------------------------------------------------------------------- */

const char  *key_state_repr(key_state_t state);

/* ------------------------------------------------------------------------- *
 * TRISTATE
 * ------------------------------------------------------------------------- */

const char  *tristate_repr(tristate_t state);

/* ------------------------------------------------------------------------- *
 * FPSTATE
 * ------------------------------------------------------------------------- */

fpstate_t    fpstate_parse(const char *name);
const char  *fpstate_repr (fpstate_t state);

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
/* ========================================================================= *
 * DATAPIPE_HOOK
 * ========================================================================= */

/* For each datatype used in datapipes there exist:
 * - datapipe_hook_<type>_value
 * - datapipe_hook_<type>_changes
 *
 * These are used in expansion of #DATAPIPE_INIT() macro and must resolve
 * either to a valid callback function or null pointer.
 */

static const char *
datapipe_hook_int_value(gconstpointer data)
{
    static char buf[32];
    snprintf(buf, sizeof buf, "%d", GPOINTER_TO_INT(data));
    return buf;
}
#define datapipe_hook_int_change 0

static const char *
datapipe_hook_boolean_value(gconstpointer data)
{
    return data ? "true" : "false";
}
#define datapipe_hook_boolean_change 0

static const char *
datapipe_hook_string_value(gconstpointer data)
{
    return data;
}
#define datapipe_hook_string_change 0

static const char *
datapipe_hook_impulse_value(gconstpointer data)
{
    (void)data;
    return "impulse";
}
#define datapipe_hook_impulse_change 0

static const char *
datapipe_hook_input_event_value(gconstpointer data)
{
    static char buf[64];
    const struct input_event *ev = data;
    if( !ev )
        return "NULL event!";
    snprintf(buf, sizeof buf, "type: %s, code: %s, value: %d",
             evdev_get_event_type_name(ev->type),
             evdev_get_event_code_name(ev->type, ev->code),
             ev->value);
    return buf;
}
#define datapipe_hook_input_event_change 0

static const char *
datapipe_hook_input_event_ptr_value(gconstpointer data)
{
    const struct input_event *const*evp = data;
    if( !evp )
        return "NULL event ptr!";
    return datapipe_hook_input_event_value(*evp);
}
#define datapipe_hook_input_event_ptr_change 0

static const char *
datapipe_hook_display_state_value(gconstpointer data)
{
    display_state_t display_state = GPOINTER_TO_INT(data);
    return display_state_repr(display_state);
}
#define datapipe_hook_display_state_change 0

static const char *
datapipe_hook_uiexception_type_value(gconstpointer data)
{
    uiexception_type_t value = GPOINTER_TO_INT(data);
    return uiexception_type_repr(value);
}
#define datapipe_hook_uiexception_type_change 0

static const char *
datapipe_hook_lockkey_state_value(gconstpointer data)
{
    key_state_t key_state = GPOINTER_TO_INT(data);
    return key_state_repr(key_state);
}
#define datapipe_hook_lockkey_state_change 0

static const char *
datapipe_hook_tristate_value(gconstpointer data)
{
    tristate_t value = GPOINTER_TO_INT(data);
    return tristate_repr(value);
}
#define datapipe_hook_tristate_change 0

static const char *
datapipe_hook_cover_state_value(gconstpointer data)
{
    cover_state_t value = GPOINTER_TO_INT(data);
    return cover_state_repr(value);
}
#define datapipe_hook_cover_state_change 0

static const char *
datapipe_hook_orientation_state_value(gconstpointer data)
{
    orientation_state_t value = GPOINTER_TO_INT(data);
    return orientation_state_repr(value);
}
#define datapipe_hook_orientation_state_change 0

static const char *
datapipe_hook_alarm_ui_state_value(gconstpointer data)
{
    alarm_ui_state_t value = GPOINTER_TO_INT(data);
    return alarm_state_repr(value);
}
#define datapipe_hook_alarm_ui_state_change 0

static const char *
datapipe_hook_system_state_value(gconstpointer data)
{
    system_state_t value = GPOINTER_TO_INT(data);
    return system_state_repr(value);
}
#define datapipe_hook_system_state_change 0

static const char *
datapipe_hook_submode_value(gconstpointer data)
{
    submode_t value = GPOINTER_TO_INT(data);
    return submode_repr(value);
}
static const char *
datapipe_hook_submode_change(gconstpointer prev, gconstpointer curr)
{
    submode_t v1 = GPOINTER_TO_INT(prev);
    submode_t v2 = GPOINTER_TO_INT(curr);
    return submode_change_repr(v1, v2);
}
static const char *
datapipe_hook_call_state_value(gconstpointer data)
{
    call_state_t value = GPOINTER_TO_INT(data);
    return call_state_repr(value);
}
#define datapipe_hook_call_state_change 0

static const char *
datapipe_hook_call_type_value(gconstpointer data)
{
    call_type_t value = GPOINTER_TO_INT(data);
    return call_type_repr(value);
}
#define datapipe_hook_call_type_change 0

static const char *
datapipe_hook_tklock_request_value(gconstpointer data)
{
    tklock_request_t value = GPOINTER_TO_INT(data);
    return tklock_request_repr(value);
}
#define datapipe_hook_tklock_request_change 0

493 494 495 496 497 498 499 500
static const char *
datapipe_hook_charger_type_value(gconstpointer data)
{
    charger_type_t value = GPOINTER_TO_INT(data);
    return charger_type_repr(value);
}
#define datapipe_hook_charger_type_change 0

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
static const char *
datapipe_hook_charger_state_value(gconstpointer data)
{
    charger_state_t value = GPOINTER_TO_INT(data);
    return charger_state_repr(value);
}
#define datapipe_hook_charger_state_change 0

static const char *
datapipe_hook_battery_status_value(gconstpointer data)
{
    battery_status_t value = GPOINTER_TO_INT(data);
    return battery_status_repr(value);
}
#define datapipe_hook_battery_status_change 0

517 518 519 520 521 522 523 524
static const char *
datapipe_hook_battery_state_value(gconstpointer data)
{
    battery_state_t value = GPOINTER_TO_INT(data);
    return battery_state_repr(value);
}
#define datapipe_hook_battery_state_change 0

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
static const char *
datapipe_hook_camera_button_state_value(gconstpointer data)
{
    camera_button_state_t value = GPOINTER_TO_INT(data);
    return camera_button_state_repr(value);
}
#define datapipe_hook_camera_button_state_change 0

static const char *
datapipe_hook_audio_route_value(gconstpointer data)
{
    audio_route_t value = GPOINTER_TO_INT(data);
    return audio_route_repr(value);
}
#define datapipe_hook_audio_route_change 0

static const char *
datapipe_hook_usb_cable_state_value(gconstpointer data)
{
    usb_cable_state_t value = GPOINTER_TO_INT(data);
    return usb_cable_state_repr(value);
}
#define datapipe_hook_usb_cable_state_change 0

static const char *
datapipe_hook_thermal_state_value(gconstpointer data)
{
    thermal_state_t value = GPOINTER_TO_INT(data);
    return thermal_state_repr(value);
}
#define datapipe_hook_thermal_state_change 0

static const char *
datapipe_hook_service_state_value(gconstpointer data)
{
    service_state_t value = GPOINTER_TO_INT(data);
    return service_state_repr(value);
}
#define datapipe_hook_service_state_change 0

static const char *
datapipe_hook_devicelock_state_value(gconstpointer data)
{
    devicelock_state_t value = GPOINTER_TO_INT(data);
    return devicelock_state_repr(value);
}
#define datapipe_hook_devicelock_state_change 0

static const char *
datapipe_hook_fpstate_value(gconstpointer data)
{
    fpstate_t value = GPOINTER_TO_INT(data);
    return fpstate_repr(value);
}
#define datapipe_hook_fpstate_change 0

581 582 583 584
/* ========================================================================= *
 * Data
 * ========================================================================= */

585 586 587
/* Available datapipes */

/** LED brightness */
588
datapipe_t led_brightness_pipe                  = DATAPIPE_INIT(led_brightness, int, 0, 0, DATAPIPE_FILTERING_ALLOWED, DATAPIPE_CACHE_INDATA);
589 590

/** LPM brightness */
591
datapipe_t lpm_brightness_pipe                  = DATAPIPE_INIT(lpm_brightness, int, 0, 0, DATAPIPE_FILTERING_ALLOWED, DATAPIPE_CACHE_INDATA);
592 593

/** State of device; read only */
594
datapipe_t device_inactive_pipe                 = DATAPIPE_INIT(device_inactive, boolean, true, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
595 596

/** Device inactivity events; read only */
597
datapipe_t inactivity_event_pipe                = DATAPIPE_INIT(inactivity_event, boolean, true, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
598 599

/** LED pattern to activate; read only */
600
datapipe_t led_pattern_activate_pipe            = DATAPIPE_INIT(led_pattern_activate, string, 0, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_NOTHING);
601 602

/** LED pattern to deactivate; read only */
603
datapipe_t led_pattern_deactivate_pipe          = DATAPIPE_INIT(led_pattern_deactivate, string, 0, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_NOTHING);
604

605
/** resumed from suspend notification; read only */
606
datapipe_t resume_detected_event_pipe           = DATAPIPE_INIT(resume_detected_event, impulse, 0, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_NOTHING);
607

608
/** Non-synthetized user activity; read only */
609
datapipe_t user_activity_event_pipe             = DATAPIPE_INIT(user_activity_event, input_event, 0, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_NOTHING);
610 611

/** State of display; read only */
612
datapipe_t display_state_curr_pipe              = DATAPIPE_INIT(display_state_curr, display_state, MCE_DISPLAY_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
613 614

/** Desired state of display; write only */
615
datapipe_t display_state_request_pipe           = DATAPIPE_INIT(display_state_request, display_state, MCE_DISPLAY_UNDEF, 0, DATAPIPE_FILTERING_ALLOWED, DATAPIPE_CACHE_DEFAULT);
616 617

/** Next (non-transitional) state of display; read only */
618
datapipe_t display_state_next_pipe              = DATAPIPE_INIT(display_state_next, display_state, MCE_DISPLAY_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
619 620

/** exceptional ui state; read write */
621
datapipe_t uiexception_type_pipe                = DATAPIPE_INIT(uiexception_type, uiexception_type, UIEXCEPTION_TYPE_NONE, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
622 623 624 625 626 627

/**
 * Display brightness;
 * bits 0-7 is brightness in percent (0-100)
 * upper 8 bits is high brightness boost (0-2)
 */
628
datapipe_t display_brightness_pipe              = DATAPIPE_INIT(display_brightness, int, 3, 0, DATAPIPE_FILTERING_ALLOWED, DATAPIPE_CACHE_INDATA);
629 630

/** Key backlight */
631
datapipe_t key_backlight_brightness_pipe        = DATAPIPE_INIT(key_backlight_brightness, int, 0, 0, DATAPIPE_FILTERING_ALLOWED, DATAPIPE_CACHE_INDATA);
632 633

/** A key has been pressed */
634
datapipe_t keypress_event_pipe                  = DATAPIPE_INIT(keypress_event, input_event_ptr, 0, sizeof (struct input_event), DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_NOTHING);
635 636

/** Touchscreen activity took place */
637
datapipe_t touchscreen_event_pipe               = DATAPIPE_INIT(touchscreen_event, input_event_ptr, 0, sizeof (struct input_event), DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_NOTHING);
638 639

/** The lock-key has been pressed; read only */
640
datapipe_t lockkey_state_pipe                   = DATAPIPE_INIT(lockkey_state, lockkey_state, KEY_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
641

642
/** The init-done condition has been reached; read only */
643
datapipe_t init_done_pipe                       = DATAPIPE_INIT(init_done, tristate, TRISTATE_UNKNOWN, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
644

645
/** Keyboard open/closed; read only */
646
datapipe_t keyboard_slide_state_pipe            = DATAPIPE_INIT(keyboard_slide_state, cover_state, COVER_CLOSED, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
647

648
/** Keyboard available; read only */
649
datapipe_t keyboard_available_state_pipe        = DATAPIPE_INIT(keyboard_available_state, cover_state, COVER_CLOSED, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
650

651
/** Lid sensor is working state; read/write */
652
datapipe_t lid_sensor_is_working_pipe           = DATAPIPE_INIT(lid_sensor_is_working, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
653

654
/** Lid cover sensor open/closed; read only */
655
datapipe_t lid_sensor_actual_pipe               = DATAPIPE_INIT(lid_sensor_actual, cover_state, COVER_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
656 657

/** Lid cover policy state; read only */
658
datapipe_t lid_sensor_filtered_pipe             = DATAPIPE_INIT(lid_sensor_filtered, cover_state, COVER_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
659 660

/** Lens cover open/closed; read only */
661
datapipe_t lens_cover_state_pipe                = DATAPIPE_INIT(lens_cover_state, cover_state, COVER_CLOSED, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
662 663

/** Proximity sensor; read only */
664
datapipe_t proximity_sensor_actual_pipe         = DATAPIPE_INIT(proximity_sensor_actual, cover_state, COVER_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
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
/** Proximity sensor; read only */
datapipe_t proximity_sensor_effective_pipe      = DATAPIPE_INIT(proximity_sensor_effective, cover_state, COVER_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);

/** Proximity sensor on-demand control; write only
 *
 * The data fed into proximity_sensor_required_pipe needs to
 * be a statically allocated const string with format like:
 *
 *   "<PREFIX><NAME>"
 *
 * Where <PREFIX> is one of:
 *
 * - PROXIMITY_SENSOR_REQUIRED_ADD: <NAME> is added to the
 *   list of resons to keep on-demand proximity sensor mode
 *   active.
 *
 * - PROXIMITY_SENSOR_REQUIRED_REM: <NAME> is from the the
 *   list of resons to keep on-demand proximity sensor mode
 *   active.
 */
datapipe_t proximity_sensor_required_pipe       = DATAPIPE_INIT(proximity_sensor_required, string, 0, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_NOTHING);

/** proximity blanking; read only */
datapipe_t proximity_blanked_pipe               = DATAPIPE_INIT(proximity_blanked, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);

691
/** Ambient light sensor; read only */
692
datapipe_t light_sensor_actual_pipe             = DATAPIPE_INIT(light_sensor_actual, int, 400, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
693

694
/** Filtered ambient light level; read only */
695
datapipe_t light_sensor_filtered_pipe           = DATAPIPE_INIT(light_sensor_filtered, int, 400, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
696

697
/** Temporary ambient light sensor enable; read/write */
698
datapipe_t light_sensor_poll_request_pipe       = DATAPIPE_INIT(light_sensor_poll_request, boolean, false, 0, DATAPIPE_FILTERING_ALLOWED, DATAPIPE_CACHE_DEFAULT);
699

700
/** Orientation sensor; read only */
701
datapipe_t orientation_sensor_actual_pipe       = DATAPIPE_INIT(orientation_sensor_actual, orientation_state, MCE_ORIENTATION_UNDEFINED, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
702 703

/** The alarm UI state */
704
datapipe_t alarm_ui_state_pipe                  = DATAPIPE_INIT(alarm_ui_state, alarm_ui_state, MCE_ALARM_UI_INVALID_INT32, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
705

706
/** The device state; read only */
707
datapipe_t system_state_pipe                    = DATAPIPE_INIT(system_state, system_state, MCE_SYSTEM_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
708 709

/** Enable/disable radios */
710
datapipe_t master_radio_enabled_pipe            = DATAPIPE_INIT(master_radio_enabled, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
711 712

/** The device submode */
713
datapipe_t submode_pipe                         = DATAPIPE_INIT(submode, submode, MCE_SUBMODE_NORMAL, 0, DATAPIPE_FILTERING_ALLOWED, DATAPIPE_CACHE_DEFAULT);
714 715

/** The call state */
716
datapipe_t call_state_pipe                      = DATAPIPE_INIT(call_state, call_state, CALL_STATE_NONE, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
717

718 719 720
/** The ignore incoming call "state".
 *
 * Note: Related actions happen when true is executed on the
721
 * datapipe, but the cached value always remains at false.
722
 */
723
datapipe_t ignore_incoming_call_event_pipe      = DATAPIPE_INIT(ignore_incoming_call_event, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_NOTHING);
724

725
/** The call type */
726
datapipe_t call_type_pipe                       = DATAPIPE_INIT(call_type, call_type, CALL_TYPE_NORMAL, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
727 728

/** The touchscreen/keypad lock state */
729
datapipe_t tklock_request_pipe                  = DATAPIPE_INIT(tklock_request, tklock_request, TKLOCK_REQUEST_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
730

731
/** UI side is in a state where user interaction is expected */
732
datapipe_t interaction_expected_pipe            = DATAPIPE_INIT(interaction_expected, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
733

734 735 736
/** Charger type; read only */
datapipe_t charger_type_pipe                    = DATAPIPE_INIT(charger_type, charger_type, CHARGER_TYPE_NONE, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);

737
/** Charger state; read only */
738
datapipe_t charger_state_pipe                   = DATAPIPE_INIT(charger_state, charger_state, CHARGER_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
739 740

/** Battery status; read only */
741
datapipe_t battery_status_pipe                  = DATAPIPE_INIT(battery_status, battery_status, BATTERY_STATUS_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
742

743 744 745
/** Battery state; read only */
datapipe_t battery_state_pipe                   = DATAPIPE_INIT(battery_state, battery_state, BATTERY_STATE_UNKNOWN, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);

746
/** Battery charge level; read only */
747
datapipe_t battery_level_pipe                   = DATAPIPE_INIT(battery_level, int, MCE_BATTERY_LEVEL_UNKNOWN, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
748

749
/** Topmost window PID; read only */
750
datapipe_t topmost_window_pid_pipe              = DATAPIPE_INIT(topmost_window_pid, int, -1, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
751

752
/** Camera button; read only */
753
datapipe_t camera_button_state_pipe             = DATAPIPE_INIT(camera_button_state, camera_button_state, CAMERA_BUTTON_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
754 755

/** The inactivity timeout; read only */
756
datapipe_t inactivity_delay_pipe                = DATAPIPE_INIT(inactivity_delay, int, DEFAULT_INACTIVITY_DELAY, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
757 758

/** Audio routing state; read only */
759
datapipe_t audio_route_pipe                     = DATAPIPE_INIT(audio_route, audio_route, AUDIO_ROUTE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
760 761

/** USB cable has been connected/disconnected; read only */
762
datapipe_t usb_cable_state_pipe                 = DATAPIPE_INIT(usb_cable_state, usb_cable_state, USB_CABLE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
763 764

/** A jack connector has been connected/disconnected; read only */
765
datapipe_t jack_sense_state_pipe                = DATAPIPE_INIT(jack_sense_state, cover_state, COVER_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
766 767

/** Power save mode is active; read only */
768
datapipe_t power_saving_mode_active_pipe        = DATAPIPE_INIT(power_saving_mode_active, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
769 770

/** Thermal state; read only */
771
datapipe_t thermal_state_pipe                   = DATAPIPE_INIT(thermal_state, thermal_state, THERMAL_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
772 773

/** Heartbeat; read only */
774
datapipe_t heartbeat_event_pipe                 = DATAPIPE_INIT(heartbeat_event, impulse, 0, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_NOTHING);
775

776
/** compositor availability; read only */
777
datapipe_t compositor_service_state_pipe        = DATAPIPE_INIT(compositor_service_state, service_state, SERVICE_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
778

779
/** lipstick availability; read only */
780
datapipe_t lipstick_service_state_pipe          = DATAPIPE_INIT(lipstick_service_state, service_state, SERVICE_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
781

782
/** devicelock availability; read only */
783
datapipe_t devicelock_service_state_pipe        = DATAPIPE_INIT(devicelock_service_state, service_state, SERVICE_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
784

785
/** usbmoded availability; read only */
786
datapipe_t usbmoded_service_state_pipe          = DATAPIPE_INIT(usbmoded_service_state, service_state, SERVICE_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
787

788
/** ngfd availability; read only */
789
datapipe_t ngfd_service_state_pipe              = DATAPIPE_INIT(ngfd_service_state, service_state, SERVICE_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
790

791
/** Request ngfd event playing */
792
datapipe_t ngfd_event_request_pipe              = DATAPIPE_INIT(ngfd_event_request, string, 0, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_NOTHING);
793

794
/** dsme availability; read only */
795
datapipe_t dsme_service_state_pipe              = DATAPIPE_INIT(dsme_service_state, service_state, SERVICE_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
796

797
/** thermalmanager availability; read only */
798
datapipe_t thermalmanager_service_state_pipe    = DATAPIPE_INIT(thermalmanager_service_state, service_state, SERVICE_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
799

800
/** bluez availability; read only */
801
datapipe_t bluez_service_state_pipe             = DATAPIPE_INIT(bluez_service_state, service_state, SERVICE_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
802

803
/** PackageKit Locked status; read only */
804
datapipe_t packagekit_locked_pipe               = DATAPIPE_INIT(packagekit_locked, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
805

806
/** Update mode active status; read only */
807
datapipe_t osupdate_running_pipe                = DATAPIPE_INIT(osupdate_running, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
808

809
/** Shutting down; read only */
810
datapipe_t shutting_down_pipe                   = DATAPIPE_INIT(shutting_down, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
811

812
/** Device Lock state; read only */
813
datapipe_t devicelock_state_pipe                = DATAPIPE_INIT(devicelock_state, devicelock_state, DEVICELOCK_STATE_UNDEFINED, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
814

815
/** touchscreen input detected; read only */
816
datapipe_t touch_detected_pipe                  = DATAPIPE_INIT(touch_detected, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
817

818
/** touchscreen input grab required; read/write */
819
datapipe_t touch_grab_wanted_pipe               = DATAPIPE_INIT(touch_grab_wanted, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
820 821

/** touchscreen input grab active; read only */
822
datapipe_t touch_grab_active_pipe               = DATAPIPE_INIT(touch_grab_active, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
823 824

/** keypad input grab required; read/write */
825
datapipe_t keypad_grab_wanted_pipe              = DATAPIPE_INIT(keypad_grab_wanted, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
826 827

/** keypad input grab active; read only */
828
datapipe_t keypad_grab_active_pipe              = DATAPIPE_INIT(keypad_grab_active, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
829 830

/** music playback active; read only */
831
datapipe_t music_playback_ongoing_pipe          = DATAPIPE_INIT(music_playback_ongoing, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
832

833
/** fingerprint daemon availability; read only */
834
datapipe_t fpd_service_state_pipe               = DATAPIPE_INIT(fpd_service_state, service_state, SERVICE_STATE_UNDEF, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
835 836

/** fingerprint daemon state; read only */
837
datapipe_t fpstate_pipe                         = DATAPIPE_INIT(fpstate, fpstate, FPSTATE_UNSET, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
838 839

/** fingerprint is enrolling; read only */
840
datapipe_t enroll_in_progress_pipe              = DATAPIPE_INIT(enroll_in_progress, boolean, false, 0, DATAPIPE_FILTERING_DENIED, DATAPIPE_CACHE_DEFAULT);
841 842 843 844

/* ========================================================================= *
 * Functions
 * ========================================================================= */
845

846 847
/** Get name of datapipe
 *
848
 * @param self The datapipe, or NULL
849 850 851 852
 *
 * @return datapipe name, or suitable placeholder value
 */
const char *
853
datapipe_name(const datapipe_t *self)
854 855
{
    const char *name = "invalid";
856 857
    if( self )
        name = self->dp_name ?: "unnamed";
858 859 860
    return name;
}

861 862
/** Get value of datapipe
 *
863
 * @param self The datapipe
864 865 866
 *
 * @return datapipe value, as void pointer
 */
867
gconstpointer
868
datapipe_value(const datapipe_t *self)
869
{
870
    return self->dp_cached_data;
871 872
}

873 874
/** Set value of datapipe
 *
875 876 877 878
 * Note: Do not call this function from outside datapipe.c.
 *
 * While such calls exist to facilitate legacy hacks in the
 * display plugin - no new code like that should be added.
879
 *
880 881
 * FIXME: Remove offending call sites and make this function
 *        static.
882 883 884 885 886 887 888 889 890 891
 *
 * The proper way to modify datapipe content is to use
 * #datapipe_exec_full() function.
 *
 * @param self The datapipe
 * @paran data The value, as void pointer
 */
void
datapipe_set_value(datapipe_t *self, gconstpointer data)
{
892
    gconstpointer prev = self->dp_cached_data;
893
    self->dp_cached_data = data;
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914

    if( prev != data ) {
        if( self->dp_change_repr_cb ) {
            datapipe_log(self, "%s: %s",
                         datapipe_name(self),
                         self->dp_change_repr_cb(prev, data));
        }
        else if( self->dp_value_repr_cb ) {
            gchar *saved = g_strdup(self->dp_value_repr_cb(prev));
            datapipe_log(self, "%s: %s -> %s",
                         datapipe_name(self),
                         saved,
                         self->dp_value_repr_cb(data));
            g_free(saved);
        }
        else {
            datapipe_log(self, "%s: %p -> %p",
                         datapipe_name(self),
                         prev, data);
        }
    }
915 916
}

917
/** Garbage collect stale callback slots
918
 *
919 920 921 922 923 924
 * While it can be assumed to be extremely rare, filters and triggers can end
 * up being added/removed due to datapipe activity. In order to allow O(N)
 * traversal in datapipe_exec_full() slots with removed callbacks are
 * zeroed and left behind to be collected from idle callback.
 *
 * @param self The datapipe
925
 */
926
static void
927
datapipe_gc(datapipe_t *self)
928
{
929 930
    /* Cancel pending idle callback */
    datapipe_cancel_gc(self);
931

932 933 934 935
    /* Flush out slots with null callback */
    self->dp_input_triggers  = g_slist_remove_all(self->dp_input_triggers, 0);
    self->dp_filters         = g_slist_remove_all(self->dp_filters, 0);
    self->dp_output_triggers = g_slist_remove_all(self->dp_output_triggers, 0);
936 937
}

938
/** Garbage collect idle callback
939
 *
940 941 942
 * @param self The datapipe, as void pointer
 *
 * @return G_SOURCE_REMOVE
943
 */
944 945
static gboolean
datapipe_gc_cb(gpointer aptr)
946
{
947 948 949 950
    datapipe_t *self = aptr;
    self->dp_gc_id = 0;
    datapipe_gc(self);
    return G_SOURCE_REMOVE;
951 952
}

953
/** Schedule garbage collection
954
 *
955
 * @param self The datapipe, as void pointer
956
 */
957
static void
958
datapipe_schedule_gc(datapipe_t *self)
959
{
960 961
    if( !self->dp_gc_id ) {
        self->dp_gc_id = g_idle_add(datapipe_gc_cb, self);
962
    }
963
}
964

965 966 967 968 969 970 971 972 973 974
/** Cancel garbage collection
 *
 * @param self The datapipe, as void pointer
 */
static void
datapipe_cancel_gc(datapipe_t *self)
{
    if( self->dp_gc_id ) {
        g_source_remove(self->dp_gc_id),
            self->dp_gc_id = 0;
975
    }
976 977
}

978
/** Execute the datapipe
979 980 981
 *
 * Note: Use #datapipe_exec_full() macro instead of calling
 *       this function directly.
982
 *
983
 * @param self The datapipe to execute
984 985 986
 * @param indata The input data to run through the datapipe
 * @return The processed data
 */
987
gconstpointer
988 989
datapipe_exec_full_real(datapipe_t *self, gconstpointer indata,
                        const char *file, const char *func)
990
{
991 992
    gconstpointer outdata = NULL;

993
    if (self == NULL) {
994
        mce_log(LL_ERR,
995
                "datapipe_exec_full_real() called "
996 997 998 999
                "without a valid datapipe");
        goto EXIT;
    }

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
    if( mce_log_p(LL_DEBUG) ||
        mce_log_p_(LL_DEBUG, file, func) ||
        mce_log_p_(LL_DEBUG, __FILE__, datapipe_name(self)) ) {

        if( self->dp_value_repr_cb ) {
            mce_log_unconditional(LL_DEBUG, file, func, "%s: execute %s",
                         datapipe_name(self),
                         self->dp_value_repr_cb(indata));
        }
        else {
            mce_log_unconditional(LL_DEBUG, file, func, "%s: execute %p",
                                  datapipe_name(self), indata);
        }
    }

1015 1016
    guint token = ++self->dp_token;

1017
    datapipe_cache_t cache_indata = self->dp_cache;
1018

1019
    /* Optionally cache the value at the input stage */
1020
    if( cache_indata & (DATAPIPE_CACHE_INDATA | DATAPIPE_CACHE_OUTDATA) ) {
1021
        datapipe_set_value(self, indata);
1022 1023 1024
    }

    /* Execute input value callbacks */
1025 1026
    for( GSList *item = self->dp_input_triggers; item; item = item->next ) {
        void (*trigger)(gconstpointer input) = item->data;
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
        if( !trigger )
            continue;

        if( self->dp_token != token ) {
            mce_log(LL_WARN, "%s: recursion detected at input triggers",
                    datapipe_name(self));
            goto EXIT;
        }

        trigger(indata);
1037
    }
1038 1039

    /* Determine output value */
1040 1041 1042 1043
    outdata = indata;
    if( self->dp_read_only == DATAPIPE_FILTERING_ALLOWED ) {
        for( GSList *item = self->dp_filters; item; item = item->next ) {
            gconstpointer (*filter)(gconstpointer input) = item->data;
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
            if( !filter )
                continue;

            if( self->dp_token != token ) {
                mce_log(LL_WARN, "%s: recursion detected at input filters",
                        datapipe_name(self));
                goto EXIT;
            }

            outdata = filter(outdata);
1054
        }
1055 1056 1057
    }
    /* Optionally cache the value at the output stage */
    if( cache_indata & DATAPIPE_CACHE_OUTDATA ) {
1058
        datapipe_set_value(self, outdata);
1059 1060 1061
    }

    /* Execute output value callbacks */
1062 1063
    for( GSList *item = self->dp_output_triggers; item; item = item->next ) {
        void (*trigger)(gconstpointer input) = item->data;
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
        if( !trigger )
            continue;

        if( self->dp_token != token ) {
            mce_log(LL_WARN, "%s: recursion detected at output triggers",
                    datapipe_name(self));
            goto EXIT;
        }

        trigger(outdata);
1074
    }
1075 1076

EXIT:
1077
    return outdata;
1078 1079 1080 1081 1082
}

/**
 * Append a filter to an existing datapipe
 *
1083
 * @param self The datapipe to manipulate
1084 1085
 * @param filter The filter to add to the datapipe
 */
1086
static void
1087
datapipe_add_filter(datapipe_t *self,
1088
                    gpointer (*filter)(gpointer data))
1089
{
1090 1091
    if (self == NULL) {
        mce_log(LL_ERR, "called with NULL datapipe");
1092 1093 1094 1095
        goto EXIT;
    }

    if (filter == NULL) {
1096
        mce_log(LL_ERR, "called with NULL filter");
1097 1098 1099
        goto EXIT;
    }

1100 1101
    if (self->dp_read_only == DATAPIPE_FILTERING_DENIED) {
        mce_log(LL_ERR, "called on read only datapipe");
1102 1103 1104
        goto EXIT;
    }

1105
    self->dp_filters = g_slist_append(self->dp_filters, filter);
1106 1107

EXIT:
1108
    return;
1109 1110 1111 1112 1113 1114
}

/**
 * Remove a filter from an existing datapipe
 * Non-existing filters are ignored
 *
1115
 * @param self The datapipe to manipulate
1116 1117
 * @param filter The filter to remove from the datapipe
 */
1118
static void
1119
datapipe_remove_filter(datapipe_t *self,
1120
                       gpointer (*filter)(gpointer data))
1121
{
1122 1123
    if (self == NULL) {
        mce_log(LL_ERR, "called with NULL datapipe");
1124 1125 1126 1127
        goto EXIT;
    }

    if (filter == NULL) {
1128
        mce_log(LL_ERR, "called with NULL filter");
1129 1130 1131
        goto EXIT;
    }

1132 1133
    if (self->dp_read_only == DATAPIPE_FILTERING_DENIED) {
        mce_log(LL_ERR, "called on read only datapipe");
1134 1135 1136
        goto EXIT;
    }

1137 1138 1139 1140 1141
    GSList *itm = g_slist_find(self->dp_filters, filter);
    if( !itm )
        mce_log(LL_DEBUG, "called with non-existing filter");
    else
        itm->data = 0, datapipe_schedule_gc(self);
1142 1143

EXIT:
1144
    return;
1145 1146 1147 1148 1149
}

/**
 * Append an input trigger to an existing datapipe
 *
1150
 * @param self The datapipe to manipulate
1151 1152
 * @param trigger The trigger to add to the datapipe
 */
1153
static void
1154
datapipe_add_input_trigger(datapipe_t *self,
1155
                           void (*trigger)(gconstpointer data))
1156
{
1157 1158
    if (self == NULL) {
        mce_log(LL_ERR, "called with NULL datapipe");
1159 1160 1161 1162
        goto EXIT;
    }

    if (trigger == NULL) {
1163
        mce_log(LL_ERR, "called with NULL trigger");
1164 1165 1166
        goto EXIT;
    }

1167 1168
    self->dp_input_triggers = g_slist_append(self->dp_input_triggers,
                                             trigger);
1169 1170

EXIT:
1171
    return;
1172 1173 1174 1175 1176 1177
}

/**
 * Remove an input trigger from an existing datapipe
 * Non-existing triggers are ignored
 *
1178
 * @param self The datapipe to manipulate
1179 1180
 * @param trigger The trigger to remove from the datapipe
 */
1181
static void
1182
datapipe_remove_input_trigger(datapipe_t *self,
1183
                              void (*trigger)(gconstpointer data))
1184
{
1185 1186
    if (self == NULL) {
        mce_log(LL_ERR, "called with NULL datapipe");
1187 1188 1189 1190
        goto EXIT;
    }

    if (trigger == NULL) {
1191
        mce_log(LL_ERR, "called with NULL trigger");
1192 1193 1194
        goto EXIT;
    }

1195 1196 1197 1198 1199
    GSList *itm = g_slist_find(self->dp_input_triggers, trigger);
    if( !itm )
        mce_log(LL_DEBUG, "called with non-existing trigger");
    else
        itm->data = 0, datapipe_schedule_gc(self);
1200 1201

EXIT:
1202
    return;
1203 1204 1205 1206 1207
}

/**
 * Append an output trigger to an existing datapipe
 *
1208
 * @param self The datapipe to manipulate
1209 1210
 * @param trigger The trigger to add to the datapipe
 */
1211
static void
1212
datapipe_add_output_trigger(datapipe_t *self,
1213
                            void (*trigger)(gconstpointer data))
1214
{
1215 1216
    if (self == NULL) {
        mce_log(LL_ERR, "called with NULL datapipe");
1217 1218 1219 1220
        goto EXIT;
    }

    if (trigger == NULL) {
1221
        mce_log(LL_ERR, "called with NULL trigger");
1222 1223 1224
        goto EXIT;
    }

1225 1226
    self->dp_output_triggers = g_slist_append(self->dp_output_triggers,
                                              trigger);
1227 1228

EXIT:
1229
    return;
1230 1231 1232 1233 1234 1235
}

/**
 * Remove an output trigger from an existing datapipe
 * Non-existing triggers are ignored
 *
1236
 * @param self The datapipe to manipulate
1237 1238
 * @param trigger The trigger to remove from the datapipe
 */
1239
static void
1240
datapipe_remove_output_trigger(datapipe_t *self,
1241
                               void (*trigger)(gconstpointer data))
1242
{
1243 1244
    if (self == NULL) {
        mce_log(LL_ERR, "called with NULL datapipe");
1245 1246 1247 1248
        goto EXIT;
    }

    if (trigger == NULL) {
1249
        mce_log(LL_ERR, "called with NULL trigger");
1250 1251
        goto EXIT;
    }
1252

1253 1254 1255 1256 1257
    GSList *itm = g_slist_find(self->dp_output_triggers, trigger);
    if( !itm )
        mce_log(LL_DEBUG, "called with non-existing trigger");
    else
        itm->data = 0, datapipe_schedule_gc(self);
1258
EXIT:
1259
    return;
1260 1261 1262 1263 1264
}

/**
 * Deinitialize a datapipe
 *
1265
 * @param self The datapipe to manipulate
1266
 */
1267
static void
1268
datapipe_free(datapipe_t *self)
1269
{
1270
    if (self == NULL) {
1271 1272 1273 1274 1275 1276
        mce_log(LL_ERR,
                "datapipe_free() called "
                "without a valid datapipe");
        goto EXIT;
    }

1277 1278
    datapipe_gc(self);

1279
    /* Warn about still registered filters/triggers */
1280
    if (self->dp_filters != NULL) {
1281 1282 1283 1284 1285
        mce_log(LL_INFO,
                "datapipe_free() called on a datapipe that "
                "still has registered filter(s)");
    }

1286
    if (self->dp_input_triggers != NULL) {
1287 1288 1289 1290 1291
        mce_log(LL_INFO,
                "datapipe_free() called on a datapipe that "
                "still has registered input_trigger(s)");
    }

1292
    if (self->dp_output_triggers != NULL) {
1293 1294 1295 1296 1297
        mce_log(LL_INFO,
                "datapipe_free() called on a datapipe that "
                "still has registered output_trigger(s)");
    }

1298
EXIT:
1299
    return;
1300
}
1301 1302 1303 1304 1305

/** Setup all datapipes
 */
void mce_datapipe_init(void)
{
1306 1307
    /* Currently all datapipes are initialized statically
     * and this function exists for the sake of symmetry.
1308
     */
1309 1310 1311 1312 1313 1314
}

/** Free all datapipes
 */
void mce_datapipe_quit(void)
{
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
    datapipe_free(&thermal_state_pipe);
    datapipe_free(&power_saving_mode_active_pipe);
    datapipe_free(&jack_sense_state_pipe);
    datapipe_free(&usb_cable_state_pipe);
    datapipe_free(&audio_route_pipe);
    datapipe_free(&inactivity_delay_pipe);
    datapipe_free(&battery_level_pipe);
    datapipe_free(&topmost_window_pid_pipe);
    datapipe_free(&camera_button_state_pipe);
    datapipe_free(&battery_status_pipe);
1325
    datapipe_free(&battery_state_pipe);
1326
    datapipe_free(&charger_type_pipe);
1327 1328 1329 1330
    datapipe_free(&charger_state_pipe);
    datapipe_free(&interaction_expected_pipe);
    datapipe_free(&tklock_request_pipe);
    datapipe_free(&proximity_sensor_actual_pipe);
1331 1332 1333
    datapipe_free(&proximity_sensor_effective_pipe);
    datapipe_free(&proximity_sensor_required_pipe);
    datapipe_free(&proximity_blanked_pipe);
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
    datapipe_free(&light_sensor_actual_pipe);
    datapipe_free(&light_sensor_filtered_pipe);
    datapipe_free(&light_sensor_poll_request_pipe);
    datapipe_free(&orientation_sensor_actual_pipe);
    datapipe_free(&lens_cover_state_pipe);
    datapipe_free(&lid_sensor_is_working_pipe);
    datapipe_free(&lid_sensor_actual_pipe);
    datapipe_free(&lid_sensor_filtered_pipe);
    datapipe_free(&keyboard_slide_state_pipe);
    datapipe_free(&keyboard_available_state_pipe);
    datapipe_free(&lockkey_state_pipe);
    datapipe_free(&init_done_pipe);
    datapipe_free(&device_inactive_pipe);
    datapipe_free(&inactivity_event_pipe);
    datapipe_free(&touchscreen_event_pipe);
    datapipe_free(&keypress_event_pipe);
    datapipe_free(&key_backlight_brightness_pipe);
    datapipe_free(&user_activity_event_pipe);
    datapipe_free(&led_pattern_deactivate_pipe);
    datapipe_free(&led_pattern_activate_pipe);
    datapipe_free(&resume_detected_event_pipe);
    datapipe_free(&led_brightness_pipe);
    datapipe_free(&lpm_brightness_pipe);
    datapipe_free(&display_brightness_pipe);
    datapipe_free(&display_state_curr_pipe);
    datapipe_free(&display_state_request_pipe);
    datapipe_free(&display_state_next_pipe);
    datapipe_free(&uiexception_type_pipe);
    datapipe_free(&submode_pipe);
    datapipe_free(&alarm_ui_state_pipe);
    datapipe_free(&call_type_pipe);
    datapipe_free(&call_state_pipe);
    datapipe_free(&ignore_incoming_call_event_pipe);
    datapipe_free(&master_radio_enabled_pipe);
    datapipe_free(&system_state_pipe);
    datapipe_free(&heartbeat_event_pipe);
    datapipe_free(&compositor_service_state_pipe);
    datapipe_free(&lipstick_service_state_pipe);
    datapipe_free(&devicelock_service_state_pipe);
    datapipe_free(&usbmoded_service_state_pipe);
    datapipe_free(&ngfd_service_state_pipe);
    datapipe_free(&ngfd_event_request_pipe);
    datapipe_free(&dsme_service_state_pipe);
1377
    datapipe_free(&thermalmanager_service_state_pipe);
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
    datapipe_free(&bluez_service_state_pipe);
    datapipe_free(&packagekit_locked_pipe);
    datapipe_free(&osupdate_running_pipe);
    datapipe_free(&shutting_down_pipe);
    datapipe_free(&devicelock_state_pipe);
    datapipe_free(&touch_grab_active_pipe);
    datapipe_free(&touch_grab_wanted_pipe);
    datapipe_free(&touch_detected_pipe);
    datapipe_free(&keypad_grab_active_pipe);
    datapipe_free(&keypad_grab_wanted_pipe);
    datapipe_free(&music_playback_ongoing_pipe);
    datapipe_free(&fpd_service_state_pipe);
    datapipe_free(&fpstate_pipe);
    datapipe_free(&enroll_in_progress_pipe);
1392
}
1393

1394 1395 1396 1397 1398 1399 1400 1401 1402
/** Convert submode_t bitmap changes to human readable string
 *
 * @param prev  submode_t bitmap changed from
 * @param curr  submode_t bitmap changed to
 *
 * @return human readable representation of submode
 */
const char *submode_change_repr(submode_t prev, submode_t curr)
{
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
    static const struct {
        submode_t bit; const char *name;
    } lut[] = {
        { MCE_SUBMODE_INVALID,          "INVALID"          },
        { MCE_SUBMODE_TKLOCK,           "TKLOCK"           },
        { MCE_SUBMODE_EVEATER,          "EVEATER"          },
        { MCE_SUBMODE_BOOTUP,           "BOOTUP"           },
        { MCE_SUBMODE_TRANSITION,       "TRANSITION"       },
        { MCE_SUBMODE_AUTORELOCK,       "AUTORELOCK"       },
        { MCE_SUBMODE_VISUAL_TKLOCK,    "VISUAL_TKLOCK"    },
        { MCE_SUBMODE_POCKET,           "POCKET"           },
        { MCE_SUBMODE_PROXIMITY_TKLOCK, "PROXIMITY_TKLOCK" },
        { MCE_SUBMODE_MALF,             "MALF"             },
        { 0,0 }
    };

    static char buff[128];
    char *pos = buff;
    char *end = buff + sizeof buff - 1;

    auto inline void add(const char *str)
    {
        while( pos < end && *str )
            *pos++ = *str++;
    }

    for( int i = 0; lut[i].name; ++i ) {
        const char *tag = 0;

        if( curr & lut[i].bit ) {
            tag = (prev & lut[i].bit) ? "" : "+";
        }
        else if( prev & lut[i].bit ) {
            tag = "-";
        }

        if( tag ) {
            if( pos> buff ) add(" ");
            add(tag);
            add(lut[i].name);
        }
    }
    if( pos == buff ) {
        add("NONE");
    }
    *pos = 0;
    return buff;
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
}

/** Convert submode_t bitmap to human readable string
 *
 * @param submode submode_t bitmap
 *
 * @return human readable representation of submode
 */
const char *submode_repr(submode_t submode)
{
1460
    return submode_change_repr(submode, submode);
1461 1462
}

1463 1464 1465 1466 1467 1468 1469 1470
/** Convert system_state_t enum to human readable string
 *
 * @param state system_state_t enumeration value
 *
 * @return human readable representation of state
 */
const char *system_state_repr(system_state_t state)
{
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
    const char *res = "UNKNOWN";

    switch( state ) {
    case MCE_SYSTEM_STATE_UNDEF:    res = "UNDEF";    break;
    case MCE_SYSTEM_STATE_SHUTDOWN: res = "SHUTDOWN"; break;
    case MCE_SYSTEM_STATE_USER:     res = "USER";     break;
    case MCE_SYSTEM_STATE_ACTDEAD:  res = "ACTDEAD";  break;
    case MCE_SYSTEM_STATE_REBOOT:   res = "REBOOT";   break;
    case MCE_SYSTEM_STATE_BOOT:     res = "BOOT";     break;
    default: break;
    }

    return res;
1484 1485
}

1486
/** Convert devicelock_state_t enum to human readable string
1487
 *
1488
 * @param state devicelock_state_t enumeration value
1489 1490 1491
 *
 * @return human readable representation of state
 */
1492
const char *devicelock_state_repr(devicelock_state_t state)
1493
{
1494
    const char *res = "unknown";
1495

1496 1497 1498 1499 1500 1501
    switch( state ) {
    case DEVICELOCK_STATE_UNLOCKED:  res = "unlocked";  break;
    case DEVICELOCK_STATE_LOCKED:    res = "locked";    break;
    case DEVICELOCK_STATE_UNDEFINED: res = "undefined"; break;
    default: break;
    }
1502

1503
    return res;
1504
}
1505

1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521