100

How would one go about encoding this chunk of C code in a .chs file so that c2hs can transform it to something relatively nice?

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;
    };
};
Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
  • 12
    It is more productive for you to actually try to solve the problem first, then come with specific questions about the language and/or tools. Questions that get at the heart of any confusion, and avoid unnecessary complexity of a specific application, are even better. For example, you could ask about a simple struct and/or about a simple union then apply that knowledge to your problem. – Thomas M. DuBuisson May 18 '14 at 03:56
  • 2
    @ThomasM.DuBuisson I think you make a reasonable point. I'm going to work through this a bit further. –  May 18 '14 at 10:31
  • @unsymbol do you have an answer for you question? Please consider adding it here :) – alfakini May 13 '15 at 21:38
  • 1
    Hi unsymbol, any news on this? Did you get it working and how? – Casper Thule Hansen Jun 13 '15 at 22:39

1 Answers1

1

How about this: change the code so that you name the members. The layout in memory is the same so that it will be binary compatible. You would have to do this patch for each version of the 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;
    };
};
h4ck3rm1k3
  • 2,060
  • 22
  • 33