
All, So I'm trying to wrap this structure: struct GtkSourceTagStyle { /* readonly */ gboolean is_default; guint mask; GdkColor foreground; GdkColor background; gboolean italic; gboolean bold; gboolean underline; gboolean strikethrough; /* Reserved for future expansion */ guint8 reserved[16]; }; My problem is with the GdkColor members foreground & background. They are themselves structures so c2hs cannot make {#get #} mappings for them. Of course Color has already been wrapped (in gtk2hs/gtk/general/Structs.hsc) and is a member of the Storable class so all we need is to get a pointer to the member to be able to apply peek/poke to it. This is not hard if we manually edit the .hs file produced from the .chs binding file: instance Storable SourceTagStyle where peek ptr = do isDefault' <- (\ptr -> do {peekByteOff ptr 0 ::IO CInt}) ptr mask' <- (\ptr -> do {peekByteOff ptr 4 ::IO CUInt}) ptr foreground' <- (\ptr -> do {peekByteOff ptr 8 ::IO Color}) ptr --manually edited background' <- (\ptr -> do {peekByteOff ptr 8+12 ::IO Color}) ptr --manually edited italic' <- (\ptr -> do {peekByteOff ptr 32 ::IO CInt}) ptr bold' <- (\ptr -> do {peekByteOff ptr 36 ::IO CInt}) ptr ... So as you see we're very close, all we need is a way of automatically getting the structure offsets (which is what c2hs does for all the other members). What is the right way do to this? Duncan

