ole32: Remove unnecessary assert(This) calls.
[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 <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 "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 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(dxerr);
47
48 typedef struct {
49     HRESULT      hr;
50     const CHAR*  resultA;
51     const WCHAR* resultW;
52     const CHAR*  descriptionA;
53     const WCHAR* descriptionW;
54 } error_info;
55
56 #include "errors.h"
57
58 const char * WINAPI DXGetErrorString9A(HRESULT hr)
59 {
60     unsigned int i, j, k = 0;
61     TRACE("(0x%08lx)\n", hr);
62
63     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
64         j = k + (i / 2);
65         if (hr == info[j].hr)
66             return info[j].resultA;
67         if ((unsigned int)hr > (unsigned int)info[j].hr) {
68             k = j + 1;
69             i--;
70         }
71     }
72
73     return "Unknown";
74 }
75
76 const WCHAR * WINAPI DXGetErrorString9W(HRESULT hr)
77 {
78     static const WCHAR unknown[] = { 'U', 'n', 'k', 'n', 'o', 'w', 'n', 0 };
79     unsigned int i, j, k = 0;
80     TRACE("(0x%08lx)\n", hr);
81
82     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
83         j = k + (i / 2);
84         if (hr == info[j].hr)
85             return info[j].resultW;
86         if ((unsigned int)hr > (unsigned int)info[j].hr) {
87             k = j + 1;
88             i--;
89         }
90     }
91
92     return unknown;
93 }
94
95 const char * WINAPI DXGetErrorDescription9A(HRESULT hr)
96 {
97     unsigned int i, j, k = 0;
98     TRACE("(0x%08lx)\n", hr);
99
100     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
101         j = k + (i / 2);
102         if (hr == info[j].hr)
103             return info[j].descriptionA;
104         if ((unsigned int)hr > (unsigned int)info[j].hr) {
105             k = j + 1;
106             i--;
107         }
108     }
109
110     return "n/a";
111 }
112
113 const WCHAR * WINAPI DXGetErrorDescription9W(HRESULT hr)
114 {
115     static const WCHAR na[] = { 'n', '/', 'a', 0 };
116     unsigned int i, j, k = 0;
117     TRACE("(0x%08lx)\n", hr);
118
119     for (i = sizeof(info)/sizeof(info[0]); i != 0; i /= 2) {
120         j = k + (i / 2);
121         if (hr == info[j].hr)
122             return info[j].descriptionW;
123         if ((unsigned int)hr > (unsigned int)info[j].hr) {
124             k = j + 1;
125             i--;
126         }
127     }
128
129     return na;
130 }
131
132 HRESULT WINAPI DXTraceA(const char* strFile, DWORD dwLine, HRESULT hr, const char*  strMsg, BOOL bPopMsgBox)
133 {
134     char msg[1024];
135     TRACE("(%p,%ld,0x%08lx,%p,%d)\n", strFile, dwLine, hr, strMsg, bPopMsgBox);
136
137     if (bPopMsgBox) {
138         snprintf(msg, sizeof(msg), "File: %s\nLine: %ld\nError Code: %s (0x%08lx)\nCalling: %s",
139             strFile, dwLine, DXGetErrorString9A(hr), hr, strMsg);
140         MessageBoxA(0, msg, "Unexpected error encountered", MB_OK|MB_ICONERROR);
141     } else {
142         snprintf(msg, sizeof(msg), "%s(%ld): %s (hr=%s (0x%08lx))", strFile,
143             dwLine, strMsg, DXGetErrorString9A(hr), hr);
144         OutputDebugStringA(msg);
145     }
146
147     return hr;
148 }
149
150 HRESULT WINAPI DXTraceW(const char* strFile, DWORD dwLine, HRESULT hr, const WCHAR* strMsg, BOOL bPopMsgBox)
151 {
152     WCHAR msg[1024];
153     TRACE("(%p,%ld,0x%08lx,%p,%d)\n", strFile, dwLine, hr, strMsg, bPopMsgBox);
154
155     if (bPopMsgBox) {
156         static const WCHAR format[] = { 'F','i','l','e',':',' ','%','s','\\','n','L','i','n',
157             'e',':',' ','%','l','d','\\','n','E','r','r','o','r',' ','C','o',
158             'd','e',':',' ','%','s',' ','(','0','x','%','0','8','l','x',')',
159             '\\','n','C','a','l','l','i','n','g',':',' ','%','s',0 };
160         static const WCHAR caption[] = { 'U','n','e','x','p','e','c','t','e','d',' ','e','r',
161             'r','o','r',' ','e','n','c','o','u','n','t','e','r','e','d',0 };
162         /* FIXME: should use wsnprintf */
163         wsprintfW(msg, format, strFile, dwLine,
164                   DXGetErrorString9W(hr), hr, strMsg);
165         MessageBoxW(0, msg, caption, MB_OK|MB_ICONERROR);
166     } else {
167         static const WCHAR format[] = { '%','s','(','%','l','d',')',':',' ','%','s',' ','(',
168             'h','r','=','%','s',' ','(','0','x','%','0','8','l','x',')',')',' ',
169             0 };
170         /* FIXME: should use wsnprintf */
171         wsprintfW(msg, format, strFile, dwLine, strMsg,
172                   DXGetErrorString9W(hr), hr);
173         OutputDebugStringW(msg);
174     }
175
176     return hr;
177 }