timerclient.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "api.h"
  2. #include <api/timer/api_timer.h>
  3. #include <api/timer/timerclient.h>
  4. #define CBCLASS TimerClientI
  5. START_DISPATCH;
  6. // this one doesn't map directly so that we can catch deferredcb timers
  7. VCB(TIMERCLIENT_TIMERCALLBACK, timerclient_handleDeferredCallback);
  8. CB(TIMERCLIENT_GETMASTERCLIENT, timerclient_getMasterClient);
  9. VCB(TIMERCLIENT_ONMASTERMUX, timerclient_onMasterClientMultiplex);
  10. CB(TIMERCLIENT_GETDEPPTR, timerclient_getDependencyPtr);
  11. CB(TIMERCLIENT_GETSKIPPED, timerclient_getSkipped);
  12. VCB(TIMERCLIENT_SETSKIPPED, timerclient_setSkipped);
  13. VCB(TIMERCLIENT_POSTDEFERREDCB , timerclient_postDeferredCallback);
  14. CB(TIMERCLIENT_ONDEFERREDCB , timerclient_onDeferredCallback);
  15. CB(TIMERCLIENT_GETNAME, timerclient_getName);
  16. END_DISPATCH;
  17. TimerClientI::TimerClientI()
  18. : skipped(0), timerdelay(0), disallowset(0)
  19. { }
  20. TimerClientI::~TimerClientI()
  21. {
  22. disallowset = 1;
  23. if (cbs.getNumItems() > 0)
  24. {
  25. TimerToken token;
  26. if (tokens.reverseGetItem(DEFERREDCB_TIMERID, &token)) // TODO: it would be nice to have a combo get/del, so we don't have to search twice
  27. {
  28. WASABI_API_TIMER->timer_remove(this, token);
  29. tokens.delItem(token);
  30. }
  31. }
  32. cbs.deleteAll();
  33. }
  34. int TimerClientI::timerclient_setTimer(intptr_t id, int ms)
  35. {
  36. if (disallowset) return 0;
  37. ASSERTPR(id > 0, "A timer id cannot be <= 0");
  38. ASSERTPR(id != DEFERREDCB_TIMERID, "please chose another timer id");
  39. TimerToken token = WASABI_API_TIMER->timer_add(this, id, ms);
  40. tokens.addItem(token, id);
  41. return 1;
  42. }
  43. int TimerClientI::timerclient_killTimer(intptr_t id)
  44. {
  45. TimerToken token;
  46. if (tokens.reverseGetItem(id, &token)) // TODO: it would be nice to have a combo get/del, so we don't have to search twice
  47. {
  48. WASABI_API_TIMER->timer_remove(this, token);
  49. tokens.delItem(token);
  50. }
  51. return 1;
  52. }
  53. void TimerClientI::timerclient_postDeferredCallback(intptr_t param1, intptr_t param2, int mindelay)
  54. {
  55. if (disallowset) return ;
  56. for (int i = 0;i < cbs.getNumItems();i++)
  57. {
  58. deferred_callback *cb = cbs.enumItem(i);
  59. if (cb->param1 == param1 && cb->param2 == param2)
  60. {
  61. cbs.removeByPos(i);
  62. break;
  63. }
  64. }
  65. deferred_callback *c = new deferred_callback;
  66. c->origin = this;
  67. c->param1 = param1;
  68. c->param2 = param2;
  69. cbs.addItem(c);
  70. TimerToken token = WASABI_API_TIMER->timer_add(this, DEFERREDCB_TIMERID, MAX(1, mindelay));
  71. tokens.addItem(token, DEFERREDCB_TIMERID);
  72. }
  73. void TimerClientI::timerclient_handleDeferredCallback(TimerToken token)
  74. {
  75. // let deriving class handle it. note we call into here even if
  76. // it's the deferred cb id, since older versions did that too,
  77. // expecting the user to call down on the method.
  78. #ifdef WIN64
  79. LPARAM id;
  80. #else
  81. intptr_t id;
  82. #endif
  83. if (tokens.getItem(token, &id))
  84. {
  85. timerclient_timerCallback((int)id);
  86. // process our deferred cb id
  87. if (id == DEFERREDCB_TIMERID)
  88. {
  89. WASABI_API_TIMER->timer_remove(this, token);
  90. PtrList<deferred_callback> temp(cbs);
  91. cbs.removeAll();
  92. foreach(temp)
  93. deferred_callback *c = temp.getfor();
  94. c->origin->timerclient_onDeferredCallback(c->param1, c->param2);
  95. delete c;
  96. endfor
  97. }
  98. }
  99. }