1/*
2 * Copyright (c) 2005 Alexander Gottwald
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *
22 * Except as contained in this notice, the name(s) of the above copyright
23 * holders shall not be used in advertising or otherwise to promote the sale,
24 * use or other dealings in this Software without prior written authorization.
25 */
26#include "util.h"
27
28std::string win32_error::message(DWORD errorcode)
29{
30    LPVOID lpMsgBuf;
31    if (!FormatMessage(
32                FORMAT_MESSAGE_ALLOCATE_BUFFER |
33                FORMAT_MESSAGE_FROM_SYSTEM |
34                FORMAT_MESSAGE_IGNORE_INSERTS,
35                NULL,
36                errorcode,
37                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
38                (LPTSTR) &lpMsgBuf,
39                0,
40                NULL ))
41    {
42        return "Unknown error in FormatMessage";
43    }
44
45    std::string ret((LPCTSTR)lpMsgBuf);
46    LocalFree( lpMsgBuf );
47    return ret;
48}
49
50void MessageDebug::debug(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, const char *prefix)
51{
52#ifdef _DEBUG
53    static const char *psn_notify[] = {
54        "PSN_SETACTIVE",
55        "PSN_KILLACTIVE",
56        "PSN_APPLY",
57        "PSN_RESET",
58        NULL,
59        "PSN_HELP",
60        "PSN_WIZBACK",
61        "PSN_WIZNEXT",
62        "PSN_WIZFINISH",
63        "PSN_QUERYCANCEL" };
64    if (uMsg == WM_NOTIFY)
65    {
66        LPNMHDR pnmh = (LPNMHDR)lParam;
67        int psn_index = -(int)pnmh->code - 200;
68        if (psn_index >= 0 && psn_index < 10 && psn_notify[psn_index])
69            printf("%s: %08x %04x WM_NOTIFY (%s)\n", prefix, hwnd, wParam, psn_notify[psn_index]);
70        else if (pnmh->code < NOTIFY_NAMES_LEN && notify_names[pnmh->code])
71            printf("%s: %08x %04x WM_NOTIFY (%s)\n", prefix, hwnd, wParam, notify_names[pnmh->code]);
72        else
73            printf("%s: %08x %04x WM_NOTIFY (%u)\n", prefix, hwnd, wParam, pnmh->code);
74    }
75    else if (uMsg >= MESSAGE_NAMES_LEN)
76        if (uMsg >= WM_USER)
77            printf("%s: %08x %04x %08x WM_USER + %d\n", prefix, hwnd, wParam, lParam, uMsg - WM_USER);
78        else
79            printf("%s: %08x %04x %08x %d\n", prefix, hwnd, wParam, lParam, uMsg);
80    else if (uMsg >= 0 && uMsg < MESSAGE_NAMES_LEN && message_names[uMsg])
81        printf("%s: %08x %04x %08x %s\n", prefix, hwnd, wParam, lParam, message_names[uMsg]);
82#endif
83}
84
85
86const char * MessageDebug::message_names[MESSAGE_NAMES_LEN] = {
87	"WM_NULL",
88	"WM_CREATE",
89	"WM_DESTROY",
90	"WM_MOVE",
91	"4",
92	"WM_SIZE",
93	"WM_ACTIVATE",
94	"WM_SETFOCUS",
95	"WM_KILLFOCUS",
96	"9",
97	"WM_ENABLE",
98	"WM_SETREDRAW",
99	"WM_SETTEXT",
100	"WM_GETTEXT",
101	"WM_GETTEXTLENGTH",
102	"WM_PAINT",
103	"WM_CLOSE",
104	"WM_QUERYENDSESSION",
105	"WM_QUIT",
106	"WM_QUERYOPEN",
107	"WM_ERASEBKGND",
108	"WM_SYSCOLORCHANGE",
109	"WM_ENDSESSION",
110	"23",
111	"WM_SHOWWINDOW",
112	"25",
113	"WM_WININICHANGE",
114	"WM_DEVMODECHANGE",
115	"WM_ACTIVATEAPP",
116	"WM_FONTCHANGE",
117	"WM_TIMECHANGE",
118	"WM_CANCELMODE",
119	NULL /* WM_SETCURSOR */,
120	"WM_MOUSEACTIVATE",
121	"WM_CHILDACTIVATE",
122	"WM_QUEUESYNC",
123	"WM_GETMINMAXINFO",
124	"37",
125	"WM_PAINTICON",
126	"WM_ICONERASEBKGND",
127	"WM_NEXTDLGCTL",
128	"41",
129	"WM_SPOOLERSTATUS",
130	"WM_DRAWITEM",
131	"WM_MEASUREITEM",
132	"WM_DELETEITEM",
133	"WM_VKEYTOITEM",
134	"WM_CHARTOITEM",
135	"WM_SETFONT",
136	"WM_GETFONT",
137	"WM_SETHOTKEY",
138	"WM_GETHOTKEY",
139	"52",
140	"53",
141	"54",
142	"WM_QUERYDRAGICON",
143	"56",
144	"WM_COMPAREITEM",
145	"58",
146	"59",
147	"60",
148	"61",
149	"62",
150	"63",
151	"64",
152	"WM_COMPACTING",
153	"66",
154	"67",
155	"WM_COMMNOTIFY",
156	"69",
157	"WM_WINDOWPOSCHANGING",
158	"WM_WINDOWPOSCHANGED",
159	"WM_POWER",
160	"73",
161	"WM_COPYDATA",
162	"WM_CANCELJOURNAL",
163	"76",
164	"77",
165	"WM_NOTIFY",
166	"79",
167	"WM_INPUTLANGCHANGEREQUEST",
168	"WM_INPUTLANGCHANGE",
169	"WM_TCARD",
170	"WM_HELP",
171	"WM_USERCHANGED",
172	"WM_NOTIFYFORMAT",
173	"86",
174	"87",
175	"88",
176	"89",
177	"90",
178	"91",
179	"92",
180	"93",
181	"94",
182	"95",
183	"96",
184	"97",
185	"98",
186	"99",
187	"100",
188	"101",
189	"102",
190	"103",
191	"104",
192	"105",
193	"106",
194	"107",
195	"108",
196	"109",
197	"110",
198	"111",
199	"112",
200	"113",
201	"114",
202	"115",
203	"116",
204	"117",
205	"118",
206	"119",
207	"120",
208	"121",
209	"122",
210	"WM_CONTEXTMENU",
211	"WM_STYLECHANGING",
212	"WM_STYLECHANGED",
213	"WM_DISPLAYCHANGE",
214	"WM_GETICON",
215	"WM_SETICON",
216	"WM_NCCREATE",
217	"WM_NCDESTROY",
218	"WM_NCCALCSIZE",
219	NULL /* WM_NCHITTEST */,
220	"WM_NCPAINT",
221	"WM_NCACTIVATE",
222	"WM_GETDLGCODE",
223	"WM_SYNCPAINT",
224	"137",
225	"138",
226	"139",
227	"140",
228	"141",
229	"142",
230	"143",
231	"144",
232	"145",
233	"146",
234	"147",
235	"148",
236	"149",
237	"150",
238	"151",
239	"152",
240	"153",
241	"154",
242	"155",
243	"156",
244	"157",
245	"158",
246	"159",
247	NULL /* WM_NCMOUSEMOVE */,
248	"WM_NCLBUTTONDOWN",
249	"WM_NCLBUTTONUP",
250	"WM_NCLBUTTONDBLCLK",
251	"WM_NCRBUTTONDOWN",
252	"WM_NCRBUTTONUP",
253	"WM_NCRBUTTONDBLCLK",
254	"WM_NCMBUTTONDOWN",
255	"WM_NCMBUTTONUP",
256	"WM_NCMBUTTONDBLCLK",
257	"170",
258	"171",
259	"172",
260	"173",
261	"174",
262	"175",
263	"176",
264	"177",
265	"178",
266	"179",
267	"180",
268	"181",
269	"182",
270	"183",
271	"184",
272	"185",
273	"186",
274	"187",
275	"188",
276	"189",
277	"190",
278	"191",
279	"192",
280	"193",
281	"194",
282	"195",
283	"196",
284	"197",
285	"198",
286	"199",
287	"200",
288	"201",
289	"202",
290	"203",
291	"204",
292	"205",
293	"206",
294	"207",
295	"208",
296	"209",
297	"210",
298	"211",
299	"212",
300	"213",
301	"214",
302	"215",
303	"216",
304	"217",
305	"218",
306	"219",
307	"220",
308	"221",
309	"222",
310	"223",
311	"224",
312	"225",
313	"226",
314	"227",
315	"228",
316	"229",
317	"230",
318	"231",
319	"232",
320	"233",
321	"234",
322	"235",
323	"236",
324	"237",
325	"238",
326	"239",
327	"240",
328	"241",
329	"242",
330	"243",
331	"244",
332	"245",
333	"246",
334	"247",
335	"248",
336	"249",
337	"250",
338	"251",
339	"252",
340	"253",
341	"254",
342	"255",
343	"WM_KEYDOWN",
344	"WM_KEYUP",
345	"WM_CHAR",
346	"WM_DEADCHAR",
347	"WM_SYSKEYDOWN",
348	"WM_SYSKEYUP",
349	"WM_SYSCHAR",
350	"WM_SYSDEADCHAR",
351	"WM_CONVERTREQUESTEX",
352	"265",
353	"266",
354	"267",
355	"268",
356	"WM_IME_STARTCOMPOSITION",
357	"WM_IME_ENDCOMPOSITION",
358	"WM_IME_KEYLAST",
359	"WM_INITDIALOG",
360	"WM_COMMAND",
361	"WM_SYSCOMMAND",
362	NULL /* WM_TIMER */,
363	"WM_HSCROLL",
364	"WM_VSCROLL",
365	"WM_INITMENU",
366	"WM_INITMENUPOPUP",
367	"280",
368	"281",
369	"282",
370	"283",
371	"284",
372	"285",
373	"286",
374	"WM_MENUSELECT",
375	"WM_MENUCHAR",
376	"WM_ENTERIDLE",
377	"290",
378	"291",
379	"292",
380	"293",
381	"294",
382	"295",
383	"296",
384	"297",
385	"298",
386	"299",
387	"300",
388	"301",
389	"302",
390	"303",
391	"304",
392	"305",
393	"WM_CTLCOLORMSGBOX",
394	"WM_CTLCOLOREDIT",
395	"WM_CTLCOLORLISTBOX",
396	"WM_CTLCOLORBTN",
397	"WM_CTLCOLORDLG",
398	"WM_CTLCOLORSCROLLBAR",
399	"WM_CTLCOLORSTATIC",
400	"313",
401	"314",
402	"315",
403	"316",
404	"317",
405	"318",
406	"319",
407	"320",
408	"321",
409	"322",
410	"323",
411	"324",
412	"325",
413	"326",
414	"327",
415	"328",
416	"329",
417	"330",
418	"331",
419	"332",
420	"333",
421	"334",
422	"335",
423	"336",
424	"337",
425	"338",
426	"339",
427	"340",
428	"341",
429	"342",
430	"343",
431	"344",
432	"345",
433	"346",
434	"347",
435	"348",
436	"349",
437	"350",
438	"351",
439	"352",
440	"353",
441	"354",
442	"355",
443	"356",
444	"357",
445	"358",
446	"359",
447	"360",
448	"361",
449	"362",
450	"363",
451	"364",
452	"365",
453	"366",
454	"367",
455	"368",
456	"369",
457	"370",
458	"371",
459	"372",
460	"373",
461	"374",
462	"375",
463	"376",
464	"377",
465	"378",
466	"379",
467	"380",
468	"381",
469	"382",
470	"383",
471	"384",
472	"385",
473	"386",
474	"387",
475	"388",
476	"389",
477	"390",
478	"391",
479	"392",
480	"393",
481	"394",
482	"395",
483	"396",
484	"397",
485	"398",
486	"399",
487	"400",
488	"401",
489	"402",
490	"403",
491	"404",
492	"405",
493	"406",
494	"407",
495	"408",
496	"409",
497	"410",
498	"411",
499	"412",
500	"413",
501	"414",
502	"415",
503	"416",
504	"417",
505	"418",
506	"419",
507	"420",
508	"421",
509	"422",
510	"423",
511	"424",
512	"425",
513	"426",
514	"427",
515	"428",
516	"429",
517	"430",
518	"431",
519	"432",
520	"433",
521	"434",
522	"435",
523	"436",
524	"437",
525	"438",
526	"439",
527	"440",
528	"441",
529	"442",
530	"443",
531	"444",
532	"445",
533	"446",
534	"447",
535	"448",
536	"449",
537	"450",
538	"451",
539	"452",
540	"453",
541	"454",
542	"455",
543	"456",
544	"457",
545	"458",
546	"459",
547	"460",
548	"461",
549	"462",
550	"463",
551	"464",
552	"465",
553	"466",
554	"467",
555	"468",
556	"469",
557	"470",
558	"471",
559	"472",
560	"473",
561	"474",
562	"475",
563	"476",
564	"477",
565	"478",
566	"479",
567	"480",
568	"481",
569	"482",
570	"483",
571	"484",
572	"485",
573	"486",
574	"487",
575	"488",
576	"489",
577	"490",
578	"491",
579	"492",
580	"493",
581	"494",
582	"495",
583	"496",
584	"497",
585	"498",
586	"499",
587	"500",
588	"501",
589	"502",
590	"503",
591	"504",
592	"505",
593	"506",
594	"507",
595	"508",
596	"509",
597	"510",
598	"511",
599	NULL /* WM_MOUSEMOVE */,
600	"WM_LBUTTONDOWN",
601	"WM_LBUTTONUP",
602	"WM_LBUTTONDBLCLK",
603	"WM_RBUTTONDOWN",
604	"WM_RBUTTONUP",
605	"WM_RBUTTONDBLCLK",
606	"WM_MBUTTONDOWN",
607	"WM_MBUTTONUP",
608	"WM_MBUTTONDBLCLK",
609	"WM_MOUSEWHEEL",
610	"WM_XBUTTONDOWN",
611	"WM_XBUTTONUP",
612	"WM_XBUTTONDBLCLK",
613	"526",
614	"527",
615	"WM_PARENTNOTIFY",
616	"WM_ENTERMENULOOP",
617	"WM_EXITMENULOOP",
618	"WM_NEXTMENU",
619	"WM_SIZING",
620	"WM_CAPTURECHANGED",
621	"WM_MOVING",
622	"535",
623	"WM_POWERBROADCAST",
624	"WM_DEVICECHANGE",
625	"538",
626	"539",
627	"540",
628	"541",
629	"542",
630	"543",
631	"WM_MDICREATE",
632	"WM_MDIDESTROY",
633	"WM_MDIACTIVATE",
634	"WM_MDIRESTORE",
635	"WM_MDINEXT",
636	"WM_MDIMAXIMIZE",
637	"WM_MDITILE",
638	"WM_MDICASCADE",
639	"WM_MDIICONARRANGE",
640	"WM_MDIGETACTIVE",
641	"554",
642	"555",
643	"556",
644	"557",
645	"558",
646	"559",
647	"WM_MDISETMENU",
648	"WM_ENTERSIZEMOVE",
649	"WM_EXITSIZEMOVE",
650	"WM_DROPFILES",
651	"WM_MDIREFRESHMENU",
652	"565",
653	"566",
654	"567",
655	"568",
656	"569",
657	"570",
658	"571",
659	"572",
660	"573",
661	"574",
662	"575",
663	"576",
664	"577",
665	"578",
666	"579",
667	"580",
668	"581",
669	"582",
670	"583",
671	"584",
672	"585",
673	"586",
674	"587",
675	"588",
676	"589",
677	"590",
678	"591",
679	"592",
680	"593",
681	"594",
682	"595",
683	"596",
684	"597",
685	"598",
686	"599",
687	"600",
688	"601",
689	"602",
690	"603",
691	"604",
692	"605",
693	"606",
694	"607",
695	"608",
696	"609",
697	"610",
698	"611",
699	"612",
700	"613",
701	"614",
702	"615",
703	"616",
704	"617",
705	"618",
706	"619",
707	"620",
708	"621",
709	"622",
710	"623",
711	"624",
712	"625",
713	"626",
714	"627",
715	"628",
716	"629",
717	"630",
718	"631",
719	"632",
720	"633",
721	"634",
722	"635",
723	"636",
724	"637",
725	"638",
726	"639",
727	"640",
728	"WM_IME_SETCONTEXT",
729	"WM_IME_NOTIFY",
730	"WM_IME_CONTROL",
731	"WM_IME_COMPOSITIONFULL",
732	"WM_IME_SELECT",
733	"WM_IME_CHAR",
734	"647",
735	"648",
736	"649",
737	"650",
738	"651",
739	"652",
740	"653",
741	"654",
742	"655",
743	"WM_IME_KEYDOWN",
744	"WM_IME_KEYUP",
745	"658",
746	"659",
747	"660",
748	"661",
749	"662",
750	"663",
751	"664",
752	"665",
753	"666",
754	"667",
755	"668",
756	"669",
757	"670",
758	"671",
759	"672",
760	"WM_MOUSEHOVER",
761	"674",
762	"WM_MOUSELEAVE",
763	"676",
764	"677",
765	"678",
766	"679",
767	"680",
768	"681",
769	"682",
770	"683",
771	"684",
772	"685",
773	"686",
774	"687",
775	"688",
776	"689",
777	"690",
778	"691",
779	"692",
780	"693",
781	"694",
782	"695",
783	"696",
784	"697",
785	"698",
786	"699",
787	"700",
788	"701",
789	"702",
790	"703",
791	"704",
792	"705",
793	"706",
794	"707",
795	"708",
796	"709",
797	"710",
798	"711",
799	"712",
800	"713",
801	"714",
802	"715",
803	"716",
804	"717",
805	"718",
806	"719",
807	"720",
808	"721",
809	"722",
810	"723",
811	"724",
812	"725",
813	"726",
814	"727",
815	"728",
816	"729",
817	"730",
818	"731",
819	"732",
820	"733",
821	"734",
822	"735",
823	"736",
824	"737",
825	"738",
826	"739",
827	"740",
828	"741",
829	"742",
830	"743",
831	"744",
832	"745",
833	"746",
834	"747",
835	"748",
836	"749",
837	"750",
838	"751",
839	"752",
840	"753",
841	"754",
842	"755",
843	"756",
844	"757",
845	"758",
846	"759",
847	"760",
848	"761",
849	"762",
850	"763",
851	"764",
852	"765",
853	"766",
854	"767",
855	"WM_CUT",
856	"WM_COPY",
857	"WM_PASTE",
858	"WM_CLEAR",
859	"WM_UNDO",
860	"WM_RENDERFORMAT",
861	"WM_RENDERALLFORMATS",
862	"WM_DESTROYCLIPBOARD",
863	"WM_DRAWCLIPBOARD",
864	"WM_PAINTCLIPBOARD",
865	"WM_VSCROLLCLIPBOARD",
866	"WM_SIZECLIPBOARD",
867	"WM_ASKCBFORMATNAME",
868	"WM_CHANGECBCHAIN",
869	"WM_HSCROLLCLIPBOARD",
870	"WM_QUERYNEWPALETTE",
871	"WM_PALETTEISCHANGING",
872	"WM_PALETTECHANGED",
873	"WM_HOTKEY",
874	"787",
875	"788",
876	"789",
877	"790",
878	"WM_PRINT",
879	"WM_PRINTCLIENT",
880	"793",
881	"794",
882	"795",
883	"796",
884	"797",
885	"798",
886	"799",
887	"800",
888	"801",
889	"802",
890	"803",
891	"804",
892	"805",
893	"806",
894	"807",
895	"808",
896	"809",
897	"810",
898	"811",
899	"812",
900	"813",
901	"814",
902	"815",
903	"816",
904	"817",
905	"818",
906	"819",
907	"820",
908	"821",
909	"822",
910	"823",
911	"824",
912	"825",
913	"826",
914	"827",
915	"828",
916	"829",
917	"830",
918	"831",
919	"832",
920	"833",
921	"834",
922	"835",
923	"836",
924	"837",
925	"838",
926	"839",
927	"840",
928	"841",
929	"842",
930	"843",
931	"844",
932	"845",
933	"846",
934	"847",
935	"848",
936	"849",
937	"850",
938	"851",
939	"852",
940	"853",
941	"854",
942	"855",
943	"856",
944	"857",
945	"858",
946	"859",
947	"860",
948	"861",
949	"862",
950	"863",
951	"864",
952	"865",
953	"866",
954	"867",
955	"868",
956	"869",
957	"870",
958	"871",
959	"872",
960	"873",
961	"874",
962	"875",
963	"876",
964	"877",
965	"878",
966	"879",
967	"880",
968	"881",
969	"882",
970	"883",
971	"884",
972	"885",
973	"886",
974	"887",
975	"888",
976	"889",
977	"890",
978	"891",
979	"892",
980	"893",
981	"894",
982	"895",
983	"896",
984	"897",
985	"898",
986	"899",
987	"900",
988	"901",
989	"902",
990	"903",
991	"904",
992	"905",
993	"906",
994	"907",
995	"908",
996	"909",
997	"910",
998	"911",
999	"912",
1000	"913",
1001	"914",
1002	"915",
1003	"916",
1004	"917",
1005	"918",
1006	"919",
1007	"920",
1008	"921",
1009	"922",
1010	"923",
1011	"924",
1012	"925",
1013	"926",
1014	"927",
1015	"928",
1016	"929",
1017	"930",
1018	"931",
1019	"932",
1020	"933",
1021	"934",
1022	"935",
1023	"936",
1024	"937",
1025	"938",
1026	"939",
1027	"940",
1028	"941",
1029	"942",
1030	"943",
1031	"944",
1032	"945",
1033	"946",
1034	"947",
1035	"948",
1036	"949",
1037	"950",
1038	"951",
1039	"952",
1040	"953",
1041	"954",
1042	"955",
1043	"956",
1044	"957",
1045	"958",
1046	"959",
1047	"960",
1048	"961",
1049	"962",
1050	"963",
1051	"964",
1052	"965",
1053	"966",
1054	"967",
1055	"968",
1056	"969",
1057	"970",
1058	"971",
1059	"972",
1060	"973",
1061	"974",
1062	"975",
1063	"976",
1064	"977",
1065	"978",
1066	"979",
1067	"980",
1068	"981",
1069	"982",
1070	"983",
1071	"984",
1072	"985",
1073	"986",
1074	"987",
1075	"988",
1076	"989",
1077	"990",
1078	"991",
1079	"992",
1080	"993",
1081	"994",
1082	"995",
1083	"996",
1084	"997",
1085	"998",
1086	"999",
1087	"1000",
1088	"1001",
1089	"1002",
1090	"1003",
1091	"1004",
1092	"1005",
1093	"1006",
1094	"1007",
1095	"1008",
1096	"1009",
1097	"1010",
1098	"1011",
1099	"1012",
1100	"1013",
1101	"1014",
1102	"1015",
1103	"1016",
1104	"1017",
1105	"1018",
1106	"1019",
1107	"1020",
1108	"1021",
1109	"1022",
1110	"1023"
1111};
1112
1113