Added more error codes to dxerr8 and dxerr9 which were extracted from
[wine] / dlls / dxerr8 / dxerr8.c
1 /*
2  * DirectX 8 error routines
3  *
4  * Copyright 2004-2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COM_NO_WINDOWS_H
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winnls.h"
30
31 #include "mmsystem.h"
32 #include "dsound.h"
33 #include "dmerror.h"
34 #include "ddraw.h"
35 #include "dinput.h"
36 #include "vfwmsgs.h"
37
38 #include "dxerr8.h"
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(dxerr);
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 DXGetErrorString8A(HRESULT hr)
55 {
56     unsigned int i, j, k = 0;
57     TRACE("(0x%08lx)\n", hr);
58
59     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
60         j = k + (i / 2);
61         if (hr == info[j].hr)
62             return info[j].resultA;
63         if ((unsigned int)hr > (unsigned int)info[j].hr) {
64             k = j + 1;
65             i--;
66         }
67     }
68
69     return "Unknown";
70 }
71
72 const WCHAR * WINAPI DXGetErrorString8W(HRESULT hr)
73 {
74     static const WCHAR unknown[] = { 'U', 'n', 'k', 'n', 'o', 'w', 'n', 0 };
75     unsigned int i, j, k = 0;
76     TRACE("(0x%08lx)\n", hr);
77
78     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
79         j = k + (i / 2);
80         if (hr == info[j].hr)
81             return info[j].resultW;
82         if ((unsigned int)hr > (unsigned int)info[j].hr) {
83             k = j + 1;
84             i--;
85         }
86     }
87
88     return unknown;
89 }
90
91 const char * WINAPI DXGetErrorDescription8A(HRESULT hr)
92 {
93     unsigned int i, j, k = 0;
94     TRACE("(0x%08lx)\n", hr);
95
96     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
97         j = k + (i / 2);
98         if (hr == info[j].hr)
99             return info[j].descriptionA;
100         if ((unsigned int)hr > (unsigned int)info[j].hr) {
101             k = j + 1;
102             i--;
103         }
104     }
105
106     return "n/a";
107 }
108
109 const WCHAR * WINAPI DXGetErrorDescription8W(HRESULT hr)
110 {
111     static const WCHAR na[] = { 'n', '/', 'a', 0 };
112     unsigned int i, j, k = 0;
113     TRACE("(0x%08lx)\n", hr);
114
115     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
116         j = k + (i / 2);
117         if (hr == info[j].hr)
118             return info[j].descriptionW;
119         if ((unsigned int)hr > (unsigned int)info[j].hr) {
120             k = j + 1;
121             i--;
122         }
123     }
124
125     return na;
126 }
127
128 HRESULT WINAPI DXTraceA(const char* strFile, DWORD dwLine, HRESULT hr, const char*  strMsg, BOOL bPopMsgBox)
129 {
130     char msg[1024];
131     TRACE("(%p,%ld,0x%08lx,%p,%d)\n", strFile, dwLine, hr, strMsg, bPopMsgBox);
132
133     if (bPopMsgBox) {
134         snprintf(msg, sizeof(msg), "File: %s\nLine: %ld\nError Code: %s (0x%08lx)\nCalling: %s",
135             strFile, dwLine, DXGetErrorString8A(hr), hr, strMsg);
136         MessageBoxA(0, msg, "Unexpected error encountered", MB_OK|MB_ICONERROR);
137     } else {
138         snprintf(msg, sizeof(msg), "%s(%ld): %s (hr=%s (0x%08lx))", strFile,
139             dwLine, strMsg, DXGetErrorString8A(hr), hr);
140         OutputDebugStringA(msg);
141     }
142
143     return hr;
144 }
145
146 HRESULT WINAPI DXTraceW(const char* strFile, DWORD dwLine, HRESULT hr, const WCHAR* strMsg, BOOL bPopMsgBox)
147 {
148     WCHAR msg[1024];
149     TRACE("(%p,%ld,0x%08lx,%p,%d)\n", strFile, dwLine, hr, strMsg, bPopMsgBox);
150
151     if (bPopMsgBox) {
152         static const WCHAR format[] = { 'F','i','l','e',':',' ','%','s','\\','n','L','i','n',
153             'e',':',' ','%','l','d','\\','n','E','r','r','o','r',' ','C','o',
154             'd','e',':',' ','%','s',' ','(','0','x','%','0','8','l','x',')',
155             '\\','n','C','a','l','l','i','n','g',':',' ','%','s',0 };
156         static const WCHAR caption[] = { 'U','n','e','x','p','e','c','t','e','d',' ','e','r',
157             'r','o','r',' ','e','n','c','o','u','n','t','e','r','e','d',0 };
158         /* FIXME: should use wsnprintf */
159         wsprintfW(msg, format, strFile, dwLine,
160                   DXGetErrorString8W(hr), hr, strMsg);
161         MessageBoxW(0, msg, caption, MB_OK|MB_ICONERROR);
162     } else {
163         static const WCHAR format[] = { '%','s','(','%','l','d',')',':',' ','%','s',' ','(',
164             'h','r','=','%','s',' ','(','0','x','%','0','8','l','x',')',')',' ',
165             0 };
166         /* FIXME: should use wsnprintf */
167         wsprintfW(msg, format, strFile, dwLine, strMsg,
168                   DXGetErrorString8W(hr), hr);
169         OutputDebugStringW(msg);
170     }
171
172     return hr;
173 }