Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
130 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
427 farthen 6
//    This file is part of emCORE.
130 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
130 theseven 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
//
427 farthen 13
//    emCORE is distributed in the hope that it will be useful,
130 theseven 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
427 farthen 19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
130 theseven 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
static struct button_hook_entry button_hooks[BUTTON_MAX_HOOKS] IBSS_ATTR;
35
static struct mutex button_mutex;
36
 
37
 
38
void button_init()
39
{
40
    memset(button_hooks, 0, sizeof(button_hooks));
41
    mutex_init(&button_mutex);
42
}
43
 
44
int button_register_handler(void (*handler)(enum button_event, int which, int value))
45
{
46
    int i;
47
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
48
    for (i = 0; i < BUTTON_MAX_HOOKS; i++)
49
        if (button_hooks[i].owner == NULL)
50
        {
51
            button_hooks[i].owner = current_thread;
52
            button_hooks[i].handler = handler;
169 theseven 53
            mutex_unlock(&button_mutex);
130 theseven 54
            return 0;
55
        }
56
    mutex_unlock(&button_mutex);
57
    return -1;
58
}
59
 
60
int button_unregister_handler(void (*handler)(enum button_event, int which, int value))
61
{
62
    int i;
63
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
64
    for (i = 0; i < BUTTON_MAX_HOOKS; i++)
65
        if (button_hooks[i].handler == handler)
66
        {
67
            button_hooks[i].owner = NULL;
68
            button_hooks[i].handler = NULL;
168 theseven 69
            mutex_unlock(&button_mutex);
130 theseven 70
            return 0;
71
        }
72
    mutex_unlock(&button_mutex);
73
    return -1;
74
}
75
 
76
void button_send_event(enum button_event eventtype, int which, int value)
77
{
198 theseven 78
    DEBUGF("Sending button event: %d, %02X, %02X", eventtype, which, value);
130 theseven 79
    int i;
80
    for (i = 0; i < BUTTON_MAX_HOOKS; i++)
81
        if (button_hooks[i].owner != NULL)
132 theseven 82
            button_hooks[i].handler(eventtype, which, value);
130 theseven 83
}
84
 
85
void button_unregister_all_of_thread(struct scheduler_thread* process)
86
{
87
    int i;
88
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
89
    for (i = 0; i < BUTTON_MAX_HOOKS; i++)
90
        if (button_hooks[i].owner == process)
91
        {
92
            button_hooks[i].owner = NULL;
93
            button_hooks[i].handler = NULL;
94
        }
95
    mutex_unlock(&button_mutex);
96
}