Getting windows messages

I would like to get the WM_DEVICECHANGED message which is sent to top
level windows when there is a device added or removed. I couldn't
find anything in the wiki or the group archives discussing this
although I do remember similar topics discussed several times.

What I am actually doing is a program that will detect when the
CD drive is ejected and then automatically hibernates my computer.
The reason I am doing that is because I am busy converting my
laserdiscs to DVD and it takes several hours for the program
to do the conversion to DVD/mpeg data and then burn the disc.
When the disc is burnt, it is ejected. Consequently I leave
this stuff running overnight, but want my machine powered down
when it is done.

I did start doing the program in C++ but it takes SO much longer
than in Python!

Roger

:slight_smile: I think

might be a better place to search for that kind of functionality.

···

On Tue, 31 Aug 2004 21:21:35 -0700, Roger Binns <rogerb@rogerbinns.com> wrote:

I would like to get the WM_DEVICECHANGED message which is sent to top
level windows when there is a device added or removed. I couldn't
find anything in the wiki or the group archives discussing this
although I do remember similar topics discussed several times.

I did start doing the program in C++ but it takes SO much longer
than in Python!

--
Peter Damoc
Hacker Wannabe

Roger Binns schrieb:

I would like to get the WM_DEVICECHANGED message which is sent to top
level windows when there is a device added or removed. I couldn't
find anything in the wiki or the group archives discussing this
although I do remember similar topics discussed several times.

What I am actually doing is a program that will detect when the
CD drive is ejected and then automatically hibernates my computer.
The reason I am doing that is because I am busy converting my
laserdiscs to DVD and it takes several hours for the program
to do the conversion to DVD/mpeg data and then burn the disc.
When the disc is burnt, it is ejected. Consequently I leave
this stuff running overnight, but want my machine powered down
when it is done.

I did start doing the program in C++ but it takes SO much longer
than in Python!

Roger

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Hi Roger,

I would suggest to use the win32api python extensions.
Install the
http://starship.python.net/crew/mhammond/win32/Downloads.html
file.

There is a module called win32event. Install it and look for the examples. (import win32event)

use something like:

WaitForSingleObject(callbackEvent, 60)==win32event.WAIT_OBJECT_0

Markus

Peter Damoc wrote:

:slight_smile: I think
Python Releases for Windows | Python.org
might be a better place to search for that kind of functionality.

Errr, no. I want to do the gui in wxPython. I just need one
event form the underlying Windows event loop. I have already
written the code in win32all to deal with the message and
parse all the information out, as well as a module to hibernate
the machine.

Roger

Markus von Ehr wrote:

I would suggest to use the win32api python extensions.
Install the
http://starship.python.net/crew/mhammond/win32/Downloads.html
file.

I have no problem dealing with the event itself. That code is
long since done and is easy.

I do not want to write my gui using win32all.

Consequently I need to be using the gui and evenloop of wxPython
but still get this one message.

I do not want wxPython or win32all. I want both in the same
program!

Roger

Have you tried just setting a hibernate timeout in the power options?
I'm not sure whether windows takes into account CPU activity (as well
as user interaction) to determine what constitutes idle time, but it's
at least worth a try.

···

On Tue, 31 Aug 2004 21:21:35 -0700, Roger Binns <rogerb@rogerbinns.com> wrote:

What I am actually doing is a program that will detect when the
CD drive is ejected and then automatically hibernates my computer.
The reason I am doing that is because I am busy converting my
laserdiscs to DVD and it takes several hours for the program
to do the conversion to DVD/mpeg data and then burn the disc.
When the disc is burnt, it is ejected. Consequently I leave
this stuff running overnight, but want my machine powered down
when it is done.

--
charl p. botha http://cpbotha.net/ http://visualisation.tudelft.nl/

:slight_smile: use some kind of IPC and make the module that hibernates comunicate with your app.
The simplest thing I could think is... a file... the module that hibernates reads from a file to see if the operation has completed and if the operation is still not done the hiberntation module sleeps for a period of time after which... it checks again... The GUI app... simply writes the file when done and exits... :slight_smile:

···

On Wed, 1 Sep 2004 02:17:32 -0700, Roger Binns <rogerb@rogerbinns.com> wrote:

Peter Damoc wrote:

:slight_smile: I think
Python Releases for Windows | Python.org
might be a better place to search for that kind of functionality.

Errr, no. I want to do the gui in wxPython. I just need one
event form the underlying Windows event loop. I have already
written the code in win32all to deal with the message and
parse all the information out, as well as a module to hibernate
the machine.

--
Peter Damoc
Hacker Wannabe

Markus von Ehr wrote:

Roger Binns schrieb:

I would like to get the WM_DEVICECHANGED message which is sent to top
level windows when there is a device added or removed. I couldn't
find anything in the wiki or the group archives discussing this
although I do remember similar topics discussed several times.

