/src/ftk_source_timer.c

http://ftk.googlecode.com/ · C · 134 lines · 80 code · 25 blank · 29 comment · 8 complexity · c77b4261bc89666dffa7e81af4482fec MD5 · raw file

  1. /*
  2. * File: ftk_source_timer.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: timer.
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2009-10-03 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_typedef.h"
  31. #include "ftk_source_timer.h"
  32. typedef struct _SourceTimerPrivInfo
  33. {
  34. FtkTimer action;
  35. void* user_data;
  36. size_t interval;
  37. size_t next_time;
  38. }PrivInfo;
  39. static int ftk_source_timer_get_fd(FtkSource* thiz)
  40. {
  41. return -1;
  42. }
  43. static int ftk_source_timer_check(FtkSource* thiz)
  44. {
  45. DECL_PRIV(thiz, priv);
  46. int t = priv->next_time - ftk_get_relative_time();
  47. t = t < 0 ? 0 : t;
  48. return t;
  49. }
  50. static void ftk_source_timer_calc_timer(PrivInfo* priv)
  51. {
  52. priv->next_time = ftk_get_relative_time() + priv->interval;
  53. return;
  54. }
  55. Ret ftk_source_timer_reset(FtkSource* thiz)
  56. {
  57. DECL_PRIV(thiz, priv);
  58. return_val_if_fail(priv != NULL, RET_FAIL);
  59. thiz->disable = 0;
  60. ftk_source_timer_calc_timer(priv);
  61. return RET_OK;
  62. }
  63. Ret ftk_source_timer_modify(FtkSource* thiz, int interval)
  64. {
  65. DECL_PRIV(thiz, priv);
  66. return_val_if_fail(priv != NULL, RET_FAIL);
  67. priv->interval = interval;
  68. ftk_source_timer_calc_timer(priv);
  69. return RET_OK;
  70. }
  71. static Ret ftk_source_timer_dispatch(FtkSource* thiz)
  72. {
  73. Ret ret = RET_FAIL;
  74. DECL_PRIV(thiz, priv);
  75. return_val_if_fail(priv->action != NULL, RET_REMOVE);
  76. if(thiz->disable > 0)
  77. {
  78. ftk_source_timer_calc_timer(priv);
  79. return RET_OK;
  80. }
  81. ret = priv->action(priv->user_data);
  82. ftk_source_timer_calc_timer(priv);
  83. return ret;
  84. }
  85. static void ftk_source_timer_destroy(FtkSource* thiz)
  86. {
  87. FTK_ZFREE(thiz, sizeof(FtkSource) + sizeof(PrivInfo));
  88. return;
  89. }
  90. FtkSource* ftk_source_timer_create(int interval, FtkTimer action, void* user_data)
  91. {
  92. FtkSource* thiz = NULL;
  93. return_val_if_fail(interval > 0 && action != NULL, NULL);
  94. thiz = (FtkSource*)FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
  95. if(thiz != NULL)
  96. {
  97. DECL_PRIV(thiz, priv);
  98. thiz->get_fd = ftk_source_timer_get_fd;
  99. thiz->check = ftk_source_timer_check;
  100. thiz->dispatch = ftk_source_timer_dispatch;
  101. thiz->destroy = ftk_source_timer_destroy;
  102. thiz->ref = 1;
  103. priv->interval = interval;
  104. priv->user_data = user_data;
  105. priv->action = action;
  106. ftk_source_timer_calc_timer(priv);
  107. }
  108. return thiz;
  109. }