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
//
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;
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;
69
            return 0;
70
        }
71
    mutex_unlock(&button_mutex);
72
    return -1;
73
}
74
 
75
void button_send_event(enum button_event eventtype, int which, int value)
76
{
77
    int i;
78
    for (i = 0; i < BUTTON_MAX_HOOKS; i++)
79
        if (button_hooks[i].owner != NULL)
132 theseven 80
            button_hooks[i].handler(eventtype, which, value);
130 theseven 81
}
82
 
83
void button_unregister_all_of_thread(struct scheduler_thread* process)
84
{
85
    int i;
86
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
87
    for (i = 0; i < BUTTON_MAX_HOOKS; i++)
88
        if (button_hooks[i].owner == process)
89
        {
90
            button_hooks[i].owner = NULL;
91
            button_hooks[i].handler = NULL;
92
        }
93
    mutex_unlock(&button_mutex);
94
}