Commit 777652e8 authored by Romain Bignon's avatar Romain Bignon

generate graph

parent 0b559dff
all:
gcc -o irssleep -Wall -Wextra -ggdb main.c
gcc -o irssleep -Wall -Wextra -ggdb main.c `imlib2-config --cflags` `imlib2-config --libs`
#include <Imlib2.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
......@@ -6,7 +7,7 @@
#include <stdlib.h>
#include <getopt.h>
#define IDLE_TIME 30*60
#define IDLE_TIME 60*60
static int is_in_list(const char *elem, const char **list)
{
......@@ -382,11 +383,141 @@ void print_progress(float x, float tot)
fflush(stdout);
}
void print_date(const char *text, time_t t)
{
struct tm lt;
char s[300];
localtime_r(&t, &lt);
strftime(s, sizeof(s), "%D %H:%M:%S", &lt);
if (text)
printf("%s: %s\n", text, s);
else
printf("%s\n", s);
}
time_t reset_day(time_t ts)
{
struct tm lt;
localtime_r(&ts, &lt);
lt.tm_sec = 0;
lt.tm_min = 0;
lt.tm_hour = 0;
return mktime(&lt);
}
int create_image(struct data* data, const char *filename)
{
const char *ext;
struct period *p;
time_t first, last;
int days, i;
first = reset_day(data->periods->start);
for (p = data->periods; p; p = p->next)
last = p->end;
last = reset_day(last);
days = difftime(last, first)/(3600*24) + 1;
print_date("Last", last);
print_date("First", first);
const int xgraph = 50;
const int ygraph = 0;
const int hgraph = 24*6;
const int wgraph = days;
Imlib_Image image = imlib_create_image(xgraph + wgraph,
ygraph + hgraph + 50);
imlib_context_set_image(image);
imlib_add_path_to_font_path("/usr/share/fonts/dejavu");
Imlib_Font font = imlib_load_font("DejaVuSans/10");
imlib_context_set_font(font);
for (i = 0; i < 24; ++i)
{
if (!(i % 2))
{
char text[6];
snprintf(text, sizeof text, "%02d:00", i);
imlib_text_draw(5, ygraph + (i*6) - 3, text);
}
}
for (i = 0; i < days; ++i)
{
struct tm lt;
/* TODO Does not work two times in a year because of summer time. */
time_t day_ts = first + (i*24*3600);
localtime_r(&day_ts, &lt);
if (lt.tm_mday == 1 && (lt.tm_mon % 2))
{
char text[10];
strftime(text, sizeof text, "%b %g", &lt);
imlib_context_set_color(255, 255, 255, 255);
imlib_context_set_image(image);
imlib_text_draw(xgraph + i + 5, ygraph + hgraph, text);
imlib_image_fill_rectangle(xgraph + i, ygraph + hgraph, 1, 15);
}
if (lt.tm_wday == 0 || lt.tm_wday == 6)
{
imlib_context_set_color(50, 50, 50, 255);
imlib_context_set_image(image);
imlib_image_fill_rectangle(xgraph + i, 0, 1, hgraph);
}
}
imlib_free_font();
for (p = data->periods; p; p = p->next)
{
struct tm lt_start, lt_end;
int first_day, last_day;
int day, nmonth = 0;
localtime_r(&p->start, &lt_start);
localtime_r(&p->end, &lt_end);
first_day = difftime(reset_day(p->start), first)/(3600*24);
last_day = difftime(reset_day(p->end), first)/(3600*24);
for (day = first_day; day <= last_day; ++day)
{
int start_h = 0;
int end_h = 24*6-1;
if (day == first_day)
start_h = lt_start.tm_hour * 6 + lt_start.tm_min/10;
if (day == last_day)
end_h = lt_end.tm_hour * 6 + lt_end.tm_min/10;
imlib_context_set_color(255, 0, 130, 255);
imlib_context_set_image(image);
imlib_image_fill_rectangle(xgraph + day, ygraph + start_h, 1, end_h-start_h);
}
}
ext = strrchr(filename, '.');
if (ext)
imlib_image_set_format(ext + 1);
else
imlib_image_set_format("png");
printf("Saving in %s (%s)\n", filename, ext);
ImlibLoadError err = IMLIB_LOAD_ERROR_UNKNOWN;
imlib_save_image_with_error_return(filename, &err);
imlib_free_image();
return err;
}
int main(int argc, char **argv)
{
static struct option long_options[] =
{
{ "forks", 1, NULL, 'j' },
{ "output", 1, NULL, 'o' },
{ "help", 1, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
......@@ -395,11 +526,15 @@ int main(int argc, char **argv)
int hsock = 0;
fd_set fdset;
struct data data = {0};
char imagefile[100] = "output.png";
const char *nicks[] = {"romain", "Progs", "vaginus", NULL};
while((i = getopt_long(argc, argv, "j:h", long_options, &option_index)) != -1)
while((i = getopt_long(argc, argv, "o:j:h", long_options, &option_index)) != -1)
switch(i)
{
case 'o':
strncpy(imagefile, optarg, sizeof(imagefile));
break;
case 'j':
nprocess = atoi(optarg);
break;
......@@ -487,10 +622,13 @@ int main(int argc, char **argv)
}
}
}
putchar('\n');
/* struct period* p;
for (p = data.periods; p; p = p->next)
printf("From %ld to %ld\n", p->start, p->end);*/
if (!data.periods)
{
fprintf(stderr, "Error: no data!\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
exit(create_image(&data, imagefile));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment