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