diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-12 13:33:20 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-12 13:33:20 -0700 |
commit | f9bfb42504ffb56fdcc028efbe1177fe1d45a26c (patch) | |
tree | 724071a9bb7aff5453fec10d001162eb81855a8d /core | |
parent | 107e4b8a5ea04c76af8f80597233f110c6abddcf (diff) | |
parent | 8d3e68b74d8ff0fad398617ea37eb4e5422f3718 (diff) |
Merge pull request #439 from Alek900/master
Added support for userdata in callbacks
Diffstat (limited to 'core')
-rw-r--r-- | core/Messenger.c | 40 | ||||
-rw-r--r-- | core/Messenger.h | 40 | ||||
-rw-r--r-- | core/friend_requests.c | 9 | ||||
-rw-r--r-- | core/friend_requests.h | 2 |
4 files changed, 54 insertions, 37 deletions
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) | |||
395 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); | 395 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); |
396 | static uint8_t friend_request_isset = 0; */ | 396 | static uint8_t friend_request_isset = 0; */ |
397 | /* set the function that will be executed when a friend request is received. */ | 397 | /* set the function that will be executed when a friend request is received. */ |
398 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t)) | 398 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata) |
399 | { | 399 | { |
400 | callback_friendrequest(function); | 400 | callback_friendrequest(function, userdata); |
401 | } | 401 | } |
402 | 402 | ||
403 | /* set the function that will be executed when a message from a friend is received. */ | 403 | /* set the function that will be executed when a message from a friend is received. */ |
404 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) | 404 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) |
405 | { | 405 | { |
406 | m->friend_message = function; | 406 | m->friend_message = function; |
407 | m->friend_message_isset = 1; | 407 | m->friend_message_isset = 1; |
408 | m->friend_message_userdata = userdata; | ||
408 | } | 409 | } |
409 | 410 | ||
410 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) | 411 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) |
411 | { | 412 | { |
412 | m->friend_action = function; | 413 | m->friend_action = function; |
413 | m->friend_action_isset = 1; | 414 | m->friend_action_isset = 1; |
415 | m->friend_action_userdata = userdata; | ||
414 | } | 416 | } |
415 | 417 | ||
416 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) | 418 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) |
417 | { | 419 | { |
418 | m->friend_namechange = function; | 420 | m->friend_namechange = function; |
419 | m->friend_namechange_isset = 1; | 421 | m->friend_namechange_isset = 1; |
422 | m->friend_namechange_userdata = userdata; | ||
420 | } | 423 | } |
421 | 424 | ||
422 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) | 425 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata) |
423 | { | 426 | { |
424 | m->friend_statusmessagechange = function; | 427 | m->friend_statusmessagechange = function; |
425 | m->friend_statusmessagechange_isset = 1; | 428 | m->friend_statusmessagechange_isset = 1; |
429 | m->friend_statuschange_userdata = userdata; | ||
426 | } | 430 | } |
427 | 431 | ||
428 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS)) | 432 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata) |
429 | { | 433 | { |
430 | m->friend_userstatuschange = function; | 434 | m->friend_userstatuschange = function; |
431 | m->friend_userstatuschange_isset = 1; | 435 | m->friend_userstatuschange_isset = 1; |
436 | m->friend_userstatuschange_userdata = userdata; | ||
432 | } | 437 | } |
433 | 438 | ||
434 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t)) | 439 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata) |
435 | { | 440 | { |
436 | m->read_receipt = function; | 441 | m->read_receipt = function; |
437 | m->read_receipt_isset = 1; | 442 | m->read_receipt_isset = 1; |
443 | m->read_receipt_userdata = userdata; | ||
438 | } | 444 | } |
439 | 445 | ||
440 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t)) | 446 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata) |
441 | { | 447 | { |
442 | m->friend_connectionstatuschange = function; | 448 | m->friend_connectionstatuschange = function; |
443 | m->friend_connectionstatuschange_isset = 1; | 449 | m->friend_connectionstatuschange_isset = 1; |
450 | m->friend_connectionstatuschange_userdata = userdata; | ||
444 | } | 451 | } |
445 | 452 | ||
446 | static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status) | 453 | 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_ | |||
452 | const uint8_t was_connected = m->friendlist[friendnumber].status == FRIEND_ONLINE; | 459 | const uint8_t was_connected = m->friendlist[friendnumber].status == FRIEND_ONLINE; |
453 | const uint8_t is_connected = status == FRIEND_ONLINE; | 460 | const uint8_t is_connected = status == FRIEND_ONLINE; |
454 | if (is_connected != was_connected) | 461 | if (is_connected != was_connected) |
455 | m->friend_connectionstatuschange(m, friendnumber, is_connected); | 462 | m->friend_connectionstatuschange(m, friendnumber, is_connected, m->friend_connectionstatuschange_userdata); |
456 | } | 463 | } |
457 | 464 | ||
458 | void set_friend_status(Messenger *m, int friendnumber, uint8_t status) | 465 | void set_friend_status(Messenger *m, int friendnumber, uint8_t status) |
@@ -569,7 +576,7 @@ void doFriends(Messenger *m) | |||
569 | if (data_length >= MAX_NAME_LENGTH || data_length == 0) | 576 | if (data_length >= MAX_NAME_LENGTH || data_length == 0) |
570 | break; | 577 | break; |
571 | if(m->friend_namechange_isset) | 578 | if(m->friend_namechange_isset) |
572 | m->friend_namechange(m, i, data, data_length); | 579 | m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata); |
573 | memcpy(m->friendlist[i].name, data, data_length); | 580 | memcpy(m->friendlist[i].name, data, data_length); |
574 | m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ | 581 | m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ |
575 | break; | 582 | break; |
@@ -580,7 +587,8 @@ void doFriends(Messenger *m) | |||
580 | uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); | 587 | uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); |
581 | memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); | 588 | memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
582 | if (m->friend_statusmessagechange_isset) | 589 | if (m->friend_statusmessagechange_isset) |
583 | m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); | 590 | m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH), |
591 | m->friend_statuschange_userdata); | ||
584 | set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); | 592 | set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
585 | free(status); | 593 | free(status); |
586 | break; | 594 | break; |
@@ -590,7 +598,7 @@ void doFriends(Messenger *m) | |||
590 | break; | 598 | break; |
591 | USERSTATUS status = data[0]; | 599 | USERSTATUS status = data[0]; |
592 | if (m->friend_userstatuschange_isset) | 600 | if (m->friend_userstatuschange_isset) |
593 | m->friend_userstatuschange(m, i, status); | 601 | m->friend_userstatuschange(m, i, status, m->friend_userstatuschange_userdata); |
594 | set_friend_userstatus(m, i, status); | 602 | set_friend_userstatus(m, i, status); |
595 | break; | 603 | break; |
596 | } | 604 | } |
@@ -603,12 +611,12 @@ void doFriends(Messenger *m) | |||
603 | write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); | 611 | write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); |
604 | } | 612 | } |
605 | if (m->friend_message_isset) | 613 | if (m->friend_message_isset) |
606 | (*m->friend_message)(m, i, message, message_length); | 614 | (*m->friend_message)(m, i, message, message_length, m->friend_message_userdata); |
607 | break; | 615 | break; |
608 | } | 616 | } |
609 | case PACKET_ID_ACTION: { | 617 | case PACKET_ID_ACTION: { |
610 | if (m->friend_action_isset) | 618 | if (m->friend_action_isset) |
611 | (*m->friend_action)(m, i, data, data_length); | 619 | (*m->friend_action)(m, i, data, data_length, m->friend_action_userdata); |
612 | break; | 620 | break; |
613 | } | 621 | } |
614 | case PACKET_ID_RECEIPT: { | 622 | case PACKET_ID_RECEIPT: { |
@@ -618,7 +626,7 @@ void doFriends(Messenger *m) | |||
618 | memcpy(&msgid, data, sizeof(msgid)); | 626 | memcpy(&msgid, data, sizeof(msgid)); |
619 | msgid = ntohl(msgid); | 627 | msgid = ntohl(msgid); |
620 | if (m->read_receipt_isset) | 628 | if (m->read_receipt_isset) |
621 | (*m->read_receipt)(m, i, msgid); | 629 | (*m->read_receipt)(m, i, msgid, m->read_receipt_userdata); |
622 | break; | 630 | break; |
623 | } | 631 | } |
624 | } | 632 | } |
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 { | |||
105 | Friend *friendlist; | 105 | Friend *friendlist; |
106 | uint32_t numfriends; | 106 | uint32_t numfriends; |
107 | 107 | ||
108 | void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t); | 108 | void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void*); |
109 | uint8_t friend_message_isset; | 109 | uint8_t friend_message_isset; |
110 | void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t); | 110 | void* friend_message_userdata; |
111 | void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void*); | ||
111 | uint8_t friend_action_isset; | 112 | uint8_t friend_action_isset; |
112 | void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t); | 113 | void* friend_action_userdata; |
114 | void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*); | ||
113 | uint8_t friend_namechange_isset; | 115 | uint8_t friend_namechange_isset; |
114 | void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t); | 116 | void* friend_namechange_userdata; |
117 | void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*); | ||
115 | uint8_t friend_statusmessagechange_isset; | 118 | uint8_t friend_statusmessagechange_isset; |
116 | void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS); | 119 | void* friend_statusmessagechange_userdata; |
120 | void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS, void*); | ||
117 | uint8_t friend_userstatuschange_isset; | 121 | uint8_t friend_userstatuschange_isset; |
118 | void (*read_receipt)(struct Messenger *m, int, uint32_t); | 122 | void* friend_userstatuschange_userdata; |
123 | void (*read_receipt)(struct Messenger *m, int, uint32_t, void*); | ||
119 | uint8_t read_receipt_isset; | 124 | uint8_t read_receipt_isset; |
120 | void (*friend_statuschange)(struct Messenger *m, int, uint8_t); | 125 | void* read_receipt_userdata; |
126 | void (*friend_statuschange)(struct Messenger *m, int, uint8_t, void*); | ||
121 | uint8_t friend_statuschange_isset; | 127 | uint8_t friend_statuschange_isset; |
122 | void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t); | 128 | void* friend_statuschange_userdata; |
129 | void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void*); | ||
123 | uint8_t friend_connectionstatuschange_isset; | 130 | uint8_t friend_connectionstatuschange_isset; |
131 | void* friend_connectionstatuschange_userdata; | ||
124 | 132 | ||
125 | 133 | ||
126 | } Messenger; | 134 | } Messenger; |
@@ -230,29 +238,29 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno); | |||
230 | 238 | ||
231 | /* set the function that will be executed when a friend request is received. | 239 | /* set the function that will be executed when a friend request is received. |
232 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 240 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ |
233 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t)); | 241 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata); |
234 | 242 | ||
235 | /* set the function that will be executed when a message from a friend is received. | 243 | /* set the function that will be executed when a message from a friend is received. |
236 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | 244 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ |
237 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); | 245 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata); |
238 | 246 | ||
239 | /* set the function that will be executed when an action from a friend is received. | 247 | /* set the function that will be executed when an action from a friend is received. |
240 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ | 248 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ |
241 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); | 249 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata); |
242 | 250 | ||
243 | /* set the callback for name changes | 251 | /* set the callback for name changes |
244 | function(int friendnumber, uint8_t *newname, uint16_t length) | 252 | function(int friendnumber, uint8_t *newname, uint16_t length) |
245 | you are not responsible for freeing newname */ | 253 | you are not responsible for freeing newname */ |
246 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); | 254 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata); |
247 | 255 | ||
248 | /* set the callback for status message changes | 256 | /* set the callback for status message changes |
249 | function(int friendnumber, uint8_t *newstatus, uint16_t length) | 257 | function(int friendnumber, uint8_t *newstatus, uint16_t length) |
250 | you are not responsible for freeing newstatus */ | 258 | you are not responsible for freeing newstatus */ |
251 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); | 259 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata); |
252 | 260 | ||
253 | /* set the callback for status type changes | 261 | /* set the callback for status type changes |
254 | function(int friendnumber, USERSTATUS kind) */ | 262 | function(int friendnumber, USERSTATUS kind) */ |
255 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS)); | 263 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata); |
256 | 264 | ||
257 | /* set the callback for read receipts | 265 | /* set the callback for read receipts |
258 | function(int friendnumber, uint32_t receipt) | 266 | function(int friendnumber, uint32_t receipt) |
@@ -261,7 +269,7 @@ void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USE | |||
261 | has been received on the other side. since core doesn't | 269 | has been received on the other side. since core doesn't |
262 | track ids for you, receipt may not correspond to any message | 270 | track ids for you, receipt may not correspond to any message |
263 | in that case, you should discard it. */ | 271 | in that case, you should discard it. */ |
264 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t)); | 272 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata); |
265 | 273 | ||
266 | /* set the callback for connection status changes | 274 | /* set the callback for connection status changes |
267 | function(int friendnumber, uint8_t status) | 275 | function(int friendnumber, uint8_t status) |
@@ -271,7 +279,7 @@ void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, u | |||
271 | note that this callback is not called when adding friends, thus the "after | 279 | note that this callback is not called when adding friends, thus the "after |
272 | being previously online" part. it's assumed that when adding friends, | 280 | being previously online" part. it's assumed that when adding friends, |
273 | their connection status is offline. */ | 281 | their connection status is offline. */ |
274 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t)); | 282 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata); |
275 | 283 | ||
276 | /* run this at startup | 284 | /* run this at startup |
277 | * returns allocated instance of Messenger on success | 285 | * 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) | |||
57 | return num; | 57 | return num; |
58 | } | 58 | } |
59 | 59 | ||
60 | static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t); | 60 | static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void*); |
61 | static uint8_t handle_friendrequest_isset = 0; | 61 | static uint8_t handle_friendrequest_isset = 0; |
62 | 62 | static void* handle_friendrequest_userdata; | |
63 | /* set the function that will be executed when a friend request is received. */ | 63 | /* set the function that will be executed when a friend request is received. */ |
64 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | 64 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata) |
65 | { | 65 | { |
66 | handle_friendrequest = function; | 66 | handle_friendrequest = function; |
67 | handle_friendrequest_isset = 1; | 67 | handle_friendrequest_isset = 1; |
68 | handle_friendrequest_userdata = userdata; | ||
68 | } | 69 | } |
69 | 70 | ||
70 | 71 | ||
@@ -121,7 +122,7 @@ static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t len | |||
121 | return 1; | 122 | return 1; |
122 | 123 | ||
123 | addto_receivedlist(public_key); | 124 | addto_receivedlist(public_key); |
124 | (*handle_friendrequest)(public_key, data, len); | 125 | (*handle_friendrequest)(public_key, data, len, handle_friendrequest_userdata); |
125 | } else { /* if request is not for us, try routing it. */ | 126 | } else { /* if request is not for us, try routing it. */ |
126 | if(route_packet(packet + 1, packet, length) == length) | 127 | if(route_packet(packet + 1, packet, length) == length) |
127 | return 0; | 128 | 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); | |||
37 | 37 | ||
38 | /* set the function that will be executed when a friend request for us is received. | 38 | /* set the function that will be executed when a friend request for us is received. |
39 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 39 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ |
40 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); | 40 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata); |
41 | 41 | ||
42 | /* sets up friendreq packet handlers */ | 42 | /* sets up friendreq packet handlers */ |
43 | void friendreq_init(void); | 43 | void friendreq_init(void); |