views:

54

answers:

1

This is a part of a plt-scheme wrapper library:

(define InputMask

  (_bitmask '(NoEventMask =            #x00000000

            KeyPressMask =             #x00000001

            KeyReleaseMask =           #x00000002

            ...

            OwnerGrabButtonMask =      #x01000000)

      _long))

The thing is I cant figure out how to access fields in a bitmask (or enum for that matter). How can I get KeyPressMask value for example?

+3  A: 

You don't. Adding ctypes is easy: to make a new ctype, you need to provide an existing ctype to build on, and two functions -- one to translate whatever to the existing type, and one to translate the other way.

Now, the _bitmask type does just that -- it builds on _int (but in your case, it's on _long), and the two translation functions translate a list of symbols to an integer, and an integer to a list of symbols. Once such a type is used, you don't need to know the value of KeyPressMask -- you just know that you can pass '(KeyPressMask) as an InputMask input to the foreign function, and that will be translated to the appropriate number; and you also know that when you get the result value from a function that has an InputMask output, then it will be a list of symbols that might contain KeyPressMask. The bottom line is that on the Scheme side you don't deal with numbers -- only with symbol lists.

If you do need to access these values for some obscure reason, then you can build your own ctype in some other way -- using make-ctype (as I outlined above) should be very easy.

Eli Barzilay
Thanx! I've tried 'KeyPressMask without parentheses (silly me :)) I might miss part where this is explained in docs, but I'm sure it isn't explained on "Enumerations and Masks" page.
Slartibartfast
Well, the docs for that says: "the resulting mapping translates a *list of symbols* to a number and back". In any case, if you have an idea for a more obvious phrasing I'll commit it. (The foreign docs could use some clarification in general...)
Eli Barzilay