~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to Disks/src/applet-disks.c

  • Committer: Matthieu Baerts
  • Date: 2011-08-09 19:51:45 UTC
  • mfrom: (1.1.12 upstream)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: matttbe@gmail.com-20110809195145-6t55jtykipujuyn5
* New upstream release. (LP: #823514)
* Upstream (short) ChangeLog:
 - The Power-Manager applet has been rewritten to work on any plateform.
 - Compiz-icon applet has been replaced by Composite-Manager.
 - Improved integration in the XFCE, LXDE and GNOME3 desktop.
 - Logout is now working even if there is no/an unknow session manager.
 - Several new DBus methods lets you interact on the dock more easily.
 - Added the possibility to shutdown/logout with GMenu.
 - Improved Status-Notifier and added its own watcher.
 - Added a new applet: Impulse (still need testing).
 - Fixed a few bugs as usual.
* debian/patches:
 - Removed all patches (they was already unapplied).
* debian/copyright:
 - Updated with new files/directory.
* debian/cairo-dock-plug-ins.install:
 - Added status-notifier-watcher used by Status-Notifier applet.
* debian/control:
 - Updated the version of cairo-dock-[core;data;dev].
 - Bump Standard-Version to 3.9.2.
 - Added fftw3, pulse (Impulse) and upower-glib (Logout)
    as new dependences for the build process.
 - Added indicator-[me;messages] as new recommendations
    for cairo-dock-plug-ins (needed for [Me;Messaging]Menu applets).
 - Removed gawk from the build dependences list (no longer needed).
* cairo-dock-plug-ins package needs to be recompiled (LP: #811579)

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <glib.h>
25
25
#include <glib/gprintf.h>
26
26
 
 
27
#include <sys/statvfs.h>
 
28
 
27
29
#include "applet-struct.h"
28
30
#include "applet-notifications.h"
29
31
#include "applet-disks.h"
88
90
 
89
91
void cd_disks_format_value_on_icon (CairoDataRenderer *pRenderer, int iNumValue, gchar *cFormatBuffer, int iBufferLength, CairoDockModuleInstance *myApplet)
90
92
{
91
 
        static gchar s_upRateFormatted[11];
92
 
        double fValue = cairo_data_renderer_get_normalized_current_value_with_latency (pRenderer, iNumValue);
93
 
        int i = iNumValue / 2;
94
 
        CDDiskSpeedData *pSpeed = g_list_nth_data (myData.lDisks, i);
95
 
        fValue *= (iNumValue == i * 2 ? pSpeed->uMaxReadRate : pSpeed->uMaxWriteRate);
96
 
 
97
 
        _cd_speed_formatRate (fValue, s_upRateFormatted, 11, FALSE);
98
 
        snprintf (cFormatBuffer, iBufferLength,
99
 
                "%s%s",
100
 
                cairo_data_renderer_can_write_values (pRenderer) ? (iNumValue == i * 2 ?"↑" : "↓") : "",
101
 
                s_upRateFormatted);
 
93
        if (iNumValue < (int) myConfig.iNumberParts)
 
94
                {
 
95
                        double *pSize;
 
96
                        pSize = g_list_nth_data (myData.lParts, iNumValue);
 
97
                        snprintf (cFormatBuffer, iBufferLength,
 
98
                                "%.f%%",
 
99
                                *pSize * 100);
 
100
                }
 
101
        else
 
102
        {
 
103
                static gchar s_upRateFormatted[11];
 
104
                double fValue = cairo_data_renderer_get_normalized_current_value_with_latency (pRenderer, iNumValue);
 
105
                int i = iNumValue / 2;
 
106
                CDDiskSpeedData *pSpeed = g_list_nth_data (myData.lDisks, i);
 
107
                fValue *= (iNumValue == i * 2 ? pSpeed->uMaxReadRate : pSpeed->uMaxWriteRate);
 
108
        
 
109
                _cd_speed_formatRate (fValue, s_upRateFormatted, 11, FALSE);
 
110
                snprintf (cFormatBuffer, iBufferLength,
 
111
                        "%s%s",
 
112
                        cairo_data_renderer_can_write_values (pRenderer) ? (iNumValue == i * 2 ?"↑" : "↓") : "",
 
113
                        s_upRateFormatted);
 
114
        }
102
115
}
103
116
 
104
117
 
107
120
        static gchar s_readRateFormatted[11], s_writeRateFormatted[11];
108
121
        static double s_fValues[CD_DISKS_NB_MAX_VALUES];
109
122
                //~ memset (s_fValues, 0, sizeof (s_fValues));
110
 
        CDDiskSpeedData *pSpeed;
111
123
        GString *sLabel = g_string_new ("");
112
124
        gsize i;
113
125
 
114
126
        CD_APPLET_ENTER;
 
127
        if (myConfig.iNumberParts > 0)
 
128
        {
 
129
                double *pSize;
 
130
                for (i = 0; i < myConfig.iNumberParts; i++)
 
131
                {
 
132
                        pSize = g_list_nth_data (myData.lParts, i);
 
133
                        s_fValues[i] = *pSize;
 
134
                        //cd_warning("Partition %d = %s : %.1f%%", i, myConfig.cParts[i], *pSize * 100);
 
135
 
 
136
                        if (myConfig.iInfoDisplay == CAIRO_DOCK_INFO_ON_LABEL)
 
137
                        {
 
138
                                if (i > 0) 
 
139
                                        g_string_append (sLabel, " - ");
 
140
 
 
141
                                g_string_append_printf (sLabel, "%s : %.1f%%", myConfig.cParts[i], *pSize * 100);
 
142
 
 
143
                        }
 
144
                }
 
145
        }
 
146
        
115
147
        if (myData.iNumberDisks > 0)
116
148
        {
 
149
                CDDiskSpeedData *pSpeed;
117
150
                for (i = 0; i < myData.iNumberDisks; i++)
118
151
                {
119
152
                        pSpeed = g_list_nth_data (myData.lDisks, i);
144
177
                        }
145
178
 
146
179
                }
147
 
                
 
180
        }
 
181
 
 
182
        if (myConfig.iNumberParts + myData.iNumberDisks > 0)
 
183
        {
148
184
                /// Display data
149
185
                CD_APPLET_RENDER_NEW_DATA_ON_MY_ICON (s_fValues);
150
186
                if (myConfig.iInfoDisplay == CAIRO_DOCK_INFO_ON_LABEL)
151
187
                        CD_APPLET_SET_NAME_FOR_MY_ICON (sLabel->str);
152
 
                g_string_free (sLabel, TRUE);
153
188
        }
154
189
 
 
190
        g_string_free (sLabel, TRUE);
155
191
        CD_APPLET_LEAVE (TRUE);
156
192
}
157
193
 
