1. -- API Brief (how it would be used) --
  2. _watch_all = RegistryWatcher(HKEY_LOCAL_MACHINE, "Software/Microsoft/Windows-NT/CurrentVersion/Fonts", [=]() {
  3. ... My font refresh code ...
  4. });
  5. _watch_user = RegistryWatcher(HKEY_CURRENT_USER, "Software/Microsoft/Windows-NT/CurrentVersion/Fonts", [=]() {
  6. ... My font refresh code ...
  7. });
  8. -- CPP FILE BEGINS --
  9. // SPDX-License-Identifier: GPL-2.0-or-later
  10. /*
  11. * Monitor the windows registry for changes and other windows registry utilities.
  12. *
  13. * Authors:
  14. * Martin Owens <doctormo@geek-2.com>
  15. *
  16. * The contents of this file may be used under the GNU General Public License Version 2 or later.
  17. *
  18. */
  19. #ifdef _WIN32
  20. #include <util/win32-registry.h>
  21. namespace Inkscape {
  22. namespace Util {
  23. RegistryWatcher::RegistryWatcher(HKEY rootkey, std::string subkey, std::function<bool ()> &callback)
  24. : _callback(callback)
  25. {
  26. // Get the HKEY from the string
  27. char *key = g_locale_from_utf8(subkey.c_str(), -1, NULL, NULL, NULL);
  28. if (RegOpenKeyExA(rootkey, key, 0, KEY_NOTIFY, &_regkey) != ERROR_SUCCESS) {
  29. char *errmsg = g_win32_error_message(GetLastError());
  30. g_warning("Unable to open registery key: %s", errmsg);
  31. g_free(errmsg);
  32. return;
  33. }
  34. g_free(key);
  35. // Create an event object
  36. if (!(_change_event = CreateEvent(NULL, TRUE, FALSE, NULL))) {
  37. char *errmsg = g_win32_error_message(GetLastError());
  38. g_warning("Unable to create registery event: %s", errmsg);
  39. g_free(errmsg);
  40. return;
  41. }
  42. // Create a notification
  43. DWORD filter = REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET;
  44. if (RegNotifyChangeKeyValue(_regkey, TRUE, filter, _change_event, TRUE) != ERROR_SUCCESS) {
  45. g_warning("Unable to watch windows registery");
  46. CloseHandle(_change_event);
  47. _change_event = nullptr;
  48. }
  49. // Watch for changes
  50. _poll = Glib::signal_timeout().connect_seconds(sigc::mem_fun(*this, &RegistryWatcher::on_poll), 3000);
  51. }
  52. RegistryWatcher::~RegistryWatcher()
  53. {
  54. _poll.disconnect();
  55. if (_change_event) {
  56. CloseHandle(_change_event);
  57. _change_event = nullptr;
  58. }
  59. }
  60. bool RegisteryWatch::on_poll()
  61. {
  62. if (_change_event) {
  63. // XXX Check _change_event here somehow without blocking
  64. }
  65. }
  66. } // namespace Util
  67. } // namespace Inkscape
  68. #endif // WIN32
  69. /*
  70. Local Variables:
  71. mode:c++
  72. c-file-style:"stroustrup"
  73. c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  74. indent-tabs-mode:nil
  75. fill-column:99
  76. End:
  77. */
  78. -- HEADER BEGINS --
  79. // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
  80. // SPDX-License-Identifier: GPL-2.0-or-later
  81. /*
  82. * Monitor the windows registery for changes and other windows registry utilities.
  83. *
  84. * Authors:
  85. * Martin Owens <doctormo@geek-2.com>
  86. *
  87. * The contents of this file may be used under the GNU General Public License Version 2 or later.
  88. *
  89. */
  90. #ifdef _WIN32
  91. #ifndef UTILS_WINREG_H
  92. #define UTILS_WINREG_H
  93. #include <string>
  94. #include <functional>
  95. #include <glib.h>
  96. #include "win32dep.h"
  97. namespace Inkscape {
  98. namespace Util {
  99. class RegistryWatcher
  100. {
  101. public:
  102. RegistryWatcher(HKEY rootkey, std::string hkey, std::function<bool ()> &callback);
  103. ~RegistryWatcher();
  104. private:
  105. std::function<bool ()> _callback;
  106. HKEY _regkey = NULL;
  107. // These could be made static in the future to improve performance
  108. sigc::connection _poll;
  109. HANDLE _change_event = NULL;
  110. };
  111. } // namespace Util
  112. } // namespace Inkscape
  113. #endif // UTILS_WINREG_H
  114. #endif // WIN32
  115. /*
  116. Local Variables:
  117. mode:c++
  118. c-file-style:"stroustrup"
  119. c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  120. indent-tabs-mode:nil
  121. fill-column:99
  122. End:
  123. */
  124. // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
 
 

255

winreg watcher

-

PasteBin

Zeilen
143
Wörter
373
Größe
3,9 KB
Erstellt
Unterversionen
2
Typ
text/x-c++hdr
Lesser General Public License v2 (LGPLv2)
Bitte melde Dich an, um einen Kommentar zu verfassen!