msvcrt: Implement _seh_longjmp_unwind4 for i386.
[wine] / dlls / dxerr9 / dxerr9.c
1 /*
2  * DirectX 9 error routines
3  *
4  * Copyright 2004 Robert Reif
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30
31 #include "mmsystem.h"
32 #include "dmerror.h"
33 #include "ddraw.h"
34 #include "dinput.h"
35 #include "vfwmsgs.h"
36 #include "mmstream.h"
37 #include "dplay8.h"
38 #include "dxfile.h"
39 #include "d3d9.h"
40 #include "dsound.h"
41
42 #include "dxerr9.h"
43
44 typedef struct {
45     HRESULT      hr;
46     const CHAR*  resultA;
47     const WCHAR* resultW;
48     const CHAR*  descriptionA;
49     const WCHAR* descriptionW;
50 } error_info;
51
52 #include "errors.h"
53
54 const char * WINAPI DXGetErrorString9A(HRESULT hr)
55 {
56     unsigned int i, j, k = 0;
57
58     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
59         j = k + (i / 2);
60         if (hr == info[j].hr)
61             return info[j].resultA;
62         if ((unsigned int)hr > (unsigned int)info[j].hr) {
63             k = j + 1;
64             i--;
65         }
66     }
67
68     return "Unknown";
69 }
70
71 const WCHAR * WINAPI DXGetErrorString9W(HRESULT hr)
72 {
73     static const WCHAR unknown[] = { 'U', 'n', 'k', 'n', 'o', 'w', 'n', 0 };
74     unsigned int i, j, k = 0;
75
76     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
77         j = k + (i / 2);
78         if (hr == info[j].hr)
79             return info[j].resultW;
80         if ((unsigned int)hr > (unsigned int)info[j].hr) {
81             k = j + 1;
82             i--;
83         }
84     }
85
86     return unknown;
87 }
88
89 const char * WINAPI DXGetErrorDescription9A(HRESULT hr)
90 {
91     unsigned int i, j, k = 0;
92
93     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
94         j = k + (i / 2);
95         if (hr == info[j].hr)
96             return info[j].descriptionA;
97         if ((unsigned int)hr > (unsigned int)info[j].hr) {
98             k = j + 1;
99             i--;
100         }
101     }
102
103     return "n/a";
104 }
105
106 const WCHAR * WINAPI DXGetErrorDescription9W(HRESULT hr)
107 {
108     static const WCHAR na[] = { 'n', '/', 'a', 0 };
109     unsigned int i, j, k = 0;
110
111     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
112         j = k + (i / 2);
113         if (hr == info[j].hr)
114             return info[j].descriptionW;
115         if ((unsigned int)hr > (unsigned int)info[j].hr) {
116             k = j + 1;
117             i--;
118         }
119     }
120
121     return na;
122 }
123
124 HRESULT WINAPI DXTraceA(const char* strFile, DWORD dwLine, HRESULT hr, const char*  strMsg, BOOL bPopMsgBox)
125 {
126     char msg[1024];
127
128     if (bPopMsgBox) {
129         snprintf(msg, sizeof(msg), "File: %s\nLine: %d\nError Code: %s (0x%08x)\nCalling: %s",
130             strFile, dwLine, DXGetErrorString9A(hr), hr, strMsg);
131         MessageBoxA(0, msg, "Unexpected error encountered", MB_OK|MB_ICONERROR);
132     } else {
133         snprintf(msg, sizeof(msg), "%s(%d): %s (hr=%s (0x%08x))", strFile,
134             dwLine, strMsg, DXGetErrorString9A(hr), hr);
135         OutputDebugStringA(msg);
136     }
137
138     return hr;
139 }
140
141 HRESULT WINAPI DXTraceW(const char* strFile, DWORD dwLine, HRESULT hr, const WCHAR* strMsg, BOOL bPopMsgBox)
142 {
143     WCHAR msg[1024];
144
145     if (bPopMsgBox) {
146         static const WCHAR format[] = { 'F','i','l','e',':',' ','%','s','\\','n','L','i','n',
147             'e',':',' ','%','l','d','\\','n','E','r','r','o','r',' ','C','o',
148             'd','e',':',' ','%','s',' ','(','0','x','%','0','8','l','x',')',
149             '\\','n','C','a','l','l','i','n','g',':',' ','%','s',0 };
150         static const WCHAR caption[] = { 'U','n','e','x','p','e','c','t','e','d',' ','e','r',
151             'r','o','r',' ','e','n','c','o','u','n','t','e','r','e','d',0 };
152         /* FIXME: should use wsnprintf */
153         wsprintfW(msg, format, strFile, dwLine,
154                   DXGetErrorString9W(hr), hr, strMsg);
155         MessageBoxW(0, msg, caption, MB_OK|MB_ICONERROR);
156     } else {
157         static const WCHAR format[] = { '%','s','(','%','l','d',')',':',' ','%','s',' ','(',
158             'h','r','=','%','s',' ','(','0','x','%','0','8','l','x',')',')',' ',
159             0 };
160         /* FIXME: should use wsnprintf */
161         wsprintfW(msg, format, strFile, dwLine, strMsg,
162                   DXGetErrorString9W(hr), hr);
163         OutputDebugStringW(msg);
164     }
165
166     return hr;
167 }