timer.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:4k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*****************************************************************************
  2.  *
  3.  * This program is free software ; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  16.  *
  17.  * $Id: timer.c 339 2005-11-15 11:22:45Z picard $
  18.  *
  19.  * The Core Pocket Media Player
  20.  * Copyright (c) 2004-2005 Gabor Kovacs
  21.  *
  22.  ****************************************************************************/
  23. #include "common.h"
  24. static const datatable Params[] = 
  25. {
  26. { TIMER_TIME, TYPE_INT, DF_HIDDEN },
  27. { TIMER_SPEED, TYPE_FRACTION, DF_HIDDEN },
  28. { TIMER_PLAY, TYPE_BOOL, DF_HIDDEN },
  29. DATATABLE_END(TIMER_CLASS)
  30. };
  31. int TimerEnum(void* p, int* No, datadef* Param)
  32. {
  33. return NodeEnumTable(No,Param,Params);
  34. }
  35. static const nodedef Timer =
  36. {
  37. CF_ABSTRACT,
  38. TIMER_CLASS,
  39. NODE_CLASS,
  40. PRI_DEFAULT,
  41. };
  42. //--------------------------------------------------------------------------------------
  43. typedef struct systimer
  44. {
  45. node Node;
  46. void* Section;
  47. bool_t Play;
  48. fraction Speed;
  49. fraction SpeedTime;
  50. tick_t TickStart;
  51. int TimeRef;
  52. } systimer;
  53. static int Get(systimer* p, int No, void* Data, int Size)
  54. {
  55. int Result = ERR_INVALID_PARAM;
  56. switch (No)
  57. {
  58. case TIMER_PLAY: GETVALUE(p->Play,bool_t); break;
  59. case TIMER_SPEED: GETVALUE(p->Speed,fraction); break;
  60. case TIMER_TIME:
  61. assert(Size == sizeof(tick_t));
  62. LockEnter(p->Section);
  63. if (p->Speed.Num==0) // benchmark mode
  64. *(tick_t*)Data = TIME_BENCH;
  65. else
  66. if (p->Play)
  67. *(tick_t*)Data = p->TickStart + Scale(GetTimeTick()-p->TimeRef,p->SpeedTime.Num,p->SpeedTime.Den);
  68. else
  69. *(tick_t*)Data = p->TickStart;
  70. LockLeave(p->Section);
  71. Result = ERR_NONE;
  72. break;
  73. }
  74. return Result;
  75. }
  76. static int Set(systimer* p, int No, const void* Data, int Size)
  77. {
  78. int Result = ERR_INVALID_PARAM;
  79. switch (No)
  80. {
  81. case TIMER_TIME:
  82. assert(Size == sizeof(tick_t));
  83. LockEnter(p->Section);
  84. p->TickStart = *(tick_t*)Data;
  85. p->TimeRef = GetTimeTick();
  86. LockLeave(p->Section);
  87. Result = ERR_NONE;
  88. break;
  89. case TIMER_SPEED:
  90. assert(Size == sizeof(fraction));
  91. if (!EqFrac(&p->Speed, (const fraction*)Data))
  92. {
  93. LockEnter(p->Section);
  94. if (p->Play)
  95. {
  96. int t = GetTimeTick();
  97. p->TickStart += Scale(t-p->TimeRef,p->SpeedTime.Num,p->SpeedTime.Den);
  98. p->TimeRef = t;
  99. }
  100. p->Speed = *(fraction*)Data;
  101. p->SpeedTime = p->Speed;
  102. p->SpeedTime.Num *= TICKSPERSEC;
  103. p->SpeedTime.Den *= GetTimeFreq();
  104. LockLeave(p->Section);
  105. }
  106. Result = ERR_NONE;
  107. break;
  108. case TIMER_PLAY:
  109. assert(Size == sizeof(bool_t));
  110. if (p->Play != *(bool_t*)Data)
  111. {
  112. int t;
  113. LockEnter(p->Section);
  114. t = GetTimeTick();
  115. p->Play = *(bool_t*)Data;
  116. if (!p->Play) // save time
  117. p->TickStart += Scale(t-p->TimeRef,p->SpeedTime.Num,p->SpeedTime.Den);
  118. p->TimeRef = t;
  119. LockLeave(p->Section);
  120. }
  121. Result = ERR_NONE;
  122. break;
  123. }
  124. return Result;
  125. }
  126. static int Create(systimer* p)
  127. {
  128. p->Node.Enum = (nodeenum)TimerEnum;
  129. p->Node.Get = (nodeget)Get;
  130. p->Node.Set = (nodeset)Set;
  131. p->Speed.Den = p->Speed.Num = 1;
  132. p->SpeedTime.Num = TICKSPERSEC;
  133. p->SpeedTime.Den = GetTimeFreq();
  134. p->Section = LockCreate();
  135. return ERR_NONE;
  136. }
  137. static void Delete(systimer* p)
  138. {
  139. LockDelete(p->Section);
  140. }
  141. static const nodedef SysTimer =
  142. {
  143. sizeof(systimer)|CF_GLOBAL,
  144. SYSTIMER_ID,
  145. TIMER_CLASS,
  146. PRI_MAXIMUM,
  147. (nodecreate)Create,
  148. (nodedelete)Delete,
  149. };
  150. void Timer_Init()
  151. {
  152. NodeRegisterClass(&Timer);
  153. NodeRegisterClass(&SysTimer);
  154. }
  155. void Timer_Done()
  156. {
  157. NodeUnRegisterClass(SYSTIMER_ID);
  158. NodeUnRegisterClass(TIMER_CLASS);
  159. }