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?
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.
Or use GetAsyncKeyState for <space>, and GetCursorPos...
Thanks.
Where and what do I start studying for this?
Andy
Holy cow !!!
Lots of complicated ways of doing something that's built into the API...
GetLastInputInfo (http://msdn.microsoft.com/en-us/library/ms646302%28VS.85%29.aspx)
Edgar
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
It would be best to hook into the Firefox window.
Baluga Boo,
I did a search for "hook into window" and didn't find anything.
Andy
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
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.
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
Maybe something like Auto Shutdown (https://addons.mozilla.org/af/firefox/addon/5452/versions/)?
Quote from: sinsi on November 19, 2010, 10:41:19 AM
Maybe something like Auto Shutdown (https://addons.mozilla.org/af/firefox/addon/5452/versions/)?
:bg Now that just takes all the fun out of it. Good find 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)
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.
Sinsi,
I will accomplish the goal with learning as the priority.
Quote from: donkey on November 19, 2010, 02:41:00 AM
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
Masm complained about invoke FindWindow,"MozillaUIWindowClass",NULL
I can use this to find if it's running.
invoke Process32First,hProcesses,ADDR pe32
.IF eax
.WHILE bLoop
invoke CompareString, LOCALE_USER_DEFAULT, NORM_IGNORECASE, addr pe32.szExeFile, -1, lpszExecutable, -1
.IF eax==2 ; check if strings are equal in lexical value
I have to plan the order of execution now.
1. Find if FF is running
2. Use my GetLastInputInfo code to watch input activity
3. Figure out which values to subtract to determine when 5 minutes of "user inactivity" have occured
4. Shutdown FF gracefully