First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xwin / xlaunch / config.cc
blobb6bf65ae3488cb2283fdad3f3f14b33f14591238
1 /*
2 * Copyright (c) 2005 Alexander Gottwald
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
22 * Except as contained in this notice, the name(s) of the above copyright
23 * holders shall not be used in advertising or otherwise to promote the sale,
24 * use or other dealings in this Software without prior written authorization.
26 #include "config.h"
27 #include "window/util.h"
28 #include <msxml2.h>
29 #include <stdexcept>
31 const CLSID CLSID_DOMDocument40 = {0x88d969c0,0xf192,0x11d4,0xa6,0x5f,0x00,0x40,0x96,0x32,0x51,0xe5};
32 const CLSID CLSID_DOMDocument30 = {0xf5078f32,0xc551,0x11d3,0x89,0xb9,0x00,0x00,0xf8,0x1f,0xe2,0x21};
33 const IID IID_IXMLDOMDocument2 = {0x2933BF95,0x7B36,0x11d2,0xB2,0x0E,0x00,0xC0,0x4F,0x98,0x3E,0x60};
35 #define HRCALL(x, msg) if (FAILED(x)) { throw std::runtime_error("OLE Error:" msg " failed"); };
37 char *wcconvert(const wchar_t *wstr)
39 int chars = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
40 if (chars == 0)
41 throw win32_error("WideCharToMultiByte");
42 char *mbstr = new char[chars];
43 chars = WideCharToMultiByte(CP_ACP, 0, wstr, -1, mbstr, chars, NULL, NULL);
44 if (chars == 0)
45 throw win32_error("WideCharToMultiByte");
46 return mbstr;
49 wchar_t *mbconvert(const char *mbstr)
51 int chars = MultiByteToWideChar(CP_ACP, 0, mbstr, -1, NULL, 0);
52 if (chars == 0)
53 throw win32_error("MultiByteToWideChar");
54 wchar_t *wstr = new wchar_t[chars];
55 chars = MultiByteToWideChar(CP_ACP, 0, mbstr, -1, wstr, chars);
56 if (chars == 0)
57 throw win32_error("MultiByteToWideChar");
58 return wstr;
61 VARIANT VariantString(const char *filename)
64 wchar_t *str = mbconvert(filename);
66 VARIANT var;
67 VariantInit(&var);
68 V_BSTR(&var) = SysAllocString(str);
69 V_VT(&var) = VT_BSTR;
71 delete [] str;
72 return var;
75 VARIANT VariantString(const wchar_t *str)
77 VARIANT var;
78 VariantInit(&var);
79 V_BSTR(&var) = SysAllocString(str);
80 V_VT(&var) = VT_BSTR;
81 return var;
84 IXMLDOMDocument2 *CreateDocument()
86 IXMLDOMDocument2 *doc = NULL;
88 CoInitialize(NULL);
90 HRCALL(CoCreateInstance(CLSID_DOMDocument40, NULL, CLSCTX_INPROC_SERVER,
91 IID_IXMLDOMDocument2, (void**)&doc), "CoCreateInstance");
93 try {
94 HRCALL(doc->put_async(VARIANT_FALSE), "put_async");
95 HRCALL(doc->put_validateOnParse(VARIANT_FALSE), "put_validateOnParse");
96 HRCALL(doc->put_resolveExternals(VARIANT_FALSE), "put_resolveExternals");
98 IXMLDOMProcessingInstruction *pi = NULL;
99 IXMLDOMElement *root = NULL;
100 BSTR xml = SysAllocString(L"xml");
101 BSTR ver = SysAllocString(L"version='1.0'");
102 HRCALL(doc->createProcessingInstruction(xml,ver, &pi),
103 "createProcessingInstruction");
104 HRCALL(doc->appendChild(pi, NULL),
105 "appendChild");
106 pi->Release();
107 SysFreeString(xml);
108 SysFreeString(ver);
110 BSTR elemname = SysAllocString(L"XLaunch");
111 HRCALL(doc->createElement(elemname, &root), "createElement");
112 HRCALL(doc->appendChild(root, NULL), "appendChild");
113 SysFreeString(elemname);
114 } catch (...)
116 doc->Release();
117 throw;
119 return doc;
122 void setAttribute(IXMLDOMElement *elem, const wchar_t *name, const wchar_t *value)
124 BSTR str = SysAllocString(name);
125 VARIANT var = VariantString(value);
126 HRCALL(elem->setAttribute(str, var), "setAttribute");
127 VariantClear(&var);
128 SysFreeString(str);
131 void setAttribute(IXMLDOMElement *elem, const wchar_t *name, const char *value)
133 wchar_t *wstr = mbconvert(value);
134 setAttribute(elem, name, wstr);
135 delete [] wstr;
136 return;
139 void CConfig::Save(const char *filename)
141 IXMLDOMDocument2 *doc = CreateDocument();
142 IXMLDOMElement *root = NULL;
144 HRCALL(doc->get_documentElement(&root), "get_documentElement");
146 switch (window)
148 case MultiWindow:
149 setAttribute(root, L"WindowMode", L"MultiWindow");
150 break;
151 case Fullscreen:
152 setAttribute(root, L"WindowMode", L"Fullscreen");
153 break;
154 default:
155 case Windowed:
156 setAttribute(root, L"WindowMode", L"Windowed");
157 break;
158 case Nodecoration:
159 setAttribute(root, L"WindowMode", L"Nodecoration");
160 break;
162 switch (client)
164 default:
165 case NoClient:
166 setAttribute(root, L"ClientMode", L"NoClient");
167 break;
168 case StartProgram:
169 setAttribute(root, L"ClientMode", L"StartProgram");
170 break;
171 case XDMCP:
172 setAttribute(root, L"ClientMode", L"XDMCP");
173 break;
175 setAttribute(root, L"LocalClient", local?L"True":L"False");
176 setAttribute(root, L"Display", display.c_str());
177 setAttribute(root, L"Program", program.c_str());
178 setAttribute(root, L"RemoteProtocol", protocol.c_str());
179 setAttribute(root, L"RemoteHost", host.c_str());
180 setAttribute(root, L"RemoteUser", user.c_str());
181 setAttribute(root, L"XDMCPHost", xdmcp_host.c_str());
182 setAttribute(root, L"XDMCPBroadcast", broadcast?L"True":L"False");
183 setAttribute(root, L"XDMCPIndirect", indirect?L"True":L"False");
184 setAttribute(root, L"Clipboard", clipboard?L"True":L"False");
185 setAttribute(root, L"ExtraParams", extra_params.c_str());
187 VARIANT var = VariantString(filename);
188 HRCALL(doc->save(var), "save");
189 VariantClear(&var);
192 root->Release();
193 doc->Release();
196 BOOL getAttribute(IXMLDOMElement *elem, const wchar_t *name, std::string &ret)
198 VARIANT var;
199 HRCALL(elem->getAttribute((OLECHAR*)name, &var), "getAttribute");
200 if (V_VT(&var) != VT_NULL && V_VT(&var) == VT_BSTR)
202 char *str = wcconvert(V_BSTR(&var));
203 ret = str;
204 delete [] str;
205 return true;
207 return false;
210 BOOL getAttributeBool(IXMLDOMElement *elem, const wchar_t *name, bool &ret)
212 std::string str;
213 if (getAttribute(elem, name, str))
215 if (str == "True")
216 ret = true;
217 else
218 ret = false;
219 return true;
221 return false;
225 void CConfig::Load(const char *filename)
227 IXMLDOMDocument2 *doc = CreateDocument();
228 IXMLDOMElement *root = NULL;
230 VARIANT var = VariantString(filename);
231 VARIANT_BOOL status;
232 HRCALL(doc->load(var, &status), "load");
233 VariantClear(&var);
235 if (status == VARIANT_FALSE)
237 doc->Release();
238 return;
241 HRCALL(doc->get_documentElement(&root), "get_documentElement");
243 std::string windowMode;
244 std::string clientMode;
246 if (getAttribute(root, L"WindowMode", windowMode))
248 if (windowMode == "MultiWindow")
249 window = MultiWindow;
250 else if (windowMode == "Fullscreen")
251 window = Fullscreen;
252 else if (windowMode == "Windowed")
253 window = Windowed;
254 else if (windowMode == "Nodecoration")
255 window = Nodecoration;
257 if (getAttribute(root, L"ClientMode", clientMode))
259 if (clientMode == "NoClient")
260 client = NoClient;
261 else if (clientMode == "StartProgram")
262 client = StartProgram;
263 else if (clientMode == "XDMCP")
264 client = XDMCP;
267 getAttributeBool(root, L"LocalClient", local);
268 getAttribute(root, L"Display", display);
269 getAttribute(root, L"Program", program);
270 getAttribute(root, L"RemoteProtocol", protocol);
271 getAttribute(root, L"RemoteHost", host);
272 getAttribute(root, L"RemoteUser", user);
273 getAttribute(root, L"XDMCPHost", xdmcp_host);
274 getAttributeBool(root, L"XDMCPBroadcast", broadcast);
275 getAttributeBool(root, L"XDMCPIndirect", indirect);
276 getAttributeBool(root, L"Clipboard", clipboard);
277 getAttribute(root, L"ExtraParams", extra_params);
280 doc->Release();