Added dxerr8 and dxerr9 libraries.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /*
22  * TODO:
23  *      Add error codes for D3D9, D3DX9, D3D8, D3DX8, DDRAW, DPLAY8, DMUSIC, DINPUT and DSHOW.
24  *      Sort list for faster lookup.
25  */
26
27 #include <stdarg.h>
28 #include <stdio.h>
29
30 #define COM_NO_WINDOWS_H
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winnls.h"
36
37 #include "mmsystem.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;
59     TRACE("(0x%08lx)\n", hr);
60
61     for (i = 0; i < sizeof(info)/sizeof(info[0]); i++) {
62         if (hr == info[i].hr)
63             return info[i].resultA;
64     }
65
66     return "Unknown";
67 }
68
69 const WCHAR * WINAPI DXGetErrorString9W(HRESULT hr)
70 {
71     static const WCHAR unknown[] = { 'U', 'n', 'k', 'n', 'o', 'w', 'n', 0 };
72     unsigned int i;
73     TRACE("(0x%08lx)\n", hr);
74
75     for (i = 0; i < sizeof(info)/sizeof(info[0]); i++) {
76         if (hr == info[i].hr) {
77             return info[i].resultW;
78         }
79     }
80
81     return unknown;
82 }
83
84 const char * WINAPI DXGetErrorDescription9A(HRESULT hr)
85 {
86     unsigned int i;
87     TRACE("(0x%08lx)\n", hr);
88
89     for (i = 0; i < sizeof(info)/sizeof(info[0]); i++) {
90         if (hr == info[i].hr)
91             return info[i].descriptionA;
92     }
93
94     return "n/a";
95 }
96
97 const WCHAR * WINAPI DXGetErrorDescription9W(HRESULT hr)
98 {
99     static const WCHAR na[] = { 'n', '/', 'a', 0 };
100     unsigned int i;
101     TRACE("(0x%08lx)\n", hr);
102
103     for (i = 0; i < sizeof(info)/sizeof(info[0]); i++) {
104         if (hr == info[i].hr) {
105             return info[i].descriptionW;
106         }
107     }
108
109     return na;
110 }
111
112 HRESULT WINAPI DXTraceA(const char* strFile, DWORD dwLine, HRESULT hr, const char*  strMsg, BOOL bPopMsgBox)
113 {
114     char msg[1024];
115     TRACE("(%p,%ld,0x%08lx,%p,%d)\n", strFile, dwLine, hr, strMsg, bPopMsgBox);
116
117     if (bPopMsgBox) {
118         snprintf(msg, sizeof(msg), "File: %s\nLine: %ld\nError Code: %s (0x%08lx)\nCalling: %s",
119             strFile, dwLine, DXGetErrorString9A(hr), hr, strMsg);
120         MessageBoxA(0, msg, "Unexpected error encountered", MB_OK|MB_ICONERROR);
121     } else {
122         snprintf(msg, sizeof(msg), "%s(%ld): %s (hr=%s (0x%08lx))", strFile,
123             dwLine, strMsg, DXGetErrorString9A(hr), hr);
124         OutputDebugStringA(msg);
125     }
126
127     return hr;
128 }
129
130 HRESULT WINAPI DXTraceW(const char* strFile, DWORD dwLine, HRESULT hr, const WCHAR* strMsg, BOOL bPopMsgBox)
131 {
132     WCHAR msg[1024];
133     TRACE("(%p,%ld,0x%08lx,%p,%d)\n", strFile, dwLine, hr, strMsg, bPopMsgBox);
134
135     if (bPopMsgBox) {
136         WCHAR format[] = { 'F','i','l','e',':',' ','%','s','\\','n','L','i','n',
137             'e',':',' ','%','l','d','\\','n','E','r','r','o','r',' ','C','o',
138             'd','e',':',' ','%','s',' ','(','0','x','%','0','8','l','x',')',
139             '\\','n','C','a','l','l','i','n','g',':',' ','%','s',0 };
140         WCHAR caption[] = { 'U','n','e','x','p','e','c','t','e','d',' ','e','r',
141             'r','o','r',' ','e','n','c','o','u','n','t','e','r','e','d',0 }; 
142         /* FIXME: should use wsnprintf */
143         wsprintfW(msg, format, strFile, dwLine, 
144                   DXGetErrorString9W(hr), hr, strMsg);
145         MessageBoxW(0, msg, caption, MB_OK|MB_ICONERROR);
146     } else {
147         WCHAR format[] = { '%','s','(','%','l','d',')',':',' ','%','s',' ','(',
148             'h','r','=','%','s',' ','(','0','x','%','0','8','l','x',')',')',' ',
149             0 };
150         /* FIXME: should use wsnprintf */
151         wsprintfW(msg, format, strFile, dwLine, strMsg, 
152                   DXGetErrorString9W(hr), hr);
153         OutputDebugStringW(msg);
154     }
155
156     return hr;
157 }