program.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <rtthread.h>
  2. #include <rtgui/rtgui_server.h>
  3. #include <rtgui/rtgui_system.h>
  4. #include <rtgui/rtgui_app.h>
  5. #include <rtgui/widgets/window.h>
  6. #include <rtgui/widgets/list_view.h>
  7. #include <rtgui/rtgui_xml.h>
  8. #include <rtgui/widgets/panel.h>
  9. #ifdef _WIN32_NATIVE
  10. #include <io.h>
  11. #include <dirent.h>
  12. #include <sys/stat.h>
  13. #define PATH_SEPARATOR '\\'
  14. #else
  15. #include <dfs_posix.h>
  16. #define PATH_SEPARATOR '/'
  17. #endif
  18. #define APP_PATH "/programs"
  19. #define ITEM_MAX 10
  20. static struct rtgui_list_item *items = RT_NULL;
  21. static rtgui_list_view_t* _view = RT_NULL;
  22. static int pos = -1;
  23. typedef enum
  24. {
  25. IDLE,
  26. READ_NAME,
  27. READ_ICON,
  28. READ_AUTHOR,
  29. READ_LICENSE,
  30. }XML_STATUS;
  31. static int xml_event_handler(rt_uint8_t event, const char* text, rt_size_t len, void* user)
  32. {
  33. static XML_STATUS status = IDLE;
  34. char fn[64];
  35. if(event == EVENT_START)
  36. {
  37. if(strcmp(text, "name") == 0)
  38. status = READ_NAME;
  39. else if(strcmp(text, "image") == 0)
  40. status = READ_ICON;
  41. else if(strcmp(text, "author") == 0)
  42. status = READ_AUTHOR;
  43. else if(strcmp(text, "license") == 0)
  44. status = READ_LICENSE;
  45. }
  46. else if(event == EVENT_TEXT)
  47. {
  48. switch(status)
  49. {
  50. case READ_NAME:
  51. items[++pos].name = rt_strdup(text);
  52. items[pos].parameter = items[pos].name;
  53. break;
  54. case READ_ICON:
  55. rt_snprintf(fn, sizeof(fn), "%s/%s", APP_PATH, text);
  56. items[pos].image = rtgui_image_create(fn, RT_TRUE);
  57. if(items[pos].image == RT_NULL) rt_kprintf("image create failed\n");
  58. break;
  59. case READ_AUTHOR:
  60. break;
  61. case READ_LICENSE:
  62. break;
  63. }
  64. status = IDLE;
  65. }
  66. return 1;
  67. }
  68. static int xml_load_items(const char* filename)
  69. {
  70. struct rtgui_filerw* filerw;
  71. char buffer[512];
  72. rtgui_xml_t *xml;
  73. int length;
  74. /* create filerw context */
  75. filerw = rtgui_filerw_create_file(filename, "rb");
  76. if (filerw == RT_NULL)
  77. {
  78. rt_kprintf("read file fail %s\n", filename);
  79. return 0;
  80. }
  81. length = rtgui_filerw_read(filerw, buffer, 512, 1);
  82. if(length <= 0)
  83. {
  84. rt_kprintf("read fail\n");
  85. rtgui_filerw_close(filerw);
  86. return 0;
  87. }
  88. xml = rtgui_xml_create(512, xml_event_handler, RT_NULL);
  89. if (xml != RT_NULL)
  90. {
  91. rtgui_xml_parse(xml, buffer, length);
  92. rtgui_xml_destroy(xml);
  93. }
  94. rtgui_filerw_close(filerw);
  95. return 0;
  96. }
  97. static void exec_app(rtgui_widget_t* widget, void* parameter)
  98. {
  99. char path[64];
  100. rt_module_t module;
  101. RT_ASSERT(parameter != RT_NULL);
  102. rt_snprintf(path, sizeof(path), "%s/%s/%s.mo", APP_PATH,
  103. (char*)parameter, (char*)parameter);
  104. #ifndef _WIN32
  105. module = rt_module_find((const char*)parameter);
  106. if(module == RT_NULL)
  107. rt_module_open(path);
  108. else
  109. {
  110. struct rtgui_app* app;
  111. RT_ASSERT(module->module_thread);
  112. app = (struct rtgui_app*)(module->module_thread->user_data);
  113. if(app != RT_NULL) rtgui_app_activate(app);
  114. else rt_kprintf("application is null\n");
  115. }
  116. #endif
  117. }
  118. static void scan_app_dir(const char* path)
  119. {
  120. DIR* dir;
  121. struct dirent* entry;
  122. char fn[32];
  123. dir = opendir(path);
  124. if (dir == RT_NULL)
  125. {
  126. rt_kprintf("open directory %s failed\n", path);
  127. return;
  128. }
  129. do
  130. {
  131. entry = readdir(dir);
  132. if (entry != RT_NULL)
  133. {
  134. if(entry->d_type == DFS_DT_REG) break;
  135. rt_sprintf(fn, "%s/%s/%s.xml", path, entry->d_name, entry->d_name);
  136. xml_load_items(fn);
  137. }
  138. } while(entry != RT_NULL);
  139. /* close directory */
  140. closedir(dir);
  141. }
  142. struct rtgui_panel* program_create(struct rtgui_panel* panel)
  143. {
  144. int i = 0;
  145. struct rtgui_rect rect;
  146. RT_ASSERT(panel != RT_NULL);
  147. rtgui_widget_get_extent(RTGUI_WIDGET(panel), &rect);
  148. items = (struct rtgui_list_item *) rtgui_malloc((ITEM_MAX) * sizeof(struct rtgui_list_item));
  149. for(i=0; i< ITEM_MAX; i++) items[i].action = exec_app;
  150. /* create application list */
  151. rtgui_rect_inflate(&rect, -15);
  152. scan_app_dir(APP_PATH);
  153. if(pos >= 0)
  154. {
  155. _view = rtgui_list_view_create(items, pos + 1, &rect, RTGUI_LIST_VIEW_ICON);
  156. rtgui_container_add_child(RTGUI_CONTAINER(panel), RTGUI_WIDGET(_view));
  157. }
  158. return RTGUI_PANEL(panel);
  159. }