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
 
431 theseven 29
static struct button_hook_entry* head_button_hook IBSS_ATTR;
130 theseven 30
static struct mutex button_mutex;
31
 
32
 
33
void button_init()
34
{
431 theseven 35
    head_button_hook = NULL;
130 theseven 36
    mutex_init(&button_mutex);
37
}
38
 
39
int button_register_handler(void (*handler)(enum button_event, int which, int value))
40
{
431 theseven 41
    struct button_hook_entry* hook;
42
    hook = (struct button_hook_entry*)malloc(sizeof(struct button_hook_entry));
43
    hook->owner = current_thread;
44
    hook->handler = handler;
130 theseven 45
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
431 theseven 46
    hook->next = head_button_hook;
47
    head_button_hook = hook;
130 theseven 48
    mutex_unlock(&button_mutex);
431 theseven 49
    return 0;
130 theseven 50
}
51
 
52
int button_unregister_handler(void (*handler)(enum button_event, int which, int value))
53
{
431 theseven 54
    struct button_hook_entry* h;
55
    struct button_hook_entry* handle = NULL;
433 theseven 56
    int result = -1;
130 theseven 57
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
431 theseven 58
    if (head_button_hook && head_button_hook->handler == handler)
59
    {
60
        handle = head_button_hook;
61
        head_button_hook = head_button_hook->next;
433 theseven 62
        free(handle);
63
        result = 0;
431 theseven 64
    }
65
    else
66
    {
433 theseven 67
        for (h = head_button_hook; h && h->next; h = h->next);
68
            if (h->next->handler != handler)
69
            {
70
                handle = h->next;
71
                h->next = h->next->next;
72
                free(handle);
73
                result = 0;
74
            }
431 theseven 75
    }
130 theseven 76
    mutex_unlock(&button_mutex);
431 theseven 77
    if (handle) free(handle);
78
    return result;
130 theseven 79
}
80
 
81
void button_send_event(enum button_event eventtype, int which, int value)
82
{
198 theseven 83
    DEBUGF("Sending button event: %d, %02X, %02X", eventtype, which, value);
431 theseven 84
    struct button_hook_entry* h;
85
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
86
    for (h = head_button_hook; h; h = h->next)
87
        h->handler(eventtype, which, value);
88
    mutex_unlock(&button_mutex);
130 theseven 89
}
90
 
91
void button_unregister_all_of_thread(struct scheduler_thread* process)
92
{
431 theseven 93
    struct button_hook_entry* h;
94
    struct button_hook_entry* prev;
130 theseven 95
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
431 theseven 96
    while (head_button_hook && head_button_hook->owner == process)
97
    {
98
        prev = head_button_hook;
99
        head_button_hook = head_button_hook->next;
100
        free(prev);
101
    }
433 theseven 102
    for (h = head_button_hook; h; h = h->next)
431 theseven 103
    {
104
        while (h && h->owner == process)
130 theseven 105
        {
431 theseven 106
            prev->next = h->next;
107
            free(h);
130 theseven 108
        }
431 theseven 109
        prev = h;
110
    }
130 theseven 111
    mutex_unlock(&button_mutex);
112
}