timer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <precomp.h>
  2. #include "timer.h"
  3. #include <api/script/scriptmgr.h>
  4. #include <api/script/objects/rootobject.h>
  5. //#include <api/wac/main.h>//CUT!!
  6. //#include <api/timer/genwnd.h> //CUT!!
  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. TimerScriptController _timerController;
  50. TimerScriptController *timerController=&_timerController;
  51. // -- Functions table -------------------------------------
  52. function_descriptor_struct TimerScriptController::exportedFunction[] = {
  53. {L"setDelay", 1, (void*)STimer::script_setDelay },
  54. {L"getDelay", 0, (void*)STimer::script_getDelay },
  55. {L"start", 0, (void*)STimer::script_start },
  56. {L"stop", 0, (void*)STimer::script_stop },
  57. {L"onTimer", 0, (void*)STimer::script_onTimer },
  58. {L"isRunning",0, (void*)STimer::script_isRunning },
  59. {L"getSkipped",0, (void*)STimer::script_getSkipped },
  60. };
  61. // --------------------------------------------------------
  62. const wchar_t *TimerScriptController::getClassName() {
  63. return L"Timer";
  64. }
  65. const wchar_t *TimerScriptController::getAncestorClassName() {
  66. return L"Object";
  67. }
  68. ScriptObjectController *TimerScriptController::getAncestorController() {
  69. return rootScriptObjectController;
  70. }
  71. ScriptObject *TimerScriptController::instantiate() {
  72. STimer *s = new STimer;
  73. ASSERT(s != NULL);
  74. return s->getScriptObject();
  75. }
  76. void TimerScriptController::destroy(ScriptObject *o) {
  77. STimer *s = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  78. ASSERT(s != NULL);
  79. delete s;
  80. }
  81. void *TimerScriptController::encapsulate(ScriptObject *o) {
  82. return NULL; // no encapsulation for timer yet
  83. }
  84. void TimerScriptController::deencapsulate(void *o) {
  85. }
  86. int TimerScriptController::getNumFunctions() {
  87. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  88. }
  89. const function_descriptor_struct *TimerScriptController::getExportedFunctions() {
  90. return exportedFunction;
  91. }
  92. GUID TimerScriptController::getClassGuid() {
  93. return timerGuid;
  94. }
  95. scriptVar STimer::script_onTimer(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  96. SCRIPT_FUNCTION_INIT;
  97. PROCESS_HOOKS0(o, timerController);
  98. SCRIPT_FUNCTION_CHECKABORTEVENT;
  99. SCRIPT_EXEC_EVENT0(o);
  100. }
  101. scriptVar STimer::script_setDelay(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar d) {
  102. SCRIPT_FUNCTION_INIT;
  103. ASSERT(SOM::isNumeric(&d));
  104. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  105. if (t) t->setDelay(SOM::makeInt(&d));
  106. RETURN_SCRIPT_VOID;
  107. }
  108. scriptVar STimer::script_getDelay(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  109. SCRIPT_FUNCTION_INIT;
  110. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  111. if (t) return MAKE_SCRIPT_INT(t->getDelay());
  112. RETURN_SCRIPT_ZERO;
  113. }
  114. scriptVar STimer::script_start(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  115. SCRIPT_FUNCTION_INIT;
  116. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  117. if (t) t->start();
  118. RETURN_SCRIPT_VOID;
  119. }
  120. scriptVar STimer::script_stop(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  121. SCRIPT_FUNCTION_INIT;
  122. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  123. if (t) t->stop();
  124. RETURN_SCRIPT_VOID;
  125. }
  126. scriptVar STimer::script_isRunning(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  127. SCRIPT_FUNCTION_INIT;
  128. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  129. if (t) return MAKE_SCRIPT_BOOLEAN(t->isRunning());
  130. RETURN_SCRIPT_ZERO;
  131. }
  132. scriptVar STimer::script_getSkipped(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  133. SCRIPT_FUNCTION_INIT;
  134. STimer *t = static_cast<STimer *>(o->vcpu_getInterface(timerGuid));
  135. if (t) return MAKE_SCRIPT_INT(t->timerclient_getSkipped());
  136. RETURN_SCRIPT_ZERO;
  137. }