~ / howtos /

Mouse scroll wheel events with GLUT under MacOSX

Copyright © 2006—2007, Renaud Blanch.
last updates : september, 27 2007
- added intel mac 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

Warnings

The framework provided has been compiled separately for PowerPC and intel processors. It has been tested on a G5 iMac running Mac OS X Tiger (10.4), on a G4 PowerBook running Mac OS X Panther (10.3), and on an intel MacBook Pro running Mac OS X Tiger (10.4.8).

[plan9:~] blanch% date
Thu May 18 11:13:35 CEST 2006
[plan9:~] blanch% uname -mprs
Darwin 8.6.0 Power Macintosh powerpc
[plan9:~] blanch%
[sunset:~] blanch% date
Mon Oct  9 13:57:50 CEST 2006
[sunset:~] blanch% uname -mprs
Darwin 8.8.1 i386 i386

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

[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 a compiled version of the modified GLUT framework for PowerPC processors and another modified GLUT framework for intel processors. 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% 

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.

[plan9:~] blanch% cd temp/
[plan9:~/temp] blanch% open ~/Desktop/glut.dmg 
[plan9:~/temp] blanch% cp -R /Volumes/glut .
cp: /Volumes/glut/.Trashes: Permission denied
[plan9:~/temp] blanch% cd glut/
[plan9:~/temp/glut] blanch% patch < ~/Desktop/glut.patch 
patching file Foreground.c
patching file GLUTView.m
patching file glut.h
patching file macx_event.m

The official GLUT distribution comes with an object file Foreground.o that provides an internal function to glut (__glutSetForeground) but the code needed to build this object is not provided. If you are using a PowerPC mac, you can use it after updating its date as detailed in the GLUT Readme.txt:

# !!! PowerPC only 
[plan9:~/temp/glut] blanch% ranlib Foreground.o 

If you are on an intel mac, you have to build your own Foreground.o. The patch creates the source file needed for that, you only need to build it:

# !!! intel only 
[sunset:~/temp/glut] blanch% make Foreground.o
cc    -c -o Foreground.o Foreground.c

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

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

    Build Configurations:
        Development (Active)
        Deployment
        Default

    If no build configuration is specified "Default" is used.
[plan9:~/temp/glut] blanch% xcodebuild -configuration Deployment
(NOTE: project GLUT_External was written by an older version of Xcode (38) -- 
temporarily upgrading it to version 42 (without modifying project file))

=== BUILDING FRAMEWORK TARGET GLUT (Framework) WITH CONFIGURATION Deployment ===

...

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

Feedback & help

Feedback is welcome at .

I also need your help: the source of GLUT distributed as a code sample by Apple dates back from 2003. They have fixed bugs since (e.g., a bug affecting submenus redisplay has been corrected in 2004), but the newer version of source is not available. I've asked for help on the mac-opengl mailing-list without success. If you know someone inside Apple who can help me, thanks for asking him that question!