You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegConfig.cxx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // -=- RegConfig.cxx
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <malloc.h>
  23. #include <rfb_win32/RegConfig.h>
  24. #include <rfb/LogWriter.h>
  25. //#include <rdr/HexOutStream.h>
  26. using namespace rfb;
  27. using namespace rfb::win32;
  28. static LogWriter vlog("RegConfig");
  29. RegConfig::RegConfig(EventManager* em) : eventMgr(em), event(CreateEvent(0, TRUE, FALSE, 0)), callback(0) {
  30. if (em->addEvent(event, this))
  31. eventMgr = em;
  32. }
  33. RegConfig::~RegConfig() {
  34. if (eventMgr)
  35. eventMgr->removeEvent(event);
  36. }
  37. bool RegConfig::setKey(const HKEY rootkey, const char* keyname) {
  38. try {
  39. key.createKey(rootkey, keyname);
  40. processEvent(event);
  41. return true;
  42. } catch (rdr::Exception& e) {
  43. vlog.debug("%s", e.str());
  44. return false;
  45. }
  46. }
  47. void RegConfig::loadRegistryConfig(RegKey& key) {
  48. DWORD i = 0;
  49. try {
  50. while (1) {
  51. const char *name = key.getValueName(i++);
  52. if (!name) break;
  53. std::string value = key.getRepresentation(name);
  54. if (!Configuration::setParam(name, value.c_str()))
  55. vlog.info("unable to process %s", name);
  56. }
  57. } catch (rdr::SystemException& e) {
  58. if (e.err != 6)
  59. vlog.error("%s", e.str());
  60. }
  61. }
  62. void RegConfig::processEvent(HANDLE /*event*/) {
  63. vlog.info("registry changed");
  64. // Reinstate the registry change notifications
  65. ResetEvent(event);
  66. key.awaitChange(true, REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET, event);
  67. // Load settings
  68. loadRegistryConfig(key);
  69. // Notify the callback, if supplied
  70. if (callback)
  71. callback->regConfigChanged();
  72. }
  73. RegConfigThread::RegConfigThread() : config(&eventMgr), thread_id(-1) {
  74. }
  75. RegConfigThread::~RegConfigThread() {
  76. PostThreadMessage(thread_id, WM_QUIT, 0, 0);
  77. wait();
  78. }
  79. bool RegConfigThread::start(const HKEY rootKey, const char* keyname) {
  80. if (config.setKey(rootKey, keyname)) {
  81. Thread::start();
  82. while (thread_id == (DWORD)-1)
  83. Sleep(0);
  84. return true;
  85. }
  86. return false;
  87. }
  88. void RegConfigThread::worker() {
  89. BOOL result = 0;
  90. MSG msg;
  91. thread_id = GetCurrentThreadId();
  92. while ((result = eventMgr.getMessage(&msg, 0, 0, 0)) > 0) {}
  93. if (result < 0)
  94. throw rdr::SystemException("RegConfigThread failed", GetLastError());
  95. }