~ / howtos /

Mouse scroll wheel events with GLUT under MacOSX

Copyright © 2006—2008, Renaud Blanch.
last updates :
- added intel mac support (september, 27 2007).
- updated to new glut sources from apple (march, 6 2008).

You should be redirected to an up to date version of this page (including HiDPI support).

What?

I often use GLUT to prototype software using OpenGL. One feature is lacking in this library: the support for mouse scroll wheel events. This support is available for Linux and for GLUT/Win32 using a patched version of GLUT. It mimics the Linux behaviour, where the scroll wheel is mapped on 2 pseudo-buttons. The scroll wheel events are thus received in the traditional mouse callback:

// compatibility with original GLUT

#if !defined(GLUT_WHEEL_UP)
#  define GLUT_WHEEL_UP   3
#  define GLUT_WHEEL_DOWN 4
#endif


void mouse(int button, int state, int x, int y);


int main(int argc, char *argv[]) {
   // ...
   glutMouseFunc(mouse);
   // ...
}


void mouse(int button, int state, int x, int y) {
   switch(button) {
   case GLUT_LEFT_BUTTON:
   case GLUT_RIGHT_BUTTON:
      // ...
      break;

   case GLUT_WHEEL_UP:
   case GLUT_WHEEL_DOWN:
   
      // GLUT sends only GLUT_UP events for scroll wheel events
      assert(state == GLUT_UP);
      
      // ...
      break;
   }
}

You'll find below a patched version of GLUT for Mac OS X that reproduces this behaviour so the same code should compile & run unchanged.

However, you should be aware that replacing the vendor provided version of GLUT can potentially change the behaviour of existing programs under some circumstances. The following mouse event handler:


void mouse(int button, int state, int x, int y) {
   switch(button) {
   case GLUT_LEFT_BUTTON:
   case GLUT_MIDDLE_BUTTON:
   case GLUT_RIGHT_BUTTON:
      // ...
      break;

   default:
      // never reached with original GLUT
      assert(false);
      break;
   }
}

will work as expected. With the original GLUT, the default clause is never reached whereas with this modified version, it is.

Getting & installing

Previous version

Previous version of this howto is available. It provides the same information for older versions of Mac OS X and GLUT.

Warning

You are going to replace the vendor provided version of GLUT that comes with your system. You may want to archive it before proceeding:

[plan9:~] blanch% cd /System/Library/Frameworks/
[plan9:/System/Library/Frameworks] blanch% sudo mv GLUT.framework GLUT.framework.original
Password:
[plan9:/System/Library/Frameworks] blanch% 

Binaries

I provide universal builds of the modified GLUT framework for Mac OS X Leopard (10.5) and another modified GLUT framework for Mac OS X Tiger (10.4). Putting the framework in the right location (/System/Library/Frameworks/ or /Library/Frameworks/) will make the new feature available to all GLUT applications. Be careful to preserve the symbolic links of the framework while copying it (either use the finder to drag-and-drop the framework to the right location or use the ditto command line):

[plan9:~] blanch% cd Desktop/
[plan9:~/Desktop] blanch% open glut-framework-*.dmg        
[plan9:~/Desktop] blanch% sudo ditto /Volumes/glut-framework/GLUT.framework /System/Library/Frameworks/GLUT.framework
Password:
[plan9:~/Desktop] blanch% 

The frameworks have been both cross compiled on an intel MacBook Pro running Mac OS X Leopard (10.5.2), using the MacOSX10.4u.sdk and MacOSX10.5.sdk. The 10.5 version has been tested on an intel MacBook Pro and a G5 Mac Pro both running Mac OS X Leopard (10.5.2). It should also work on 64 bits systems.

[sunset:~/home] blanch% date
Jeu  6 mar 2008 11:05:02 CET
[sunset:~/home] blanch% uname -mprs
Darwin 9.2.0 i386 i386
[taquet:~/home] blanch% date
Jeu  6 mar 2008 15:59:44 CET
[taquet:~/home] blanch% uname -mprs
Darwin 9.2.0 Power Macintosh powerpc

The 10.4 version is 32 bits only. It has been reported to work on an intel Quad Mac Pro running Mac OS X Tiger (10.4.11).

Building yourself

If you want (or need) to compile the modified GLUT yourself, you'll need the original GLUT sources provided by Apple (local mirror), and the following patch.

[sunset:~] blanch% cd temp/
[sunset:~/temp] blanch% open ~/Desktop/glut.dmg 
[sunset:~/temp] blanch% cp -R /Volumes/glut .
[sunset:~/temp] blanch% cd glut/
[sunset:~/temp/glut] blanch% patch < ~/Desktop/glut.patch 
patching file GLUTView.m
patching file glut.h

If you are using Mac OS X Tiger, you will also need to apply this additional patch in order to compile (maybe only needed for cross compilation on 10.5).

You can now build the GLUT framework using the XCode tools:

[sunset:~/temp/glut] blanch% xcodebuild -list
Information about project "GLUT_External":
    Targets:
        GLUT (Active)

    Build Configurations:
        Debug
        Release (Active)

    If no build configuration is specified "Debug" is used.
[sunset:~/temp/glut] blanch% xcodebuild -configuration Release
=== BUILDING NATIVE TARGET GLUT WITH CONFIGURATION Release ===

...

** BUILD SUCCEEDED **
[sunset:~/temp/glut] blanch% ls -l build/Release/
total 0
drwxr-xr-x   6 blanch  blanch  204 Mar 20 10:57 GLUT.framework
[sunset:~/temp/glut] blanch% 
 

Feedback

Feedback is welcome at .