On Wed, Oct 22, 2003 at 02:51:15PM +0100, Duncan Coutts wrote:
All,
So I'm trying to wrap this structure:
struct GtkSourceTagStyle {
/* readonly */ gboolean is_default;
guint mask;
GdkColor foreground; GdkColor background;
gboolean italic; gboolean bold; gboolean underline; gboolean strikethrough;
/* Reserved for future expansion */ guint8 reserved[16]; };
My problem is with the GdkColor members foreground & background. They are themselves structures so c2hs cannot make {#get #} mappings for them. Of course Color has already been wrapped (in gtk2hs/gtk/general/Structs.hsc) and is a member of the Storable class so all we need is to get a pointer to the member to be able to apply peek/poke to it. This is not hard if we manually edit the .hs file produced from the .chs binding file:
I kinda object to use c2hs for marshalling structures. When I wrote the marshalling code for graphics contexts for gtk+hs, I used c2hs only to find out that it calculates the wrong offsets. Moreover, it is doomed to produce the wrong offsets in some circumstances with bitfields. Windows uses a different layout than Unix, even if you use gcc on both platforms. (gcc behaves differently so that you can use the precompiled Microsoft libraries). So I recommend to create a new .hsc file that does the marshalling of that structure. The foreground and background color is then simply another call to peek and poke, just like in the marshalling code of GCValues: instance Storable GCValues where sizeOf _ = #{const sizeof(GdkGCValues)} alignment _ = alignment (undefined::Color) peek ptr = do foreground_ <- peek (#{ptr GdkGCValues, foreground} ptr) background_ <- peek (#{ptr GdkGCValues, background} ptr) .... (this is from gtk/general/Structs.hsc) Hope this helps, Axel.

Axel Simon
On Wed, Oct 22, 2003 at 02:51:15PM +0100, Duncan Coutts wrote:
All,
So I'm trying to wrap this structure:
struct GtkSourceTagStyle {
/* readonly */ gboolean is_default;
guint mask;
GdkColor foreground; GdkColor background;
gboolean italic; gboolean bold; gboolean underline; gboolean strikethrough;
/* Reserved for future expansion */ guint8 reserved[16]; };
My problem is with the GdkColor members foreground & background. They are themselves structures so c2hs cannot make {#get #} mappings for them. Of course Color has already been wrapped (in gtk2hs/gtk/general/Structs.hsc) and is a member of the Storable class so all we need is to get a pointer to the member to be able to apply peek/poke to it. This is not hard if we manually edit the .hs file produced from the .chs binding file:
I kinda object to use c2hs for marshalling structures. When I wrote the marshalling code for graphics contexts for gtk+hs, I used c2hs only to find out that it calculates the wrong offsets.
That was a bug in c2hs, which has been fixed since then.
Moreover, it is doomed to produce the wrong offsets in some circumstances with bitfields. Windows uses a different layout than Unix, even if you use gcc on both platforms. (gcc behaves differently so that you can use the precompiled Microsoft libraries).
Why do you think that c2hs cannot handle this? If you look at the files "c2hs/toplevel/C2HSConfig.hs.in" and "c2hs/toplevel/c2hs_config.c" in the c2hs source tree, you will see that c2hs actually goes to quite some trouble to find out how to properly access bitfields on the particular OS and architecture it is running on. If you have got any example, where this does not work properly, please submit it as a c2hs bug report. Cheers, Manuel

On Thu, Oct 23, 2003 at 12:20:01PM +1000, Manuel M. T. Chakravarty wrote:
I kinda object to use c2hs for marshalling structures. When I wrote the marshalling code for graphics contexts for gtk+hs, I used c2hs only to find out that it calculates the wrong offsets.
That was a bug in c2hs, which has been fixed since then.
Oh, sorry. I thought I'd seen it in the TODO list of c2hs recently - it's not there anymore though.
Moreover, it is doomed to produce the wrong offsets in some circumstances with bitfields. Windows uses a different layout than Unix, even if you use gcc on both platforms. (gcc behaves differently so that you can use the precompiled Microsoft libraries).
Why do you think that c2hs cannot handle this? If you look at the files "c2hs/toplevel/C2HSConfig.hs.in" and "c2hs/toplevel/c2hs_config.c" in the c2hs source tree, you will see that c2hs actually goes to quite some trouble to find out how to properly access bitfields on the particular OS and architecture it is running on. If you have got any example, where this does not work properly, please submit it as a c2hs bug report.
Ok, I'm wrong there. BTW, would it be possible that c2hs spits out the platform specific versions of types (i.e. Int32 instead of CInt). I think CInt is rather for hand-written marshalling code whereas Int32 is more accurate for a specific platform. At the moment it's very annoying that hsc2hs and c2hs have different opinions as to what C's basic data types map to. Cheers, Axel.

Duncan Coutts
All,
So I'm trying to wrap this structure:
struct GtkSourceTagStyle {
/* readonly */ gboolean is_default;
guint mask;
GdkColor foreground; GdkColor background;
gboolean italic; gboolean bold; gboolean underline; gboolean strikethrough;
/* Reserved for future expansion */ guint8 reserved[16]; };
My problem is with the GdkColor members foreground & background. They are themselves structures so c2hs cannot make {#get #} mappings for them. Of course Color has already been wrapped (in gtk2hs/gtk/general/Structs.hsc) and is a member of the Storable class so all we need is to get a pointer to the member to be able to apply peek/poke to it. This is not hard if we manually edit the .hs file produced from the .chs binding file:
instance Storable SourceTagStyle where peek ptr = do isDefault' <- (\ptr -> do {peekByteOff ptr 0 ::IO CInt}) ptr mask' <- (\ptr -> do {peekByteOff ptr 4 ::IO CUInt}) ptr foreground' <- (\ptr -> do {peekByteOff ptr 8 ::IO Color}) ptr --manually edited background' <- (\ptr -> do {peekByteOff ptr 8+12 ::IO Color}) ptr --manually edited italic' <- (\ptr -> do {peekByteOff ptr 32 ::IO CInt}) ptr bold' <- (\ptr -> do {peekByteOff ptr 36 ::IO CInt}) ptr ...
So as you see we're very close, all we need is a way of automatically getting the structure offsets (which is what c2hs does for all the other members). What is the right way do to this?
To be honest, I don't quite get what the problem is. Maybe because I haven't seen the .chs file from which you compile this. Provided you have the right Storable instances, there shouldn't be a problem {#get #}-ing complex structures. Cheers, Manuel
participants (3)
-
Axel Simon
-
Duncan Coutts
-
Manuel M T Chakravarty