/src/ftk_source_idle.c

http://ftk.googlecode.com/ · C · 125 lines · 76 code · 20 blank · 29 comment · 11 complexity · 69200b0d3f4dfd9ad592ece4882f1ee2 MD5 · raw file

  1. /*
  2. * File: ftk_source_idle.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: idle source
  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_source_idle.h"
  31. typedef struct _SourceIdlePrivInfo
  32. {
  33. FtkIdle action;
  34. void* user_data;
  35. }PrivInfo;
  36. static int ftk_source_idle_get_fd(FtkSource* thiz)
  37. {
  38. return -1;
  39. }
  40. static int ftk_source_idle_check(FtkSource* thiz)
  41. {
  42. return 0;
  43. }
  44. static Ret ftk_source_idle_dispatch(FtkSource* thiz)
  45. {
  46. DECL_PRIV(thiz, priv);
  47. return_val_if_fail(priv->action != NULL, RET_REMOVE);
  48. if(thiz->disable > 0)
  49. {
  50. return RET_OK;
  51. }
  52. return priv->action(priv->user_data);
  53. }
  54. static void ftk_source_idle_destroy(FtkSource* thiz)
  55. {
  56. FTK_ZFREE(thiz, sizeof(FtkSource) + sizeof(PrivInfo));
  57. return;
  58. }
  59. FtkSource* ftk_source_idle_create(FtkIdle action, void* user_data)
  60. {
  61. FtkSource* thiz = NULL;
  62. return_val_if_fail(action != NULL, NULL);
  63. thiz = (FtkSource*)FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
  64. if(thiz != NULL)
  65. {
  66. DECL_PRIV(thiz, priv);
  67. thiz->get_fd = ftk_source_idle_get_fd;
  68. thiz->check = ftk_source_idle_check;
  69. thiz->dispatch = ftk_source_idle_dispatch;
  70. thiz->destroy = ftk_source_idle_destroy;
  71. thiz->ref = 1;
  72. priv->action = action;
  73. priv->user_data = user_data;
  74. }
  75. return thiz;
  76. }
  77. #ifdef FTK_SOURCE_IDLE_TEST
  78. Ret my_action(void* user_data)
  79. {
  80. int *p = (int*)user_data;
  81. (*p)++;
  82. return *p == 10 ? RET_REMOVE : RET_OK;
  83. }
  84. int main(int argc, char* argv[])
  85. {
  86. int n = 0;
  87. FtkSource* thiz = ftk_source_idle_create(my_action, &n);
  88. while(1)
  89. {
  90. assert(ftk_source_check(thiz) == 0);
  91. assert(ftk_source_get_fd(thiz) < 0);
  92. if(n < 9)
  93. {
  94. assert(ftk_source_dispatch(thiz) == RET_OK);
  95. }
  96. else
  97. {
  98. assert(ftk_source_dispatch(thiz) == RET_REMOVE);
  99. break;
  100. }
  101. }
  102. ftk_source_destroy(thiz);
  103. return 0;
  104. }
  105. #endif/*FTK_SOURCE_IDLE_TEST*/