From 8d3e68b74d8ff0fad398617ea37eb4e5422f3718 Mon Sep 17 00:00:00 2001 From: alek900 Date: Mon, 12 Aug 2013 14:23:46 +0200 Subject: Added support for userdata in callbacks --- core/Messenger.c | 40 ++++++++++++++++++++++++---------------- core/Messenger.h | 40 ++++++++++++++++++++++++---------------- core/friend_requests.c | 9 +++++---- core/friend_requests.h | 2 +- 4 files changed, 54 insertions(+), 37 deletions(-) (limited to 'core') diff --git a/core/Messenger.c b/core/Messenger.c index a6189941..ebde5a78 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -395,52 +395,59 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno) /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); static uint8_t friend_request_isset = 0; */ /* set the function that will be executed when a friend request is received. */ -void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t)) +void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata) { - callback_friendrequest(function); + callback_friendrequest(function, userdata); } /* set the function that will be executed when a message from a friend is received. */ -void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) +void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) { m->friend_message = function; m->friend_message_isset = 1; + m->friend_message_userdata = userdata; } -void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) +void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) { m->friend_action = function; m->friend_action_isset = 1; + m->friend_action_userdata = userdata; } -void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) +void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) { m->friend_namechange = function; m->friend_namechange_isset = 1; + m->friend_namechange_userdata = userdata; } -void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) +void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) { m->friend_statusmessagechange = function; m->friend_statusmessagechange_isset = 1; + m->friend_statuschange_userdata = userdata; } -void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS)) +void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata) { m->friend_userstatuschange = function; m->friend_userstatuschange_isset = 1; + m->friend_userstatuschange_userdata = userdata; } -void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t)) +void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata) { m->read_receipt = function; m->read_receipt_isset = 1; + m->read_receipt_userdata = userdata; } -void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t)) +void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata) { m->friend_connectionstatuschange = function; m->friend_connectionstatuschange_isset = 1; + m->friend_connectionstatuschange_userdata = userdata; } static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status) @@ -452,7 +459,7 @@ static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_ const uint8_t was_connected = m->friendlist[friendnumber].status == FRIEND_ONLINE; const uint8_t is_connected = status == FRIEND_ONLINE; if (is_connected != was_connected) - m->friend_connectionstatuschange(m, friendnumber, is_connected); + m->friend_connectionstatuschange(m, friendnumber, is_connected, m->friend_connectionstatuschange_userdata); } void set_friend_status(Messenger *m, int friendnumber, uint8_t status) @@ -569,7 +576,7 @@ void doFriends(Messenger *m) if (data_length >= MAX_NAME_LENGTH || data_length == 0) break; if(m->friend_namechange_isset) - m->friend_namechange(m, i, data, data_length); + m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata); memcpy(m->friendlist[i].name, data, data_length); m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ break; @@ -580,7 +587,8 @@ void doFriends(Messenger *m) uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); if (m->friend_statusmessagechange_isset) - m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); + m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH), + m->friend_statuschange_userdata); set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); free(status); break; @@ -590,7 +598,7 @@ void doFriends(Messenger *m) break; USERSTATUS status = data[0]; if (m->friend_userstatuschange_isset) - m->friend_userstatuschange(m, i, status); + m->friend_userstatuschange(m, i, status, m->friend_userstatuschange_userdata); set_friend_userstatus(m, i, status); break; } @@ -603,12 +611,12 @@ void doFriends(Messenger *m) write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); } if (m->friend_message_isset) - (*m->friend_message)(m, i, message, message_length); + (*m->friend_message)(m, i, message, message_length, m->friend_message_userdata); break; } case PACKET_ID_ACTION: { if (m->friend_action_isset) - (*m->friend_action)(m, i, data, data_length); + (*m->friend_action)(m, i, data, data_length, m->friend_action_userdata); break; } case PACKET_ID_RECEIPT: { @@ -618,7 +626,7 @@ void doFriends(Messenger *m) memcpy(&msgid, data, sizeof(msgid)); msgid = ntohl(msgid); if (m->read_receipt_isset) - (*m->read_receipt)(m, i, msgid); + (*m->read_receipt)(m, i, msgid, m->read_receipt_userdata); break; } } diff --git a/core/Messenger.h b/core/Messenger.h index 36e24596..fa69d104 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -105,22 +105,30 @@ typedef struct Messenger { Friend *friendlist; uint32_t numfriends; - void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t); + void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void*); uint8_t friend_message_isset; - void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t); + void* friend_message_userdata; + void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void*); uint8_t friend_action_isset; - void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t); + void* friend_action_userdata; + void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*); uint8_t friend_namechange_isset; - void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t); + void* friend_namechange_userdata; + void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*); uint8_t friend_statusmessagechange_isset; - void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS); + void* friend_statusmessagechange_userdata; + void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS, void*); uint8_t friend_userstatuschange_isset; - void (*read_receipt)(struct Messenger *m, int, uint32_t); + void* friend_userstatuschange_userdata; + void (*read_receipt)(struct Messenger *m, int, uint32_t, void*); uint8_t read_receipt_isset; - void (*friend_statuschange)(struct Messenger *m, int, uint8_t); + void* read_receipt_userdata; + void (*friend_statuschange)(struct Messenger *m, int, uint8_t, void*); uint8_t friend_statuschange_isset; - void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t); + void* friend_statuschange_userdata; + void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void*); uint8_t friend_connectionstatuschange_isset; + void* friend_connectionstatuschange_userdata; } Messenger; @@ -230,29 +238,29 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno); /* set the function that will be executed when a friend request is received. function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ -void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t)); +void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata); /* set the function that will be executed when a message from a friend is received. function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ -void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); +void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata); /* set the function that will be executed when an action from a friend is received. function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ -void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); +void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata); /* set the callback for name changes function(int friendnumber, uint8_t *newname, uint16_t length) you are not responsible for freeing newname */ -void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); +void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata); /* set the callback for status message changes function(int friendnumber, uint8_t *newstatus, uint16_t length) you are not responsible for freeing newstatus */ -void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); +void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata); /* set the callback for status type changes function(int friendnumber, USERSTATUS kind) */ -void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS)); +void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata); /* set the callback for read receipts function(int friendnumber, uint32_t receipt) @@ -261,7 +269,7 @@ void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USE has been received on the other side. since core doesn't track ids for you, receipt may not correspond to any message in that case, you should discard it. */ -void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t)); +void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata); /* set the callback for connection status changes function(int friendnumber, uint8_t status) @@ -271,7 +279,7 @@ void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, u note that this callback is not called when adding friends, thus the "after being previously online" part. it's assumed that when adding friends, their connection status is offline. */ -void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t)); +void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata); /* run this at startup * returns allocated instance of Messenger on success diff --git a/core/friend_requests.c b/core/friend_requests.c index 422cc028..5e9b447c 100644 --- a/core/friend_requests.c +++ b/core/friend_requests.c @@ -57,14 +57,15 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length) return num; } -static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t); +static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void*); static uint8_t handle_friendrequest_isset = 0; - +static void* handle_friendrequest_userdata; /* set the function that will be executed when a friend request is received. */ -void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) +void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata) { handle_friendrequest = function; handle_friendrequest_isset = 1; + handle_friendrequest_userdata = userdata; } @@ -121,7 +122,7 @@ static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t len return 1; addto_receivedlist(public_key); - (*handle_friendrequest)(public_key, data, len); + (*handle_friendrequest)(public_key, data, len, handle_friendrequest_userdata); } else { /* if request is not for us, try routing it. */ if(route_packet(packet + 1, packet, length) == length) return 0; diff --git a/core/friend_requests.h b/core/friend_requests.h index 708d8f66..f18107ce 100644 --- a/core/friend_requests.h +++ b/core/friend_requests.h @@ -37,7 +37,7 @@ int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length); /* set the function that will be executed when a friend request for us is received. function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ -void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); +void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata); /* sets up friendreq packet handlers */ void friendreq_init(void); -- cgit v1.2.3