OSDN Git Service

リセット
[momiji/momiji_main.git] / Core / Momiji.Core.Winmm.cpp
1 /*
2 [momiji music component library]
3 ---------------------------------------------------------------------
4 Momiji.Core.Winmm.cpp
5         
6 ---------------------------------------------------------------------
7 Copyright (C) 2011 tyiki badwell {miria@users.sourceforge.jp}.
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program.  If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
21 ---------------------------------------------------------------------
22 */
23 #include "StdAfx.h"
24
25 #include "Momiji.Interop.Winmm.h"
26 #include "Momiji.Core.Winmm.h"
27
28 namespace Momiji {
29 namespace Core {
30 namespace Winmm {
31
32         DriverCallBack::DriverCallBack(System::Boolean async):
33                 _async(async)
34         {
35                 #ifdef _DEBUG
36                         System::Console::WriteLine("[{0}]",__FUNCTION__);
37                 #endif
38
39                 this->_callBack = InteropServices::GCHandle::Alloc(gcnew Interop::Winmm::DriverCallBack::Delegate(this, &DriverCallBack::DriverCallBackProc));
40         }
41
42         DriverCallBack::~DriverCallBack()
43         {
44                 #ifdef _DEBUG
45                         System::Console::WriteLine("[{0}]",__FUNCTION__);
46                 #endif
47                 this->!DriverCallBack();
48         }
49
50         DriverCallBack::!DriverCallBack()
51         {
52                 #ifdef _DEBUG
53                         System::Console::WriteLine("[{0}]",__FUNCTION__);
54                 #endif
55                 if (this->_callBack.IsAllocated)
56                 {
57                         this->_callBack.Free();
58                 }
59         }
60
61         Interop::Winmm::DriverCallBack::Delegate^ DriverCallBack::GetDriverCallBackProc()
62         {
63                 return safe_cast<Interop::Winmm::DriverCallBack::Delegate^>(this->_callBack.Target);
64         }
65
66         void DriverCallBack::DriverCallBackProc(
67                 System::IntPtr  hdrvr,
68                 Interop::Winmm::DriverCallBack::MM_EXT_WINDOW_MESSAGE   uMsg,
69                 System::IntPtr  dwUser,
70                 System::IntPtr  dw1,
71                 System::IntPtr  dw2
72         )
73         {
74                 #ifdef _DEBUG
75                         System::Console::WriteLine("[{0}] [{1,8:X}][{2,8:X}][{3,8:X}][{4,8:X}][{5,8:X}]", __FUNCTION__, hdrvr, uMsg, dwUser, dw1, dw2);
76                 #endif
77                 try
78                 {
79                         auto params = gcnew Interop::Winmm::DriverCallBack::DriverEventArgs();
80                         params->hdrvr = hdrvr;
81                         params->uMsg = uMsg;
82                         params->dwUser = dwUser;
83                         params->dw1 = dw1;
84                         params->dw2 = dw2;
85
86                         if (this->_async)
87                         {
88                                 System::Threading::ThreadPool::QueueUserWorkItem(
89                                         gcnew System::Threading::WaitCallback(this, &DriverCallBack::DoEvent),
90                                         params
91                                 );
92                         }
93                         else
94                         {
95                                 this->DoEvent(params);
96                         }
97                 }
98                 catch(System::Exception^ e)
99                 {
100                         #ifdef _DEBUG
101                                 System::Console::WriteLine("[{0}] コールバック中のエラー[{1}]", __FUNCTION__, e->ToString());
102                         #endif
103                 }
104                 #ifdef _DEBUG
105                         System::Console::WriteLine("[{0}] OUT", __FUNCTION__);
106                 #endif
107         }
108
109         void DriverCallBack::DoEvent(
110                 System::Object^ stateInfo
111         )
112         {
113                 #ifdef _DEBUG
114                         System::Console::WriteLine("[{0}] [{1}] start", __FUNCTION__, System::Threading::Thread::CurrentThread->GetHashCode());
115                 #endif
116
117                 auto params = safe_cast<Interop::Winmm::DriverCallBack::DriverEventArgs^>(stateInfo);
118                 try
119                 {
120                         this->OnEvent(this, params);
121                 }
122                 catch(System::Exception^ e)
123                 {
124                         #ifdef _DEBUG
125                                 System::Console::WriteLine("[{0}] コールバック中のエラー[{1}]", __FUNCTION__, e->ToString());
126                         #endif
127                 }
128
129                 #ifdef _DEBUG
130                         System::Console::WriteLine("[{0}] [{1}] end", __FUNCTION__, System::Threading::Thread::CurrentThread->GetHashCode());
131                 #endif
132         }
133
134
135
136
137
138         TimerCallBack::TimerCallBack(System::Boolean async):
139                 _async(async)
140         {
141                 #ifdef _DEBUG
142                         System::Console::WriteLine("[{0}]",__FUNCTION__);
143                 #endif
144
145                 this->_callBack = InteropServices::GCHandle::Alloc(gcnew Interop::Winmm::TimerCallBack::Delegate(this, &TimerCallBack::TimerCallBackProc));
146         }
147
148         TimerCallBack::~TimerCallBack()
149         {
150                 #ifdef _DEBUG
151                         System::Console::WriteLine("[{0}]",__FUNCTION__);
152                 #endif
153                 this->!TimerCallBack();
154         }
155
156         TimerCallBack::!TimerCallBack()
157         {
158                 #ifdef _DEBUG
159                         System::Console::WriteLine("[{0}]",__FUNCTION__);
160                 #endif
161                 if (this->_callBack.IsAllocated)
162                 {
163                         this->_callBack.Free();
164                 }
165         }
166
167         Interop::Winmm::TimerCallBack::Delegate^ TimerCallBack::GetTimerCallBackProc()
168         {
169                 return safe_cast<Interop::Winmm::TimerCallBack::Delegate^>(this->_callBack.Target);
170         }
171
172         void TimerCallBack::TimerCallBackProc(
173                 System::UInt32  uTimerID,
174                 System::UInt32  uMsg,
175                 System::IntPtr  dwUser,
176                 System::IntPtr  dw1,
177                 System::IntPtr  dw2
178         )
179         {
180                 #ifdef _DEBUG
181                         System::Console::WriteLine("[{0}] [{1,8:X}][{2,8:X}][{3,8:X}][{4,8:X}][{5,8:X}]", __FUNCTION__, uTimerID, uMsg, dwUser, dw1, dw2);
182                 #endif
183                 try
184                 {
185                         auto params = gcnew Interop::Winmm::TimerCallBack::TimerEventArgs();
186                         params->uTimerID = uTimerID;
187                         params->uMsg = uMsg;
188                         params->dwUser = dwUser;
189                         params->dw1 = dw1;
190                         params->dw2 = dw2;
191
192                         if (this->_async)
193                         {
194                                 System::Threading::ThreadPool::QueueUserWorkItem(
195                                         gcnew System::Threading::WaitCallback(this, &TimerCallBack::DoEvent),
196                                         params
197                                 );
198                         }
199                         else
200                         {
201                                 this->DoEvent(params);
202                         }
203                 }
204                 catch(System::Exception^ e)
205                 {
206                         #ifdef _DEBUG
207                                 System::Console::WriteLine("[{0}] コールバック中のエラー[{1}]", __FUNCTION__, e->ToString());
208                         #endif
209                 }
210                 #ifdef _DEBUG
211                         System::Console::WriteLine("[{0}] OUT", __FUNCTION__);
212                 #endif
213         }
214
215         void TimerCallBack::DoEvent(
216                 System::Object^ stateInfo
217         )
218         {
219                 #ifdef _DEBUG
220                         System::Console::WriteLine("[{0}] [{1}] start", __FUNCTION__, System::Threading::Thread::CurrentThread->GetHashCode());
221                 #endif
222
223                 auto params = safe_cast<Interop::Winmm::TimerCallBack::TimerEventArgs^>(stateInfo);
224                 try
225                 {
226                         this->OnEvent(this, params);
227                 }
228                 catch(System::Exception^ e)
229                 {
230                         System::Console::WriteLine("[{0}] コールバック中のエラー[{1}]", __FUNCTION__, e->ToString());
231                 }
232
233                 #ifdef _DEBUG
234                         System::Console::WriteLine("[{0}] [{1}] end", __FUNCTION__, System::Threading::Thread::CurrentThread->GetHashCode());
235                 #endif
236         }
237 }
238 }
239 }