183
219
        double fTimeElapsed = g_timer_elapsed (myData.pClock, NULL);
184
220
        g_timer_start (myData.pClock);
185
221
 
186
 
        if (!(myData.iNumberDisks > 0))
 
222
        if (!(myConfig.iNumberParts + myData.iNumberDisks > 0))
187
223
        {
188
224
                cairo_dock_downgrade_task_frequency (myData.pPeriodicTask);
189
225
                cd_warning("Disks : No disk defined");
190
226
        }
191
 
        g_return_if_fail ((fTimeElapsed > 0.1) || (myData.iNumberDisks > 0));
 
227
        g_return_if_fail ((fTimeElapsed > 0.1) || (myConfig.iNumberParts + myData.iNumberDisks > 0));
192
228
        
193
 
        char disk_name [16];
194
 
        //~ int disk_major;
195
 
 
196
 
        FILE* fd;
197
 
        buff[BUFFSIZE-1] = 0; 
198
 
        fd = fopen (DISKS_SPEED_DATA_PIPE, "rb");
199
 
        if (!fd) 
200
 
        {
201
 
                cairo_dock_downgrade_task_frequency (myData.pPeriodicTask);
202
 
                cd_warning("Disks : Your kernel doesn't support diskstat. (2.5.70 or above required)");
203
 
        }
204
 
        else
