Re: [Pharo-dev] How to listen for windows messages?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-dev] How to listen for windows messages?

Eliot Miranda-2
 
Hi Phil, Hi All,

On Tue, Feb 28, 2017 at 4:29 AM, [hidden email] <[hidden email]> wrote:
Hi,

Pharo is itself a Windows application (under Windows of course).

So Window event loop is picking up the events and transforming them into Pharo events.

Right, but this is *not good enough* :-)  It is a fundamentally different architecture.  The processing of Windows events in the Windows VM transforms *event callbacks* into   an *event queue*.  This is fine for events such as mouse clicks, keyboard presses, etc.  But it is fundamentally broken for events such as those that ask an application to quit because the OS is about to exit, or events that try to obtain mouse feedback while moving a native window, etc, etc.

So one either needs to extend the VM event queue so that one can install callbacks for certain kinds of events still providing a cross-platform interface), or, as Vassili did for Newspeak native windows (which was fully working in 2008, with the ability to switch a window between emulated (Morphic) and native at will or on image startup), interface to the native Windows event pump via callbacks.



You can check this in https://github.com/pharo-project/pharo-vm

in opensmalltalk-vm\platforms\win32\vm\sqWin32Window.c

https://github.com/pharo-project/pharo-vm/blob/master/opensmalltalk-vm/platforms/win32/vm/sqWin32Window.c

Like L235...

LRESULT CALLBACK MainWndProcW(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)

You'll see that we stick a lot of stuff into evt->... in various ways.

There is a default fallback at the end.

default:
/* Unprocessed messages may be processed outside the current
module. If firstMessageHook is non-NULL and returns a non
zero value, the message has been successfully processed */
if(firstMessageHook)
if((*firstMessageHook)(hwnd, message, wParam, lParam))
return 1;
return DefWindowProcW(hwnd,message,wParam,lParam);


{


So, if firstMessageHook exists, one can do whatever.

Now, a Windows manager may need other stuff and so on.
What you are looking at is how to hook this VM side with more of the general Windows system.

These hooks are globals, so should be accessible.

/****************************************************************************/
/* Message Processing */
/****************************************************************************/
/* The last dispatched event. It is used for the event processing mechanism. */
MSG *lastMessage = NULL;
/* The entry to the message hooks called from the window procedure.
If another module requires to process messages by itself, it should
put its message procedure in this place. */
messageHook firstMessageHook = 0;
/* The entry to a pre-message hook. Can be used to intercept any messages
to the squeak main window. Useful for modules that wish to be notified
about certain messages before they are processed. */
messageHook preMessageHook = 0;

Never played with these but any Windows integration is of interest to me.

So, go ahead. Nothing would resist focused work.

Be aware that there is another VM underway with a cleanup of all of this but this is not released yet.

Keep us posted.
Phil




On Tue, Feb 28, 2017 at 9:33 AM, Torsten Bergmann <[hidden email]> wrote:

>
> Hi,
>
> I guess what you want to achieve will not be an easy task for you if you are new to programming.
> But it is always good to have a goal and if you have time to learn I'm pretty sure you will master
> it.
>
> Even when OS-Windows inspire you for automizing tasks in other Windows processes (like autoit) you
> should not start with my OS-Windows project directly as contributing to it requires knowledge
> on your side on Smalltalk as well as Win32 C programming.
>
> First start with learning Smalltalk and Pharo
>   - http://files.pharo.org/books/
>   - http://pharo.pharocloud.com/pharobooks
>   - http://stephane.ducasse.free.fr/FreeBooks.html
>
> After having an idea about Smalltalk and Pharo you should try to learn UFFI which
> is Pharos unified foreign function interface and ability to call C DLL's.
>
> https://ci.inria.fr/pharo-contribution/view/Books/job/PharoBookWorkInProgress/lastSuccessfulBuild/artifact/book-result/UnifiedFFI/
>
> Sideways start learning about the Win32 API and C as this helps to better understand
> how things are done on the Windows side. There are many, many tutorials out there.
> Try to call or wrap some simple DLL functions on you own first.
>
> There is a nice and tiny C compiler if you want to try out some C samples
> http://www.pellesc.de/index.php?page=download&lang=en
>
> With this initial knowledge you can try to understand why and how OS-Windows was done.
> I wrote many tests - just add a breakpoint and run them to debug through the code to understand.
> Google for the API descriptions of the Win32 functions that are called to get an understanding
> how Pharo and C get connected.
>
> Also check how you work with callbacks in UFFI (as this would be required for a hook).
>
> If you are then still interested in interception windows messages you should start reading here
> https://msdn.microsoft.com/en-us/library/windows/desktop/ms632589(v=vs.85).aspx
>
> Primarily one has to wrap SetWindowsHookEx API function and friends and wrap a good API
> for them in Pharo.
>
> Hope that helps to get started.
>
> Regards
> Torsten
>
> > Gesendet: Dienstag, 28. Februar 2017 um 06:04 Uhr
> > Von: lw1990 <[hidden email]>
> > An: [hidden email]
> > Betreff: [Pharo-dev] How to listen for windows messages?
> >
> > In the OS-Windows package, there exists the ability to use the windows api in
> > Pharo.
> > This is very powerful, and I intend to add more of the windows api into it
> > (it's only partially implemented).
> >
> > This appears to all have been done with DllCalls (FFI calls) so far.
> >
> > A big part of interacting with windows is listening to/intercepting and
> > responding to windows messages.
> > For example, every time a user presses a key on the keyboard, a windows
> > message will happen in the background for that key. If Pharo was aware of
> > these messages, then Pharo could do things in response to hotkeys pressed on
> > Windows (outside of a pharo window).
> >
> > It would also make reacting to events potentially nicer. Like making a
> > windows-desktop-manager in Pharo that can tell when a new window is created
> > by windows (maybe they opened notepad). It would certainly be better than an
> > infinite loop or timer of 'get active window and compare to last active
> > window and see if it changed'. Instead it would be 'when receive the
> > WM_MESSAGE for new window created, notify Pharo so it can react'.
> >
> > How can I set up a windows message hook in Pharo?
> > Please keep in mind I'm very new to programming, but I work from home and
> > have lots of time to learn :-)
> >
> >
> >
> > --
> > View this message in context: http://forum.world.st/How-to-listen-for-windows-messages-tp4936285.html
> > Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.
> >
> >
>



--
_,,,^..^,,,_
best, Eliot
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-dev] How to listen for windows messages?

