/src/ftk_source_primary.c

http://ftk.googlecode.com/ · C · 146 lines · 94 code · 22 blank · 30 comment · 14 complexity · e1b7f705c41e8aff045d7cf4f7eedc65 MD5 · raw file

  1. /*
  2. * File: ftk_source_primary.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: gui primary 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. * 2010-10-02 Jiao JinXing <jiaojinxing1987@gmail.com> add ftk_pipe_check for rt-thread support.
  29. *
  30. */
  31. #include "ftk_pipe.h"
  32. #include "ftk_globals.h"
  33. #include "ftk_source_primary.h"
  34. #include "ftk_sources_manager.h"
  35. #define MAX_EVENTS 128
  36. typedef struct _SourcePrimaryPrivInfo
  37. {
  38. FtkPipe* pipe;
  39. FtkOnEvent on_event;
  40. void* user_data;
  41. }PrivInfo;
  42. static int ftk_source_primary_get_fd(FtkSource* thiz)
  43. {
  44. DECL_PRIV(thiz, priv);
  45. return_val_if_fail(priv != NULL, -1);
  46. return ftk_pipe_get_read_handle(priv->pipe);
  47. }
  48. static int ftk_source_primary_check(FtkSource* thiz)
  49. {
  50. #ifndef RT_THREAD
  51. return -1;
  52. #else
  53. DECL_PRIV(thiz, priv);
  54. return_val_if_fail(priv != NULL, -1);
  55. return ftk_pipe_check(priv->pipe);
  56. #endif
  57. }
  58. static Ret ftk_source_primary_dispatch(FtkSource* thiz)
  59. {
  60. FtkEvent event;
  61. DECL_PRIV(thiz, priv);
  62. int ret = ftk_pipe_read(priv->pipe, &event, sizeof(FtkEvent));
  63. return_val_if_fail(ret == sizeof(FtkEvent), RET_REMOVE);
  64. switch(event.type)
  65. {
  66. case FTK_EVT_NOP:
  67. {
  68. break;
  69. }
  70. case FTK_EVT_ADD_SOURCE:
  71. {
  72. ftk_sources_manager_add(ftk_default_sources_manager(), (FtkSource*)event.u.extra);
  73. break;
  74. }
  75. case FTK_EVT_REMOVE_SOURCE:
  76. {
  77. ftk_sources_manager_remove(ftk_default_sources_manager(), (FtkSource*)event.u.extra);
  78. break;
  79. }
  80. default:
  81. {
  82. if(priv->on_event != NULL)
  83. {
  84. priv->on_event(priv->user_data, &event);
  85. }
  86. }
  87. }
  88. return RET_OK;
  89. }
  90. static void ftk_source_primary_destroy(FtkSource* thiz)
  91. {
  92. if(thiz != NULL)
  93. {
  94. DECL_PRIV(thiz, priv);
  95. ftk_pipe_destroy(priv->pipe);
  96. FTK_ZFREE(thiz, sizeof(FtkSource) + sizeof(PrivInfo));
  97. }
  98. return;
  99. }
  100. FtkSource* ftk_source_primary_create(FtkOnEvent on_event, void* user_data)
  101. {
  102. FtkSource* thiz = (FtkSource*)FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
  103. if(thiz != NULL)
  104. {
  105. DECL_PRIV(thiz, priv);
  106. thiz->get_fd = ftk_source_primary_get_fd;
  107. thiz->check = ftk_source_primary_check;
  108. thiz->dispatch = ftk_source_primary_dispatch;
  109. thiz->destroy = ftk_source_primary_destroy;
  110. thiz->ref = 1;
  111. priv->pipe = ftk_pipe_create();
  112. priv->on_event = on_event;
  113. priv->user_data = user_data;
  114. }
  115. return thiz;
  116. }
  117. Ret ftk_source_queue_event(FtkSource* thiz, FtkEvent* event)
  118. {
  119. int ret = 0;
  120. DECL_PRIV(thiz, priv);
  121. return_val_if_fail(thiz != NULL && event != NULL, RET_FAIL);
  122. ret = ftk_pipe_write(priv->pipe, event, sizeof(FtkEvent));
  123. return ret == sizeof(FtkEvent) ? RET_OK : RET_FAIL;
  124. }