blob: 90980c33b31b5b66d3d18cae85d6f7c2dee8fe76 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- RegConfig.cxx
20
21#include <malloc.h>
22
23#include <rfb_win32/RegConfig.h>
24#include <rfb/LogWriter.h>
25#include <rfb/util.h>
26//#include <rdr/HexOutStream.h>
27
28using namespace rfb;
29using namespace rfb::win32;
30
31
32static LogWriter vlog("RegConfig");
33
34
35RegConfig::RegConfig(EventManager* em) : eventMgr(em), event(CreateEvent(0, TRUE, FALSE, 0)), callback(0) {
36 if (em->addEvent(event, this))
37 eventMgr = em;
38}
39
40RegConfig::~RegConfig() {
41 if (eventMgr)
42 eventMgr->removeEvent(event);
43}
44
45bool RegConfig::setKey(const HKEY rootkey, const TCHAR* keyname) {
46 try {
47 key.createKey(rootkey, keyname);
48 processEvent(event);
49 return true;
50 } catch (rdr::Exception& e) {
51 vlog.debug(e.str());
52 return false;
53 }
54}
55
56void RegConfig::loadRegistryConfig(RegKey& key) {
57 DWORD i = 0;
58 try {
59 while (1) {
Peter Åstrandb22dbef2008-12-09 14:57:53 +000060 TCharArray name(tstrDup(key.getValueName(i++)));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000061 if (!name.buf) break;
Peter Åstrandb22dbef2008-12-09 14:57:53 +000062 TCharArray value(key.getRepresentation(name.buf));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000063 if (!value.buf || !Configuration::setParam(CStr(name.buf), CStr(value.buf)))
Peter Åstrand4e46da72008-12-10 12:11:10 +000064 vlog.info("unable to process %s", name.buf);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000065 }
66 } catch (rdr::SystemException& e) {
67 if (e.err != 6)
68 vlog.error(e.str());
69 }
70}
71
72void RegConfig::processEvent(HANDLE event_) {
73 vlog.info("registry changed");
74
75 // Reinstate the registry change notifications
76 ResetEvent(event);
77 key.awaitChange(true, REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET, event);
78
79 // Load settings
80 loadRegistryConfig(key);
81
82 // Notify the callback, if supplied
83 if (callback)
84 callback->regConfigChanged();
85}
86
87
88RegConfigThread::RegConfigThread() : Thread("RegConfigThread"), config(&eventMgr) {
89}
90
91RegConfigThread::~RegConfigThread() {
92 join();
93}
94
95bool RegConfigThread::start(const HKEY rootKey, const TCHAR* keyname) {
96 if (config.setKey(rootKey, keyname)) {
97 Thread::start();
98 return true;
99 }
100 return false;
101}
102
103void RegConfigThread::run() {
104 DWORD result = 0;
105 MSG msg;
106 while ((result = eventMgr.getMessage(&msg, 0, 0, 0)) > 0) {}
107 if (result < 0)
108 throw rdr::SystemException("RegConfigThread failed", GetLastError());
109}
110
111Thread* RegConfigThread::join() {
112 PostThreadMessage(getThreadId(), WM_QUIT, 0, 0);
113 return Thread::join();
114}