aboutsummaryrefslogtreecommitdiff
path: root/src/drv/ir/control.c
blob: 6be2cf10f06b9135fb28416797688d67651ba1a3 (plain) (blame)
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
#include "drv/ir/control.h"

#include "drv/ir/ir.h"
#include "shared/map.h"

typedef struct {
  void* closure;
  void (*fn)(uint32_t code, void* closure);
} ir_code_listener_t;

#define integer_cmp_(a, b) (a - b)
MAP_DECL(uint32_t, ir_code_listener_t);
MAP_IMPL(uint32_t, ir_code_listener_t, integer_cmp_, null_dtor);

static map_t(uint32_t, ir_code_listener_t) * listeners;

static void ir_callback(const ir_code_t* ir)
{
  uint32_t code;
  if (ir_generic_decode(ir, &code)) {
    ir_code_listener_t* l =
        map_get(uint32_t, ir_code_listener_t)(listeners, code);

    if (l) {
      l->fn(code, l->closure);
    }
  }
}

void add_ir_code_callback_(
    uint32_t code, void (*fn)(uint32_t, void*), void* closure)
{
  ir_code_listener_t l;
  l.fn = fn;
  l.closure = closure;
  map_put(uint32_t, ir_code_listener_t)(listeners, code, l);
}

void enable_ir_control()
{
  listeners = map_new(uint32_t, ir_code_listener_t)();
  add_ir_callback(ir_callback);
}