device_info.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <rtgui/rtgui.h>
  2. #include <rtgui/rtgui_system.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <rtgui/widgets/view.h>
  6. #include <rtgui/widgets/workbench.h>
  7. #ifdef RT_USING_LWIP
  8. #include <lwip/err.h>
  9. #include <lwip/dns.h>
  10. #include <lwip/netif.h>
  11. #endif
  12. static struct rtgui_view* device_view = RT_NULL;
  13. int get_thread_cnt()
  14. {
  15. int cnt = 0;
  16. struct rt_list_node *list, *node;
  17. extern struct rt_object_information rt_object_container[];
  18. list = &rt_object_container[RT_Object_Class_Thread].object_list;
  19. for (node = list->next; node != list; node = node->next)
  20. {
  21. cnt ++;
  22. }
  23. return cnt;
  24. }
  25. /*
  26. * Device Information View
  27. * Thread:
  28. * IP Address:
  29. * Gateway:
  30. * DNS:
  31. */
  32. static rt_bool_t view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  33. {
  34. switch (event->type)
  35. {
  36. case RTGUI_EVENT_PAINT:
  37. {
  38. struct rtgui_dc* dc;
  39. struct rtgui_rect rect;
  40. char* line;
  41. rt_uint32_t total, used, max_used;
  42. line = rtgui_malloc(256);
  43. if (line == RT_NULL) return RT_FALSE;
  44. dc = rtgui_dc_begin_drawing(widget);
  45. if (dc == RT_NULL)
  46. {
  47. rtgui_free(line);
  48. return RT_FALSE;
  49. }
  50. rtgui_widget_get_rect(widget, &rect);
  51. /* fill background */
  52. rtgui_dc_fill_rect(dc, &rect);
  53. rect.y2 = rect.y1 + 18;
  54. {
  55. rt_uint16_t rect_width;
  56. rtgui_color_t saved;
  57. rtgui_rect_t mem_rect = rect;
  58. rtgui_rect_inflate(&mem_rect, -2);
  59. rtgui_dc_draw_rect(dc, &mem_rect);
  60. rtgui_rect_inflate(&mem_rect, -1);
  61. rect_width = rtgui_rect_width(mem_rect);
  62. saved = RTGUI_WIDGET_BACKGROUND(widget);
  63. RTGUI_WIDGET_BACKGROUND(widget) = light_grey;
  64. mem_rect.x2 = mem_rect.x1 + (max_used * rect_width / total);
  65. rtgui_dc_fill_rect(dc, &mem_rect);
  66. RTGUI_WIDGET_BACKGROUND(widget) = blue;
  67. mem_rect.x2 = mem_rect.x1 + (used * rect_width / total);
  68. rtgui_dc_fill_rect(dc, &mem_rect);
  69. /* restore color */
  70. RTGUI_WIDGET_BACKGROUND(widget) = saved;
  71. }
  72. rect.y1 += 18; rect.y2 += 18;
  73. sprintf(line, "盄最杅: %d", get_thread_cnt());
  74. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  75. #ifdef RT_USING_LWIP
  76. {
  77. struct ip_addr ip_addr;
  78. struct _ip_addr
  79. {
  80. rt_uint8_t addr0, addr1, addr2, addr3;
  81. } *addr;
  82. addr = (struct _ip_addr*)&netif_default->ip_addr.addr;
  83. sprintf(line, "IP華硊 : %d.%d.%d.%d", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
  84. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  85. addr = (struct _ip_addr*)&netif_default->gw.addr;
  86. sprintf(line, "厙壽華硊: %d.%d.%d.%d", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
  87. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  88. addr = (struct _ip_addr*)&netif_default->netmask.addr;
  89. sprintf(line, "厙釐栚鎢: %d.%d.%d.%d", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
  90. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  91. #if LWIP_DNS
  92. ip_addr = dns_getserver(0);
  93. addr = (struct _ip_addr*)&ip_addr;
  94. sprintf(line, "DNS華硊 : %d.%d.%d.%d", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
  95. rtgui_dc_draw_text(dc, line, &rect); rect.y1 += 16; rect.y2 += 16;
  96. #endif
  97. }
  98. #endif
  99. rtgui_dc_end_drawing(dc);
  100. rtgui_free(line);
  101. return RT_FALSE;
  102. }
  103. case RTGUI_EVENT_KBD:
  104. {
  105. struct rtgui_event_kbd* ekbd;
  106. ekbd = (struct rtgui_event_kbd*)event;
  107. if (ekbd->type == RTGUI_KEYDOWN && ekbd->key == RTGUIK_RETURN)
  108. {
  109. rtgui_workbench_t* workbench;
  110. workbench = RTGUI_WORKBENCH(RTGUI_WIDGET(device_view)->parent);
  111. rtgui_workbench_remove_view(workbench, device_view);
  112. rtgui_view_destroy(device_view);
  113. device_view = RT_NULL;
  114. }
  115. }
  116. return RT_FALSE;
  117. }
  118. /* use parent event handler */
  119. return rtgui_view_event_handler(widget, event);
  120. }
  121. rtgui_view_t *device_view_create(rtgui_workbench_t* workbench)
  122. {
  123. if (device_view != RT_NULL)
  124. {
  125. rtgui_view_show(device_view, RT_FALSE);
  126. }
  127. else
  128. {
  129. /* create a view */
  130. device_view = rtgui_view_create("Device Info");
  131. /* set view event handler */
  132. rtgui_widget_set_event_handler(RTGUI_WIDGET(device_view), view_event_handler);
  133. /* this view can be focused */
  134. RTGUI_WIDGET(device_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  135. /* add view to workbench */
  136. rtgui_workbench_add_view(workbench, device_view);
  137. }
  138. return device_view;
  139. }