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
 
503 theseven 39
struct button_hook_entry* button_register_handler(void (*handler)(void*, enum button_event,
40
                                                                  int, int),
41
                                                  void* user)
130 theseven 42
{
431 theseven 43
    struct button_hook_entry* hook;
44
    hook = (struct button_hook_entry*)malloc(sizeof(struct button_hook_entry));
503 theseven 45
    if (!hook) return NULL;
431 theseven 46
    hook->owner = current_thread;
47
    hook->handler = handler;
503 theseven 48
    hook->user = user;
130 theseven 49
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
431 theseven 50
    hook->next = head_button_hook;
51
    head_button_hook = hook;
130 theseven 52
    mutex_unlock(&button_mutex);
503 theseven 53
    return hook;
130 theseven 54
}
55
 
503 theseven 56
int button_unregister_handler(struct button_hook_entry* hook)
130 theseven 57
{
431 theseven 58
    struct button_hook_entry* h;
433 theseven 59
    int result = -1;
130 theseven 60
    mutex_lock(&button_mutex, TIMEOUT_BLOCK);
503 theseven 61
    if (head_button_hook == hook)
431 theseven 62
    {
63
        head_button_hook = head_button_hook->next;
503 theseven 64
        free(hook);
433 theseven 65
        result = 0;
431 theseven 66
    }
67
    else
68
    {
433 theseven 69
        for (h = head_button_hook; h && h->next; h = h->next);
503 theseven 70
            if (h->next == hook)
433 theseven 71
            {
72
                h->next = h->next->next;
503 theseven 73
                free(hook);
433 theseven 74
                result = 0;
75
            }
431 theseven 76
    }
130 theseven 77
    mutex_unlock(&button_mutex);
431 theseven 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)
503 theseven 87
        h->handler(h->user, eventtype, which, value);
431 theseven 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)
460 theseven 103
        while (h->next && h->next->owner == process)
130 theseven 104
        {
460 theseven 105
            prev = h->next;
106
            h->next = h->next->next;
107
            free(prev);
130 theseven 108
        }
109
    mutex_unlock(&button_mutex);
110
}