stimer.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "api.h"
  2. #include "stimer.h"
  3. #include <api/script/objcontroller.h>
  4. #include <api/script/objects/rootobj.h>
  5. #include "Factory.h"
  6. extern Factory factory;
  7. STimer::STimer() {
  8. getScriptObject()->vcpu_setInterface(timerGuid, (void *)static_cast<STimer *>(this));
  9. getScriptObject()->vcpu_setClassName(L"Timer");
  10. getScriptObject()->vcpu_setController(timerController);
  11. delay = 1000;
  12. started = 0;
  13. }
  14. STimer::~STimer() {
  15. }
  16. void STimer::setDelay(int d) {
  17. delay = d;
  18. if (started)
  19. timerclient_setTimer(STIMER_ID, getDelay());
  20. }
  21. int STimer::getDelay(void) {
  22. return delay;
  23. }
  24. void STimer::start(void) {
  25. if (started) { stop(); }
  26. timerclient_setTimer(STIMER_ID, getDelay());
  27. started = 1;
  28. }
  29. void STimer::stop(void) {
  30. if (!started) return;
  31. timerclient_killTimer(STIMER_ID);
  32. started = 0;
  33. }
  34. void STimer::onTimer(void)
  35. {
  36. if (started)
  37. {
  38. script_onTimer(SCRIPT_CALL, getScriptObject());
  39. }
  40. }
  41. void STimer::timerclient_timerCallback(int id)
  42. {
  43. if (id == STIMER_ID)
  44. onTimer();
  45. }
  46. int STimer::isRunning() {
  47. return started;
  48. }
  49. extern ScriptObjectController *script_root;
  50. TimerScriptController _timerController;
  51. TimerScriptController *timerController=&_timerController;
  52. // -- Functions table -------------------------------------
  53. function_descriptor_struct TimerScriptController::exportedFunction[] = {
  54. {L"setDelay", 1, (void*)STimer::script_setDelay },
  55. {L"getDelay", 0, (void*)STimer::script_getDelay },
  56. {L"start", 0, (void*)STimer::script_start },
  57. {L"stop", 0, (void*)STimer::script_stop },
  58. {L"onTimer", 0, (void*)STimer::script_onTimer },
  59. {L"isRunning",0, (void*)STimer::script_isRunning },
  60. {L"getSkipped",0, (void*)STimer::script_getSkipped },
  61. };
  62. // --------------------------------------------------------
  63. const wchar_t *TimerScriptController::getClassName() {
  64. return L"Timer";
  65. }
  66. const wchar_t *TimerScriptController::getAncestorClassName() {
  67. return L"Object";
  68. }
  69. ScriptObjectController *TimerScriptController::getAncestorController() {
  70. return script_root;
  71. }
  72. ScriptObject *TimerScriptController::instantiate() {
  73. if (!WASABI_API_TIMER)
  74. {
  75. WASABI_API_TIMER = (TimerApi *)factory.GetInterface(0);
  76. }
  77. STimer *s = new STimer;
  78. ASSERT(s != NULL);
  79. return s->getScriptObject();
  80. }
  81. void TimerScriptController::destroy(ScriptObject *o) {
  82. STimer *s = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  83. ASSERT(s != NULL);
  84. delete s;
  85. }
  86. void *TimerScriptController::encapsulate(ScriptObject *o) {
  87. return NULL; // no encapsulation for timer yet
  88. }
  89. void TimerScriptController::deencapsulate(void *o) {
  90. }
  91. int TimerScriptController::getNumFunctions() {
  92. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  93. }
  94. const function_descriptor_struct *TimerScriptController::getExportedFunctions() {
  95. return exportedFunction;
  96. }
  97. GUID TimerScriptController::getClassGuid() {
  98. return timerGuid;
  99. }
  100. scriptVar STimer::script_onTimer(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  101. SCRIPT_FUNCTION_INIT;
  102. PROCESS_HOOKS0(o, timerController);
  103. SCRIPT_FUNCTION_CHECKABORTEVENT;
  104. SCRIPT_EXEC_EVENT0(o);
  105. }
  106. static bool isNumeric(int t)
  107. {
  108. return (t == SCRIPT_INT || t == SCRIPT_BOOLEAN || t == SCRIPT_FLOAT || t == SCRIPT_DOUBLE);
  109. }
  110. scriptVar STimer::script_setDelay(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar d) {
  111. SCRIPT_FUNCTION_INIT;
  112. ASSERT(isNumeric(d.type));
  113. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  114. if (t) t->setDelay(d.data.idata);
  115. RETURN_SCRIPT_VOID;
  116. }
  117. scriptVar STimer::script_getDelay(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  118. SCRIPT_FUNCTION_INIT;
  119. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  120. if (t) return MAKE_SCRIPT_INT(t->getDelay());
  121. RETURN_SCRIPT_ZERO;
  122. }
  123. scriptVar STimer::script_start(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  124. SCRIPT_FUNCTION_INIT;
  125. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  126. if (t) t->start();
  127. RETURN_SCRIPT_VOID;
  128. }
  129. scriptVar STimer::script_stop(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  130. SCRIPT_FUNCTION_INIT;
  131. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  132. if (t) t->stop();
  133. RETURN_SCRIPT_VOID;
  134. }
  135. scriptVar STimer::script_isRunning(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  136. SCRIPT_FUNCTION_INIT;
  137. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  138. if (t) return MAKE_SCRIPT_BOOLEAN(t->isRunning());
  139. RETURN_SCRIPT_ZERO;
  140. }
  141. scriptVar STimer::script_getSkipped(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  142. SCRIPT_FUNCTION_INIT;
  143. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  144. if (t) return MAKE_SCRIPT_INT(t->timerclient_getSkipped());
  145. RETURN_SCRIPT_ZERO;
  146. }