utility.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005-2013 Nullsoft, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * Neither the name of Nullsoft nor the names of its contributors may be used to
  14. endorse or promote products derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "api__vis_milk2.h"
  25. #include "utility.h"
  26. #include <math.h>
  27. #include <locale.h>
  28. #include <windows.h>
  29. #ifdef _DEBUG
  30. #define D3D_DEBUG_INFO // declare this before including d3d9.h
  31. #endif
  32. #include <d3d9.h>
  33. #include "../Winamp/wa_ipc.h"
  34. #include "resource.h"
  35. #include <shellapi.h>
  36. intptr_t myOpenURL(HWND hwnd, wchar_t *loc)
  37. {
  38. if (loc)
  39. {
  40. bool override=false;
  41. WASABI_API_SYSCB->syscb_issueCallback(SysCallback::BROWSER, BrowserCallback::ONOPENURL, reinterpret_cast<intptr_t>(loc), reinterpret_cast<intptr_t>(&override));
  42. if (!override)
  43. return (intptr_t)ShellExecuteW(hwnd, L"open", loc, NULL, NULL, SW_SHOWNORMAL);
  44. else
  45. return 33;
  46. }
  47. return 33;
  48. }
  49. float PowCosineInterp(float x, float pow)
  50. {
  51. // input (x) & output should be in range 0..1.
  52. // pow > 0: tends to push things toward 0 and 1
  53. // pow < 0: tends to push things toward 0.5.
  54. if (x<0)
  55. return 0;
  56. if (x>1)
  57. return 1;
  58. int bneg = (pow < 0) ? 1 : 0;
  59. if (bneg)
  60. pow = -pow;
  61. if (pow>1000) pow=1000;
  62. int its = (int)pow;
  63. for (int i=0; i<its; i++)
  64. {
  65. if (bneg)
  66. x = InvCosineInterp(x);
  67. else
  68. x = CosineInterp(x);
  69. }
  70. float x2 = (bneg) ? InvCosineInterp(x) : CosineInterp(x);
  71. float dx = pow - its;
  72. return ((1-dx)*x + (dx)*x2);
  73. }
  74. float AdjustRateToFPS(float per_frame_decay_rate_at_fps1, float fps1, float actual_fps)
  75. {
  76. // returns the equivalent per-frame decay rate at actual_fps
  77. // basically, do all your testing at fps1 and get a good decay rate;
  78. // then, in the real application, adjust that rate by the actual fps each time you use it.
  79. float per_second_decay_rate_at_fps1 = powf(per_frame_decay_rate_at_fps1, fps1);
  80. float per_frame_decay_rate_at_fps2 = powf(per_second_decay_rate_at_fps1, 1.0f/actual_fps);
  81. return per_frame_decay_rate_at_fps2;
  82. }
  83. float GetPrivateProfileFloatW(wchar_t *szSectionName, wchar_t *szKeyName, float fDefault, wchar_t *szIniFile)
  84. {
  85. wchar_t string[64];
  86. wchar_t szDefault[64];
  87. float ret = fDefault;
  88. _swprintf_l(szDefault, L"%f", g_use_C_locale, fDefault);
  89. if (GetPrivateProfileStringW(szSectionName, szKeyName, szDefault, string, 64, szIniFile) > 0)
  90. {
  91. _swscanf_l(string, L"%f", g_use_C_locale, &ret);
  92. }
  93. return ret;
  94. }
  95. bool WritePrivateProfileFloatW(float f, wchar_t *szKeyName, wchar_t *szIniFile, wchar_t *szSectionName)
  96. {
  97. wchar_t szValue[32];
  98. _swprintf_l(szValue, L"%f", g_use_C_locale, f);
  99. return (WritePrivateProfileStringW(szSectionName, szKeyName, szValue, szIniFile) != 0);
  100. }
  101. bool WritePrivateProfileIntW(int d, wchar_t *szKeyName, wchar_t *szIniFile, wchar_t *szSectionName)
  102. {
  103. wchar_t szValue[32];
  104. swprintf(szValue, L"%d", d);
  105. return (WritePrivateProfileStringW(szSectionName, szKeyName, szValue, szIniFile) != 0);
  106. }
  107. void SetScrollLock(int bNewState, bool bPreventHandling)
  108. {
  109. if(bPreventHandling) return;
  110. if (bNewState != (GetKeyState(VK_SCROLL) & 1))
  111. {
  112. // Simulate a key press
  113. keybd_event( VK_SCROLL,
  114. 0x45,
  115. KEYEVENTF_EXTENDEDKEY | 0,
  116. 0 );
  117. // Simulate a key release
  118. keybd_event( VK_SCROLL,
  119. 0x45,
  120. KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
  121. 0);
  122. }
  123. }
  124. void RemoveExtension(wchar_t *str)
  125. {
  126. wchar_t *p = wcsrchr(str, L'.');
  127. if (p) *p = 0;
  128. }
  129. static void ShiftDown(wchar_t *str)
  130. {
  131. while (*str)
  132. {
  133. str[0] = str[1];
  134. str++;
  135. }
  136. }
  137. void RemoveSingleAmpersands(wchar_t *str)
  138. {
  139. while (*str)
  140. {
  141. if (str[0] == L'&')
  142. {
  143. if (str[1] == L'&') // two in a row: replace with single ampersand, move on
  144. str++;
  145. ShiftDown(str);
  146. }
  147. else
  148. str = CharNextW(str);
  149. }
  150. }
  151. void TextToGuid(char *str, GUID *pGUID)
  152. {
  153. if (!str) return;
  154. if (!pGUID) return;
  155. DWORD d[11];
  156. sscanf(str, "%X %X %X %X %X %X %X %X %X %X %X",
  157. &d[0], &d[1], &d[2], &d[3], &d[4], &d[5], &d[6], &d[7], &d[8], &d[9], &d[10]);
  158. pGUID->Data1 = (DWORD)d[0];
  159. pGUID->Data2 = (WORD)d[1];
  160. pGUID->Data3 = (WORD)d[2];
  161. pGUID->Data4[0] = (BYTE)d[3];
  162. pGUID->Data4[1] = (BYTE)d[4];
  163. pGUID->Data4[2] = (BYTE)d[5];
  164. pGUID->Data4[3] = (BYTE)d[6];
  165. pGUID->Data4[4] = (BYTE)d[7];
  166. pGUID->Data4[5] = (BYTE)d[8];
  167. pGUID->Data4[6] = (BYTE)d[9];
  168. pGUID->Data4[7] = (BYTE)d[10];
  169. }
  170. void GuidToText(GUID *pGUID, char *str, int nStrLen)
  171. {
  172. // note: nStrLen should be set to sizeof(str).
  173. if (!str) return;
  174. if (!nStrLen) return;
  175. str[0] = 0;
  176. if (!pGUID) return;
  177. DWORD d[11];
  178. d[0] = (DWORD)pGUID->Data1;
  179. d[1] = (DWORD)pGUID->Data2;
  180. d[2] = (DWORD)pGUID->Data3;
  181. d[3] = (DWORD)pGUID->Data4[0];
  182. d[4] = (DWORD)pGUID->Data4[1];
  183. d[5] = (DWORD)pGUID->Data4[2];
  184. d[6] = (DWORD)pGUID->Data4[3];
  185. d[7] = (DWORD)pGUID->Data4[4];
  186. d[8] = (DWORD)pGUID->Data4[5];
  187. d[9] = (DWORD)pGUID->Data4[6];
  188. d[10] = (DWORD)pGUID->Data4[7];
  189. sprintf(str, "%08X %04X %04X %02X %02X %02X %02X %02X %02X %02X %02X",
  190. d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10]);
  191. }
  192. /*
  193. int GetPentiumTimeRaw(unsigned __int64 *cpu_timestamp)
  194. {
  195. // returns 0 on failure, 1 on success
  196. // warning: watch out for wraparound!
  197. // note: it's probably better to use QueryPerformanceFrequency
  198. // and QueryPerformanceCounter()!
  199. // get high-precision time:
  200. __try
  201. {
  202. unsigned __int64 *dest = (unsigned __int64 *)cpu_timestamp;
  203. __asm
  204. {
  205. _emit 0xf // these two bytes form the 'rdtsc' asm instruction,
  206. _emit 0x31 // available on Pentium I and later.
  207. mov esi, dest
  208. mov [esi ], eax // lower 32 bits of tsc
  209. mov [esi+4], edx // upper 32 bits of tsc
  210. }
  211. return 1;
  212. }
  213. __except(EXCEPTION_EXECUTE_HANDLER)
  214. {
  215. return 0;
  216. }
  217. return 0;
  218. }
  219. double GetPentiumTimeAsDouble(unsigned __int64 frequency)
  220. {
  221. // returns < 0 on failure; otherwise, returns current cpu time, in seconds.
  222. // warning: watch out for wraparound!
  223. // note: it's probably better to use QueryPerformanceFrequency
  224. // and QueryPerformanceCounter()!
  225. if (frequency==0)
  226. return -1.0;
  227. // get high-precision time:
  228. __try
  229. {
  230. unsigned __int64 high_perf_time;
  231. unsigned __int64 *dest = &high_perf_time;
  232. __asm
  233. {
  234. _emit 0xf // these two bytes form the 'rdtsc' asm instruction,
  235. _emit 0x31 // available on Pentium I and later.
  236. mov esi, dest
  237. mov [esi ], eax // lower 32 bits of tsc
  238. mov [esi+4], edx // upper 32 bits of tsc
  239. }
  240. __int64 time_s = (__int64)(high_perf_time / frequency); // unsigned->sign conversion should be safe here
  241. __int64 time_fract = (__int64)(high_perf_time % frequency); // unsigned->sign conversion should be safe here
  242. // note: here, we wrap the timer more frequently (once per week)
  243. // than it otherwise would (VERY RARELY - once every 585 years on
  244. // a 1 GHz), to alleviate floating-point precision errors that start
  245. // to occur when you get to very high counter values.
  246. double ret = (time_s % (60*60*24*7)) + (double)time_fract/(double)((__int64)frequency);
  247. return ret;
  248. }
  249. __except(EXCEPTION_EXECUTE_HANDLER)
  250. {
  251. return -1.0;
  252. }
  253. return -1.0;
  254. }
  255. */
  256. #ifdef _DEBUG
  257. void OutputDebugMessage(char *szStartText, HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  258. {
  259. // note: this function does NOT log WM_MOUSEMOVE, WM_NCHITTEST, or WM_SETCURSOR
  260. // messages, since they are so frequent.
  261. // note: these identifiers were pulled from winuser.h
  262. //if (msg == WM_MOUSEMOVE || msg == WM_NCHITTEST || msg == WM_SETCURSOR)
  263. // return;
  264. #ifdef _DEBUG
  265. char buf[64];
  266. int matched = 1;
  267. sprintf(buf, "WM_");
  268. switch(msg)
  269. {
  270. case 0x0001: lstrcat(buf, "CREATE"); break;
  271. case 0x0002: lstrcat(buf, "DESTROY"); break;
  272. case 0x0003: lstrcat(buf, "MOVE"); break;
  273. case 0x0005: lstrcat(buf, "SIZE"); break;
  274. case 0x0006: lstrcat(buf, "ACTIVATE"); break;
  275. case 0x0007: lstrcat(buf, "SETFOCUS"); break;
  276. case 0x0008: lstrcat(buf, "KILLFOCUS"); break;
  277. case 0x000A: lstrcat(buf, "ENABLE"); break;
  278. case 0x000B: lstrcat(buf, "SETREDRAW"); break;
  279. case 0x000C: lstrcat(buf, "SETTEXT"); break;
  280. case 0x000D: lstrcat(buf, "GETTEXT"); break;
  281. case 0x000E: lstrcat(buf, "GETTEXTLENGTH"); break;
  282. case 0x000F: lstrcat(buf, "PAINT"); break;
  283. case 0x0010: lstrcat(buf, "CLOSE"); break;
  284. case 0x0011: lstrcat(buf, "QUERYENDSESSION"); break;
  285. case 0x0012: lstrcat(buf, "QUIT"); break;
  286. case 0x0013: lstrcat(buf, "QUERYOPEN"); break;
  287. case 0x0014: lstrcat(buf, "ERASEBKGND"); break;
  288. case 0x0015: lstrcat(buf, "SYSCOLORCHANGE"); break;
  289. case 0x0016: lstrcat(buf, "ENDSESSION"); break;
  290. case 0x0018: lstrcat(buf, "SHOWWINDOW"); break;
  291. case 0x001A: lstrcat(buf, "WININICHANGE"); break;
  292. case 0x001B: lstrcat(buf, "DEVMODECHANGE"); break;
  293. case 0x001C: lstrcat(buf, "ACTIVATEAPP"); break;
  294. case 0x001D: lstrcat(buf, "FONTCHANGE"); break;
  295. case 0x001E: lstrcat(buf, "TIMECHANGE"); break;
  296. case 0x001F: lstrcat(buf, "CANCELMODE"); break;
  297. case 0x0020: lstrcat(buf, "SETCURSOR"); break;
  298. case 0x0021: lstrcat(buf, "MOUSEACTIVATE"); break;
  299. case 0x0022: lstrcat(buf, "CHILDACTIVATE"); break;
  300. case 0x0023: lstrcat(buf, "QUEUESYNC"); break;
  301. case 0x0024: lstrcat(buf, "GETMINMAXINFO"); break;
  302. case 0x0026: lstrcat(buf, "PAINTICON"); break;
  303. case 0x0027: lstrcat(buf, "ICONERASEBKGND"); break;
  304. case 0x0028: lstrcat(buf, "NEXTDLGCTL"); break;
  305. case 0x002A: lstrcat(buf, "SPOOLERSTATUS"); break;
  306. case 0x002B: lstrcat(buf, "DRAWITEM"); break;
  307. case 0x002C: lstrcat(buf, "MEASUREITEM"); break;
  308. case 0x002D: lstrcat(buf, "DELETEITEM"); break;
  309. case 0x002E: lstrcat(buf, "VKEYTOITEM"); break;
  310. case 0x002F: lstrcat(buf, "CHARTOITEM"); break;
  311. case 0x0030: lstrcat(buf, "SETFONT"); break;
  312. case 0x0031: lstrcat(buf, "GETFONT"); break;
  313. case 0x0032: lstrcat(buf, "SETHOTKEY"); break;
  314. case 0x0033: lstrcat(buf, "GETHOTKEY"); break;
  315. case 0x0037: lstrcat(buf, "QUERYDRAGICON"); break;
  316. case 0x0039: lstrcat(buf, "COMPAREITEM"); break;
  317. case 0x0041: lstrcat(buf, "COMPACTING"); break;
  318. case 0x0044: lstrcat(buf, "COMMNOTIFY"); break;
  319. case 0x0046: lstrcat(buf, "WINDOWPOSCHANGING"); break;
  320. case 0x0047: lstrcat(buf, "WINDOWPOSCHANGED"); break;
  321. case 0x0048: lstrcat(buf, "POWER"); break;
  322. case 0x004A: lstrcat(buf, "COPYDATA"); break;
  323. case 0x004B: lstrcat(buf, "CANCELJOURNAL"); break;
  324. #if(WINVER >= 0x0400)
  325. case 0x004E: lstrcat(buf, "NOTIFY"); break;
  326. case 0x0050: lstrcat(buf, "INPUTLANGCHANGEREQUEST"); break;
  327. case 0x0051: lstrcat(buf, "INPUTLANGCHANGE"); break;
  328. case 0x0052: lstrcat(buf, "TCARD"); break;
  329. case 0x0053: lstrcat(buf, "HELP"); break;
  330. case 0x0054: lstrcat(buf, "USERCHANGED"); break;
  331. case 0x0055: lstrcat(buf, "NOTIFYFORMAT"); break;
  332. case 0x007B: lstrcat(buf, "CONTEXTMENU"); break;
  333. case 0x007C: lstrcat(buf, "STYLECHANGING"); break;
  334. case 0x007D: lstrcat(buf, "STYLECHANGED"); break;
  335. case 0x007E: lstrcat(buf, "DISPLAYCHANGE"); break;
  336. case 0x007F: lstrcat(buf, "GETICON"); break;
  337. case 0x0080: lstrcat(buf, "SETICON"); break;
  338. #endif
  339. case 0x0081: lstrcat(buf, "NCCREATE"); break;
  340. case 0x0082: lstrcat(buf, "NCDESTROY"); break;
  341. case 0x0083: lstrcat(buf, "NCCALCSIZE"); break;
  342. case 0x0084: lstrcat(buf, "NCHITTEST"); break;
  343. case 0x0085: lstrcat(buf, "NCPAINT"); break;
  344. case 0x0086: lstrcat(buf, "NCACTIVATE"); break;
  345. case 0x0087: lstrcat(buf, "GETDLGCODE"); break;
  346. case 0x0088: lstrcat(buf, "SYNCPAINT"); break;
  347. case 0x00A0: lstrcat(buf, "NCMOUSEMOVE"); break;
  348. case 0x00A1: lstrcat(buf, "NCLBUTTONDOWN"); break;
  349. case 0x00A2: lstrcat(buf, "NCLBUTTONUP"); break;
  350. case 0x00A3: lstrcat(buf, "NCLBUTTONDBLCLK"); break;
  351. case 0x00A4: lstrcat(buf, "NCRBUTTONDOWN"); break;
  352. case 0x00A5: lstrcat(buf, "NCRBUTTONUP"); break;
  353. case 0x00A6: lstrcat(buf, "NCRBUTTONDBLCLK"); break;
  354. case 0x00A7: lstrcat(buf, "NCMBUTTONDOWN"); break;
  355. case 0x00A8: lstrcat(buf, "NCMBUTTONUP"); break;
  356. case 0x00A9: lstrcat(buf, "NCMBUTTONDBLCLK"); break;
  357. case 0x0100: lstrcat(buf, "KEYDOWN"); break;
  358. case 0x0101: lstrcat(buf, "KEYUP"); break;
  359. case 0x0102: lstrcat(buf, "CHAR"); break;
  360. case 0x0103: lstrcat(buf, "DEADCHAR"); break;
  361. case 0x0104: lstrcat(buf, "SYSKEYDOWN"); break;
  362. case 0x0105: lstrcat(buf, "SYSKEYUP"); break;
  363. case 0x0106: lstrcat(buf, "SYSCHAR"); break;
  364. case 0x0107: lstrcat(buf, "SYSDEADCHAR"); break;
  365. case 0x0108: lstrcat(buf, "KEYLAST"); break;
  366. #if(WINVER >= 0x0400)
  367. case 0x010D: lstrcat(buf, "IME_STARTCOMPOSITION"); break;
  368. case 0x010E: lstrcat(buf, "IME_ENDCOMPOSITION"); break;
  369. case 0x010F: lstrcat(buf, "IME_COMPOSITION"); break;
  370. //case 0x010F: lstrcat(buf, "IME_KEYLAST"); break;
  371. #endif
  372. case 0x0110: lstrcat(buf, "INITDIALOG"); break;
  373. case 0x0111: lstrcat(buf, "COMMAND"); break;
  374. case 0x0112: lstrcat(buf, "SYSCOMMAND"); break;
  375. case 0x0113: lstrcat(buf, "TIMER"); break;
  376. case 0x0114: lstrcat(buf, "HSCROLL"); break;
  377. case 0x0115: lstrcat(buf, "VSCROLL"); break;
  378. case 0x0116: lstrcat(buf, "INITMENU"); break;
  379. case 0x0117: lstrcat(buf, "INITMENUPOPUP"); break;
  380. case 0x011F: lstrcat(buf, "MENUSELECT"); break;
  381. case 0x0120: lstrcat(buf, "MENUCHAR"); break;
  382. case 0x0121: lstrcat(buf, "ENTERIDLE"); break;
  383. #if(WINVER >= 0x0500)
  384. case 0x0122: lstrcat(buf, "MENURBUTTONUP"); break;
  385. case 0x0123: lstrcat(buf, "MENUDRAG"); break;
  386. case 0x0124: lstrcat(buf, "MENUGETOBJECT"); break;
  387. case 0x0125: lstrcat(buf, "UNINITMENUPOPUP"); break;
  388. case 0x0126: lstrcat(buf, "MENUCOMMAND"); break;
  389. #endif
  390. case 0x0132: lstrcat(buf, "CTLCOLORMSGBOX"); break;
  391. case 0x0133: lstrcat(buf, "CTLCOLOREDIT"); break;
  392. case 0x0134: lstrcat(buf, "CTLCOLORLISTBOX"); break;
  393. case 0x0135: lstrcat(buf, "CTLCOLORBTN"); break;
  394. case 0x0136: lstrcat(buf, "CTLCOLORDLG"); break;
  395. case 0x0137: lstrcat(buf, "CTLCOLORSCROLLBAR"); break;
  396. case 0x0138: lstrcat(buf, "CTLCOLORSTATIC"); break;
  397. //case 0x0200: lstrcat(buf, "MOUSEFIRST"); break;
  398. case 0x0200: lstrcat(buf, "MOUSEMOVE"); break;
  399. case 0x0201: lstrcat(buf, "LBUTTONDOWN"); break;
  400. case 0x0202: lstrcat(buf, "LBUTTONUP"); break;
  401. case 0x0203: lstrcat(buf, "LBUTTONDBLCLK"); break;
  402. case 0x0204: lstrcat(buf, "RBUTTONDOWN"); break;
  403. case 0x0205: lstrcat(buf, "RBUTTONUP"); break;
  404. case 0x0206: lstrcat(buf, "RBUTTONDBLCLK"); break;
  405. case 0x0207: lstrcat(buf, "MBUTTONDOWN"); break;
  406. case 0x0208: lstrcat(buf, "MBUTTONUP"); break;
  407. case 0x0209: lstrcat(buf, "MBUTTONDBLCLK"); break;
  408. #if (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)
  409. case 0x020A: lstrcat(buf, "MOUSEWHEEL"); break;
  410. case 0x020E: lstrcat(buf, "MOUSELAST"); break;
  411. #else
  412. //case 0x0209: lstrcat(buf, "MOUSELAST"); break;
  413. #endif
  414. case 0x0210: lstrcat(buf, "PARENTNOTIFY"); break;
  415. case 0x0211: lstrcat(buf, "ENTERMENULOOP"); break;
  416. case 0x0212: lstrcat(buf, "EXITMENULOOP"); break;
  417. #if(WINVER >= 0x0400)
  418. case 0x0213: lstrcat(buf, "NEXTMENU"); break;
  419. case 0x0214: lstrcat(buf, "SIZING"); break;
  420. case 0x0215: lstrcat(buf, "CAPTURECHANGED"); break;
  421. case 0x0216: lstrcat(buf, "MOVING"); break;
  422. case 0x0218: lstrcat(buf, "POWERBROADCAST"); break;
  423. case 0x0219: lstrcat(buf, "DEVICECHANGE"); break;
  424. #endif
  425. /*
  426. case 0x0220: lstrcat(buf, "MDICREATE"); break;
  427. case 0x0221: lstrcat(buf, "MDIDESTROY"); break;
  428. case 0x0222: lstrcat(buf, "MDIACTIVATE"); break;
  429. case 0x0223: lstrcat(buf, "MDIRESTORE"); break;
  430. case 0x0224: lstrcat(buf, "MDINEXT"); break;
  431. case 0x0225: lstrcat(buf, "MDIMAXIMIZE"); break;
  432. case 0x0226: lstrcat(buf, "MDITILE"); break;
  433. case 0x0227: lstrcat(buf, "MDICASCADE"); break;
  434. case 0x0228: lstrcat(buf, "MDIICONARRANGE"); break;
  435. case 0x0229: lstrcat(buf, "MDIGETACTIVE"); break;
  436. */
  437. case 0x0230: lstrcat(buf, "MDISETMENU"); break;
  438. case 0x0231: lstrcat(buf, "ENTERSIZEMOVE"); break;
  439. case 0x0232: lstrcat(buf, "EXITSIZEMOVE"); break;
  440. case 0x0233: lstrcat(buf, "DROPFILES"); break;
  441. case 0x0234: lstrcat(buf, "MDIREFRESHMENU"); break;
  442. /*
  443. #if(WINVER >= 0x0400)
  444. case 0x0281: lstrcat(buf, "IME_SETCONTEXT"); break;
  445. case 0x0282: lstrcat(buf, "IME_NOTIFY"); break;
  446. case 0x0283: lstrcat(buf, "IME_CONTROL"); break;
  447. case 0x0284: lstrcat(buf, "IME_COMPOSITIONFULL"); break;
  448. case 0x0285: lstrcat(buf, "IME_SELECT"); break;
  449. case 0x0286: lstrcat(buf, "IME_CHAR"); break;
  450. #endif
  451. #if(WINVER >= 0x0500)
  452. case 0x0288: lstrcat(buf, "IME_REQUEST"); break;
  453. #endif
  454. #if(WINVER >= 0x0400)
  455. case 0x0290: lstrcat(buf, "IME_KEYDOWN"); break;
  456. case 0x0291: lstrcat(buf, "IME_KEYUP"); break;
  457. #endif
  458. */
  459. #if(_WIN32_WINNT >= 0x0400)
  460. case 0x02A1: lstrcat(buf, "MOUSEHOVER"); break;
  461. case 0x02A3: lstrcat(buf, "MOUSELEAVE"); break;
  462. #endif
  463. case 0x0300: lstrcat(buf, "CUT"); break;
  464. case 0x0301: lstrcat(buf, "COPY"); break;
  465. case 0x0302: lstrcat(buf, "PASTE"); break;
  466. case 0x0303: lstrcat(buf, "CLEAR"); break;
  467. case 0x0304: lstrcat(buf, "UNDO"); break;
  468. case 0x0305: lstrcat(buf, "RENDERFORMAT"); break;
  469. case 0x0306: lstrcat(buf, "RENDERALLFORMATS"); break;
  470. case 0x0307: lstrcat(buf, "DESTROYCLIPBOARD"); break;
  471. case 0x0308: lstrcat(buf, "DRAWCLIPBOARD"); break;
  472. case 0x0309: lstrcat(buf, "PAINTCLIPBOARD"); break;
  473. case 0x030A: lstrcat(buf, "VSCROLLCLIPBOARD"); break;
  474. case 0x030B: lstrcat(buf, "SIZECLIPBOARD"); break;
  475. case 0x030C: lstrcat(buf, "ASKCBFORMATNAME"); break;
  476. case 0x030D: lstrcat(buf, "CHANGECBCHAIN"); break;
  477. case 0x030E: lstrcat(buf, "HSCROLLCLIPBOARD"); break;
  478. case 0x030F: lstrcat(buf, "QUERYNEWPALETTE"); break;
  479. case 0x0310: lstrcat(buf, "PALETTEISCHANGING"); break;
  480. case 0x0311: lstrcat(buf, "PALETTECHANGED"); break;
  481. case 0x0312: lstrcat(buf, "HOTKEY"); break;
  482. #if(WINVER >= 0x0400)
  483. case 0x0317: lstrcat(buf, "PRINT"); break;
  484. case 0x0318: lstrcat(buf, "PRINTCLIENT"); break;
  485. case 0x0358: lstrcat(buf, "HANDHELDFIRST"); break;
  486. case 0x035F: lstrcat(buf, "HANDHELDLAST"); break;
  487. case 0x0360: lstrcat(buf, "AFXFIRST"); break;
  488. case 0x037F: lstrcat(buf, "AFXLAST"); break;
  489. #endif
  490. case 0x0380: lstrcat(buf, "PENWINFIRST"); break;
  491. case 0x038F: lstrcat(buf, "PENWINLAST"); break;
  492. default:
  493. sprintf(buf, "unknown");
  494. matched = 0;
  495. break;
  496. }
  497. int n = strlen(buf);
  498. int desired_len = 24;
  499. int spaces_to_append = desired_len-n;
  500. if (spaces_to_append>0)
  501. {
  502. for (int i=0; i<spaces_to_append; i++)
  503. buf[n+i] = ' ';
  504. buf[desired_len] = 0;
  505. }
  506. char buf2[256];
  507. if (matched)
  508. sprintf(buf2, "%shwnd=%08x, msg=%s, w=%08x, l=%08x\n", szStartText, hwnd, buf, wParam, lParam);
  509. else
  510. sprintf(buf2, "%shwnd=%08x, msg=unknown/0x%08x, w=%08x, l=%08x\n", szStartText, hwnd, msg, wParam, lParam);
  511. OutputDebugString(buf2);
  512. #endif
  513. }
  514. #endif
  515. void DownloadDirectX(HWND hwnd)
  516. {
  517. wchar_t szUrl[] = L"http://www.microsoft.com/download/details.aspx?id=35";
  518. intptr_t ret = myOpenURL(NULL, szUrl);
  519. if (ret <= 32)
  520. {
  521. wchar_t buf[1024];
  522. switch(ret)
  523. {
  524. case SE_ERR_FNF:
  525. case SE_ERR_PNF:
  526. swprintf(buf, WASABI_API_LNGSTRINGW(IDS_URL_COULD_NOT_OPEN), szUrl);
  527. break;
  528. case SE_ERR_ACCESSDENIED:
  529. case SE_ERR_SHARE:
  530. swprintf(buf, WASABI_API_LNGSTRINGW(IDS_ACCESS_TO_URL_WAS_DENIED), szUrl);
  531. break;
  532. case SE_ERR_NOASSOC:
  533. swprintf(buf, WASABI_API_LNGSTRINGW(IDS_ACCESS_TO_URL_FAILED_DUE_TO_NO_ASSOC), szUrl);
  534. break;
  535. default:
  536. swprintf(buf, WASABI_API_LNGSTRINGW(IDS_ACCESS_TO_URL_FAILED_CODE_X), szUrl, ret);
  537. break;
  538. }
  539. MessageBoxW(hwnd, buf, WASABI_API_LNGSTRINGW(IDS_ERROR_OPENING_URL),
  540. MB_OK|MB_SETFOREGROUND|MB_TOPMOST|MB_TASKMODAL);
  541. }
  542. }
  543. void MissingDirectX(HWND hwnd)
  544. {
  545. // DIRECTX MISSING OR CORRUPT -> PROMPT TO GO TO WEB.
  546. wchar_t title[128];
  547. int ret = MessageBoxW(hwnd,
  548. #ifndef D3D_SDK_VERSION
  549. --- error; you need to #include <d3d9.h> ---
  550. #endif
  551. #if (D3D_SDK_VERSION==120)
  552. // plugin was *built* using the DirectX 9.0 sdk, therefore,
  553. // the dx9.0 runtime is missing or corrupt
  554. "Failed to initialize DirectX 9.0 or later.\n"
  555. "Milkdrop requires d3dx9_31.dll to be installed.\n"
  556. "\n"
  557. "Would you like to be taken to:\n"
  558. "http://www.microsoft.com/download/details.aspx?id=35,\n"
  559. "where you can update DirectX 9.0?\n"
  560. XXXXXXX
  561. #else
  562. // plugin was *built* using some other version of the DirectX9 sdk, such as
  563. // 9.1b; therefore, we don't know exactly what version to tell them they need
  564. // to install; so we ask them to go get the *latest* version.
  565. WASABI_API_LNGSTRINGW(IDS_DIRECTX_MISSING_OR_CORRUPT_TEXT)
  566. #endif
  567. ,
  568. WASABI_API_LNGSTRINGW_BUF(IDS_DIRECTX_MISSING_OR_CORRUPT, title, 128),
  569. MB_YESNO|MB_SETFOREGROUND|MB_TOPMOST|MB_TASKMODAL);
  570. if (ret==IDYES)
  571. DownloadDirectX(hwnd);
  572. }
  573. bool CheckForMMX()
  574. {
  575. #ifdef _WIN64
  576. return true; // All x64 processors support SSE
  577. #else
  578. DWORD bMMX = 0;
  579. DWORD *pbMMX = &bMMX;
  580. __try {
  581. __asm {
  582. mov eax, 1
  583. cpuid
  584. mov edi, pbMMX
  585. mov dword ptr [edi], edx
  586. }
  587. }
  588. __except(EXCEPTION_EXECUTE_HANDLER)
  589. {
  590. bMMX = 0;
  591. }
  592. if (bMMX & 0x00800000) // check bit 23
  593. return true;
  594. return false;
  595. #endif
  596. }
  597. bool CheckForSSE()
  598. {
  599. #ifdef _WIN64
  600. return true; // All x64 processors support SSE
  601. #else
  602. /*
  603. The SSE instruction set was introduced with the Pentium III and features:
  604. * Additional MMX instructions such as min/max
  605. * Prefetch and write-through instructions for optimizing data movement
  606. from and to the L2/L3 caches and main memory
  607. * 8 New 128 bit XMM registers (xmm0..xmm7) and corresponding 32 bit floating point
  608. (single precision) instructions
  609. */
  610. DWORD bSSE = 0;
  611. DWORD *pbSSE = &bSSE;
  612. __try {
  613. __asm
  614. {
  615. mov eax, 1
  616. cpuid
  617. mov edi, pbSSE
  618. mov dword ptr [edi], edx
  619. }
  620. }
  621. __except(EXCEPTION_EXECUTE_HANDLER)
  622. {
  623. bSSE = 0;
  624. }
  625. if (bSSE & 0x02000000) // check bit 25
  626. return true;
  627. return false;
  628. #endif
  629. }
  630. void GetDesktopFolder(char *szDesktopFolder) // should be MAX_PATH len.
  631. {
  632. // returns the path to the desktop folder, WITHOUT a trailing backslash.
  633. szDesktopFolder[0] = 0;
  634. ITEMIDLIST pidl;
  635. ZeroMemory(&pidl, sizeof(pidl));
  636. if (!SHGetPathFromIDList(&pidl, szDesktopFolder))
  637. szDesktopFolder[0] = 0;
  638. }
  639. void ExecutePidl(LPITEMIDLIST pidl, char *szPathAndFile, char *szWorkingDirectory, HWND hWnd)
  640. {
  641. // This function was based on code by Jeff Prosise.
  642. // Note: for some reason, ShellExecuteEx fails when executing
  643. // *shortcuts* (.lnk files) from the desktop, using their PIDLs.
  644. // So, if that fails, we try again w/the plain old text filename
  645. // (szPathAndFile).
  646. char szVerb[] = "open";
  647. char szFilename2[MAX_PATH];
  648. sprintf(szFilename2, "%s.lnk", szPathAndFile);
  649. // -without the "no-verb" pass,
  650. // certain icons still don't work (like shortcuts
  651. // to IE, VTune...)
  652. // -without the "context menu" pass,
  653. // certain others STILL don't work (Netscape...)
  654. // -without the 'ntry' pass, shortcuts (to folders/files)
  655. // don't work
  656. for (int verb_pass=0; verb_pass<2; verb_pass++)
  657. {
  658. for (int ntry=0; ntry<3; ntry++)
  659. {
  660. for (int context_pass=0; context_pass<2; context_pass++)
  661. {
  662. SHELLEXECUTEINFO sei = { sizeof(sei) };
  663. sei.hwnd = hWnd;
  664. sei.fMask = SEE_MASK_FLAG_NO_UI;
  665. if (context_pass==1)
  666. sei.fMask |= SEE_MASK_INVOKEIDLIST;
  667. sei.lpVerb = (verb_pass) ? NULL : szVerb;
  668. sei.lpDirectory = szWorkingDirectory;
  669. sei.nShow = SW_SHOWNORMAL;
  670. if (ntry==0)
  671. {
  672. // this case works for most non-shortcuts
  673. sei.fMask |= SEE_MASK_IDLIST;
  674. sei.lpIDList = pidl;
  675. }
  676. else if (ntry==1)
  677. {
  678. // this case is required for *shortcuts to folders* to work
  679. sei.lpFile = szPathAndFile;
  680. }
  681. else if (ntry==2)
  682. {
  683. // this case is required for *shortcuts to files* to work
  684. sei.lpFile = szFilename2;
  685. }
  686. if (ShellExecuteEx(&sei))
  687. return;
  688. }
  689. }
  690. }
  691. }
  692. WNDPROC g_pOldWndProc;
  693. LPCONTEXTMENU2 g_pIContext2or3;
  694. LRESULT CALLBACK HookWndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
  695. {
  696. //UINT uItem;
  697. //TCHAR szBuf[MAX_PATH];
  698. switch (msg)
  699. {
  700. case WM_DRAWITEM:
  701. case WM_MEASUREITEM:
  702. if(wp) break; // not menu related
  703. case WM_INITMENUPOPUP:
  704. g_pIContext2or3->HandleMenuMsg(msg, wp, lp);
  705. return (msg==WM_INITMENUPOPUP ? 0 : TRUE); // handled
  706. /*case WM_MENUSELECT:
  707. // if this is a shell item, get its descriptive text
  708. uItem = (UINT) LOWORD(wp);
  709. if(0 == (MF_POPUP & HIWORD(wp)) && uItem >= 1 && uItem <= 0x7fff)
  710. {
  711. g_pIContext2or3->GetCommandString(uItem-1, GCS_HELPTEXT,
  712. NULL, szBuf, sizeof(szBuf)/sizeof(szBuf[0]) );
  713. // set the status bar text
  714. ((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->SetMessageText(szBuf);
  715. return 0;
  716. }
  717. break;*/
  718. default:
  719. break;
  720. }
  721. // for all untreated messages, call the original wndproc
  722. return ::CallWindowProc(g_pOldWndProc, hWnd, msg, wp, lp);
  723. }
  724. BOOL DoExplorerMenu (HWND hwnd, LPITEMIDLIST pidlMain, POINT point)
  725. {
  726. LPMALLOC pMalloc;
  727. LPSHELLFOLDER psfFolder, psfNextFolder;
  728. LPITEMIDLIST pidlItem, pidlNextItem, *ppidl;
  729. LPCONTEXTMENU pContextMenu;
  730. CMINVOKECOMMANDINFO ici;
  731. UINT nCount, nCmd;
  732. BOOL bResult;
  733. HMENU hMenu;
  734. //
  735. // Get pointers to the shell's IMalloc interface and the desktop's
  736. // IShellFolder interface.
  737. //
  738. bResult = FALSE;
  739. if (!SUCCEEDED (SHGetMalloc (&pMalloc)))
  740. return bResult;
  741. if (!SUCCEEDED (SHGetDesktopFolder (&psfFolder))) {
  742. pMalloc->Release();
  743. return bResult;
  744. }
  745. if (nCount = GetItemCount (pidlMain)) // nCount must be > 0
  746. {
  747. //
  748. // Initialize psfFolder with a pointer to the IShellFolder
  749. // interface of the folder that contains the item whose context
  750. // menu we're after, and initialize pidlItem with a pointer to
  751. // the item's item ID. If nCount > 1, this requires us to walk
  752. // the list of item IDs stored in pidlMain and bind to each
  753. // subfolder referenced in the list.
  754. //
  755. pidlItem = pidlMain;
  756. while (--nCount) {
  757. //
  758. // Create a 1-item item ID list for the next item in pidlMain.
  759. //
  760. pidlNextItem = DuplicateItem (pMalloc, pidlItem);
  761. if (pidlNextItem == NULL) {
  762. psfFolder->Release();
  763. pMalloc->Release();
  764. return bResult;
  765. }
  766. //
  767. // Bind to the folder specified in the new item ID list.
  768. //
  769. if (!SUCCEEDED (psfFolder->BindToObject(pidlNextItem, NULL, IID_IShellFolder, (void**)&psfNextFolder))) // modified by RG
  770. {
  771. pMalloc->Free(pidlNextItem);
  772. psfFolder->Release();
  773. pMalloc->Release();
  774. return bResult;
  775. }
  776. //
  777. // Release the IShellFolder pointer to the parent folder
  778. // and set psfFolder equal to the IShellFolder pointer for
  779. // the current folder.
  780. //
  781. psfFolder->Release();
  782. psfFolder = psfNextFolder;
  783. //
  784. // Release the storage for the 1-item item ID list we created
  785. // just a moment ago and initialize pidlItem so that it points
  786. // to the next item in pidlMain.
  787. //
  788. pMalloc->Free(pidlNextItem);
  789. pidlItem = GetNextItem (pidlItem);
  790. }
  791. //
  792. // Get a pointer to the item's IContextMenu interface and call
  793. // IContextMenu::QueryContextMenu to initialize a context menu.
  794. //
  795. ppidl = &pidlItem;
  796. if (SUCCEEDED (psfFolder->GetUIObjectOf(hwnd, 1, (LPCITEMIDLIST*)ppidl, IID_IContextMenu, NULL, (void**)&pContextMenu))) // modified by RG
  797. {
  798. // try to see if we can upgrade to an IContextMenu3
  799. // or IContextMenu2 interface pointer:
  800. int level = 1;
  801. void *pCM = NULL;
  802. if (pContextMenu->QueryInterface(IID_IContextMenu3, &pCM) == NOERROR)
  803. {
  804. pContextMenu->Release();
  805. pContextMenu = (LPCONTEXTMENU)pCM;
  806. level = 3;
  807. }
  808. else if (pContextMenu->QueryInterface(IID_IContextMenu2, &pCM) == NOERROR)
  809. {
  810. pContextMenu->Release();
  811. pContextMenu = (LPCONTEXTMENU)pCM;
  812. level = 2;
  813. }
  814. hMenu = CreatePopupMenu ();
  815. if (SUCCEEDED (pContextMenu->QueryContextMenu(hMenu, 0, 1, 0x7FFF, CMF_EXPLORE)))
  816. {
  817. ClientToScreen (hwnd, &point);
  818. // install the subclassing "hook", for versions 2 or 3
  819. if (level >= 2)
  820. {
  821. g_pOldWndProc = (WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (DWORD_PTR)HookWndProc);
  822. g_pIContext2or3 = (LPCONTEXTMENU2)pContextMenu; // cast ok for ICMv3
  823. }
  824. else
  825. {
  826. g_pOldWndProc = NULL;
  827. g_pIContext2or3 = NULL;
  828. }
  829. //
  830. // Display the context menu.
  831. //
  832. nCmd = TrackPopupMenu (hMenu, TPM_LEFTALIGN |
  833. TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD,
  834. point.x, point.y, 0, hwnd, NULL);
  835. // restore old wndProc
  836. if (g_pOldWndProc)
  837. {
  838. SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)g_pOldWndProc);
  839. }
  840. //
  841. // If a command was selected from the menu, execute it.
  842. //
  843. if (nCmd >= 1 && nCmd <= 0x7fff)
  844. {
  845. ZeroMemory(&ici, sizeof(ici));
  846. ici.cbSize = sizeof (CMINVOKECOMMANDINFO);
  847. //ici.fMask = 0;
  848. ici.hwnd = hwnd;
  849. ici.lpVerb = MAKEINTRESOURCE (nCmd - 1);
  850. //ici.lpParameters = NULL;
  851. //ici.lpDirectory = NULL;
  852. ici.nShow = SW_SHOWNORMAL;
  853. //ici.dwHotKey = 0;
  854. //ici.hIcon = NULL;
  855. if (SUCCEEDED ( pContextMenu->InvokeCommand (&ici)))
  856. bResult = TRUE;
  857. }
  858. /*else if (nCmd)
  859. {
  860. PostMessage(hwnd, WM_COMMAND, nCmd, NULL); // our command
  861. }*/
  862. }
  863. DestroyMenu (hMenu);
  864. pContextMenu->Release();
  865. }
  866. }
  867. //
  868. // Clean up and return.
  869. //
  870. psfFolder->Release();
  871. pMalloc->Release();
  872. return bResult;
  873. }
  874. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  875. //
  876. // Note: a special thanks goes out to Jeff Prosise for writing & publishing
  877. // the following code!
  878. //
  879. // FUNCTION: GetItemCount
  880. //
  881. // DESCRIPTION: Computes the number of item IDs in an item ID list.
  882. //
  883. // INPUT: pidl = Pointer to an item ID list.
  884. //
  885. // RETURNS: Number of item IDs in the list.
  886. //
  887. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  888. UINT GetItemCount (LPITEMIDLIST pidl)
  889. {
  890. USHORT nLen;
  891. UINT nCount;
  892. nCount = 0;
  893. while ((nLen = pidl->mkid.cb) != 0) {
  894. pidl = GetNextItem (pidl);
  895. nCount++;
  896. }
  897. return nCount;
  898. }
  899. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  900. //
  901. // Note: a special thanks goes out to Jeff Prosise for writing & publishing
  902. // the following code!
  903. //
  904. // FUNCTION: GetNextItem
  905. //
  906. // DESCRIPTION: Finds the next item in an item ID list.
  907. //
  908. // INPUT: pidl = Pointer to an item ID list.
  909. //
  910. // RETURNS: Pointer to the next item.
  911. //
  912. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  913. LPITEMIDLIST GetNextItem (LPITEMIDLIST pidl)
  914. {
  915. USHORT nLen;
  916. if ((nLen = pidl->mkid.cb) == 0)
  917. return NULL;
  918. return (LPITEMIDLIST) (((LPBYTE) pidl) + nLen);
  919. }
  920. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  921. //
  922. // Note: a special thanks goes out to Jeff Prosise for writing & publishing
  923. // the following code!
  924. //
  925. // FUNCTION: DuplicateItem
  926. //
  927. // DESCRIPTION: Makes a copy of the next item in an item ID list.
  928. //
  929. // INPUT: pMalloc = Pointer to an IMalloc interface.
  930. // pidl = Pointer to an item ID list.
  931. //
  932. // RETURNS: Pointer to an ITEMIDLIST containing the copied item ID.
  933. //
  934. // NOTES: It is the caller's responsibility to free the memory
  935. // allocated by this function when the item ID is no longer
  936. // needed. Example:
  937. //
  938. // pidlItem = DuplicateItem (pMalloc, pidl);
  939. // .
  940. // .
  941. // .
  942. // pMalloc->lpVtbl->Free (pMalloc, pidlItem);
  943. //
  944. // Failure to free the ITEMIDLIST will result in memory
  945. // leaks.
  946. //
  947. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  948. LPITEMIDLIST DuplicateItem (LPMALLOC pMalloc, LPITEMIDLIST pidl)
  949. {
  950. USHORT nLen;
  951. LPITEMIDLIST pidlNew;
  952. nLen = pidl->mkid.cb;
  953. if (nLen == 0)
  954. return NULL;
  955. pidlNew = (LPITEMIDLIST) pMalloc->Alloc (
  956. nLen + sizeof (USHORT));
  957. if (pidlNew == NULL)
  958. return NULL;
  959. CopyMemory (pidlNew, pidl, nLen);
  960. *((USHORT*) (((LPBYTE) pidlNew) + nLen)) = 0;
  961. return pidlNew;
  962. }
  963. //----------------------------------------------------------------------
  964. // A special thanks goes out to Jeroen-bart Engelen (Yeep) for providing
  965. // his source code for getting the position & label information for all
  966. // the icons on the desktop, as found below. See his article at
  967. // http://www.digiwar.com/scripts/renderpage.php?section=2&subsection=2
  968. //----------------------------------------------------------------------
  969. void FindDesktopWindows(HWND *desktop_progman, HWND *desktopview_wnd, HWND *listview_wnd)
  970. {
  971. *desktop_progman = NULL;
  972. *desktopview_wnd = NULL;
  973. *listview_wnd = NULL;
  974. *desktop_progman = FindWindow(NULL, ("Program Manager"));
  975. if(*desktop_progman == NULL)
  976. {
  977. //MessageBox(NULL, "Unable to get the handle to the Program Manager.", "Fatal error", MB_OK|MB_ICONERROR);
  978. return;
  979. }
  980. *desktopview_wnd = FindWindowEx(*desktop_progman, NULL, "SHELLDLL_DefView", NULL);
  981. if(*desktopview_wnd == NULL)
  982. {
  983. //MessageBox(NULL, "Unable to get the handle to the desktopview.", "Fatal error", MB_OK|MB_ICONERROR);
  984. return;
  985. }
  986. // Thanks [email protected] for pointing out this works in NT 4 and not the way I did it originally.
  987. *listview_wnd = FindWindowEx(*desktopview_wnd, NULL, "SysListView32", NULL);
  988. if(*listview_wnd == NULL)
  989. {
  990. //MessageBox(NULL, "Unable to get the handle to the folderview.", "Fatal error", MB_OK|MB_ICONERROR);
  991. return;
  992. }
  993. }
  994. //----------------------------------------------------------------------
  995. int GetDesktopIconSize()
  996. {
  997. int ret = 32;
  998. // reads the key: HKEY_CURRENT_USER\Control Panel, Desktop\WindowMetrics\Shell Icon Size
  999. unsigned char buf[64];
  1000. unsigned long len = sizeof(buf);
  1001. DWORD type;
  1002. HKEY key;
  1003. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\\Desktop\\WindowMetrics", 0, KEY_READ, &key))
  1004. {
  1005. if (ERROR_SUCCESS == RegQueryValueEx(key, "Shell Icon Size", NULL, &type, (unsigned char*)buf, &len) &&
  1006. type == REG_SZ)
  1007. {
  1008. int x = _atoi_l((char*)buf, g_use_C_locale);
  1009. if (x>0 && x<=128)
  1010. ret = x;
  1011. }
  1012. RegCloseKey(key);
  1013. }
  1014. return ret;
  1015. }
  1016. //----------------------------------------------------------------------
  1017. // handy functions for populating Combo Boxes:
  1018. int SelectItemByValue(HWND ctrl, DWORD value)
  1019. {
  1020. int count = SendMessage(ctrl, CB_GETCOUNT, 0, 0);
  1021. for (int i=0; i<count; i++)
  1022. {
  1023. DWORD value_i = SendMessage( ctrl, CB_GETITEMDATA, i, 0);
  1024. if (value_i == value)
  1025. {
  1026. SendMessage( ctrl, CB_SETCURSEL, i, 0);
  1027. return i;
  1028. }
  1029. }
  1030. return -1;
  1031. }
  1032. bool ReadCBValue(HWND hwnd, DWORD ctrl_id, int* pRetValue)
  1033. {
  1034. if (!pRetValue)
  1035. return false;
  1036. HWND ctrl = GetDlgItem( hwnd, ctrl_id );
  1037. int t = SendMessage( ctrl, CB_GETCURSEL, 0, 0);
  1038. if (t == CB_ERR)
  1039. return false;
  1040. *pRetValue = (int)SendMessage( ctrl, CB_GETITEMDATA, t, 0);
  1041. return true;
  1042. }
  1043. D3DXCREATEFONTW pCreateFontW=0;
  1044. D3DXMATRIXMULTIPLY pMatrixMultiply=0;
  1045. D3DXMATRIXTRANSLATION pMatrixTranslation=0;
  1046. D3DXMATRIXSCALING pMatrixScaling=0;
  1047. D3DXMATRIXROTATION pMatrixRotationX=0, pMatrixRotationY=0, pMatrixRotationZ=0;
  1048. D3DXCREATETEXTUREFROMFILEEXW pCreateTextureFromFileExW=0;
  1049. D3DXMATRIXORTHOLH pMatrixOrthoLH = 0;
  1050. D3DXCOMPILESHADER pCompileShader=0;
  1051. D3DXMATRIXLOOKATLH pMatrixLookAtLH=0;
  1052. D3DXCREATETEXTURE pCreateTexture=0;
  1053. //----------------------------------------------------------------------
  1054. HMODULE FindD3DX9(HWND winamp)
  1055. {
  1056. HMODULE d3dx9 = (HMODULE)SendMessage(winamp,WM_WA_IPC, 0, IPC_GET_D3DX9);
  1057. if (!d3dx9 || d3dx9 == (HMODULE)1)
  1058. {
  1059. // TODO: benski> this is a quick-fix, we should call FindFirstFile() on the system directory
  1060. d3dx9=NULL;
  1061. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_36.dll");
  1062. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_35.dll");
  1063. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_34.dll");
  1064. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_33.dll");
  1065. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_32.dll");
  1066. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_31.dll");
  1067. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_30.dll");
  1068. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_29.dll");
  1069. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_28.dll");
  1070. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_27.dll");
  1071. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_26.dll");
  1072. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_25.dll");
  1073. if (!d3dx9) d3dx9 = LoadLibrary("d3dx9_24.dll");
  1074. }
  1075. if (d3dx9)
  1076. {
  1077. pCreateFontW = (D3DXCREATEFONTW) GetProcAddress(d3dx9,"D3DXCreateFontW");
  1078. pMatrixMultiply = (D3DXMATRIXMULTIPLY) GetProcAddress(d3dx9,"D3DXMatrixMultiply");
  1079. pMatrixTranslation = (D3DXMATRIXTRANSLATION)GetProcAddress(d3dx9,"D3DXMatrixTranslation");
  1080. pMatrixScaling = (D3DXMATRIXSCALING)GetProcAddress(d3dx9,"D3DXMatrixScaling");
  1081. pMatrixRotationX = (D3DXMATRIXROTATION)GetProcAddress(d3dx9,"D3DXMatrixRotationX");
  1082. pMatrixRotationY = (D3DXMATRIXROTATION)GetProcAddress(d3dx9,"D3DXMatrixRotationY");
  1083. pMatrixRotationZ = (D3DXMATRIXROTATION)GetProcAddress(d3dx9,"D3DXMatrixRotationZ");
  1084. pCreateTextureFromFileExW = (D3DXCREATETEXTUREFROMFILEEXW)GetProcAddress(d3dx9,"D3DXCreateTextureFromFileExW");
  1085. pMatrixOrthoLH = (D3DXMATRIXORTHOLH)GetProcAddress(d3dx9,"D3DXMatrixOrthoLH");
  1086. pCompileShader = (D3DXCOMPILESHADER)GetProcAddress(d3dx9,"D3DXCompileShader");
  1087. pMatrixLookAtLH = (D3DXMATRIXLOOKATLH)GetProcAddress(d3dx9,"D3DXMatrixLookAtLH");
  1088. pCreateTexture = (D3DXCREATETEXTURE)GetProcAddress(d3dx9,"D3DXCreateTexture");
  1089. }
  1090. return d3dx9;
  1091. }
  1092. LRESULT GetWinampVersion(HWND winamp)
  1093. {
  1094. static LRESULT version=0;
  1095. if (!version)
  1096. version=SendMessage(winamp,WM_WA_IPC,0,0);
  1097. return version;
  1098. }
  1099. void* GetTextResource(UINT id, int no_fallback){
  1100. void* data = 0;
  1101. HINSTANCE hinst = WASABI_API_LNG_HINST;
  1102. HRSRC rsrc = FindResource(hinst,MAKEINTRESOURCE(id),"TEXT");
  1103. if(!rsrc && !no_fallback) rsrc = FindResource((hinst = WASABI_API_ORIG_HINST),MAKEINTRESOURCE(id),"TEXT");
  1104. if(rsrc){
  1105. HGLOBAL resourceHandle = LoadResource(hinst,rsrc);
  1106. data = LockResource(resourceHandle);
  1107. }
  1108. return data;
  1109. }