| 1 |
/** |
| 2 |
* This file is a part of the Cairo-Dock project |
| 3 |
* |
| 4 |
* Copyright : (C) see the 'copyright' file. |
| 5 |
* E-mail : see the 'copyright' file. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU General Public License |
| 9 |
* as published by the Free Software Foundation; either version 3 |
| 10 |
* of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#define _BSD_SOURCE |
| 21 |
#include <stdlib.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <string.h> |
| 25 |
#include <glib/gi18n.h> |
| 26 |
#include <glib/gstdio.h> |
| 27 |
|
| 28 |
#include "applet-struct.h" |
| 29 |
#include "applet-notifications.h" |
| 30 |
#include "applet-draw.h" |
| 31 |
#include "applet-wifi.h" |
| 32 |
|
| 33 |
|
| 34 |
#define _pick_string(cValueName, cValue) \ |
| 35 |
str = g_strstr_len (cOneInfopipe, -1, cValueName);\ |
| 36 |
if (str) {\ |
| 37 |
str += strlen (cValueName) + 1;\ |
| 38 |
while (*str == ' ')\ |
| 39 |
str ++;\ |
| 40 |
if (*str == '"') {\ |
| 41 |
str ++;\ |
| 42 |
str2 = strchr (str, '"'); }\ |
| 43 |
else {\ |
| 44 |
str2 = strchr (str, ' '); }\ |
| 45 |
if (str2) {\ |
| 46 |
cValue = g_strndup (str, str2 - str); }\ |
| 47 |
else {\ |
| 48 |
cValue = g_strdup (str); }\ |
| 49 |
cd_debug ("%s : %s", cValueName, cValue); } |
| 50 |
#define _pick_value(cValueName, iValue, iMaxValue)\ |
| 51 |
str = g_strstr_len (cOneInfopipe, -1, cValueName);\ |
| 52 |
if (str) {\ |
| 53 |
str += strlen (cValueName) + 1;\ |
| 54 |
iValue = atoi (str);\ |
| 55 |
str2 = strchr (str, '/');\ |
| 56 |
if (str2)\ |
| 57 |
iMaxValue = atoi (str2+1);\ |
| 58 |
cd_debug ("%s : %d (/%d)", cValueName, iValue, iMaxValue); } |
| 59 |
|
| 60 |
void cd_wifi_get_data (gpointer data) |
| 61 |
{ |
| 62 |
myData.iPreviousQuality = myData.iQuality; |
| 63 |
myData.iQuality = -1; |
| 64 |
myData.iPrevPercent = myData.iPercent; |
| 65 |
myData.iPercent = -1; |
| 66 |
myData.iPrevSignalLevel = myData.iSignalLevel; |
| 67 |
myData.iSignalLevel = -1; |
| 68 |
myData.iPrevNoiseLevel = myData.iNoiseLevel; |
| 69 |
myData.iNoiseLevel = -1; |
| 70 |
g_free (myData.cESSID); |
| 71 |
myData.cESSID = NULL; |
| 72 |
g_free (myData.cInterface); |
| 73 |
myData.cInterface = NULL; |
| 74 |
g_free (myData.cAccessPoint); |
| 75 |
myData.cAccessPoint = NULL; |
| 76 |
|
| 77 |
/* for tests: |
| 78 |
myData.iPercent = g_random_int_range (0, 100); |
| 79 |
myData.iQuality = 5 * myData.iPercent/100; |
| 80 |
myData.cInterface = g_strdup ("toto"); |
| 81 |
return;*/ |
| 82 |
|
| 83 |
/* iwconfig prints wireless interface on stdout |
| 84 |
* and other interfaces (e.g. eth0 -> no wireless extensions.) on stderr... |
| 85 |
*/ |
| 86 |
gchar *cResult = cairo_dock_launch_command_sync_with_stderr (myData.cIWConfigPath, FALSE); // to avoid warnings... or with "sh -c \"iwconfig 2> /dev/null\"" but it uses 2 process each time... |
| 87 |
if (cResult == NULL || *cResult == '\0') // error when launching iwconfig: rights problem, iwconfig not available or no wifi interface? |
| 88 |
{ |
| 89 |
g_free (cResult); |
| 90 |
return ; |
| 91 |
} |
| 92 |
|
| 93 |
//eth0 no wireless extensions. |
| 94 |
//eth2 IEEE 802.11 Nickname:"" |
| 95 |
//Access Point: Not-Associated |
| 96 |
//wlan0 IEEE 802.11abg ESSID:"NETXHO" |
| 97 |
//Mode:Managed Frequency:2.452 GHz Access Point: 00:24:2B:48:07:21 |
| 98 |
//Bit Rate=54 Mb/s Tx-Power=15 dBm |
| 99 |
//Retry long limit:7 RTS thr:off Fragment thr:off |
| 100 |
//Encryption key:C6DA-6974-1612-DA99-3049-FDC0-1399-23BA-894E-67B0-9C8B-72C7-EE5B-5876-5C58-331B [2] |
| 101 |
//Power Management:off |
| 102 |
//Link Quality=52/70 Signal level=-58 dBm Noise level=-127 dBm |
| 103 |
//Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 |
| 104 |
//Tx excessive retries:0 Invalid misc:0 Missed beacon:0 |
| 105 |
gchar **cInfopipesList = g_strsplit (cResult, "\n", -1); |
| 106 |
g_free (cResult); |
| 107 |
gchar *cOneInfopipe, *str, *str2; |
| 108 |
int i, iMaxValue=1; // gcc rale si on n'initialise pas. |
| 109 |
for (i = 0; cInfopipesList[i] != NULL; i ++) |
| 110 |
{ |
| 111 |
cOneInfopipe = cInfopipesList[i]; |
| 112 |
if (*cOneInfopipe == '\0' || *cOneInfopipe == '\n' ) // EOL: a new interface. |
| 113 |
{ |
| 114 |
if (myData.cInterface != NULL) // we only want one, break |
| 115 |
break; |
| 116 |
else |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
if (myData.cInterface == NULL) // we don't still have any interface |
| 121 |
{ |
| 122 |
str = strchr (cOneInfopipe, ' '); // interface's name is at the beginning of the line. |
| 123 |
if (str) |
| 124 |
{ |
| 125 |
str2 = str + 1; |
| 126 |
while (*str2 == ' ') |
| 127 |
str2 ++; |
| 128 |
if (strncmp (str2, "no wireless", 11) != 0) |
| 129 |
myData.cInterface = g_strndup (cOneInfopipe, str - cOneInfopipe); |
| 130 |
} |
| 131 |
cd_debug ("interface : %s", myData.cInterface); |
| 132 |
if (myData.cInterface == NULL) // no new info, skip it |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
if (myData.cESSID == NULL) |
| 137 |
{ |
| 138 |
_pick_string ("ESSID", myData.cESSID); // eth1 IEEE 802.11g ESSID:"bla bla bla" |
| 139 |
} |
| 140 |
/*if (myData.cNickName == NULL) |
| 141 |
{ |
| 142 |
_pick_string ("Nickname", myData.cNickName); |
| 143 |
}*/ |
| 144 |
if (myData.cAccessPoint == NULL) |
| 145 |
{ |
| 146 |
_pick_string ("Access Point", myData.cAccessPoint); |
| 147 |
} |
| 148 |
|
| 149 |
if (myData.iQuality >= WIFI_NB_QUALITY) // Link Quality=54/100 Signal level=-76 dBm Noise level=-78 dBm OU Link Quality:5 Signal level:219 Noise level:177 |
| 150 |
{ |
| 151 |
iMaxValue = 0; |
| 152 |
_pick_value ("Link Quality", myData.iQuality, iMaxValue); |
| 153 |
if (iMaxValue != 0) // old version, quality in % |
| 154 |
{ |
| 155 |
myData.iPercent = 100. * myData.iQuality / iMaxValue; |
| 156 |
if (myData.iPercent <= 0) |
| 157 |
myData.iQuality = WIFI_QUALITY_NO_SIGNAL; |
| 158 |
else if (myData.iPercent < 20) |
| 159 |
myData.iQuality = WIFI_QUALITY_VERY_LOW; |
| 160 |
else if (myData.iPercent < 40) |
| 161 |
myData.iQuality = WIFI_QUALITY_LOW; |
| 162 |
else if (myData.iPercent < 60) |
| 163 |
myData.iQuality = WIFI_QUALITY_MIDDLE; |
| 164 |
else if (myData.iPercent < 80) |
| 165 |
myData.iQuality = WIFI_QUALITY_GOOD; |
| 166 |
else |
| 167 |
myData.iQuality = WIFI_QUALITY_EXCELLENT; |
| 168 |
} |
| 169 |
else |
| 170 |
{ |
| 171 |
myData.iPercent = 100. * myData.iQuality / (WIFI_NB_QUALITY-1); |
| 172 |
} |
| 173 |
} |
| 174 |
if (myData.iSignalLevel == -1) |
| 175 |
{ |
| 176 |
_pick_value ("Signal level", myData.iSignalLevel, iMaxValue); |
| 177 |
} |
| 178 |
if (myData.iNoiseLevel == -1) |
| 179 |
{ |
| 180 |
_pick_value ("Noise level", myData.iNoiseLevel, iMaxValue); |
| 181 |
} |
| 182 |
} |
| 183 |
g_strfreev (cInfopipesList); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
gboolean cd_wifi_update_from_data (gpointer data) |
| 188 |
{ |
| 189 |
if (myData.cInterface != NULL) |
| 190 |
{ |
| 191 |
myData.bWirelessExt = TRUE; |
| 192 |
cd_wifi_draw_icon (); |
| 193 |
gldi_task_set_normal_frequency (myData.pTask); |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
myData.bWirelessExt = FALSE; |
| 198 |
cd_wifi_draw_no_wireless_extension (); |
| 199 |
gldi_task_downgrade_frequency (myData.pTask); |
| 200 |
} |
| 201 |
return TRUE; |
| 202 |
} |