1 /* 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
28 using namespace rfb;
29 using namespace rfb::win32;
30
31
32 static LogWriter vlog("RegConfig");
33
34
RegConfig(EventManager * em)35 RegConfig::RegConfig(EventManager* em) : eventMgr(em), event(CreateEvent(0, TRUE, FALSE, 0)), callback(0) {
36 if (em->addEvent(event, this))
37 eventMgr = em;
38 }
39
~RegConfig()40 RegConfig::~RegConfig() {
41 if (eventMgr)
42 eventMgr->removeEvent(event);
43 }
44
setKey(const HKEY rootkey,const TCHAR * keyname)45 bool 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("%s", e.str());
52 return false;
53 }
54 }
55
loadRegistryConfig(RegKey & key)56 void RegConfig::loadRegistryConfig(RegKey& key) {
57 DWORD i = 0;
58 try {
59 while (1) {
60 TCharArray name(tstrDup(key.getValueName(i++)));
61 if (!name.buf) break;
62 TCharArray value(key.getRepresentation(name.buf));
63 if (!value.buf || !Configuration::setParam(CStr(name.buf), CStr(value.buf)))
64 vlog.info("unable to process %s", name.buf);
65 }
66 } catch (rdr::SystemException& e) {
67 if (e.err != 6)
68 vlog.error("%s", e.str());
69 }
70 }
71
processEvent(HANDLE event_)72 void 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
RegConfigThread()88 RegConfigThread::RegConfigThread() : config(&eventMgr), thread_id(-1) {
89 }
90
~RegConfigThread()91 RegConfigThread::~RegConfigThread() {
92 PostThreadMessage(thread_id, WM_QUIT, 0, 0);
93 wait();
94 }
95
start(const HKEY rootKey,const TCHAR * keyname)96 bool RegConfigThread::start(const HKEY rootKey, const TCHAR* keyname) {
97 if (config.setKey(rootKey, keyname)) {
98 Thread::start();
99 while (thread_id == (DWORD)-1)
100 Sleep(0);
101 return true;
102 }
103 return false;
104 }
105
worker()106 void RegConfigThread::worker() {
107 DWORD result = 0;
108 MSG msg;
109 thread_id = GetCurrentThreadId();
110 while ((result = eventMgr.getMessage(&msg, 0, 0, 0)) > 0) {}
111 if (result < 0)
112 throw rdr::SystemException("RegConfigThread failed", GetLastError());
113 }
114