News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Close program after set period of inactivity

Started by Magnum, November 17, 2010, 12:47:09 AM

Previous topic - Next topic

Magnum

I would like Firefox to shut itself down if there is no mouse or keyboard activity for 5 minutes or more.

What would be involved in writing such a program?
Have a great day,
                         Andy

Slugsnack

So basically you want process/window enumeration then timer on the activity. The closing is trivial. You could have a timer that loops EnumWindows() or set a global hook on CreateWindow/Ex(). Then SetWindowsHookEx() to check for activity. You will likely run into complications with enumerating windows that you had not intended to though. It's a tricky business though. I guess if you're only targeting Firefox, that simplifies matters a lot. The window enumeration is really the only tricky part originally.

jj2007

Or use GetAsyncKeyState for <space>, and GetCursorPos...

Magnum

Thanks.

Where and what do I start studying for this?

Andy
Have a great day,
                         Andy

donkey

Holy cow !!!

Lots of complicated ways of doing something that's built into the API...

GetLastInputInfo

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Magnum

With some code to show idle time, sys uptime, and last input.

It shows sys uptime and then zeros out whenever there is input activity.

When there is input, "letzten" output starts incrementing.

Now, to figure out the next step.

Have a great day,
                          Andy

http://intouch.org/magazine/daily-devotional
http://www.happynews.com

Have a great day,
                         Andy

Twister

It would be best to hook into the Firefox window.

Magnum

Baluga Boo,

I did a search for "hook into window" and didn't find anything.

Andy
Have a great day,
                         Andy

donkey

Just send it a WM_CLOSE message, that way you give it the option of not shutting down if its doing something like an update etc... Remember that Firefox and many other programs install their own updates automatically, it would not do to terminate it in the middle of writing data. The programmers of Firefox would have foreseen a situation where a user hit the close button during an update and made allowances for it, you can leverage that by being polite to the application and requesting rather than forcing it to close.

invoke FindWindow,"MozillaUIWindowClass",NULL
test eax,eax
jz NOFIREFOX ; Firefox was not found or there was an error

; Use SendMessage so we can check the return to see if the message was processed
invoke SendMessage, eax, WM_CLOSE, 0, 0
test eax,eax
jz DONE
; There was an error

DONE:
; Firefox has closed


Not sure what the macro is that you MASM guys use for inline strings, you'll have to figure that out yourself for the class name.

You can also use WaitForSingleObject to make sure it has closed by passing the window handle to that function:

invoke FindWindow,"MozillaUIWindowClass",NULL
test eax,eax
jz NOFIREFOX ; Firefox was not found or there was an error
mov ebx,eax ; Save the window handle
; In this case we'll just post the message since we aren't concerned with the return value
invoke PostMessage, ebx, WM_CLOSE, 0, 0
invoke WaitForSingleObject, ebx, 5000 ; Wait a maximum of 5 seconds
test eax,eax
jz DONE
; There was an error

DONE:
; Firefox has closed



Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Magnum

Thanks Edgar.

I didn't consider that even with no keyboard or mouse activity for 5 minutes, FF might be doing something that I would not want forcefully shutdown.

I also need to consider that I may have started a large download and walked away from my computer.


Have a great day,
                         Andy

donkey

Quote from: Magnum on November 19, 2010, 03:59:14 AM
Thanks Edgar.

I didn't consider that even with no keyboard or mouse activity for 5 minutes, FF might be doing something that I would not want forcefully shutdown.

I also need to consider that I may have started a large download and walked away from my computer.

Yes, downloads are also an issue, and one a little more complicated than you might think. Both the main FF window and the download window use the MozillaUIWindowClass window class so if you find one and there is a download in progress you may end up closing the download and not FF. Since the main FF window has a variable window title (<page> - Mozilla Firefox) you cannot use that to identify the main window, however in English versions the download window is always named "Downloads"  :wink

I'll leave it to you to identify the window title before closing it (it will also tell you if there is an active download) but GetWindowText should get you started...

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

sinsi

Light travels faster than sound, that's why some people seem bright until you hear them.

donkey

"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

sinsi

heh, I was thinking more along the lines of seeing what it does than actually using it.
This topic piqued my interest, since I am an IE diehard (suck it up doc  :bg)
Light travels faster than sound, that's why some people seem bright until you hear them.

Magnum

Quote from: sinsi on November 19, 2010, 11:23:15 AM
heh, I was thinking more along the lines of seeing what it does than actually using it.
This topic piqued my interest, since I am an IE diehard (suck it up doc  :bg)

Speaking of IE.

I just read where they were rated higher than FF in having fewer security vulnerabilities.

Chrome rated much worse than both FF and I.E.




Have a great day,
                         Andy