#ifndef _DESK_MSG_H_
#define _DESK_MSG_H_
#include "nano-X.h"
/*
 * this header file describes the message passed between 
 * apps and deskman 
 */
/* PIPE_BUF message ensure atomicity on write to pipe */
#define MSG_BUF_SIZE (PIPE_BUF - (2 *sizeof(unsigned char)))

typedef unsigned char MSG_TYPE;
typedef unsigned char MSG_SUBTYPE;
typedef struct
{
        MSG_TYPE 	type;
	MSG_SUBTYPE 	subtype;
        gchar data[MSG_BUF_SIZE];
} MSG;

/**** major message types *************/
#define MSG_TYPE_MIME		101
#define MSG_TYPE_PROTOCOL	102
#define MSG_TYPE_COMMAND	103
#define MSG_TYPE_NOTIFY		104

/***** message subtypes ***************/
#define MSG_PROTOCOL_HTTP       1 	/* http://..... */
#define MSG_PROTOCOL_HTTPS      2 	/* http://..... */
#define MSG_PROTOCOL_MAILTO     3	/* mailto://..... */
#define MSG_PROTOCOL_FILE  	4	/* file:// */

/* content itself too big to send in fifo in one atomic write */
#define MSG_MIME_TEXT_PLAIN  	1	/* absolute pathname */
#define MSG_MIME_TEXT_HTML  	2	/* absolute pathname */
#define MSG_MIME_AUDIO_X_SCPLS  3	/* http://...... */
#define MSG_MIME_AUDIO_X_MS_WMA 4	/* http://...... */
#define MSG_MIME_AUDIO_X_MS_ACF 5	/* http://...... */

#define MSG_COMMAND_CLOSE       1       /* app name */
#define MSG_COMMAND_LAUNCH      2       /* app name */
#define MSG_COMMAND_RESET       3       /* app name */
#define MSG_COMMAND_MINIMIZE    4       /* app name */
#define MSG_COMMAND_REFRESH	5	/* desktop refresh */
#define MSG_COMMAND_INPUT	6	/* switch launcher input */

#define MSG_NOTIFY_MAP_WIN	1	/* window ID */
#define MSG_NOTIFY_UNMAP_WIN	2	/* window ID */
#define MSG_NOTIFY_DESTROY_WIN	3	/* window ID */
#define MSG_NOTIFY_TS_KEY	11	/* x, y coordinates*/
#define MSG_NOTIFY_NEW_MAIL	21	/* none */
#define MSG_NOTIFY_MAIL_READ	22	/* none */
#define MSG_NOTIFY_HOTKEY_CFG   31      /* none */
#define MSG_NOTIFY_INSTALL_APP  41      /* MSG_INSTALL_INFO */
#define MSG_NOTIFY_UNINSTALL_APP  42    /* MSG_INSTALL_INFO */

/* data structures for some subtypes */

typedef GR_WINDOW_ID MSG_WINDOW_ID;
typedef gint MSG_STR_INFO_LEN;
typedef struct
{
	MSG_STR_INFO_LEN len;
	gchar *str;
} 
MSG_STR_INFO;
typedef struct
{
	GR_COORD x;
	GR_COORD y;
}

MSG_COORD_INFO;

typedef struct
{
	gint folder_index;
	gint program_index;
}
MSG_INSTALL_INFO;

extern gchar *protocol_str_table[];
#define PROTOCOL_TABLE_SIZE 5		/* must be consistent with deskmsg.c */
extern gchar *mime_str_table[];		/* must be consistent with deskmsg.c */
#define MIME_TABLE_SIZE 6

/* launcher input values */
typedef unsigned char MSG_LAUNCHER_INPUT;
#define MSG_LAUNCHER_INPUT_NXKBD_ID     1
#define MSG_LAUNCHER_INPUT_NXHWR_ID     2
#define MSG_LAUNCHER_INPUT_NXCJKKBD_ID  4


#define MSG_MIN_SIZE (sizeof(MSG_TYPE) + sizeof(MSG_SUBTYPE))
#define MSG_NOTIFY_WIN_SIZE (MSG_MIN_SIZE + sizeof(MSG_WINDOW_ID))
#define MSG_NOTIFY_COORD_SIZE (MSG_MIN_SIZE + sizeof(MSG_COORD_INFO))
#define MSG_NOTIFY_INSTALL_SIZE (MSG_MIN_SIZE + sizeof(MSG_INSTALL_INFO))
#define MSG_COMMAND_INPUT_SIZE (MSG_MIN_SIZE + sizeof(MSG_LAUNCHER_INPUT))

int msg_protocol_http_new(gchar *url, MSG *msg);
int msg_protocol_https_new(gchar *url, MSG *msg);
int msg_protocol_mailto_new(gchar *url, MSG *msg);
int msg_protocol_file_new(gchar *url, MSG *msg);

int msg_mime_text_plain_new(gchar *filepath, MSG *msg);
int msg_mime_text_html_new(gchar *filepath, MSG *msg);
int msg_mime_audio_x_scpls_new(gchar *url, MSG *msg);
int msg_mime_audio_x_ms_wma_new(gchar *url, MSG *msg);
int msg_mime_audio_x_ms_acf_new(gchar *url, MSG *msg);

int msg_map_win_new(MSG_WINDOW_ID wid, MSG *msg);
int msg_unmap_win_new(MSG_WINDOW_ID wid, MSG *msg);
int msg_destroy_win_new(MSG_WINDOW_ID wid, MSG *msg);
int msg_ts_key_new(MSG_COORD_INFO *coord, MSG *msg);
int msg_new_mail_new(MSG *msg);
int msg_mail_read_new(MSG *msg);
int msg_hotkey_config_new(MSG *msg);
int msg_install_app_new(gint folder_index, gint program_index, MSG *msg);
int msg_uninstall_app_new(gint folder_index, gint program_index, MSG *msg);
int msg_launcher_input_new(MSG_LAUNCHER_INPUT target, MSG *msg);

gboolean msg_get_from_fd(int fd, MSG *msg);
gboolean msg_send_with_fd(int fd, MSG *msg, int len);
gchar * msg_extract_string_data(char *msgData);

#define DESKMAN_PIPE "/tmp/.deskman"
#define DESKTOP_CHANNEL_PATH "/tmp/launcherapp"
typedef struct {
	int	inFd;  /* from desktop */
	int	outFd; /* send to desktop */
	guint	listenID; /* identifier for g_source_add */ 
}
DESKTOP_CHANNEL;

typedef void (*DESKTOP_CHANNEL_FUNC) (MSG *mesg, gpointer user_data);
gboolean desktop_channel_connect(DESKTOP_CHANNEL *channel);
gboolean desktop_channel_disconnect(DESKTOP_CHANNEL *channel);
gboolean desktop_channel_send(DESKTOP_CHANNEL *channel, MSG *msg, int len);
gboolean desktop_channel_listen_connect(DESKTOP_CHANNEL *channel, DESKTOP_CHANNEL_FUNC callback, gpointer user_data);
gboolean desktop_channel_listen_disconnect(DESKTOP_CHANNEL *channel);

#endif //_DESK_MSG_H_