philippeback
 


On Tue, Mar 14, 2017 at 4:27 PM, Eliot Miranda <[hidden email]> wrote:
Hi Phil, Hi All,

On Tue, Feb 28, 2017 at 4:29 AM, [hidden email] <[hidden email]> wrote:
Hi,

Pharo is itself a Windows application (under Windows of course).

So Window event loop is picking up the events and transforming them into Pharo events.

Right, but this is *not good enough* :-)  It is a fundamentally different architecture.  The processing of Windows events in the Windows VM transforms *event callbacks* into   an *event queue*.  This is fine for events such as mouse clicks, keyboard presses, etc.  But it is fundamentally broken for events such as those that ask an application to quit because the OS is about to exit, or events that try to obtain mouse feedback while moving a native window, etc, etc.

So one either needs to extend the VM event queue so that one can install callbacks for certain kinds of events still providing a cross-platform interface), or, as Vassili did for Newspeak native windows (which was fully working in 2008, with the ability to switch a window between emulated (Morphic) and native at will or on image startup), interface to the native Windows event pump via callbacks.

Yes, I understand that. But there is some bit rot in the basics already, so, first things first.  And Pharo lowcode VM is also replacing a lot of this with other ways (which kind of bury the Windows thing even deeper in way I guess).

Would the VM core be a libray, we could have more options. Look at V8 ending up in Chrome, NodeJS and Electron.
Or Lua getting all over the place. Tcl also is pretty cool on that front.

Phil



You can check this in https://github.com/pharo-project/pharo-vm

in opensmalltalk-vm\platforms\win32\vm\sqWin32Window.c

https://github.com/pharo-project/pharo-vm/blob/master/opensmalltalk-vm/platforms/win32/vm/sqWin32Window.c

Like L235...

LRESULT CALLBACK MainWndProcW(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)

You'll see that we stick a lot of stuff into evt->... in various ways.

There is a default fallback at the end.

default:
/* Unprocessed messages may be processed outside the current
module. If firstMessageHook is non-NULL and returns a non
zero value, the message has been successfully processed */
if(firstMessageHook)
if((*firstMessageHook)(hwnd, message, wParam, lParam))
return 1;
return DefWindowProcW(hwnd,message,wParam,lParam);


