#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#include <iiimp-data.h>
#include <iiimp-opcode.h>

#include "iiimp-dataP.h"


IIIMP_message *
iiimp_register_hotkeys_new(
    IIIMP_data_s *		data_s,
    IIIMP_card16		im_id,
    IIIMP_card16		scope_and_profile_id,
    HOTKEY_LIST *		hotkeys)
{
    IIIMP_message *	m;
    m = (IIIMP_message *)malloc(sizeof (IIIMP_message));
    if (NULL == m) {
	data_s->status = IIIMP_DATA_MALLOC_ERROR;
	return NULL;
    }
    m->opcode = IM_REGISTER_HOTKEYS;
    m->im_id = im_id;
    m->ic_id = -1;
    m->v.register_hotkeys.scope_and_profile_id = scope_and_profile_id; 
    m->v.register_hotkeys.scope = -1;
    m->v.register_hotkeys.profile_id = -1;
    m->v.register_hotkeys.hotkeys = hotkeys;
    return m;
}

void
iiimp_register_hotkeys_delete(IIIMP_data_s * data_s, IIIMP_message * m)
{
    if (NULL == m) return;
    iiimp_hotkey_list_delete(data_s, m->v.register_hotkeys.hotkeys);
    free(m);
}


uchar_t *
iiimp_register_hotkeys_pack(
    IIIMP_data_s *		data_s,
    IIIMP_card16		im_id,
    IIIMP_card16		scope_and_profile_id,
    HOTKEY_LIST *		hotkeys,
    size_t *			buf_size)
{
    size_t			nbyte;
    int				length;
    uchar_t *			buf;
    size_t			rest;
    uchar_t *			p;

    nbyte = 0
	+ 2	 /* input method id */
	+ 2	 /* PAD(2) */
	+ 2	 /* SCOPE and PROFILE_ID */
	+ 2;     /* byte length of list of HOTKEYS */

    if (NULL != hotkeys) {
	nbyte += hotkeys->nbyte; /* hotkey keys list */
    }

    length = (nbyte >> 2);
    *buf_size = (1 + 3 + nbyte);

    buf = (uchar_t *)malloc(1 + 3 + nbyte);
    if (NULL == buf) {
	data_s->status = IIIMP_DATA_MALLOC_ERROR;
	return NULL;
    }

    PUT_PACKET_HEADER(buf, IM_REGISTER_HOTKEYS, length);

    rest = nbyte;
    p = (buf + 4);

    PUTU16(im_id, rest, p, data_s->byte_swap);
    PUTU16(0, rest, p, data_s->byte_swap);
    PUTU16(scope_and_profile_id, rest, p, data_s->byte_swap);
    if (NULL == hotkeys) {
	PUTU16(0, rest, p, data_s->byte_swap);
    } else {
	PUTU16(hotkeys->nbyte, rest, p, data_s->byte_swap);
	iiimp_hotkey_list_pack(data_s, hotkeys, &rest, &p);
    }
    return buf;
}

IIIMP_message *
iiimp_register_hotkeys_unpack(
    IIIMP_data_s *	data_s,
    IIIMP_card7		opcode,
    size_t *		nbyte,
    const uchar_t **	ptr)
{
    IIIMP_message *			m;
    IIIMP_register_hotkeys_v *	        v;
    size_t				rest;
    const uchar_t *			p;
    int					len;

    rest = *nbyte;
    p = *ptr;

    if (rest < (2 + 2 + 2 + 2 + 0)) {
	data_s->status = IIIMP_DATA_INVALID;
	return NULL;
    }

    m = (IIIMP_message *)malloc(sizeof (IIIMP_message));
    if (NULL == m) {
	data_s->status = IIIMP_DATA_MALLOC_ERROR;
	return NULL;
    }
    v = &(m->v.register_hotkeys);

    m->opcode = opcode;
    v->hotkeys = NULL;

    GETU16(m->im_id, rest, p, data_s->byte_swap);
    m->ic_id = -1;
    SKIP16(rest, p);
    GET_SCOPE_FOR_HOTKEY(v->scope, p, data_s->byte_swap);
    GETU15(v->profile_id, p, data_s->byte_swap);
    GETU16(v->scope_and_profile_id, rest, p, data_s->byte_swap);

    GETU16(len, rest, p, data_s->byte_swap);
    if ((len < 0) || (rest < len)) {
	iiimp_register_hotkeys_delete(data_s, m);
	data_s->status = IIIMP_DATA_INVALID;
	return NULL;
    }

    v->hotkeys = iiimp_hotkey_list_unpack(data_s, &rest, &p, len);
    if (NULL == v->hotkeys) {
	iiimp_register_hotkeys_delete(data_s, m);
	return NULL;
    }

    *nbyte = rest;
    *ptr = p;

    return (IIIMP_message *)m;
}


void
iiimp_register_hotkeys_print(
    IIIMP_data_s *	data_s,
    IIIMP_message *	m)
{
    iiimp_message_header_print(data_s, m->opcode, m->im_id, -1);
    iiimp_hotkey_list_print(data_s, m->v.register_hotkeys.hotkeys);
}


/* Local Variables: */
/* c-file-style: "iiim-project" */
/* End: */