205
 
        {
206
 
                //~ cairo_dock_set_normal_task_frequency (myData.pPeriodicTask);
 
229
        if (myConfig.iNumberParts > 0)
 
230
        {
207
231
                gsize i;
208
 
                CDDiskSpeedData* pSpeed;
209
 
                long long unsigned uReadBlocks, uWriteBlocks;
210
 
                gboolean bFound;
211
 
                for (;;)
212
 
                {
213
 
                        if (!fgets(buff,BUFFSIZE-1,fd))
 
232
                struct statvfs buffer;
 
233
                int            status;
 
234
                double *pSize;
 
235
                for (i = 0; i < myConfig.iNumberParts; i++)
 
236
                {
 
237
                        pSize = g_list_nth_data (myData.lParts, i);
 
238
                        status = statvfs(myConfig.cParts[i], &buffer);
 
239
                        *pSize = 1. - (double) buffer.f_bfree / (double) buffer.f_blocks;
 
240
                }
 
241
        }
 
242
 
 
243
        if (myData.iNumberDisks > 0) 
 
244
        {
 
245
                char disk_name [16];
 
246
                //~ int disk_major;
 
247
        
 
248
                FILE* fd;
 
249
                buff[BUFFSIZE-1] = 0; 
 
250
                fd = fopen (DISKS_SPEED_DATA_PIPE, "rb");
 
251
                if (!fd) 
 
252
                {
 
253
                        cairo_dock_downgrade_task_frequency (myData.pPeriodicTask);
 
254
                        cd_warning("Disks : Your kernel doesn't support diskstat. (2.5.70 or above required)");
 
255
                }
 
256
                else
 
257
                {
 
258
                        //~ cairo_dock_set_normal_task_frequency (myData.pPeriodicTask);
 
259
                        gsize i;
 
260
                        CDDiskSpeedData* pSpeed;
 
261
                        long long unsigned uReadBlocks, uWriteBlocks;
 
262
                        gboolean bFound;
 
263
                        for (;;)
214
264
                        {
215
 
                                fclose(fd);
216
 
                                break;
217
 
                        }
218
 
        
219
 
                        /// Using Linux iostat : http://www.kernel.org/doc/Documentation/iostats.txt
220
 
                        /// gathering fields 3, 6 and 10
221
 
                        sscanf(buff,  "   %*d    %*d %15s %*u %*u %llu %*u %*u %*u %llu %*u %*u %*u %*u",
222
 
                                //&disk_major, // field 1
223
 
                                disk_name,
224
 
                                &uReadBlocks,
225
 
                                &uWriteBlocks 
226
 
                                );
227
 
 
228
 
                        bFound = FALSE;
229
 
                        if (strlen (disk_name) == 3)
230
 
                                for (i = 0; i < myData.iNumberDisks; i++)
 
265
                                if (!fgets(buff,BUFFSIZE-1,fd))
231
266
                                {
232
 
                                        pSpeed = g_list_nth_data (myData.lDisks, i);
233
 
                                        if (strcmp (pSpeed->cName, disk_name) == 0)
 
267
                                        fclose(fd);
 
268
                                        break;
 
269
                                }
 
270
                
 
271
                                /// Using Linux iostat : http://www.kernel.org/doc/Documentation/iostats.txt
 
272
                                /// gathering fields 3, 6 and 10
 
273
                                sscanf(buff,  "   %*d    %*d %15s %*u %*u %llu %*u %*u %*u %llu %*u %*u %*u %*u",
 
274
                                        //&disk_major, // field 1
 
275
                                        disk_name,
 
276
                                        &uReadBlocks,
 
277
                                        &uWriteBlocks 
 
278
                                        );
 
279
        
 
280
                                bFound = FALSE;
 
281
                                if (strlen (disk_name) == 3)
 
282
                                        for (i = 0; i < myData.iNumberDisks; i++)
234
283
                                        {
235
 
                                                if (pSpeed->bInitialized)  // la 1ere iteration on ne peut pas calculer le debit.
 
284
                                                pSpeed = g_list_nth_data (myData.lDisks, i);
 
285
                                                if (strcmp (pSpeed->cName, disk_name) == 0)
236
286
                                                {
237
 
                                                        pSpeed->uReadSpeed = (uReadBlocks - pSpeed->uReadBlocks) * DISK_BLOCK_SIZE / fTimeElapsed;
238
 
                                                        pSpeed->uWriteSpeed = (uWriteBlocks - pSpeed->uWriteBlocks) * DISK_BLOCK_SIZE / fTimeElapsed;
 
287
                                                        if (pSpeed->bInitialized)  // la 1ere iteration on ne peut pas calculer le debit.
 
288
                                                        {
 
289
                                                                pSpeed->uReadSpeed = (uReadBlocks - pSpeed->uReadBlocks) * DISK_BLOCK_SIZE / fTimeElapsed;
 
290
                                                                pSpeed->uWriteSpeed = (uWriteBlocks - pSpeed->uWriteBlocks) * DISK_BLOCK_SIZE / fTimeElapsed;
 
291
                                                        }
 
292
                                                        else
 
293
                                                                pSpeed->bInitialized = TRUE;
 
294
                                                        
 
295
                                                        pSpeed->uReadBlocks = uReadBlocks;
 
296
                                                        pSpeed->uWriteBlocks = uWriteBlocks;
 
297
                                                         //~ cd_warning("%s %u %u", pSpeed->cName, pSpeed->uReadSpeed, pSpeed->uWriteSpeed);
 
298
                                                        
 
299
                                                        pSpeed->bAcquisitionOK = TRUE;
 
300
                                                        bFound = TRUE;
 
301
                                                        break;
239
302
                                                }
240
 
                                                else
241
 
                                                        pSpeed->bInitialized = TRUE;
242
 
                                        
243
 
                                                pSpeed->uReadBlocks = uReadBlocks;
244
 
                                                pSpeed->uWriteBlocks = uWriteBlocks;
245
 
                                                 //~ cd_warning("%s %u %u", pSpeed->cName, pSpeed->uReadSpeed, pSpeed->uWriteSpeed);
246
 
                                
247
 
                                                pSpeed->bAcquisitionOK = TRUE;
248
 
                                                bFound = TRUE;
249
 
                                                break;
250
303
                                        }
251
 
                                }
252
304
 
253
 
                                // if (myConfig.bListAll && !bFound) // need monitor
254
 
                                        /// create new
255
 
                        //~ cd_warning("creation");
256
 
                                        //~ pSpeed = g_new0 (CDDiskSpeedData, 1);
257
 
                                        //~ pSpeed->cName = g_strdup (disk_name);
258
 
                                        //~ myData.lDisks = g_list_append (myData.lDisks, pSpeed);
259
 
                                        // speed_set_data (pDisk, new_reads, new_writes);
260
 
                        //~ } 
 
305
                                        // if (myConfig.bListAll && !bFound) // need monitor
 
306
                                                /// create new
 
307
                                //~ cd_warning("creation");
 
308
                                                //~ pSpeed = g_new0 (CDDiskSpeedData, 1);
 
309
                                                //~ pSpeed->cName = g_strdup (disk_name);
 
310
                                                //~ myData.lDisks = g_list_append (myData.lDisks, pSpeed);
 
311
                                                // speed_set_data (pDisk, new_reads, new_writes);
 
312
                                //~ } 
 
313
                        }
261
314
                }
262
315
        }
