MouseLockLost Event

Since we began hacking firefox, I was curious to find out how the events were handled internally.
I saw the mouselocklost event as an opportunity to hack around the event chain in firefox and see how all the differents events worked.
First I started tracking the hierarchy of the nsDOMMouseEvent, that led me to nsDOMUIEvent and nsDOMevent. On the nsDOMEvent.h file, there was an enum with the name of all firefox events. Next I looked at the nsDOMEvent.cpp, I saw that there was an array, again, with the names of all firefox events, still in the nsDOMEvent.cpp there was a method called GetEventName that used a switch statement to find which event was fired in the browser and return its name.

switch(aEventType) {
1119     return sEventNames[eDOMEvents_mousedown];
1120   case NS_MOUSE_BUTTON_UP:
1121     return sEventNames[eDOMEvents_mouseup];

and the list just kept getting bigger and bigger, I think in total, there were 134 different events.
An interesting point:

  • For some reason, every time I clicked on the browser, the NS_MOUSE_BUTTON_UP event got fired, but the NS_MOUSE_BUTTON_DOWN, and NS_MOUSE_CLICK didn’t.

So my next move was to search on mxr for the macros name to see where they were declared.
All the macros were declared in the nsGUIEvent.h file.
I wasn’t sure where to declare, and which value to give to the NS_MOUSELOCKLOST macro, so I just placed together with all the other mouse event macros and incremented by one the value of the last mousemacro:

#define NS_MOUSE_MESSAGE_START          300
#define NS_MOUSE_MOVE                   (NS_MOUSE_MESSAGE_START)
#define NS_MOUSE_BUTTON_UP              (NS_MOUSE_MESSAGE_START + 1)
#define NS_MOUSE_ENTER                  (NS_MOUSE_MESSAGE_START + 22)
#define NS_MOUSE_EXIT                   (NS_MOUSE_MESSAGE_START + 23)
#define NS_MOUSE_CLICK                  (NS_MOUSE_MESSAGE_START + 27)
#define NS_MOUSE_ACTIVATE               (NS_MOUSE_MESSAGE_START + 30)
#define NS_MOUSEENTER                   (NS_MOUSE_MESSAGE_START + 34)
#define NS_MOUSELEAVE                   (NS_MOUSE_MESSAGE_START + 35)
#define NS_MOUSELOCKLOST                (NS_MOUSE_MESSAGE_START + 36)

After declaring the mouselocklost macro, I kept searching on mxr for any other occurrences of the event macros in the source code, I found that they were also used in the nsEventNameList.h. I wasn’t sure which event name type I should use, so I added EventNameType_All, since it was used by most other mouse events.


I then decided it was time to build to see if the mouselocklost event was being registered. However, I got some compile errors:

In file included from /home/diogogmt/mozilla-central/dom/base/nsGlobalWindow.cpp:10733:
../../dist/include/nsEventNameList.h:278: error: no ‘nsresult nsGlobalWindow::GetOnmouselocklost(JSContext*, jsval*)’ member function declared in class ‘nsGlobalWindow’
In file included from /home/diogogmt/mozilla-central/dom/base/nsGlobalWindow.cpp:10733:
../../dist/include/nsEventNameList.h:278: error: no ‘nsresult nsGlobalWindow::SetOnmouselocklost(JSContext*, const jsval&)’ member function declared in class ‘nsGlobalWindow’

Those errors were familiar, they meant that the getter and setter for the event didn’t exist, and I remembered from the GetScreenX and GetScreenY example that the Get and Set were added just for the c++ code, and that in javascript would be the just the method name, without Get and Set.
I thought about adding those methods in the nsGlobalWindow.cpp, but I looked at the file and didn’t find any getter/setter for the other events, so I knew I was missing something.
I went back searching on mxr, but this time without success. My next move was go  to IRC. I followed David Humphrey suggestion and asked first on the #seneca channel, then I went to the #introduction channel, and there they told me it would be best if I asked the question on the #developers channel. That’s what I did, and in less than 5 min I got an answer, I’m amazed about the speed and quality of the answers on IRC, I think that’s one of the reasons for the huge success of Mozilla, having a very strong community of developers.

So the answer I got on the channel, was that I was missing adding the mouselocklost event to two files, nsGkAtomList.h and nsIInlineEventHandlers.idl

They also recommended that I look at this patch.

I made the necessary changes on the code, and this time it worked 🙂
I’ve commited the changes to here and here

I’m not even sure that if that is how the mouselocklost event should be implemented, but I remembered the quote from today’s class:

The perfect is the enemy of the good

and instead of trying to write the perfect line of code, I just kept hammering around lots of files, and in the end, even though it was not a final solution, is was start.

What’s my next plan?

  • Find how to dispatch an event
  • Callback functions

3 Comments on “MouseLockLost Event”

  1. Nice work! The event stuff is tricky to get right, because it’s spread across so many files. And the mouse and UI events are doubly complicated. I don’t think we have to bother with the mouse event stuff (i.e., we just need a simple event). If you look at how the mozfullscreenchange event works, which we used in class, you’ll see one way:

    Or take a look at how simple events are dispatched for the media element:

  2. […] a handle on the code (limited to the MouseLockable class) and encouraged by the progress made by Diogo and Raymond in implementing some of the tasks required for the Mouse Lock implementation, I decided […]

  3. […] with complex UI cases in Mochitests, how to exit mouse lock when the Escape key is pressed, how to add the MouseLockLost event, where to store the mouse movement state between events, etc.  There’s also been more […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s