]>
Commit | Line | Data |
---|---|---|
1 | /* ------------------------------------------------------------------------- | |
2 | * Project: Mac Notifier Support | |
3 | * Name: macnotfy.c | |
4 | * Author: Stefan CSomor | |
5 | * Purpose: Mac Notifier main file | |
6 | * CVSID: $Id$ | |
7 | * ------------------------------------------------------------------------- | |
8 | */ | |
9 | ||
10 | #include "wx/mac/macnotfy.h" | |
11 | ||
12 | const short kMaxEvents = 1000 ; | |
13 | ||
14 | struct wxMacNotificationEvents | |
15 | { | |
16 | short top ; | |
17 | short bottom ; | |
18 | ||
19 | wxMacNotificationProcPtr proc[kMaxEvents] ; | |
20 | unsigned long events[kMaxEvents] ; | |
21 | void* data[kMaxEvents] ; | |
22 | } ; | |
23 | ||
24 | typedef struct wxMacNotificationEvents wxMacNotificationEvents ; | |
25 | wxMacNotificationEvents gMacNotificationEvents ; | |
26 | ||
27 | ProcessSerialNumber gAppProcess ; | |
28 | ||
29 | void wxMacWakeUp() | |
30 | { | |
31 | ProcessSerialNumber psn ; | |
32 | Boolean isSame ; | |
33 | psn.highLongOfPSN = 0 ; | |
34 | psn.lowLongOfPSN = kCurrentProcess ; | |
35 | SameProcess( &gAppProcess , &psn , &isSame ) ; | |
36 | if ( isSame ) | |
37 | { | |
38 | PostEvent( nullEvent , 0 ) ; | |
39 | } | |
40 | else | |
41 | { | |
42 | WakeUpProcess( &gAppProcess ) ; | |
43 | } | |
44 | } | |
45 | ||
46 | void wxMacCreateNotifierTable() | |
47 | { | |
48 | GetCurrentProcess(&gAppProcess); | |
49 | gMacNotificationEvents.top = 0 ; | |
50 | gMacNotificationEvents.bottom = 0 ; | |
51 | for ( int i = 0 ; i < kMaxEvents ; ++i ) | |
52 | { | |
53 | gMacNotificationEvents.proc[i] = NULL ; | |
54 | gMacNotificationEvents.events[i] = NULL ; | |
55 | gMacNotificationEvents.data[i] = NULL ; | |
56 | } | |
57 | } | |
58 | ||
59 | void wxMacDestroyNotifierTable() | |
60 | { | |
61 | wxASSERT( gMacNotificationEvents.top == gMacNotificationEvents.bottom ) ; | |
62 | } | |
63 | ||
64 | wxMacNotifierTableRef wxMacGetNotifierTable() | |
65 | { | |
66 | return (wxMacNotifierTableRef) &gMacNotificationEvents ; | |
67 | } | |
68 | ||
69 | void wxMacAddEvent( | |
70 | wxMacNotifierTableRef table , | |
71 | wxMacNotificationProcPtr handler , | |
72 | unsigned long event , | |
73 | void* data , | |
74 | short wakeUp ) | |
75 | { | |
76 | wxMacNotificationEvents *e = (wxMacNotificationEvents *) table ; | |
77 | /* this should be protected eventually */ | |
78 | short index = e->top++ ; | |
79 | ||
80 | if ( e->top == kMaxEvents ) | |
81 | e->top = 0 ; | |
82 | ||
83 | e->proc[index] = handler ; | |
84 | e->events[index] = event ; | |
85 | e->data[index] = data ; | |
86 | if ( wakeUp ) | |
87 | wxMacWakeUp() ; | |
88 | } | |
89 | ||
90 | bool gInProcessing = false ; | |
91 | ||
92 | void wxMacRemoveAllNotifiersForData( wxMacNotifierTableRef table , void* data ) | |
93 | { | |
94 | wxMacNotificationEvents *e = (wxMacNotificationEvents *) table ; | |
95 | /* this should be protected eventually */ | |
96 | short index = e->bottom ; | |
97 | ||
98 | while ( e->top != index ) | |
99 | { | |
100 | if ( index == kMaxEvents ) | |
101 | index = 0 ; | |
102 | if ( e->data[index] == data ) | |
103 | e->data[index] = NULL ; | |
104 | index++ ; | |
105 | } | |
106 | } | |
107 | ||
108 | void wxMacProcessNotifierEvents() | |
109 | { | |
110 | // if ( gInProcessing ) | |
111 | // return ; | |
112 | ||
113 | gInProcessing = true ; | |
114 | while ( gMacNotificationEvents.top != gMacNotificationEvents.bottom ) | |
115 | { | |
116 | // consume event at bottom | |
117 | short index = gMacNotificationEvents.bottom++ ; | |
118 | if ( gMacNotificationEvents.bottom == kMaxEvents ) | |
119 | gMacNotificationEvents.bottom = 0 ; | |
120 | void* data = gMacNotificationEvents.data[index] ; | |
121 | unsigned long event = gMacNotificationEvents.events[index] ; | |
122 | wxMacNotificationProcPtr handler = gMacNotificationEvents.proc[index] ; | |
123 | ||
124 | gMacNotificationEvents.data[index] = NULL ; | |
125 | gMacNotificationEvents.events[index] = NULL ; | |
126 | gMacNotificationEvents.proc[index] = NULL ; | |
127 | ||
128 | handler( event , data ) ; | |
129 | } | |
130 | gInProcessing = false ; | |
131 | } | |
132 | ||
133 | void wxMacProcessNotifierAndPendingEvents() | |
134 | { | |
135 | wxMacProcessNotifierEvents() ; | |
136 | wxTheApp->ProcessPendingEvents() ; | |
137 | } |