├── LICENSE.txt ├── README.md └── src ├── .gitattributes ├── .gitignore ├── Monitor.cpp ├── Monitor.h ├── Overlay.cpp ├── Overlay.h ├── TrayMenu.cpp ├── TrayMenu.h ├── Util.cpp ├── Util.h ├── dimmer.ico ├── dimmer.rc ├── dimmer.sln ├── dimmer.vcxproj ├── dimmer.vcxproj.filters ├── json.hpp ├── main.cpp └── resource.h /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Casey Langen 2 | 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, 9 | this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 15 | * Neither the name of the author nor the names of other contributors may 16 | be used to endorse or promote products derived from this software 17 | without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dimmer 2 | 3 | a display dimming app for windows with multi-monitor support. 4 | 5 | **dimmer** is a no-frills program written in vanilla win32 with a minimal user interface. it lives in the system tray and uses virtually no resources. click the icon to see a list of monitors, and adjust your desired brightness. 6 | 7 | the app works by applying a semi-transparent overlay on top of all other running programs. note that, by default, the operating system will place popup menus on top of this overlay. if this annoys you, you can select the `dim popups` option in the tray menu. it's not enabled by default because it's a gross hack that eats a few cpu cycles. 8 | 9 | **dimmer** is also has very basic support for adjusting color temperature -- you can select 4000, 4500, 5000, 5500, or 6000 kelvin emulation. just like brightness, temperature can be changed on a per-monitor basis. 10 | 11 | # screenshot 12 | 13 | it works like this: 14 | 15 | ![dimmer screenshot](https://raw.githubusercontent.com/clangen/clangen-projects-static/master/dimmer/screenshots/screenshot.png) 16 | 17 | # installation 18 | 19 | download, unzip, and run! no installation or additional runtimes required. 20 | 21 | # license 22 | 23 | standard 3-clause bsd. do whatever you want with it, just don't blame me if it breaks something. 24 | -------------------------------------------------------------------------------- /src/.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /src/Monitor.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Copyright (c) 2007-2017 Casey Langen 4 | // 5 | // All rights reserved. 6 | // 7 | // Redistribution and use in source and binary forms, with or without 8 | // modification, are permitted provided that the following conditions are met: 9 | // 10 | // * Redistributions of source code must retain the above copyright notice, 11 | // this list of conditions and the following disclaimer. 12 | // 13 | // * Redistributions in binary form must reproduce the above copyright 14 | // notice, this list of conditions and the following disclaimer in the 15 | // documentation and/or other materials provided with the distribution. 16 | // 17 | // * Neither the name of the author nor the names of other contributors may 18 | // be used to endorse or promote products derived from this software 19 | // without specific prior written permission. 20 | // 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | // POSSIBILITY OF SUCH DAMAGE. 32 | // 33 | ////////////////////////////////////////////////////////////////////////////// 34 | 35 | #include "Monitor.h" 36 | #include "Util.h" 37 | #include 38 | #include "json.hpp" 39 | 40 | using namespace dimmer; 41 | using namespace nlohmann; 42 | 43 | constexpr float DEFAULT_OPACITY = 0.3f; 44 | constexpr int DEFAULT_TEMPERATURE = -1; 45 | 46 | struct MonitorOptions { 47 | float opacity; 48 | int temperature; 49 | bool enabled; 50 | 51 | MonitorOptions() { 52 | this->opacity = DEFAULT_OPACITY; 53 | this->temperature = DEFAULT_TEMPERATURE; 54 | this->enabled = true; 55 | } 56 | }; 57 | 58 | static std::map> monitorOptions; 59 | static bool pollingEnabled = false; 60 | static bool globalEnabled = true; 61 | 62 | static std::wstring getConfigFilename() { 63 | return getDataDirectory() + L"\\config.json"; 64 | } 65 | 66 | static BOOL CALLBACK MonitorEnumProc(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data) { 67 | auto monitors = reinterpret_cast*>(data); 68 | int index = (int) monitors->size(); 69 | monitors->push_back(Monitor(monitor, index)); 70 | return TRUE; 71 | } 72 | 73 | static MonitorOptions& options(Monitor& monitor) { 74 | auto id = monitor.getId(); 75 | if (monitorOptions.find(id) == monitorOptions.end()) { 76 | monitorOptions[id] = std::make_shared(); 77 | } 78 | return *monitorOptions[id]; 79 | } 80 | 81 | namespace dimmer { 82 | std::vector queryMonitors() { 83 | std::vector result; 84 | 85 | EnumDisplayMonitors( 86 | nullptr, 87 | nullptr, 88 | &MonitorEnumProc, 89 | reinterpret_cast(&result)); 90 | 91 | return result; 92 | } 93 | 94 | float getMonitorOpacity(Monitor& monitor) { 95 | return options(monitor).opacity; 96 | } 97 | 98 | void setMonitorOpacity(Monitor& monitor, float opacity) { 99 | options(monitor).opacity = opacity; 100 | saveConfig(); 101 | } 102 | 103 | int getMonitorTemperature(Monitor& monitor) { 104 | return options(monitor).temperature; 105 | } 106 | 107 | void setMonitorTemperature(Monitor& monitor, int temperature) { 108 | options(monitor).temperature = temperature; 109 | saveConfig(); 110 | } 111 | 112 | bool isPollingEnabled() { 113 | return pollingEnabled; 114 | } 115 | 116 | void setPollingEnabled(bool enabled) { 117 | pollingEnabled = enabled; 118 | saveConfig(); 119 | } 120 | 121 | extern bool isDimmerEnabled() { 122 | return globalEnabled; 123 | } 124 | 125 | extern void setDimmerEnabled(bool enabled) { 126 | if (globalEnabled != enabled) { 127 | globalEnabled = enabled; 128 | saveConfig(); 129 | } 130 | } 131 | 132 | bool isMonitorEnabled(Monitor& monitor) { 133 | return options(monitor).enabled; 134 | } 135 | 136 | void setMonitorEnabled(Monitor& monitor, bool enabled) { 137 | options(monitor).enabled = enabled; 138 | saveConfig(); 139 | } 140 | 141 | void loadConfig() { 142 | std::string config = fileToString(getConfigFilename()); 143 | try { 144 | json j = json::parse(config); 145 | auto m = j.find("monitors"); 146 | if (m != j.end()) { 147 | for (auto it = (*m).begin(); it != (*m).end(); ++it) { 148 | auto key = u8to16(it.key()); 149 | auto value = it.value(); 150 | auto options = std::make_shared(); 151 | options->opacity = value.value("opacity", DEFAULT_OPACITY); 152 | options->temperature = value.value("temperature", DEFAULT_TEMPERATURE); 153 | options->enabled = value.value("enabled", true); 154 | monitorOptions[key] = options; 155 | } 156 | } 157 | 158 | auto g = j.find("general"); 159 | if (g != j.end()) { 160 | pollingEnabled = (*g).value("pollingEnabled", false); 161 | globalEnabled = (*g).value("globalEnabled", true); 162 | } 163 | } 164 | catch (...) { 165 | /* move on... */ 166 | } 167 | } 168 | 169 | void saveConfig() { 170 | json j = { { "monitors", { } } }; 171 | json& m = j["monitors"]; 172 | 173 | auto monitors = queryMonitors(); 174 | for (auto monitor : monitors) { 175 | m[u16to8(monitor.getId())] = { 176 | { "opacity", getMonitorOpacity(monitor) }, 177 | { "temperature", getMonitorTemperature(monitor) }, 178 | { "enabled", isMonitorEnabled(monitor) } 179 | }; 180 | } 181 | 182 | j["general"] = { 183 | { "globalEnabled", globalEnabled }, 184 | { "pollingEnabled", pollingEnabled } 185 | }; 186 | 187 | stringToFile(getConfigFilename(), j.dump(2)); 188 | } 189 | } -------------------------------------------------------------------------------- /src/Monitor.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Copyright (c) 2007-2017 Casey Langen 4 | // 5 | // All rights reserved. 6 | // 7 | // Redistribution and use in source and binary forms, with or without 8 | // modification, are permitted provided that the following conditions are met: 9 | // 10 | // * Redistributions of source code must retain the above copyright notice, 11 | // this list of conditions and the following disclaimer. 12 | // 13 | // * Redistributions in binary form must reproduce the above copyright 14 | // notice, this list of conditions and the following disclaimer in the 15 | // documentation and/or other materials provided with the distribution. 16 | // 17 | // * Neither the name of the author nor the names of other contributors may 18 | // be used to endorse or promote products derived from this software 19 | // without specific prior written permission. 20 | // 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | // POSSIBILITY OF SUCH DAMAGE. 32 | // 33 | ////////////////////////////////////////////////////////////////////////////// 34 | 35 | #pragma once 36 | 37 | #include 38 | #include 39 | #include 40 | 41 | namespace dimmer { 42 | struct Monitor { 43 | Monitor(HMONITOR handle, int index) { 44 | this->handle = handle; 45 | this->index = index; 46 | this->info = {}; 47 | this->info.cbSize = sizeof(MONITORINFOEX); 48 | GetMonitorInfo(handle, &this->info); 49 | } 50 | 51 | std::wstring getId() const { 52 | return std::wstring(this->info.szDevice) + L"-" + std::to_wstring(index); 53 | } 54 | 55 | std::wstring getName() const { 56 | std::wstring name = this->info.szDevice; 57 | auto pos = name.find(L"\\\\.\\"); 58 | if (pos == 0) { 59 | name = name.substr(4); 60 | } 61 | return name; 62 | } 63 | 64 | int index; 65 | HMONITOR handle; 66 | MONITORINFOEX info; 67 | }; 68 | 69 | extern std::vector queryMonitors(); 70 | extern float getMonitorOpacity(Monitor& monitor); 71 | extern void setMonitorOpacity(Monitor& monitor, float opacity); 72 | extern int getMonitorTemperature(Monitor& monitor); 73 | extern void setMonitorTemperature(Monitor& monitor, int temperature); 74 | extern bool isMonitorEnabled(Monitor& monitor); 75 | extern void setMonitorEnabled(Monitor& monitor, bool enabled); 76 | extern bool isPollingEnabled(); 77 | extern void setPollingEnabled(bool enabled); 78 | extern bool isDimmerEnabled(); 79 | extern void setDimmerEnabled(bool enabled); 80 | extern void loadConfig(); 81 | extern void saveConfig(); 82 | } -------------------------------------------------------------------------------- /src/Overlay.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Copyright (c) 2007-2017 Casey Langen 4 | // 5 | // All rights reserved. 6 | // 7 | // Redistribution and use in source and binary forms, with or without 8 | // modification, are permitted provided that the following conditions are met: 9 | // 10 | // * Redistributions of source code must retain the above copyright notice, 11 | // this list of conditions and the following disclaimer. 12 | // 13 | // * Redistributions in binary form must reproduce the above copyright 14 | // notice, this list of conditions and the following disclaimer in the 15 | // documentation and/or other materials provided with the distribution. 16 | // 17 | // * Neither the name of the author nor the names of other contributors may 18 | // be used to endorse or promote products derived from this software 19 | // without specific prior written permission. 20 | // 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | // POSSIBILITY OF SUCH DAMAGE. 32 | // 33 | ////////////////////////////////////////////////////////////////////////////// 34 | 35 | #include "Overlay.h" 36 | #include "Monitor.h" 37 | #include 38 | #include 39 | 40 | using namespace dimmer; 41 | 42 | #define TIMER_ID 0xdeadbeef 43 | 44 | constexpr int timerTickMs = 10; 45 | constexpr wchar_t className[] = L"DimmerOverlayClass"; 46 | constexpr wchar_t windowTitle[] = L"DimmerOverlayWindow"; 47 | 48 | static ATOM overlayClass = 0; 49 | static std::map hwndToOverlay; 50 | static WORD gammaRamp[3][256]; 51 | 52 | static void registerClass(HINSTANCE instance, WNDPROC wndProc) { 53 | if (!overlayClass) { 54 | WNDCLASS wc = {}; 55 | wc.lpfnWndProc = wndProc; 56 | wc.hInstance = instance; 57 | wc.lpszClassName = className; 58 | overlayClass = RegisterClass(&wc); 59 | } 60 | } 61 | 62 | static bool enabled(Monitor& monitor) { 63 | return isDimmerEnabled() && isMonitorEnabled(monitor); 64 | } 65 | 66 | Overlay::Overlay(HINSTANCE instance, Monitor monitor) 67 | : instance(instance) 68 | , monitor(monitor) 69 | , timerId(0) 70 | , bgBrush(CreateSolidBrush(RGB(0, 0, 0))) 71 | , hwnd(nullptr) { 72 | registerClass(instance, &Overlay::windowProc); 73 | this->update(monitor); 74 | } 75 | 76 | Overlay::~Overlay() { 77 | this->disableColorTemperature(); 78 | this->disableBrigthnessOverlay(); 79 | DeleteObject(this->bgBrush); 80 | } 81 | 82 | static void colorTemperatureToRgb(int kelvin, float& red, float& green, float& blue) { 83 | kelvin /= 100; 84 | 85 | if (kelvin <= 66) { 86 | red = 255; 87 | } 88 | else { 89 | red = kelvin - 60.0f; 90 | red = (float)(329.698727446 * (pow(red, -0.1332047592))); 91 | red = std::max(0.0f, std::min(255.0f, red)); 92 | } 93 | 94 | if (kelvin <= 66) { 95 | green = (float) kelvin; 96 | green = 99.4708025861f * log(green) - 161.1195681661f; 97 | green = std::max(0.0f, std::min(255.0f, green)); 98 | } 99 | else { 100 | green = kelvin - 60.0f; 101 | green = (float)(288.1221695283 * (pow(green, -0.0755148492))); 102 | green = std::max(0.0f, std::min(255.0f, green)); 103 | } 104 | 105 | if (kelvin >= 66) { 106 | blue = 255.0f; 107 | } 108 | else { 109 | blue = kelvin - 10.0f; 110 | blue = 138.5177312231f * log(blue) - 305.0447927307f; 111 | blue = std::max(0.0f, std::min(255.0f, blue)); 112 | } 113 | 114 | red /= 255.0f; 115 | green /= 255.0f; 116 | blue /= 255.0f; 117 | } 118 | 119 | void Overlay::disableColorTemperature() { 120 | HDC dc = CreateDC(nullptr, monitor.info.szDevice, nullptr, nullptr); 121 | if (dc) { 122 | for (int i = 0; i < 256; i++) { 123 | gammaRamp[0][i] = gammaRamp[1][i] = gammaRamp[2][i] = i * 256; 124 | } 125 | SetDeviceGammaRamp(dc, gammaRamp); 126 | DeleteDC(dc); 127 | } 128 | } 129 | 130 | void Overlay::updateColorTemperature() { 131 | int temperature = getMonitorTemperature(monitor); 132 | 133 | if (!enabled(monitor) || temperature == -1) { 134 | disableColorTemperature(); 135 | } 136 | else { 137 | HDC dc = CreateDC(nullptr, monitor.info.szDevice, nullptr, nullptr); 138 | if (dc) { 139 | float red = 1.0f; 140 | float green = 1.0f; 141 | float blue = 1.0f; 142 | 143 | temperature = std::min(6000, std::max(4500, temperature)); 144 | colorTemperatureToRgb(temperature, red, green, blue); 145 | 146 | for (int i = 0; i < 256; i++) { 147 | float brightness = i * (256.0f); 148 | gammaRamp[0][i] = (short)std::max(0.0f, std::min(65535.0f, brightness * red)); 149 | gammaRamp[1][i] = (short)std::max(0.0f, std::min(65535.0f, brightness * green)); 150 | gammaRamp[2][i] = (short)std::max(0.0f, std::min(65535.0f, brightness * blue)); 151 | } 152 | 153 | SetDeviceGammaRamp(dc, gammaRamp); 154 | 155 | DeleteDC(dc); 156 | } 157 | } 158 | } 159 | 160 | void Overlay::disableBrigthnessOverlay() { 161 | this->killTimer(); 162 | if (this->hwnd) { 163 | DestroyWindow(this->hwnd); 164 | hwndToOverlay.erase(hwndToOverlay.find(this->hwnd)); 165 | this->hwnd = nullptr; 166 | } 167 | } 168 | 169 | void Overlay::updateBrightnessOverlay() { 170 | if (!enabled(monitor) || getMonitorOpacity(monitor) == 0.0f) { 171 | disableBrigthnessOverlay(); 172 | } 173 | else { 174 | if (!this->hwnd) { 175 | this->hwnd = 176 | CreateWindowEx( 177 | WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW, 178 | className, 179 | windowTitle, 180 | 0, 181 | 0, 0, 0, 0, /* dimens */ 182 | nullptr, 183 | nullptr, 184 | instance, 185 | this); 186 | 187 | hwndToOverlay[this->hwnd] = this; 188 | 189 | SetWindowLong(this->hwnd, GWL_STYLE, 0); /* removes title, borders. */ 190 | } 191 | 192 | float red, green, blue; 193 | colorTemperatureToRgb(3000, red, green, blue); 194 | red = (1.0f - red) * 256.0f; 195 | green = (1.0f - green) * 256.0f; 196 | blue = (1.0f - blue) * 256.0f; 197 | 198 | int x = monitor.info.rcMonitor.left; 199 | int y = monitor.info.rcMonitor.top; 200 | int width = monitor.info.rcMonitor.right - x; 201 | int height = monitor.info.rcMonitor.bottom - y; 202 | 203 | float value = getMonitorOpacity(this->monitor); 204 | value = std::min(1.0f, std::max(0.0f, value)); 205 | BYTE opacity = std::min((BYTE)240, (BYTE)(value * 255.0f)); 206 | 207 | SetLayeredWindowAttributes(this->hwnd, 0, opacity, LWA_ALPHA); 208 | SetWindowPos(this->hwnd, HWND_TOPMOST, x, y, width, height, SWP_FRAMECHANGED | SWP_SHOWWINDOW); 209 | 210 | UpdateWindow(this->hwnd); 211 | 212 | this->startTimer(); 213 | } 214 | } 215 | 216 | void Overlay::update(Monitor& monitor) { 217 | this->monitor = monitor; 218 | this->updateColorTemperature(); 219 | this->updateBrightnessOverlay(); 220 | } 221 | 222 | void Overlay::startTimer() { 223 | this->killTimer(); 224 | 225 | if (isPollingEnabled()) { 226 | this->timerId = SetTimer(this->hwnd, TIMER_ID, timerTickMs, nullptr); 227 | } 228 | } 229 | 230 | void Overlay::killTimer() { 231 | KillTimer(this->hwnd, this->timerId); 232 | this->timerId = 0; 233 | } 234 | 235 | LRESULT CALLBACK Overlay::windowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { 236 | auto overlay = hwndToOverlay.find(hwnd); 237 | 238 | if (overlay != hwndToOverlay.end()) { 239 | switch (msg) { 240 | case WM_PAINT: { 241 | PAINTSTRUCT ps; 242 | HDC hdc = BeginPaint(hwnd, &ps); 243 | FillRect(hdc, &ps.rcPaint, overlay->second->bgBrush); 244 | EndPaint(hwnd, &ps); 245 | return 0; 246 | } 247 | 248 | case WM_TIMER: { 249 | if (wParam == overlay->second->timerId) { 250 | BringWindowToTop(hwnd); 251 | return 0; 252 | } 253 | } 254 | } 255 | } 256 | 257 | return DefWindowProc(hwnd, msg, wParam, lParam); 258 | } -------------------------------------------------------------------------------- /src/Overlay.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Copyright (c) 2007-2017 Casey Langen 4 | // 5 | // All rights reserved. 6 | // 7 | // Redistribution and use in source and binary forms, with or without 8 | // modification, are permitted provided that the following conditions are met: 9 | // 10 | // * Redistributions of source code must retain the above copyright notice, 11 | // this list of conditions and the following disclaimer. 12 | // 13 | // * Redistributions in binary form must reproduce the above copyright 14 | // notice, this list of conditions and the following disclaimer in the 15 | // documentation and/or other materials provided with the distribution. 16 | // 17 | // * Neither the name of the author nor the names of other contributors may 18 | // be used to endorse or promote products derived from this software 19 | // without specific prior written permission. 20 | // 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | // POSSIBILITY OF SUCH DAMAGE. 32 | // 33 | ////////////////////////////////////////////////////////////////////////////// 34 | 35 | #pragma once 36 | 37 | #include 38 | #include "Monitor.h" 39 | 40 | namespace dimmer { 41 | class Overlay { 42 | public: 43 | Overlay(HINSTANCE instance, Monitor monitor); 44 | ~Overlay(); 45 | 46 | void update(Monitor& monitor); 47 | void startTimer(); 48 | void killTimer(); 49 | 50 | private: 51 | static LRESULT CALLBACK windowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); 52 | 53 | void disableColorTemperature(); 54 | void updateColorTemperature(); 55 | void disableBrigthnessOverlay(); 56 | void updateBrightnessOverlay(); 57 | 58 | Monitor monitor; 59 | HINSTANCE instance; 60 | HBRUSH bgBrush; 61 | UINT_PTR timerId; 62 | HWND hwnd; 63 | }; 64 | } -------------------------------------------------------------------------------- /src/TrayMenu.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Copyright (c) 2007-2017 Casey Langen 4 | // 5 | // All rights reserved. 6 | // 7 | // Redistribution and use in source and binary forms, with or without 8 | // modification, are permitted provided that the following conditions are met: 9 | // 10 | // * Redistributions of source code must retain the above copyright notice, 11 | // this list of conditions and the following disclaimer. 12 | // 13 | // * Redistributions in binary form must reproduce the above copyright 14 | // notice, this list of conditions and the following disclaimer in the 15 | // documentation and/or other materials provided with the distribution. 16 | // 17 | // * Neither the name of the author nor the names of other contributors may 18 | // be used to endorse or promote products derived from this software 19 | // without specific prior written permission. 20 | // 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | // POSSIBILITY OF SUCH DAMAGE. 32 | // 33 | ////////////////////////////////////////////////////////////////////////////// 34 | 35 | #include "TrayMenu.h" 36 | #include "Monitor.h" 37 | #include "resource.h" 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | 45 | using namespace dimmer; 46 | 47 | #define WM_TRAYICON (WM_USER + 2000) 48 | #define MENU_ID_EXIT 500 49 | #define MENU_ID_POLL 501 50 | #define MENU_ID_ENABLED 502 51 | #define MENU_ID_MONITOR_BASE 1000 52 | #define MENU_ID_MONITOR_USER 100 53 | #define MENU_ID_MONITOR_COLOR 1 54 | 55 | #define MENU_ID_DEFAULTK (MENU_ID_MONITOR_USER + 1) 56 | #define MENU_ID_4500K (MENU_ID_MONITOR_USER + 2) 57 | #define MENU_ID_5000K (MENU_ID_MONITOR_USER + 3) 58 | #define MENU_ID_5500K (MENU_ID_MONITOR_USER + 4) 59 | #define MENU_ID_6000K (MENU_ID_MONITOR_USER + 5) 60 | 61 | constexpr wchar_t version[] = L"v0.3"; 62 | constexpr wchar_t className[] = L"DimmerTrayMenuClass"; 63 | constexpr wchar_t windowTitle[] = L"DimmerTrayMenuWindow"; 64 | constexpr int offscreen = -32000; 65 | 66 | static ATOM overlayClass = 0; 67 | static HICON trayIcon = nullptr; 68 | static HMENU menu = nullptr; 69 | static std::map hwndToInstance; 70 | 71 | struct ColorProcContext { 72 | TrayMenu* instance; 73 | Monitor* monitor; 74 | CHOOSECOLOR* data; 75 | }; 76 | 77 | static UINT checked(int temp, int value) { 78 | return (temp == value) ? MF_CHECKED : MF_UNCHECKED; 79 | } 80 | 81 | static void refocus(HWND hwnd) { 82 | BringWindowToTop(hwnd); 83 | SetForegroundWindow(hwnd); 84 | SetFocus(hwnd); 85 | } 86 | 87 | static HMENU createMenu(HWND hwnd) { 88 | if (menu) { 89 | DestroyMenu(menu); 90 | } 91 | 92 | menu = CreatePopupMenu(); 93 | 94 | auto monitors = queryMonitors(); 95 | int i = 1; 96 | for (auto m : monitors) { 97 | const int checkedValue = (int) round(getMonitorOpacity(m) * 100.0f); 98 | UINT_PTR baseId = (MENU_ID_MONITOR_BASE * i++); 99 | 100 | /* "brightness" submenu */ 101 | HMENU brightnessMenu = CreatePopupMenu(); 102 | UINT_PTR menuId; 103 | for (int j = 0; j < 10; j++) { 104 | const int currentValue = j * 10; 105 | 106 | const std::wstring title = (j == 0) 107 | ? L"full brightness" 108 | : std::to_wstring(100 - currentValue) + L"%"; 109 | 110 | UINT flags = (currentValue == checkedValue) ? MF_CHECKED : 0; 111 | menuId = baseId + (j * 10); 112 | AppendMenu(brightnessMenu, flags, menuId, title.c_str()); 113 | } 114 | 115 | /* "temperature" submenu */ 116 | HMENU tempMenu = CreatePopupMenu(); 117 | const int currentTemp = getMonitorTemperature(m); 118 | AppendMenu(tempMenu, checked(currentTemp, -1), baseId + MENU_ID_DEFAULTK, L"default"); 119 | AppendMenu(tempMenu, checked(currentTemp, 4500), baseId + MENU_ID_4500K, L"4500k"); 120 | AppendMenu(tempMenu, checked(currentTemp, 5000), baseId + MENU_ID_5000K, L"5000k"); 121 | AppendMenu(tempMenu, checked(currentTemp, 5500), baseId + MENU_ID_5500K, L"5500k"); 122 | AppendMenu(tempMenu, checked(currentTemp, 6000), baseId + MENU_ID_6000K, L"6000k"); 123 | 124 | /* brightness, temperature popup */ 125 | HMENU brightTempMenu = CreatePopupMenu(); 126 | AppendMenu(brightTempMenu, MF_POPUP, (UINT_PTR) brightnessMenu, L"brightness"); 127 | AppendMenu(brightTempMenu, MF_POPUP, (UINT_PTR) tempMenu, L"temperature"); 128 | 129 | /* main menu */ 130 | UINT submenuEnabled = isDimmerEnabled() ? MF_ENABLED : MF_DISABLED; 131 | AppendMenu( 132 | menu, 133 | MF_POPUP | submenuEnabled, 134 | reinterpret_cast(brightTempMenu), 135 | m.getName().c_str()); 136 | } 137 | 138 | bool poll = isPollingEnabled(); 139 | AppendMenu(menu, MF_SEPARATOR, 0, L"-"); 140 | AppendMenu(menu, isDimmerEnabled() ? MF_CHECKED : MF_UNCHECKED, MENU_ID_ENABLED, L"enabled"); 141 | AppendMenu(menu, poll ? MF_CHECKED : MF_UNCHECKED, MENU_ID_POLL, L"dim popups"); 142 | AppendMenu(menu, MF_SEPARATOR, 0, L"-"); 143 | AppendMenu(menu, 0, MENU_ID_EXIT, L"exit"); 144 | return menu; 145 | } 146 | 147 | static void registerClass(HINSTANCE instance, WNDPROC wndProc) { 148 | if (!trayIcon) { 149 | trayIcon = (HICON) ::LoadIconW( 150 | GetModuleHandle(nullptr), 151 | MAKEINTRESOURCE(IDI_TRAY_ICON)); 152 | } 153 | 154 | if (!overlayClass) { 155 | WNDCLASS wc = {}; 156 | wc.lpfnWndProc = wndProc; 157 | wc.hInstance = instance; 158 | wc.lpszClassName = className; 159 | overlayClass = RegisterClass(&wc); 160 | } 161 | } 162 | 163 | TrayMenu::TrayMenu(HINSTANCE instance, MonitorsChanged callback) { 164 | this->monitorsChanged = callback; 165 | this->middleFlags = 0; 166 | 167 | registerClass(instance, &windowProc); 168 | 169 | this->hwnd = 170 | CreateWindowEx( 171 | WS_EX_TOOLWINDOW, 172 | className, 173 | windowTitle, 174 | 0, 175 | 0, 0, 0, 0, /* dimens */ 176 | nullptr, 177 | nullptr, 178 | instance, 179 | this); 180 | 181 | hwndToInstance[hwnd] = this; 182 | 183 | SetWindowLong(this->hwnd, GWL_STYLE, 0); /* removes title, borders. */ 184 | 185 | SetWindowPos( 186 | this->hwnd, 187 | nullptr, 188 | offscreen, offscreen, 50, 50, 189 | SWP_FRAMECHANGED | SWP_SHOWWINDOW); 190 | 191 | this->initIcon(); 192 | 193 | this->notify(); 194 | } 195 | 196 | TrayMenu::~TrayMenu() { 197 | Shell_NotifyIcon(NIM_DELETE, &this->iconData); 198 | DestroyWindow(this->hwnd); 199 | 200 | auto it = hwndToInstance.find(this->hwnd); 201 | if (it != hwndToInstance.end()) { 202 | hwndToInstance.erase(it); 203 | } 204 | } 205 | 206 | void TrayMenu::initIcon() { 207 | this->iconData = {}; 208 | this->iconData.hWnd = this->hwnd; 209 | this->iconData.uID = 0; 210 | this->iconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; 211 | this->iconData.uCallbackMessage = WM_TRAYICON; 212 | this->iconData.hIcon = trayIcon; 213 | 214 | static const std::wstring title = 215 | std::wstring(L"dimmer") + L" - " + std::wstring(version); 216 | 217 | ::wcscpy_s(this->iconData.szTip, 255, title.c_str()); 218 | 219 | Shell_NotifyIcon(NIM_ADD, &this->iconData); 220 | this->iconData.uVersion = NOTIFYICON_VERSION; 221 | Shell_NotifyIcon(NIM_SETVERSION, &this->iconData); 222 | } 223 | 224 | void TrayMenu::setPopupMenuChangedCallback(PopupMenuChanged callback) { 225 | this->popupMenuChanged = callback; 226 | } 227 | 228 | LRESULT CALLBACK TrayMenu::windowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { 229 | switch (msg) { 230 | case WM_KEYDOWN: { 231 | auto instance = hwndToInstance.find(hwnd)->second; 232 | if ((instance->middleFlags & MiddleDown) != 0) { 233 | /* 1 through 9 */ 234 | if (wParam >= 0x31 && wParam <= 0x39) { 235 | size_t index = wParam - 0x31; 236 | auto monitors = queryMonitors(); 237 | if (monitors.size() > index) { 238 | auto monitor = monitors[index]; 239 | setMonitorEnabled(monitor, !isMonitorEnabled(monitor)); 240 | instance->monitorsChanged(); 241 | refocus(hwnd); 242 | instance->middleFlags |= MiddleProcessed; 243 | } 244 | } 245 | return 1; 246 | } 247 | break; 248 | } 249 | 250 | case WM_TRAYICON: { 251 | auto instance = hwndToInstance.find(hwnd)->second; 252 | auto type = LOWORD(lParam); 253 | 254 | if (type == WM_MBUTTONDOWN) { 255 | if (instance->popupMenuChanged) { 256 | instance->popupMenuChanged(true); 257 | } 258 | 259 | instance->middleFlags |= MiddleDown; 260 | refocus(hwnd); 261 | return 1; 262 | } 263 | 264 | if (type == WM_MBUTTONUP) { 265 | if ((instance->middleFlags & MiddleProcessed) == 0) { 266 | setDimmerEnabled(!isDimmerEnabled()); 267 | instance->notify(); 268 | } 269 | else { 270 | if (instance->popupMenuChanged) { 271 | instance->popupMenuChanged(false); 272 | } 273 | } 274 | instance->middleFlags = 0; 275 | return 1; 276 | } 277 | 278 | if (type == WM_LBUTTONUP || type == WM_RBUTTONUP) { 279 | if (instance->popupMenuChanged) { 280 | instance->popupMenuChanged(true); 281 | } 282 | 283 | menu = createMenu(instance->hwnd); 284 | 285 | /* SetForegroundWindow + PostMessage(WM_NULL) is a hack to prevent 286 | "sticky popup menu syndrome." i hate you, win32api. */ 287 | ::SetForegroundWindow(hwnd); 288 | 289 | POINT cursor = {}; 290 | ::GetCursorPos(&cursor); 291 | 292 | /* TPM_RETURNCMD instructs this call to take over the message loop, 293 | and effectively wait, for the user to make a selection. */ 294 | DWORD id = (DWORD)TrackPopupMenuEx( 295 | menu, TPM_RETURNCMD, cursor.x, cursor.y, hwnd, nullptr); 296 | 297 | PostMessage(hwnd, WM_NULL, 0, 0); 298 | 299 | /* process the selection... */ 300 | if (id == MENU_ID_EXIT) { 301 | PostQuitMessage(0); 302 | return 1; 303 | } 304 | else if (id == MENU_ID_POLL) { 305 | setPollingEnabled(!isPollingEnabled()); 306 | } 307 | else if (id == MENU_ID_ENABLED) { 308 | setDimmerEnabled(!isDimmerEnabled()); 309 | } 310 | else if (id >= MENU_ID_MONITOR_BASE) { 311 | auto index = (id / MENU_ID_MONITOR_BASE) - 1; 312 | auto monitors = queryMonitors(); 313 | 314 | if (monitors.size() > (size_t)index) { 315 | auto monitor = monitors[index]; 316 | auto value = id - (MENU_ID_MONITOR_BASE * (index + 1)); 317 | 318 | if (value >= MENU_ID_DEFAULTK && value <= MENU_ID_6000K) { 319 | int temperature = -1; 320 | switch (value) { 321 | case MENU_ID_4500K: temperature = 4500; break; 322 | case MENU_ID_5000K: temperature = 5000; break; 323 | case MENU_ID_5500K: temperature = 5500; break; 324 | case MENU_ID_6000K: temperature = 6000; break; 325 | } 326 | setMonitorTemperature(monitor, temperature); 327 | } 328 | else if (id >= MENU_ID_MONITOR_BASE) { 329 | /* if above MENU_ID_MONITOR_USER it's not one of the % toggles */ 330 | if (value < MENU_ID_MONITOR_USER) { 331 | float opacity = (float)value / 100; 332 | setMonitorOpacity(monitor, opacity); 333 | } 334 | } 335 | } 336 | } 337 | 338 | hwndToInstance.find(hwnd)->second->notify(); 339 | 340 | if (instance->popupMenuChanged) { 341 | instance->popupMenuChanged(false); 342 | } 343 | } 344 | return 0; 345 | } 346 | 347 | case WM_DISPLAYCHANGE: { 348 | hwndToInstance.find(hwnd)->second->notify(); 349 | break; 350 | } 351 | } 352 | 353 | return DefWindowProc(hwnd, msg, wParam, lParam); 354 | } -------------------------------------------------------------------------------- /src/TrayMenu.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Copyright (c) 2007-2017 Casey Langen 4 | // 5 | // All rights reserved. 6 | // 7 | // Redistribution and use in source and binary forms, with or without 8 | // modification, are permitted provided that the following conditions are met: 9 | // 10 | // * Redistributions of source code must retain the above copyright notice, 11 | // this list of conditions and the following disclaimer. 12 | // 13 | // * Redistributions in binary form must reproduce the above copyright 14 | // notice, this list of conditions and the following disclaimer in the 15 | // documentation and/or other materials provided with the distribution. 16 | // 17 | // * Neither the name of the author nor the names of other contributors may 18 | // be used to endorse or promote products derived from this software 19 | // without specific prior written permission. 20 | // 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | // POSSIBILITY OF SUCH DAMAGE. 32 | // 33 | ////////////////////////////////////////////////////////////////////////////// 34 | 35 | #pragma once 36 | 37 | #include "Monitor.h" 38 | #include 39 | #include 40 | #include 41 | 42 | namespace dimmer { 43 | class TrayMenu { 44 | using MonitorsChanged = std::function; 45 | using PopupMenuChanged = std::function; 46 | 47 | public: 48 | TrayMenu(HINSTANCE instance, MonitorsChanged callback); 49 | ~TrayMenu(); 50 | 51 | void setPopupMenuChangedCallback(PopupMenuChanged callback); 52 | 53 | private: 54 | enum { 55 | MiddleDown = 1, 56 | MiddleProcessed = 2 57 | }; 58 | 59 | void initIcon(); 60 | 61 | void notify() { 62 | if (monitorsChanged) { 63 | monitorsChanged(); 64 | } 65 | } 66 | 67 | static LRESULT CALLBACK windowProc( 68 | HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); 69 | 70 | HWND hwnd; 71 | int middleFlags; 72 | NOTIFYICONDATA iconData; 73 | MonitorsChanged monitorsChanged; 74 | PopupMenuChanged popupMenuChanged; 75 | }; 76 | } -------------------------------------------------------------------------------- /src/Util.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Copyright (c) 2007-2017 Casey Langen 4 | // 5 | // All rights reserved. 6 | // 7 | // Redistribution and use in source and binary forms, with or without 8 | // modification, are permitted provided that the following conditions are met: 9 | // 10 | // * Redistributions of source code must retain the above copyright notice, 11 | // this list of conditions and the following disclaimer. 12 | // 13 | // * Redistributions in binary form must reproduce the above copyright 14 | // notice, this list of conditions and the following disclaimer in the 15 | // documentation and/or other materials provided with the distribution. 16 | // 17 | // * Neither the name of the author nor the names of other contributors may 18 | // be used to endorse or promote products derived from this software 19 | // without specific prior written permission. 20 | // 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | // POSSIBILITY OF SUCH DAMAGE. 32 | // 33 | ////////////////////////////////////////////////////////////////////////////// 34 | 35 | #include 36 | #include 37 | #include "Util.h" 38 | 39 | namespace dimmer { 40 | std::string u16to8(const std::wstring& utf16) { 41 | int size = WideCharToMultiByte(CP_UTF8, 0, utf16.c_str(), -1, 0, 0, 0, 0); 42 | if (size <= 0) return ""; 43 | char* buffer = new char[size]; 44 | WideCharToMultiByte(CP_UTF8, 0, utf16.c_str(), -1, buffer, size, 0, 0); 45 | std::string utf8str(buffer); 46 | delete[] buffer; 47 | return utf8str; 48 | } 49 | 50 | std::wstring u8to16(const std::string& utf8) { 51 | int size = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, 0, 0); 52 | if (size <= 0) return L""; 53 | wchar_t* buffer = new wchar_t[size]; 54 | MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, buffer, size); 55 | std::wstring utf16fn(buffer); 56 | delete[] buffer; 57 | return utf16fn; 58 | } 59 | 60 | std::string fileToString(const std::wstring& fn) { 61 | FILE* f = _wfopen(fn.c_str(), L"rb"); 62 | std::string result; 63 | 64 | if (!f) { 65 | return result; 66 | } 67 | 68 | fseek(f, 0, SEEK_END); 69 | long len = ftell(f); 70 | rewind(f); 71 | 72 | if (len > 0) { 73 | char* bytes = new char[len]; 74 | fread(static_cast(bytes), len, 1, f); 75 | result.assign(bytes, len); 76 | delete[] bytes; 77 | } 78 | 79 | fclose(f); 80 | 81 | return result; 82 | } 83 | 84 | bool stringToFile(const std::wstring& fn, const std::string& str) { 85 | FILE* f = _wfopen(fn.c_str(), L"wb"); 86 | 87 | if (!f) { 88 | return false; 89 | } 90 | 91 | size_t written = fwrite(str.c_str(), str.size(), 1, f); 92 | fclose(f); 93 | return (written == str.size()); 94 | } 95 | 96 | std::wstring getDataDirectory() { 97 | std::wstring directory; 98 | DWORD bufferSize = GetEnvironmentVariable(L"APPDATA", 0, 0); 99 | wchar_t *buffer = new wchar_t[bufferSize + 2]; 100 | GetEnvironmentVariable(L"APPDATA", buffer, bufferSize); 101 | directory.assign(buffer); 102 | directory += L"\\dimmer"; 103 | SHCreateDirectoryEx(nullptr, directory.c_str(), nullptr); 104 | delete[] buffer; 105 | return directory; 106 | } 107 | } -------------------------------------------------------------------------------- /src/Util.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Copyright (c) 2007-2017 Casey Langen 4 | // 5 | // All rights reserved. 6 | // 7 | // Redistribution and use in source and binary forms, with or without 8 | // modification, are permitted provided that the following conditions are met: 9 | // 10 | // * Redistributions of source code must retain the above copyright notice, 11 | // this list of conditions and the following disclaimer. 12 | // 13 | // * Redistributions in binary form must reproduce the above copyright 14 | // notice, this list of conditions and the following disclaimer in the 15 | // documentation and/or other materials provided with the distribution. 16 | // 17 | // * Neither the name of the author nor the names of other contributors may 18 | // be used to endorse or promote products derived from this software 19 | // without specific prior written permission. 20 | // 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | // POSSIBILITY OF SUCH DAMAGE. 32 | // 33 | ////////////////////////////////////////////////////////////////////////////// 34 | 35 | #pragma once 36 | 37 | #include 38 | 39 | namespace dimmer { 40 | extern std::string fileToString(const std::wstring& fn); 41 | extern bool stringToFile(const std::wstring& fn, const std::string& contents); 42 | extern std::wstring getDataDirectory(); 43 | extern std::string u16to8(const std::wstring& input); 44 | extern std::wstring u8to16(const std::string& input); 45 | } -------------------------------------------------------------------------------- /src/dimmer.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clangen/dimmer/23a77a808e12d782109f78b15b1353653020a288/src/dimmer.ico -------------------------------------------------------------------------------- /src/dimmer.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clangen/dimmer/23a77a808e12d782109f78b15b1353653020a288/src/dimmer.rc -------------------------------------------------------------------------------- /src/dimmer.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27004.2005 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dimmer", "dimmer.vcxproj", "{9867E299-7151-4EE9-9A0F-499F9B515060}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x86 = Debug|x86 11 | Release|x86 = Release|x86 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {9867E299-7151-4EE9-9A0F-499F9B515060}.Debug|x86.ActiveCfg = Debug|Win32 15 | {9867E299-7151-4EE9-9A0F-499F9B515060}.Debug|x86.Build.0 = Debug|Win32 16 | {9867E299-7151-4EE9-9A0F-499F9B515060}.Release|x86.ActiveCfg = Release|Win32 17 | {9867E299-7151-4EE9-9A0F-499F9B515060}.Release|x86.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {F953DD97-1D4C-4BAD-B68D-F25511DE0729} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /src/dimmer.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | 15.0 15 | {9867E299-7151-4EE9-9A0F-499F9B515060} 16 | dimmer 17 | 10.0.16299.0 18 | 19 | 20 | 21 | Application 22 | true 23 | v141_xp 24 | Unicode 25 | 26 | 27 | Application 28 | false 29 | v141_xp 30 | true 31 | Unicode 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | Level3 49 | Disabled 50 | true 51 | WIN32_LEAN_AND_MEAN;NOMINMAX;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) 52 | MultiThreadedDebug 53 | 54 | 55 | Windows 56 | Comctl32.lib;dxva2.lib;%(AdditionalDependencies) 57 | 58 | 59 | 60 | 61 | Level3 62 | MinSpace 63 | true 64 | true 65 | true 66 | WIN32_LEAN_AND_MEAN;NOMINMAX;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) 67 | MultiThreaded 68 | 69 | 70 | true 71 | true 72 | false 73 | Windows 74 | Comctl32.lib;%(AdditionalDependencies) 75 | 76 | 77 | if exist "z:\upx.exe" "z:\upx.exe" -9 "$(TargetPath)" 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /src/dimmer.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 6 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 7 | 8 | 9 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 10 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 11 | 12 | 13 | 14 | 15 | src 16 | 17 | 18 | src 19 | 20 | 21 | src 22 | 23 | 24 | src 25 | 26 | 27 | src 28 | 29 | 30 | 31 | 32 | src 33 | 34 | 35 | src 36 | 37 | 38 | src 39 | 40 | 41 | src 42 | 43 | 44 | src 45 | 46 | 47 | src 48 | 49 | 50 | 51 | 52 | Resource Files 53 | 54 | 55 | 56 | 57 | Resource Files 58 | 59 | 60 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // 3 | // Copyright (c) 2007-2017 Casey Langen 4 | // 5 | // All rights reserved. 6 | // 7 | // Redistribution and use in source and binary forms, with or without 8 | // modification, are permitted provided that the following conditions are met: 9 | // 10 | // * Redistributions of source code must retain the above copyright notice, 11 | // this list of conditions and the following disclaimer. 12 | // 13 | // * Redistributions in binary form must reproduce the above copyright 14 | // notice, this list of conditions and the following disclaimer in the 15 | // documentation and/or other materials provided with the distribution. 16 | // 17 | // * Neither the name of the author nor the names of other contributors may 18 | // be used to endorse or promote products derived from this software 19 | // without specific prior written permission. 20 | // 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | // POSSIBILITY OF SUCH DAMAGE. 32 | // 33 | ////////////////////////////////////////////////////////////////////////////// 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | #include "Monitor.h" 43 | #include "Overlay.h" 44 | #include "TrayMenu.h" 45 | #include "Util.h" 46 | 47 | #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") 48 | 49 | using OverlayPtr = std::shared_ptr; 50 | using Overlays = std::map; 51 | static Overlays overlays; 52 | static std::vector monitors; 53 | 54 | static void updateOverlays(HINSTANCE instance) { 55 | monitors = dimmer::queryMonitors(); 56 | 57 | Overlays old; 58 | std::swap(overlays, old); 59 | 60 | if (dimmer::isDimmerEnabled()) { 61 | for (auto monitor : monitors) { 62 | auto id = monitor.getId(); 63 | auto it = old.find(monitor.getId()); 64 | 65 | OverlayPtr overlay; 66 | if (it != old.end()) { 67 | overlay = it->second; 68 | overlay->update(monitor); 69 | } 70 | else { 71 | overlay = std::make_shared(instance, monitor); 72 | } 73 | 74 | overlays[id] = overlay; 75 | } 76 | } 77 | } 78 | 79 | int CALLBACK wWinMain(HINSTANCE instance, HINSTANCE prev, LPWSTR args, int showType) { 80 | InitCommonControlsEx(nullptr); 81 | 82 | dimmer::loadConfig(); 83 | 84 | dimmer::TrayMenu trayMenu(instance, [instance]() { 85 | updateOverlays(instance); 86 | }); 87 | 88 | trayMenu.setPopupMenuChangedCallback([](bool visible) { 89 | for (auto overlay : overlays) { 90 | if (visible) { 91 | overlay.second->killTimer(); 92 | } 93 | else { 94 | overlay.second->startTimer(); 95 | } 96 | } 97 | }); 98 | 99 | MSG msg = {}; 100 | while (GetMessage(&msg, nullptr, 0, 0)) { 101 | TranslateMessage(&msg); 102 | DispatchMessage(&msg); 103 | } 104 | 105 | dimmer::saveConfig(); 106 | 107 | monitors.clear(); 108 | overlays.clear(); 109 | 110 | return 0; 111 | } 112 | 113 | -------------------------------------------------------------------------------- /src/resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by dimmer.rc 4 | // 5 | #define IDI_TRAY_ICON 103 6 | 7 | // Next default values for new objects 8 | // 9 | #ifdef APSTUDIO_INVOKED 10 | #ifndef APSTUDIO_READONLY_SYMBOLS 11 | #define _APS_NEXT_RESOURCE_VALUE 104 12 | #define _APS_NEXT_COMMAND_VALUE 40001 13 | #define _APS_NEXT_CONTROL_VALUE 1001 14 | #define _APS_NEXT_SYMED_VALUE 101 15 | #endif 16 | #endif 17 | --------------------------------------------------------------------------------