I did some google-ing because I am also curious about this. These are the things that I found:

This tells you how to use win32all
http://mail.python.org/pipermail/python-list/2004-January/203888.html

But this one specifically talks about wxwidgets:
http://lists.wxwidgets.org/archive/wx-users/msg07615.html

I'm guessing MSWWindowProc is available to override in wxPython, everything else seems to be.

I'm investigating it myself now.

By the way, I did test win32all, and it definitely reports whenever a CD is inserted or removed.

So far, I haven't found MSWWindowProc to override, but I'm getting there.

John
=:->

Roger Binns wrote:

I would like to get the WM_DEVICECHANGED message which is sent to top
level windows when there is a device added or removed. I couldn't
find anything in the wiki or the group archives discussing this
although I do remember similar topics discussed several times.

I thought I had already made a wiki page for this but I also couldn't find it, so I wrote a new one.

http://wiki.wxpython.org/index.cgi/HookingTheWndProc

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Well, I wasn't able to find out how to override the specific message handlers. So I did this little workaround.
Basically, it creates one window using win32api (that is never shown), which just waits on the event message WM_DEVICECHANGED

And that window can have callbacks registered for other objects to inform whenever media has been inserted or removed.

I've tested it here, and it seems to work. I feel like it is a little bit of a hack because of the empty window. But it works quite well, so I don't mind too much.

Hopefully this can help you down the right path.
John
=:->

You might want to look at Robin Dunn's post at:

http://wiki.wxpython.org/index.cgi/HookingTheWndProc

CDNotification.py (5.16 KB)

CDNotifyDlg.py (2.68 KB)

Robin Dunn writes:

I thought I had already made a wiki page for this but I also
couldn't find it, so I wrote a new one.

http://wiki.wxpython.org/index.cgi/HookingTheWndProc

Excellent, thanks. Now, how would one watch for underlying GTK
WM events and Mac events?

···

--
Paul McNett
Independent Software Consultant
http://www.paulmcnett.com

Paul McNett wrote:

Robin Dunn writes:

I thought I had already made a wiki page for this but I also
couldn't find it, so I wrote a new one.

http://wiki.wxpython.org/index.cgi/HookingTheWndProc

Excellent, thanks. Now, how would one watch for underlying GTK
WM events and Mac events?

What are you interested in in particular? Windows is somewhat
different because *everything* is funnelled through the message
queues (and hence WndProc) inlcuding GUI stuff, but also things
like system power events, device notification, even async
networking.

The closest thing in the Linux world is d-bus:

  dbus

It does have Python APIs but I couldn't find documentation for
them. I would think the easiest way to integrate them into
a wxPython program would be to run in a seperate thread and
use wx.PostMessage to notify the main gui thread of interesting
stuff.

Roger

John Meinel wrote:

Hopefully this can help you down the right path.

I actually had already done all the win32 api stuff including
the structure decoding etc. I then started doing the gui
in C and it was too painful. I did more of it in win32all,
but really wanted wxPython since my time is valuable.

http://wiki.wxpython.org/index.cgi/HookingTheWndProc

Yes, that is the piece I needed.

from ctypes import *

You've added ctypes as an extra dependency. I just did all
mine with struct.

def drive_from_mask (mask):
n_drive = 0
while 1:
if (mask & (2 ** n_drive)): return n_drive
else: n_drive += 1

That code is wrong in that it is a list of drives, not just
one. For example you could plug in a compact flash multi-reader
or a firewire drive with multiple partitions.

wc = win32gui.WNDCLASS ()
hinst = wc.hInstance = win32api.GetModuleHandle (None)
wc.lpszClassName = "DeviceChangeDemo"
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
wc.hCursor = win32gui.LoadCursor (0, win32con.IDC_ARROW)
wc.hbrBackground = win32con.COLOR_WINDOW
wc.lpfnWndProc = message_map

I used a dialog indirect which is a little less painful than this.

I also had to write a quick C module to wrap the SetSuspendState
function. I could probably have done it with ctypes etc, but
it only took me 20 seconds in Swig :slight_smile:

Roger

Robin Dunn wrote:

I thought I had already made a wiki page for this but I also couldn't
find it, so I wrote a new one.

http://wiki.wxpython.org/index.cgi/HookingTheWndProc

Thanks you!

Roger

Charl P. Botha wrote:

Have you tried just setting a hibernate timeout in the power options?
I'm not sure whether windows takes into account CPU activity (as well
as user interaction) to determine what constitutes idle time, but it's
at least worth a try.

I would have to keep turning the timeouts on and off etc which would
be a pain. And doing it at the wrong time would be a really bad idea.
Consequently I am sticking to a particular drive being ejected.

Roger