Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 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 | |
| 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 | |
| 40 | RegConfig::~RegConfig() { |
| 41 | if (eventMgr) |
| 42 | eventMgr->removeEvent(event); |
| 43 | } |
| 44 | |
| 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) { |
Pierre Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame^] | 51 | vlog.debug("%s", e.str()); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void RegConfig::loadRegistryConfig(RegKey& key) { |
| 57 | DWORD i = 0; |
| 58 | try { |
| 59 | while (1) { |
Peter Åstrand | b22dbef | 2008-12-09 14:57:53 +0000 | [diff] [blame] | 60 | TCharArray name(tstrDup(key.getValueName(i++))); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 61 | if (!name.buf) break; |
Peter Åstrand | b22dbef | 2008-12-09 14:57:53 +0000 | [diff] [blame] | 62 | TCharArray value(key.getRepresentation(name.buf)); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 63 | if (!value.buf || !Configuration::setParam(CStr(name.buf), CStr(value.buf))) |
Peter Åstrand | 4e46da7 | 2008-12-10 12:11:10 +0000 | [diff] [blame] | 64 | vlog.info("unable to process %s", name.buf); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 65 | } |
| 66 | } catch (rdr::SystemException& e) { |
| 67 | if (e.err != 6) |
Pierre Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame^] | 68 | vlog.error("%s", e.str()); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 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 | |
| 88 | RegConfigThread::RegConfigThread() : Thread("RegConfigThread"), config(&eventMgr) { |
| 89 | } |
| 90 | |
| 91 | RegConfigThread::~RegConfigThread() { |
| 92 | join(); |
| 93 | } |
| 94 | |
| 95 | bool 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 | |
| 103 | void 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 | |
| 111 | Thread* RegConfigThread::join() { |
| 112 | PostThreadMessage(getThreadId(), WM_QUIT, 0, 0); |
| 113 | return Thread::join(); |
| 114 | } |