Screenshot instructions:
Windows
Mac
Red Hat Linux
Ubuntu
Click URL instructions:
Right-click on ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)
From: Duncan Coutts <duncan@co...> - 2003-10-22 18:32:34
|
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 |
From: Axel Simon <A.Simon@ke...> - 2003-10-22 19:32:09
|
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. |
From: Manuel M T Chakravarty <chak@cs...> - 2003-10-23 06:31:37
|
Axel Simon <A.Simon@...> wrote, > 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 |
From: Axel Simon <A.Simon@ke...> - 2003-10-23 09:56:31
|
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. |
From: Manuel M T Chakravarty <chak@cs...> - 2003-10-23 17:04:48
|
Duncan Coutts <duncan@...> 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: > > 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 |
From: Duncan Coutts <duncan@co...> - 2003-10-28 23:00:51
Attachments:
Color.chs
SourceTagStyle2.chs
|
On Thu, 2003-10-23 at 03:14, Manuel M T Chakravarty wrote: > Duncan Coutts <duncan@...> 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. [snip] > > 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. Ok, so here's what I'm doing: import Foreign import Foreign.C import Data.Bits (testBit, bit, (.|.)) import Maybe (isJust, fromMaybe) {#import Color#} {#context lib="gtk" prefix="gtk" #} {#enum SourceTagStyleMask {underscoreToCase} #} data SourceTagStyle = SourceTagStyle { isDefault :: Bool, -- readonly foreground :: Maybe Color, background :: Maybe Color, italic :: Bool, bold :: Bool, underline :: Bool, strikethrough :: Bool } instance Storable SourceTagStyle where sizeOf _ = {#sizeof SourceTagStyle#} alignment _ = alignment (undefined::{#type gboolean#}) peek ptr = do isDefault' <- {#get GtkSourceTagStyle.is_default#} ptr mask <- {#get GtkSourceTagStyle.mask#} ptr foreground' <- {#get GtkSourceTagStyle.foreground#} ptr) -- c2hs complains here background' <- {#get GtkSourceTagStyle.background#} ptr) -- c2hs complains here italic' <- {#get GtkSourceTagStyle.italic#} ptr {- etc -} return SourceTagStyle { isDefault = toBool isDefault', {- etc -} } When I run c2hs it complains: c2hs: Errors during expansion of binding hooks: SourceTagStyle2.chs:59: (column 25) [ERROR] >>> Illegal structure or union type! There is not automatic support for marshaling of structures and unions; the offending type is declared at ("/usr/include/gtk-2.0/gdk/gdktypes.h",94,1). SourceTagStyle2.chs:60: (column 25) [ERROR] >>> Illegal structure or union type! There is not automatic support for marshaling of structures and unions; the offending type is declared at ("/usr/include/gtk-2.0/gdk/gdktypes.h",94,1). I can understand why it would be hard to generate the appropriate code here because c2hs doesn't know the C->Haskell type mapping: GdkColor <-> Color.Color. The Color module defines the Color type like so: data Color = Color ({#type guint16#}) ({#type guint16#}) ({#type guint16#}) instance Storable Color where sizeOf _ = {#sizeof GdkColor#} alignment _ = alignment (undefined::{#type guint32#}) peek ptr = do red <- {#get GdkColor.red#} ptr green <- {#get GdkColor.green#} ptr blue <- {#get GdkColor.blue#} ptr return $ Color red green blue I am using a modified version of c2hs 0.10.17 (the one that is included with gtk2hs). Duncan |
From: Axel Simon <A.Simon@ke...> - 2003-10-30 13:40:13
|
On Tue, Oct 28, 2003 at 10:56:20PM +0000, Duncan Coutts wrote: > > instance Storable SourceTagStyle where > sizeOf _ = {#sizeof SourceTagStyle#} > alignment _ = alignment (undefined::{#type gboolean#}) > peek ptr = do > isDefault' <- {#get GtkSourceTagStyle.is_default#} ptr > mask <- {#get GtkSourceTagStyle.mask#} ptr > foreground' <- {#get GtkSourceTagStyle.foreground#} ptr) -- c2hs complains here At this point you probably want an offset hook in c2hs: foreground' <- peek ({#offset GtkSourceTagStyle foreground#} ptr) There is no such hook in c2hs as far as I can see in chs/CHS.hs, thus the only possiblities are: a) you marshall the three color fields by hand, thereby duplicating the code for marshalling colors: foregroundRed <- {#get GtkSourceTagSTyle.foreground.red#} ptr b) you use hsc2hs to marshal structures. The offset hook there is called "ptr". You can use it as in Structs.hsc: 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) Maybe Manuel has though about this and there is something in c2hs which I don't know about. Axel. |