263
 
}
 
316
 
 
317
}
 
318
 
 
319
#if (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 28)
 
320
void _reset_parts_list (double *pSize)
 
321
{
 
322
        g_free (pSize);
 
323
}
 
324
#endif
 
325
 
 
326
void cd_disks_reset_parts_list (CairoDockModuleInstance *myApplet)
 
327
{
 
328
        if (myConfig.iNumberParts > 0)
 
329
        {
 
330
#if (GLIB_MAJOR_VERSION > 2) || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 28)
 
331
                g_list_free_full (myData.lParts, g_free);
 
332
#else
 
333
                g_list_foreach (myData.lParts, (GFunc) _reset_parts_list, NULL);
 
334
#endif
 
335
                myData.lParts = NULL;
 
336
        }
 
337
}
 
338
 
 
339
 
 
340
void _reset_one_disk (CDDiskSpeedData *pSpeed)
 
341
{
 
342
        if (pSpeed->cName != NULL)
 
343
                g_free (pSpeed->cName);
 
344
        g_free (pSpeed);
 
345
}
 
346
 
 
347
void cd_disks_reset_disks_list (CairoDockModuleInstance *myApplet)
 
348
{
 
349
        if (myData.iNumberDisks > 0)
 
350
        {
 
351
                g_list_foreach (myData.lDisks, (GFunc) _reset_one_disk, NULL);
 
352
                g_list_free (myData.lDisks);
 
353
                myData.lDisks = NULL;
 
354
        }
 
355
}
 
356
 
264
357