{


So, if firstMessageHook exists, one can do whatever.

Now, a Windows manager may need other stuff and so on.
What you are looking at is how to hook this VM side with more of the general Windows system.

These hooks are globals, so should be accessible.

/****************************************************************************/
/* Message Processing */
/****************************************************************************/
/* The last dispatched event. It is used for the event processing mechanism. */
MSG *lastMessage = NULL;
/* The entry to the message hooks called from the window procedure.
If another module requires to process messages by itself, it should
put its message procedure in this place. */
messageHook firstMessageHook = 0;
/* The entry to a pre-message hook. Can be used to intercept any messages
to the squeak main window. Useful for modules that wish to be notified
about certain messages before they are processed. */
messageHook preMessageHook = 0;

Never played with these but any Windows integration is of interest to me.

So, go ahead. Nothing would resist focused work.

Be aware that there is another VM underway with a cleanup of all of this but this is not released yet.

Keep us posted.
Phil




On Tue, Feb 28, 2017 at 9:33 AM, Torsten Bergmann <[hidden email]> wrote:

>
> Hi,
>
> I guess what you want to achieve will not be an easy task for you if you are new to programming.
> But it is always good to have a goal and if you have time to learn I'm pretty sure you will master
> it.
>
> Even when OS-Windows inspire you for automizing tasks in other Windows processes (like autoit) you
> should not start with my OS-Windows project directly as contributing to it requires knowledge
> on your side on Smalltalk as well as Win32 C programming.
>
> First start with learning Smalltalk and Pharo
>   - http://files.pharo.org/books/
>   - http://pharo.pharocloud.com/pharobooks
>   - http://stephane.ducasse.free.fr/FreeBooks.html
>
> After having an idea about Smalltalk and Pharo you should try to learn UFFI which
> is Pharos unified foreign function interface and ability to call C DLL's.
>
> https://ci.inria.fr/pharo-contribution/view/Books/job/PharoBookWorkInProgress/lastSuccessfulBuild/artifact/book-result/UnifiedFFI/
>
> Sideways start learning about the Win32 API and C as this helps to better understand
> how things are done on the Windows side. There are many, many tutorials out there.
> Try to call or wrap some simple DLL functions on you own first.
>
> There is a nice and tiny C compiler if you want to try out some C samples
> http://www.pellesc.de/index.php?page=download&lang=en
>
> With this initial knowledge you can try to understand why and how OS-Windows was done.
> I wrote many tests - just add a breakpoint and run them to debug through the code to understand.
> Google for the API descriptions of the Win32 functions that are called to get an understanding
> how Pharo and C get connected.
>
> Also check how you work with callbacks in UFFI (as this would be required for a hook).
>
> If you are then still interested in interception windows messages you should start reading here
> https://msdn.microsoft.com/en-us/library/windows/desktop/ms632589(v=vs.85).aspx
>
> Primarily one has to wrap SetWindowsHookEx API function and friends and wrap a good API
> for them in Pharo.
>
> Hope that helps to get started.
>
> Regards
> Torsten
>
> > Gesendet: Dienstag, 28. Februar 2017 um 06:04 Uhr
> > Von: lw1990 <[hidden email]>
> > An: [hidden email]
> > Betreff: [Pharo-dev] How to listen for windows messages?
> >
> > In the OS-Windows package, there exists the ability to use the windows api in
> > Pharo.
> > This is very powerful, and I intend to add more of the windows api into it
> > (it's only partially implemented).
> >
> > This appears to all have been done with DllCalls (FFI calls) so far.
> >
> > A big part of interacting with windows is listening to/intercepting and
> > responding to windows messages.
> > For example, every time a user presses a key on the keyboard, a windows
> > message will happen in the background for that key. If Pharo was aware of
> > these messages, then Pharo could do things in response to hotkeys pressed on
> > Windows (outside of a pharo window).
> >
> > It would also make reacting to events potentially nicer. Like making a
> > windows-desktop-manager in Pharo that can tell when a new window is created
> > by windows (maybe they opened notepad). It would certainly be better than an
> > infinite loop or timer of 'get active window and compare to last active
> > window and see if it changed'. Instead it would be 'when receive the
> > WM_MESSAGE for new window created, notify Pharo so it can react'.
> >
> > How can I set up a windows message hook in Pharo?
> > Please keep in mind I'm very new to programming, but I work from home and
> > have lots of time to learn :-)
> >
> >
> >
> > --
> > View this message in context: http://forum.world.st/How-to-listen-for-windows-messages-tp4936285.html
> > Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.
> >
> >
>



--
_,,,^..^,,,_
best, Eliot