Welcome to My Blogatory!

Nightcap Update

For the past couple of weeks during the wee early morning hours before the kiddos wake, I have working on my latest pet-project, “Nightcap“. I thought that the program was mostly done. I should know by now to be more cautious of this mindset. I had been running Nightcap on my two Windows 8 PCs for months now without much ado, but when I took a closer look at the code base, I knew a couple of things needed changing before the much anticipated product release. My main concern was with the event-based approach that I used to detect keyboard and mouse activity. At first it appeared to work well, but during testing on our home PCs, my little beta testers (Thing 1 and Thing 2) noticed that their computers fell asleep while playing games. Clearly there was a lot of activity happening, but for some reason Nightcap had stopped noticing.

It turns out that Windows 8 was secretly unhooking my event monitors and replacing them with Folgers Crystals…

Well not actually that, but it might as well have. The net effect was that my app could no longer sense anything about the computer and that was kind of the whole point of Nightcap. Why did Windows have it in for my app? Perhaps Windows had deemed Nightcap a poor citizen of the OS? I did a little research and learned that in recent years Microsoft had discouraged event monitoring for programs like Nightcap for good reason. These event-hooking hogs can really slow a computer down. Microsoft recommends using Raw Input instead of events hooks because it doesn’t hamper system processes and provides a more efficient way to delivery device messages. Reading over the documentation, I could see that this approach was radically different than event hooking. Being so close to completing my app, I was desperate to find a quick-fix. Clearly, I wasn’t the first person to write an app that used event monitoring. I wasn’t thrilled about reworking the core of my app when it was 99% there. After a quick search, I learned about a workaround where you can fudge a registry setting to set the bar so low that no process, however laggy, would ever be unhooked. The “fix” worked, but the hack never sat well with me. I had sort of forgot about that workaround while I was in the final push to get Nightcap up on my website. Without this hack, I imagined thousands of computers inadvertently falling to sleep while folks were playing games or composing emails. Bottom line, this was no way to release Nightcap.

Which brings me to last week… I did some more research and decided that Raw Input processing was definitely the way to go even if I had to rewrite Nightcap. Unfortunately, the API was written for C++ and if I were to use it in my C# app, I would have to come up with a way to integrate the unmanaged code. Being new to C#, that was a learning process in itself, but I stuck with it and got the raw input code to work within Nightcap. However, after some initial testing, I noticed Nightcap had stopped responding to click events. Somehow the WM_INPUT handling was interfering with the normal event processing. After a few days of trying out various C++/C# incarnations without much luck, a new plan started to emerge. I thought,

“What if I separate out the event processing part of my app from the reporting and configuration piece?”

Eureka! I can write the input event monitoring as a Window Service and leave the C# app as-is. Only now, the visual aspect of Nightcap would interact with the behind scenes service to tell it under which circumstances to put the computer to sleep. The nice part about this approach is that the monitoring part of the program will be active even when no one is logged in. This comes in handy if your PC wakes up in the middle of the night because of LAN activity.

My blog has been neglected for the past couple of weeks, so I felt an update was in order. I hope to have a beta version of Nightcap on my website by May. As a side note, I really enjoy working with Microsoft tools once again. Visual Studio 13 is fun to work with and in my experience is the best programmer’s workbench in the industry. Debugging code is so well done and I really like how git has been integrated into the IDE. Nice work!