Esempio n. 1
0
static void
__scan_battery_apm(void)
{
    FILE                *procapm;
    static char         *sremaining = NULL, *stotal = NULL;
    static unsigned int  last_time = 0;
    static int           percentage = 0;
    const  char         *ac_status[] = { "Battery",
                                         "AC Power",
                                         "Charging" };
    int                  ac_bat;
    char                 apm_bios_ver[16], apm_drv_ver[16];
    char                 trash[10];
    
    if ((procapm = fopen("/proc/apm", "r"))) {
        int old_percentage = percentage;
        
        (void)fscanf(procapm, "%s %s %s 0x%x %s %s %d%%",
               apm_drv_ver, apm_bios_ver, trash,
               &ac_bat, trash, trash, &percentage);
        fclose(procapm);
        
        if (last_time == 0) {
            last_time = time(NULL);
            sremaining = stotal = NULL;
        }

        if (old_percentage - percentage > 0) {
            if (sremaining && stotal) {
                g_free(sremaining);
                g_free(stotal);
            }
                        
            int secs_remaining = (time(NULL) - last_time) * percentage /
                                 (old_percentage - percentage);
            sremaining = seconds_to_string(secs_remaining);
            stotal = seconds_to_string((secs_remaining * 100) / percentage);
            
            last_time = time(NULL);
        }
    } else {
        return;
    }

    if (stotal && sremaining) {
        battery_list = h_strdup_cprintf(_("\n[Battery (APM)]\n"
                                       "Charge=%d%%\n"
                                       "Remaining Charge=%s of %s\n"
                                       "Using=%s\n"
                                       "APM driver version=%s\n"
                                       "APM BIOS version=%s\n"),
                                       battery_list,
                                       percentage,
                                       sremaining, stotal,
                                       ac_status[ac_bat],
                                       apm_drv_ver, apm_bios_ver);
    } else {
        battery_list = h_strdup_cprintf(_("\n[Battery (APM)]\n"
                                       "Charge=%d%%\n"
                                       "Using=%s\n"
                                       "APM driver version=%s\n"
                                       "APM BIOS version=%s\n"),
                                       battery_list,
                                       percentage,
                                       ac_status[ac_bat],
                                       apm_drv_ver, apm_bios_ver);
    
    }
}
static MetaData *get_metadata(gchar * filename)
{
    GError *error;
    gint exit_status;
    gchar *out = NULL;
    gchar *err = NULL;
    gchar *av[255];
    gint ac = 0, i;
    gchar **output;
    gchar *ptr;
    gchar *lower;
    gfloat f;

    MetaData *ret;
    ret = g_new0(MetaData, 1);

    av[ac++] = g_strdup_printf("mplayer");
    av[ac++] = g_strdup_printf("-vo");
    av[ac++] = g_strdup_printf("null");
    av[ac++] = g_strdup_printf("-ao");
    av[ac++] = g_strdup_printf("null");
    av[ac++] = g_strdup_printf("-nomsgcolor");
    av[ac++] = g_strdup_printf("-nomsgmodule");
    av[ac++] = g_strdup_printf("-frames");
    av[ac++] = g_strdup_printf("0");
    av[ac++] = g_strdup_printf("-noidx");
    av[ac++] = g_strdup_printf("-identify");
    av[ac++] = g_strdup_printf("-nocache");
    av[ac++] = g_strdup_printf("-noidle");

    av[ac++] = g_strdup_printf("%s", filename);
    av[ac] = NULL;

    error = NULL;

    g_spawn_sync(NULL, av, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &out, &err, &exit_status, &error);

    for (i = 0; i < ac; i++) {
        g_free(av[i]);
    }

    if (error != NULL) {
        printf("Error when running: %s\n", error->message);
        g_error_free(error);
        error = NULL;
        if (out != NULL)
            g_free(out);
        if (err != NULL)
            g_free(err);
        if (ret != NULL)
            g_free(ret);
        return NULL;
    }
    output = g_strsplit(out, "\n", 0);
    ac = 0;
    while (output[ac] != NULL) {

        lower = g_ascii_strdown(output[ac], -1);
        if (strstr(output[ac], "ID_LENGTH") != NULL) {
            ptr = strstr(output[ac], "=") + 1;
            sscanf(ptr, "%f", &f);
            ret->length = seconds_to_string(f);
            ret->length_value = f;
        }

        if (g_ascii_strncasecmp(output[ac], "ID_CLIP_INFO_NAME", strlen("ID_CLIP_INFO_NAME")) == 0) {
            if (strstr(lower, "=title") != NULL || strstr(lower, "=name") != NULL) {
                ptr = strstr(output[ac + 1], "=") + 1;
                if (ptr)
                    ret->title = g_strstrip(g_locale_to_utf8(ptr, -1, NULL, NULL, NULL));
                else
                    ret->title = NULL;

                if (ret->title == NULL) {
                    ret->title = g_strdup(ptr);
                    strip_unicode(ret->title, strlen(ret->title));
                }
            }
            if (strstr(lower, "=artist") != NULL || strstr(lower, "=author") != NULL) {
                ptr = strstr(output[ac + 1], "=") + 1;
                ret->artist = g_strstrip(g_locale_to_utf8(ptr, -1, NULL, NULL, NULL));
                if (ret->artist == NULL) {
                    ret->artist = g_strdup(ptr);
                    strip_unicode(ret->artist, strlen(ret->artist));
                }
            }
            if (strstr(lower, "=album") != NULL) {
                ptr = strstr(output[ac + 1], "=") + 1;
                ret->album = g_strstrip(g_locale_to_utf8(ptr, -1, NULL, NULL, NULL));
                if (ret->album == NULL) {
                    ret->album = g_strdup(ptr);
                    strip_unicode(ret->album, strlen(ret->album));
                }
            }
        }

        if (strstr(output[ac], "ID_AUDIO_CODEC") != NULL) {
            ptr = strstr(output[ac], "=") + 1;
            ret->audio_codec = g_strdup(ptr);
            ret->audio_present = TRUE;
        }

        if (strstr(output[ac], "ID_VIDEO_CODEC") != NULL) {
            ptr = strstr(output[ac], "=") + 1;
            ret->video_codec = g_strdup(ptr);
            ret->video_present = TRUE;
        }

        if (strstr(output[ac], "ID_VIDEO_WIDTH") != NULL) {
            ptr = strstr(output[ac], "=") + 1;
            ret->width = (gint) g_strtod(ptr, NULL);
        }

        if (strstr(output[ac], "ID_VIDEO_HEIGHT") != NULL) {
            ptr = strstr(output[ac], "=") + 1;
            ret->height = (gint) g_strtod(ptr, NULL);
        }

        if (strstr(output[ac], "ID_AUDIO_BITRATE") != NULL) {
            ptr = strstr(output[ac], "=") + 1;
            ret->audio_bitrate = g_strdup(ptr);
        }

        if (strstr(output[ac], "ID_VIDEO_BITRATE") != NULL) {
            ptr = strstr(output[ac], "=") + 1;
            ret->video_bitrate = g_strdup(ptr);
        }

        if (strstr(output[ac], "ID_VIDEO_FPS") != NULL) {
            ptr = strstr(output[ac], "=") + 1;
            ret->video_fps = g_strdup(ptr);
        }

        if (strstr(output[ac], "ID_AUDIO_NCH") != NULL) {
            ptr = strstr(output[ac], "=") + 1;
            ret->audio_nch = g_strdup(ptr);
        }

        if (strstr(output[ac], "ID_DEMUXER") != NULL) {
            ptr = strstr(output[ac], "=") + 1;
            ret->demuxer = g_strdup(ptr);
        }
        g_free(lower);
        ac++;
    }

    g_strfreev(output);
    if (out)
        g_free(out);
    if (err)
        g_free(err);

    return ret;

}