与c2hs的结构和匿名联合接口

时间:2014-05-17 23:10:17

标签: haskell c2hs

如何在.chs文件中编码这一块C代码,以便c2hs可以将它转换为相对不错的东西?

typedef enum {
    MONOME_BUTTON_UP        = 0x00,
    MONOME_BUTTON_DOWN      = 0x01,
    MONOME_ENCODER_DELTA    = 0x02,
    MONOME_ENCODER_KEY_UP   = 0x03,
    MONOME_ENCODER_KEY_DOWN = 0x04,
    MONOME_TILT             = 0x05,

    /* update this if you add event types */
    MONOME_EVENT_MAX        = 0x06
} monome_event_type_t;

typedef struct monome monome_t; /* opaque data type */
typedef struct monome_event monome_event_t;

typedef void (*monome_event_callback_t)
    (const monome_event_t *event, void *data);

struct monome_event {
    monome_t *monome;
    monome_event_type_t event_type;

    /* __extension__ for anonymous unions in gcc */
    __extension__ union {
        struct {
            unsigned int x;
            unsigned int y;
        } grid;

        struct {
            unsigned int number;
            int delta;
        } encoder;

        struct {
            unsigned int sensor;
            int x;
            int y;
            int z;
        } tilt;
    };
};

1 个答案:

答案 0 :(得分:1)

如何:更改代码以便为成员命名。内存中的布局是相同的,因此它将是二进制兼容的。您必须为每个版本的lib执行此修补程序。

struct monome_event {
    monome_t *monome;
    monome_event_type_t event_type;

    /* __extension__ for anonymous unions in gcc */
    __extension__ union {
        struct me_grid {
            unsigned int x;
            unsigned int y;
        } grid;

        struct me_encoder {
            unsigned int number;
            int delta;
        } encoder;

        struct me_tilt {
            unsigned int sensor;
            int x;
            int y;
            int z;
        } tilt;
    };
};