widcommaudio.c
Upload User: wstnjxml
Upload Date: 2014-04-03
Package Size: 7248k
Code Size: 9k
Category:

Windows CE

Development Platform:

C/C++

  1. /*****************************************************************************
  2.  *
  3.  * This program is free software ; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  16.  *
  17.  * $Id: widcommaudio.c 332 2005-11-06 14:31:57Z picard $
  18.  *
  19.  * The Core Pocket Media Player
  20.  * Copyright (c) 2004-2005 Gabor Kovacs
  21.  *
  22.  ****************************************************************************/
  23. #include "../../common/common.h"
  24. #include "widcommaudio.h"
  25. #if defined(TARGET_WINCE) && defined(ARM)
  26. #define WIN32_LEAN_AND_MEAN
  27. #ifndef STRICT
  28. #define STRICT
  29. #endif
  30. #include <windows.h>
  31. #include <shellapi.h>
  32. #define MAXPOS 20
  33. static const tchar_t RegWMP10[] = T("Software\Microsoft\MediaPlayer\Skins");
  34. static const tchar_t RegWMP9[] = T("Software\Microsoft\Windows Media Player\Parameters");
  35. static const tchar_t RegRotation[] = T("System\GDI\Rotation");
  36. static const tchar_t Skin1[] = T("DefaultPortraitSkin");
  37. static const tchar_t Skin2[] = T("DefaultLandscapeSkin");
  38. static const tchar_t Skin3[] = T("DefaultSquareSkin");
  39. static const tchar_t Skin4a[] = T("SkinDir");
  40. static const tchar_t Skin4b[] = T("SkinFile");
  41. static const tchar_t ClassName[] = T("WMP for Mobile Devices");
  42. static const tchar_t TitleName[] = T("Windows Media");
  43. static HWND Wnd = NULL;
  44. static HWND Notify = NULL;
  45. static LPARAM Pos[MAXPOS];
  46. static DWORD Key[MAXPOS];
  47. static int Count;
  48. static bool_t TmpSkin = 0;
  49. static bool_t WMP10Support = 0;
  50. static DWORD Activate = 0;
  51. static BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
  52. {
  53. if (hWnd != Wnd && !(GetWindowLong(hWnd,GWL_STYLE) & WS_POPUP))
  54. {
  55. tchar_t Name[64];
  56. if (GetWindowText(hWnd,Name,64)>0 && tcscmp(Name,TitleName)==0)
  57. *(HWND*)lParam = hWnd;
  58. }
  59. return TRUE;
  60. }
  61. static HWND FindWMP()
  62. {
  63. HWND Result = NULL;
  64. EnumWindows(EnumWindowsProc,(LPARAM)&Result);
  65. return Result;
  66. }
  67. static LRESULT Proc(HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  68. {
  69. HWND WMP;
  70. int n;
  71. switch (Msg)
  72. {
  73. case WM_ACTIVATE:
  74. if (LOWORD(wParam)!=WA_INACTIVE) 
  75. {
  76. WMP = FindWMP();
  77. if (WMP)
  78. {
  79. SetForegroundWindow(WMP);
  80. return 0;
  81. }
  82. }
  83. break;
  84. case WM_COPYDATA:
  85. DestroyWindow(Wnd);
  86. Wnd = NULL;
  87. WMP = FindWMP();
  88. if (!WMP)
  89. {
  90. COPYDATASTRUCT *Data = (COPYDATASTRUCT*)lParam;
  91. tchar_t Path[MAXPATH];
  92. SHELLEXECUTEINFO Info;
  93. GetSystemPath(Path,TSIZEOF(Path),T("wmplayer.exe"));
  94. memset(&Info,0,sizeof(Info));
  95. Info.cbSize = sizeof(Info);
  96. Info.fMask = SEE_MASK_NOCLOSEPROCESS;
  97. Info.lpFile = Path;
  98. Info.nShow = SW_SHOW;
  99. Info.lpParameters = (tchar_t*)Data->lpData;
  100. ShellExecuteEx(&Info);
  101. }
  102. else
  103. {
  104. SendMessage(WMP,Msg,wParam,lParam);
  105. SetForegroundWindow(WMP);
  106. }
  107. Activate = GetTickCount();
  108. return 1;
  109. case WM_LBUTTONDOWN:
  110. if (Notify)
  111. for (n=0;n<Count;++n)
  112. if (Pos[n] == lParam)
  113. {
  114. SendMessage(Notify,WM_KEYDOWN,Key[n],0);
  115. break;
  116. }
  117. return 0;
  118. case WM_LBUTTONUP:
  119. if (Notify)
  120. for (n=0;n<Count;++n)
  121. if (Pos[n] == lParam)
  122. {
  123. SendMessage(Notify,WM_KEYUP,Key[n],0x80000000);
  124. break;
  125. }
  126. return 0;
  127. };
  128. return DefWindowProc(Wnd,Msg,wParam,lParam);
  129. };
  130. static void Override()
  131. {
  132. HWND Found = FindWindow(ClassName,TitleName);
  133. if (!Wnd || Found != Wnd)
  134. {
  135. if (Wnd) DestroyWindow(Wnd);
  136. Wnd = CreateWindowEx(0,ClassName,TitleName,WS_POPUP,0,0,0,0,NULL,NULL,GetModuleHandle(NULL),NULL);
  137. ShowWindow(Wnd,SW_HIDE);
  138. }
  139. }
  140. static bool_t ReadSkin(const tchar_t* FileName)
  141. {
  142. tchar_t s[MAXLINE];
  143. parser Parser = {NULL};
  144. stream* Skin = StreamOpen(FileName,0);
  145. if (!Skin)
  146. return 0;
  147. ParserStream(&Parser,Skin);
  148. while (ParserLine(&Parser,s,sizeof(s)))
  149. {
  150. int k = 0;
  151. tchar_t* p = s;
  152. tcsupr(s);
  153. while (*p && IsSpace(*p)) ++p;
  154. if (WMP10Support && tcsncmp(p,T("PLAYPAUSESTOP"),13)==0)
  155. {
  156. p += 13;
  157. k = 0xB3; //VK_MEDIA_PLAY_PAUSE;
  158. }
  159. else
  160. if (tcsncmp(p,T("PLAYPAUSE"),9)==0)
  161. {
  162. p += 9;
  163. k = 0xB3; //VK_MEDIA_PLAY_PAUSE;
  164. }
  165. else
  166. if (tcsncmp(p,T("PLAY"),4)==0)
  167. {
  168. p += 4;
  169. k = 0xB3; //VK_MEDIA_PLAY_PAUSE;
  170. }
  171. else
  172. if (tcsncmp(p,T("PAUSE"),5)==0)
  173. {
  174. p += 5;
  175. k = 0xB3; //VK_MEDIA_PLAY_PAUSE;
  176. }
  177. else
  178. if (tcsncmp(p,T("NEXT"),4)==0)
  179. {
  180. p += 4;
  181. k = 0xB0; //VK_MEDIA_NEXT_TRACK;
  182. }
  183. else
  184. if (tcsncmp(p,T("PREV"),4)==0)
  185. {
  186. p += 4;
  187. k = 0xB1; //VK_MEDIA_PREV_TRACK;
  188. }
  189. else
  190. if (tcsncmp(p,T("STOP"),4)==0)
  191. {
  192. p += 4;
  193. k = 0xB2; //VK_MEDIA_STOP;
  194. }
  195. if (k)
  196. {
  197. int x,y,w,h;
  198. while (*p && IsSpace(*p)) ++p;
  199. while (*p && !IsSpace(*p)) ++p;
  200. if (stscanf(p,T("%d,%d,%d,%d"),&x,&y,&w,&h)==4 && Count<MAXPOS)
  201. {
  202. Key[Count] = k;
  203. Pos[Count] = (x+(w>>1))|((y+(h>>1))<<16);
  204. ++Count;
  205. }
  206. }
  207. }
  208. ParserStream(&Parser,NULL);
  209. StreamClose(Skin);
  210. return 1;
  211. }
  212. static void ProcessSkin(HKEY Key,const tchar_t* Name1, const tchar_t* Name2)
  213. {
  214. tchar_t FileName[MAXPATH];
  215. DWORD Size = sizeof(FileName);
  216. if (RegQueryValueEx(Key,Name1,NULL,NULL,(LPBYTE)FileName, &Size) == ERROR_SUCCESS)
  217. {
  218. if (Name2)
  219. {
  220. tchar_t* Tail = FileName + tcslen(FileName);
  221. Size = sizeof(FileName) - (Tail - FileName)*sizeof(tchar_t);
  222. RegQueryValueEx(Key,Name2,NULL,NULL,(LPBYTE)Tail, &Size);
  223. }
  224. if (ReadSkin(FileName))
  225. return;
  226. }
  227. GetSystemPath(FileName,TSIZEOF(FileName),T("widcommaudio.skn"));
  228. Size = sizeof(FileName);
  229. RegSetValueEx(Key,Name1, 0, REG_SZ, (PBYTE)FileName, Size );
  230. if (Name2)
  231. RegSetValueEx(Key,Name2, 0, REG_SZ, (PBYTE)T(""), 0);
  232. if (!TmpSkin)
  233. {
  234. stream* Skin = StreamOpen(FileName,1);
  235. if (Skin)
  236. {
  237. StreamPrintf(Skin,
  238. T(" PlayPause      NA 200,100,0,0n")
  239. T(" PlayPauseStop  NA 200,100,0,0n")
  240. T(" Next           NA 216,100,0,0n")
  241. T(" Prev           NA 232,100,0,0n")
  242. T(" Play           NA 248,100,0,0n")
  243. T(" Pause          NA 264,100,0,0n")
  244. T(" Stop           NA 280,100,0,0n"));
  245. StreamClose(Skin);
  246. TmpSkin = 1;
  247. }
  248. }
  249. ReadSkin(FileName);
  250. }
  251. void WidcommAudio_Init()
  252. {
  253. Count = 0;
  254. if (QueryAdvanced(ADVANCED_WIDCOMMAUDIO))
  255. {
  256. bool_t WMP10;
  257. HDC DC;
  258. int Width,Height;
  259. DWORD Angle = 0;
  260. DWORD Disp;
  261. HKEY Key = NULL;
  262. WNDCLASS Class;
  263. tchar_t FileName[MAXPATH];
  264. memset(&Class,0,sizeof(Class));
  265. Class.style = CS_HREDRAW | CS_VREDRAW;
  266. Class.lpfnWndProc = (WNDPROC) Proc;
  267. Class.hInstance = GetModuleHandle(NULL);
  268. Class.lpszClassName = ClassName;
  269. RegisterClass(&Class);
  270. if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, RegRotation, 0, KEY_READ, &Key ) == ERROR_SUCCESS)
  271. {
  272. DWORD Size = sizeof(Angle);
  273. RegQueryValueEx( Key, T("Angle"), NULL, NULL, (LPBYTE)&Angle, &Size);
  274. RegCloseKey(Key);
  275. }
  276. GetSystemPath(FileName,TSIZEOF(FileName),T("BtCeAvIf.dll"));
  277. WMP10Support = FileDate(FileName) > 20050101000000000;
  278. WMP10 = WMP10Support && RegOpenKeyEx(HKEY_LOCAL_MACHINE, RegWMP10, 0, KEY_READ|KEY_WRITE, &Key) == ERROR_SUCCESS;
  279. if (!WMP10 && RegCreateKeyEx( HKEY_LOCAL_MACHINE, RegWMP9, 0, NULL, 0, KEY_READ|KEY_WRITE, NULL, &Key, &Disp ) != ERROR_SUCCESS)
  280. Key = NULL;
  281. if (Key)
  282. {
  283. DC = GetDC(0);
  284. Width = GetDeviceCaps(DC,HORZRES);
  285. Height = GetDeviceCaps(DC,VERTRES);
  286. ReleaseDC(0,DC);
  287. if (WMP10 && Width>0 && Width==Height)
  288. {
  289. ProcessSkin(Key,Skin3,NULL);
  290. ProcessSkin(Key,Skin1,NULL);
  291. ProcessSkin(Key,Skin2,NULL);
  292. }
  293. else
  294. if (Angle==0 || Angle==180)
  295. {
  296. ProcessSkin(Key,Skin1,NULL);
  297. ProcessSkin(Key,Skin2,NULL);
  298. ProcessSkin(Key,Skin3,NULL);
  299. if (!WMP10)
  300. ProcessSkin(Key,Skin4a,Skin4b);
  301. }
  302. else
  303. if (Angle==90 || Angle==270)
  304. {
  305. ProcessSkin(Key,Skin2,NULL);
  306. ProcessSkin(Key,Skin1,NULL);
  307. ProcessSkin(Key,Skin3,NULL);
  308. if (!WMP10)
  309. ProcessSkin(Key,Skin4a,Skin4b);
  310. }
  311. else
  312. {
  313. if (!WMP10)
  314. ProcessSkin(Key,Skin4a,Skin4b);
  315. ProcessSkin(Key,Skin1,NULL);
  316. ProcessSkin(Key,Skin2,NULL);
  317. ProcessSkin(Key,Skin3,NULL);
  318. }
  319. RegCloseKey(Key);
  320. }
  321. Override();
  322. }
  323. }
  324. void WidcommAudio_Wnd(void* p)
  325. {
  326. Notify = p;
  327. if (Wnd)
  328. Override();
  329. }
  330. void WidcommAudio_Done()
  331. {
  332. if (Wnd)
  333. DestroyWindow(Wnd);
  334. }
  335. bool_t WidcommAudio_SkipActivate()
  336. {
  337. DWORD Time = GetTickCount();
  338. return Time > Activate && (int64_t)Time - (int64_t)Activate < 100;
  339. }
  340. #else
  341. void WidcommAudio_Init() {}
  342. void WidcommAudio_Done() {}
  343. void WidcommAudio_Wnd(void* p) {}
  344. bool_t WidcommAudio_SkipActivate() { return 0; }
  345. #endif