main.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/ArgsParser.h>
  27. #include <LibCore/File.h>
  28. #include <LibGUI/Action.h>
  29. #include <LibGUI/Application.h>
  30. #include <LibGUI/ImageWidget.h>
  31. #include <LibGUI/Menu.h>
  32. #include <LibGUI/Notification.h>
  33. #include <LibGUI/Window.h>
  34. #include <LibGfx/Bitmap.h>
  35. #include <serenity.h>
  36. #include <spawn.h>
  37. #include <stdio.h>
  38. class NetworkWidget final : public GUI::ImageWidget {
  39. C_OBJECT(NetworkWidget);
  40. public:
  41. NetworkWidget(bool notifications)
  42. {
  43. m_notifications = notifications;
  44. update_widget();
  45. start_timer(5000);
  46. }
  47. private:
  48. virtual void timer_event(Core::TimerEvent&) override
  49. {
  50. update_widget();
  51. }
  52. virtual void mousedown_event(GUI::MouseEvent& event) override
  53. {
  54. if (event.button() != GUI::MouseButton::Left)
  55. return;
  56. pid_t child_pid;
  57. const char* argv[] = { "SystemMonitor", "-t", "network", nullptr };
  58. if ((errno = posix_spawn(&child_pid, "/bin/SystemMonitor", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  59. perror("posix_spawn");
  60. return;
  61. }
  62. if (disown(child_pid) < 0)
  63. perror("disown");
  64. }
  65. virtual void update_widget()
  66. {
  67. auto adapter_info = get_adapter_info();
  68. if (adapter_info == "") {
  69. set_connected(false);
  70. m_adapter_info = "No network adapters";
  71. } else {
  72. m_adapter_info = adapter_info;
  73. }
  74. set_tooltip(m_adapter_info);
  75. if (m_connected)
  76. NetworkWidget::set_bitmap(m_connected_icon);
  77. else
  78. NetworkWidget::set_bitmap(m_disconnected_icon);
  79. update();
  80. }
  81. virtual void notify_on_connect()
  82. {
  83. if (!m_notifications)
  84. return;
  85. auto notification = GUI::Notification::construct();
  86. notification->set_title("Network");
  87. notification->set_icon(m_connected_icon);
  88. notification->set_text("Network connected");
  89. notification->show();
  90. }
  91. virtual void notify_on_disconnect()
  92. {
  93. if (!m_notifications)
  94. return;
  95. auto notification = GUI::Notification::construct();
  96. notification->set_title("Network");
  97. notification->set_icon(m_disconnected_icon);
  98. notification->set_text("Network disconnected");
  99. notification->show();
  100. }
  101. virtual void set_connected(bool connected)
  102. {
  103. if (m_connected != connected) {
  104. connected ? notify_on_connect() : notify_on_disconnect();
  105. }
  106. m_connected = connected;
  107. }
  108. virtual String get_adapter_info(bool include_loopback = false)
  109. {
  110. StringBuilder adapter_info;
  111. auto file = Core::File::construct("/proc/net/adapters");
  112. if (!file->open(Core::IODevice::ReadOnly)) {
  113. fprintf(stderr, "Error: %s\n", file->error_string());
  114. return adapter_info.to_string();
  115. }
  116. auto file_contents = file->read_all();
  117. auto json = JsonValue::from_string(file_contents);
  118. if (!json.has_value())
  119. return adapter_info.to_string();
  120. int connected_adapters = 0;
  121. json.value().as_array().for_each([&adapter_info, include_loopback, &connected_adapters](auto& value) {
  122. auto if_object = value.as_object();
  123. auto ip_address = if_object.get("ipv4_address").to_string();
  124. auto ifname = if_object.get("name").to_string();
  125. if (!include_loopback)
  126. if (ifname == "loop0")
  127. return;
  128. if (ip_address != "null")
  129. connected_adapters++;
  130. adapter_info.appendf("%s: %s\n", ifname.characters(), ip_address.characters());
  131. });
  132. // show connected icon so long as at least one adapter is connected
  133. connected_adapters ? set_connected(true) : set_connected(false);
  134. return adapter_info.to_string();
  135. }
  136. String m_adapter_info;
  137. bool m_connected = false;
  138. bool m_notifications = true;
  139. RefPtr<Gfx::Bitmap> m_connected_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/network.png");
  140. RefPtr<Gfx::Bitmap> m_disconnected_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/network-disconnected.png");
  141. };
  142. int main(int argc, char* argv[])
  143. {
  144. if (pledge("stdio recvfd sendfd accept rpath unix cpath fattr unix proc exec", nullptr) < 0) {
  145. perror("pledge");
  146. return 1;
  147. }
  148. auto app = GUI::Application::construct(argc, argv);
  149. if (pledge("stdio recvfd sendfd accept rpath unix proc exec", nullptr) < 0) {
  150. perror("pledge");
  151. return 1;
  152. }
  153. if (unveil("/res", "r") < 0) {
  154. perror("unveil");
  155. return 1;
  156. }
  157. if (unveil("/tmp/portal/notify", "rw") < 0) {
  158. perror("unveil");
  159. return 1;
  160. }
  161. if (unveil("/proc/net/adapters", "r") < 0) {
  162. perror("unveil");
  163. return 1;
  164. }
  165. if (unveil("/bin/SystemMonitor", "x") < 0) {
  166. perror("unveil");
  167. return 1;
  168. }
  169. if (unveil(nullptr, nullptr) < 0) {
  170. perror("unveil");
  171. return 1;
  172. }
  173. bool display_notifications = false;
  174. const char* name = nullptr;
  175. Core::ArgsParser args_parser;
  176. args_parser.add_option(display_notifications, "Display notifications", "display-notifications", 'd');
  177. args_parser.add_option(name, "Applet name used by WindowServer.ini to set the applet order", "name", 'n', "name");
  178. args_parser.parse(argc, argv);
  179. if (name == nullptr)
  180. name = "Network";
  181. auto window = GUI::Window::construct();
  182. window->set_title(name);
  183. window->set_window_type(GUI::WindowType::Applet);
  184. window->resize(16, 16);
  185. auto& icon = window->set_main_widget<NetworkWidget>(display_notifications);
  186. icon.set_fill_with_background_color(true);
  187. icon.load_from_file("/res/icons/16x16/network.png");
  188. window->resize(16, 16);
  189. window->show();
  190. return app->exec();
  191. }