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