1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - CARD - libraries
3   File:     card_event.h
4 
5   Copyright 2007-2008 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-11-21#$
14   $Rev: 9385 $
15   $Author: yosizaki $
16 
17  *---------------------------------------------------------------------------*/
18 #ifndef NITRO_LIBRARIES_CARD_EVENT_H__
19 #define NITRO_LIBRARIES_CARD_EVENT_H__
20 
21 
22 #include <nitro.h>
23 
24 
25 // These structures and functions are used to monitor software state changes without completion notifications.
26 // Currently, these are used only by the CARD_LockRom function but are split into a separate module.
27 
28 
29 #ifdef __cplusplus
30 extern  "C"
31 {
32 #endif
33 
34 
35 /*---------------------------------------------------------------------------*/
36 /* Declarations */
37 
38 
39 // This structure is used to monitor software state changes without completion notifications.
40 typedef struct CARDEventListener
41 {
42     OSEvent     event[1];
43     OSVAlarm    valarm[1];
44     BOOL(*Condition)(void*);
45     void       *userdata;
46     u16         lockID;
47     u8          padding[2];
48 }
49 CARDEventListener;
50 
51 
52 /*---------------------------------------------------------------------------*/
53 /* functions */
54 
55 /*---------------------------------------------------------------------------*
56   Name:         CARDi_PeekEventListener
57 
58   Description:  Determines event conditions.
59                 A V-count alarm is issued if the conditions are not met.
60 
61   Arguments:    arg: CARDEventListener structure
62 
63   Returns:      None.
64  *---------------------------------------------------------------------------*/
CARDi_PeekEventListener(void * arg)65 static void CARDi_PeekEventListener(void *arg)
66 {
67     CARDEventListener  *el = (CARDEventListener *)arg;
68     if (el->Condition(el->userdata))
69     {
70         OS_SignalEvent(el->event, 0x00000001UL);
71     }
72     else
73     {
74         OS_SetVAlarm(el->valarm, HW_LCD_HEIGHT, OS_VALARM_DELAY_MAX, CARDi_PeekEventListener, el);
75     }
76 }
77 
78 /*---------------------------------------------------------------------------*
79   Name:         CARDi_InitEventListener
80 
81   Description:  Initializes an event listener structure.
82 
83   Arguments:    el: CARDEventListener structure
84 
85   Returns:      None.
86  *---------------------------------------------------------------------------*/
CARDi_InitEventListener(CARDEventListener * el)87 SDK_INLINE void CARDi_InitEventListener(CARDEventListener *el)
88 {
89     OS_InitEvent(el->event);
90     OS_CreateVAlarm(el->valarm);
91 }
92 
93 /*---------------------------------------------------------------------------*
94   Name:         CARDi_SetEventListener
95 
96   Description:   Configures an event in an event listener structure.
97 
98   Arguments:    el: CARDEventListener structure
99                 condition: Event conditions
100                 userdata: Arbitrary user-defined arguments that can be specified as event conditions
101 
102   Returns:      None.
103  *---------------------------------------------------------------------------*/
CARDi_SetEventListener(CARDEventListener * el,BOOL (* condition)(void *),void * userdata)104 SDK_INLINE void CARDi_SetEventListener(CARDEventListener *el, BOOL(*condition)(void*), void *userdata)
105 {
106     el->Condition = condition;
107     el->userdata = userdata;
108     CARDi_PeekEventListener(el);
109 }
110 
111 /*---------------------------------------------------------------------------*
112   Name:         CARDi_WaitForEvent
113 
114   Description:   Waits for an event listener structure to complete.
115 
116   Arguments:    el: CARDEventListener structure
117 
118   Returns:      None.
119  *---------------------------------------------------------------------------*/
CARDi_WaitForEvent(CARDEventListener * el)120 SDK_INLINE void CARDi_WaitForEvent(CARDEventListener *el)
121 {
122     (void)OS_WaitEventEx(el->event, 0x00000001UL, OS_EVENT_MODE_AND, 0x00000001UL);
123 }
124 
125 
126 /*---------------------------------------------------------------------------*/
127 
128 #ifdef __cplusplus
129 } // extern "C"
130 #endif
131 
132 
133 #endif // NITRO_LIBRARIES_CARD_EVENT_H__
134