Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
130 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emBIOS is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "button.h"
26
#include "thread.h"
27
 
28
 
29
#ifndef BUTTON_MAX_HOOKS
30
#define BUTTON_MAX_HOOKS 16
31
#endif
32
 
33
 
34
extern struct scheduler_thread* current_thread;
35
static struct button_hook_entry button_hooks[BUTTON_MAX_HOOKS] IBSS_ATTR;
36
static struct mutex button_mutex;
37
 
38
 
39
void button_init()
40
{
41
    memset(button_hooks, 0, sizeof(button_hooks));
42
    mutex_init(&button_mutex);
43
}
44
 
45
int button_register_handler(void (*handler)(enum button_event, int which, int value))
46
{
47
    int i;
48
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
49
    for (i = 0; i < BUTTON_MAX_HOOKS; i++)
50
        if (button_hooks[i].owner == NULL)
51
        {
52
            button_hooks[i].owner = current_thread;
53
            button_hooks[i].handler = handler;
169 theseven 54
            mutex_unlock(&button_mutex);
130 theseven 55
            return 0;
56
        }
57
    mutex_unlock(&button_mutex);
58
    return -1;
59
}
60
 
61
int button_unregister_handler(void (*handler)(enum button_event, int which, int value))
62
{
63
    int i;
64
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
65
    for (i = 0; i < BUTTON_MAX_HOOKS; i++)
66
        if (button_hooks[i].handler == handler)
67
        {
68
            button_hooks[i].owner = NULL;
69
            button_hooks[i].handler = NULL;
168 theseven 70
            mutex_unlock(&button_mutex);
130 theseven 71
            return 0;
72
        }
73
    mutex_unlock(&button_mutex);
74
    return -1;
75
}
76
 
77
void button_send_event(enum button_event eventtype, int which, int value)
78
{
198 theseven 79
    DEBUGF("Sending button event: %d, %02X, %02X", eventtype, which, value);
130 theseven 80
    int i;
81
    for (i = 0; i < BUTTON_MAX_HOOKS; i++)
82
        if (button_hooks[i].owner != NULL)
132 theseven 83
            button_hooks[i].handler(eventtype, which, value);
130 theseven 84
}
85
 
86
void button_unregister_all_of_thread(struct scheduler_thread* process)
87
{
88
    int i;
89
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
90
    for (i = 0; i < BUTTON_MAX_HOOKS; i++)
91
        if (button_hooks[i].owner == process)
92
        {
93
            button_hooks[i].owner = NULL;
94
            button_hooks[i].handler = NULL;
95
        }
96
    mutex_unlock(&button_mutex);
97
}