1 /* $Id$ */
2 /* Copyright (c) 2011-2015 Pierre Pronchery <khorben@defora.org> */
3 /* This file is part of DeforaOS Desktop Panel */
4 /* This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
15
16
17
18 #if defined(__NetBSD__) || defined(__linux__)
19 # include <sys/types.h>
20 # include <sys/ioctl.h>
21 # include <net/if.h>
22 # include <unistd.h>
23 # include <stdio.h>
24 #endif
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <libintl.h>
29 #include <System.h>
30 #include "Panel/applet.h"
31 #define _(string) gettext(string)
32
33
34 /* USB */
35 /* private */
36 /* types */
37 typedef struct _PanelApplet
38 {
39 PanelAppletHelper * helper;
40 GtkWidget * image;
41 guint timeout;
42 #if defined(__NetBSD__) || defined(__linux__)
43 int fd;
44 #endif
45 } USB;
46
47
48 /* prototypes */
49 static USB * _usb_init(PanelAppletHelper * helper, GtkWidget ** widget);
50 static void _usb_destroy(USB * usb);
51
52 /* accessors */
53 static gboolean _usb_get(USB * usb, gboolean * active);
54 static void _usb_set(USB * usb, gboolean active);
55
56 /* callbacks */
57 static gboolean _usb_on_timeout(gpointer data);
58
59
60 /* public */
61 /* variables */
62 PanelAppletDefinition applet =
63 {
64 "USB",
65 "panel-applet-usb",
66 NULL,
67 _usb_init,
68 _usb_destroy,
69 NULL,
70 FALSE,
71 TRUE
72 };
73
74
75 /* private */
76 /* functions */
77 /* usb_init */
_usb_init(PanelAppletHelper * helper,GtkWidget ** widget)78 static USB * _usb_init(PanelAppletHelper * helper, GtkWidget ** widget)
79 {
80 const int timeout = 1000;
81 USB * usb;
82 #if GTK_CHECK_VERSION(2, 12, 0)
83 char const * tooltip = NULL;
84 #endif
85
86 if((usb = malloc(sizeof(*usb))) == NULL)
87 {
88 error_set("%s: %s", applet.name, strerror(errno));
89 return NULL;
90 }
91 usb->helper = helper;
92 #if defined(__NetBSD__) || defined(__linux__)
93 usb->fd = -1;
94 tooltip = _("USB networking device connected");
95 #endif
96 usb->image = gtk_image_new_from_icon_name("panel-applet-usb",
97 panel_window_get_icon_size(helper->window));
98 #if GTK_CHECK_VERSION(2, 12, 0)
99 if(tooltip != NULL)
100 gtk_widget_set_tooltip_text(usb->image, tooltip);
101 #endif
102 usb->timeout = (_usb_on_timeout(usb) == TRUE)
103 ? g_timeout_add(timeout, _usb_on_timeout, usb) : 0;
104 gtk_widget_set_no_show_all(usb->image, TRUE);
105 *widget = usb->image;
106 return usb;
107 }
108
109
110 /* usb_destroy */
_usb_destroy(USB * usb)111 static void _usb_destroy(USB * usb)
112 {
113 if(usb->timeout > 0)
114 g_source_remove(usb->timeout);
115 #if defined(__NetBSD__) || defined(__linux__)
116 if(usb->fd >= 0)
117 close(usb->fd);
118 #endif
119 gtk_widget_destroy(usb->image);
120 free(usb);
121 }
122
123
124 /* accessors */
125 /* usb_get */
_usb_get(USB * usb,gboolean * active)126 static gboolean _usb_get(USB * usb, gboolean * active)
127 {
128 #if defined(__NetBSD__) || defined(__linux__)
129 struct ifreq ifr;
130 # if defined(__NetBSD__)
131 const char name[] = "cdce0";
132 # elif defined(__linux__)
133 const char name[] = "usb0";
134 #endif
135
136 if(usb->fd < 0 && (usb->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
137 {
138 error_set("%s: %s: %s", applet.name, "socket", strerror(errno));
139 *active = FALSE;
140 return TRUE;
141 }
142 memset(&ifr, 0, sizeof(ifr));
143 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", name);
144 if(ioctl(usb->fd, SIOCGIFFLAGS, &ifr) == -1)
145 {
146 error_set("%s: %s: %s", applet.name, name, strerror(errno));
147 close(usb->fd);
148 usb->fd = -1;
149 *active = FALSE;
150 return TRUE;
151 }
152 close(usb->fd);
153 usb->fd = -1;
154 *active = TRUE;
155 return TRUE;
156 #else
157 /* FIXME not supported */
158 *active = FALSE;
159 error_set("%s: %s", applet.name, strerror(ENOSYS));
160 return FALSE;
161 #endif
162 }
163
164
165 /* usb_set */
_usb_set(USB * usb,gboolean active)166 static void _usb_set(USB * usb, gboolean active)
167 {
168 if(active == TRUE)
169 gtk_widget_show(usb->image);
170 else
171 gtk_widget_hide(usb->image);
172 }
173
174
175 /* callbacks */
176 /* usb_on_timeout */
_usb_on_timeout(gpointer data)177 static gboolean _usb_on_timeout(gpointer data)
178 {
179 USB * usb = data;
180 gboolean active;
181
182 if(_usb_get(usb, &active) == FALSE)
183 {
184 usb->helper->error(NULL, error_get(NULL), 1);
185 _usb_set(usb, FALSE);
186 usb->timeout = 0;
187 return FALSE;
188 }
189 _usb_set(usb, active);
190 return TRUE;
191 }
192