#include <Ecore.h>
#include <Evas.h>
#define WINDOW_PAD (20)
static unsigned int input_count;
static unsigned int input_idx = 0, input_color_idx = 0, output_color_idx = 0;
static const struct color {
   unsigned char r, g, b;
} colors[] = {
  {255, 0, 0},
  {0, 255, 0},
  {0, 0, 255},
  {255, 128, 0},
  {0, 255, 128},
  {128, 0, 255},
  {255, 255, 0},
  {0, 255, 255},
  {255, 0, 255},
  {255, 0, 128},
  {128, 255, 0},
  {0, 128, 255},
  {128, 128, 0},
  {0, 128, 128},
  {128, 0, 128},
  {128, 0, 0},
  {0, 128, 0},
  {0, 0, 128},
  {255, 128, 0},
  {0, 255, 128},
  {128, 0, 255},
  {64, 64, 0},
  {0, 64, 64},
  {64, 0, 64},
  {128, 128, 0},
  {0, 128, 128},
  {128, 0, 128},
  {255, 0, 128},
  {128, 255, 0},
  {0, 128, 255},
  {128, 64, 0},
  {0, 128, 64},
  {64, 0, 128},
  {128, 0, 64},
  {64, 128, 0},
  {0, 64, 128}
};
#define MAX_COLORS (sizeof(colors) / sizeof(colors[0]))
static void
add_text(const char *text, int x, int y, int w)
{
   evas_object_move(o, x, y);
   evas_object_resize(o, w, WINDOW_PAD);
   evas_object_text_font_set(o, "Sans", 10);
   evas_object_text_text_set(o, text);
}
static void
output_rects_reset(void)
{
   output_color_idx = 0;
}
static void
{
   bx = WINDOW_PAD;
   by = WINDOW_PAD;
#define C(comp) (((int)colors[input_color_idx].comp * 128) / 255)
#undef C
   evas_object_move(o, r->
x + bx, r->
y + by);
   evas_object_resize(o, r->
w, r->
h);
   input_color_idx = (input_color_idx + 1) % MAX_COLORS;
   bx += maxw + WINDOW_PAD;
   evas_object_move(o, r->
x + bx, r->
y + by);
   evas_object_resize(o, r->
w, 1);
   evas_object_move(o, r->
x + bx, r->
y + by);
   evas_object_resize(o, 1, r->
h);
   evas_object_move(o, r->
x + bx, r->
y + by + r->
h);
   evas_object_resize(o, r->
w, 1);
   evas_object_move(o, r->
x + bx + r->
w, r->
y + by);
   evas_object_resize(o, 1, r->
h);
}
static void
{
#define C(comp) (((int)colors[output_color_idx].comp * 128) / 255)
#undef C
   evas_object_move(o, r->
x + maxw + 2 * WINDOW_PAD, r->
y + WINDOW_PAD);
   evas_object_resize(o, r->
w, r->
h);
   output_color_idx = (output_color_idx + 1) % MAX_COLORS;
}
{
   unsigned int out = 0;
   if (input_idx == input_count)
     {
        add_text("Done. Close the window to exit",
                 WINDOW_PAD, winh - WINDOW_PAD, winw - 2 * WINDOW_PAD);
     }
   output_rects_reset();
   r = input_rects[input_idx];
   printf(
"Iteration #%u: %dx%d%+d%+d\n", input_idx, r.
w, r.
h, r.
x, r.
y);
   input_idx++;
   add_input_rect(&r);
     {
        printf(
"\tOutput #%u: %dx%d%+d%+d\n", out, r1->
w, r1->
h, r1->
x, r1->
y);
        add_output_rect(r1);
        out++;
     }
}
static void
usage(const char *progname)
{
   fprintf(stderr,
           "Usage:\n\n"
           "\t%s <rect1> ... <rectN>\n\n"
           "with rectangles being in the format:\n"
           "\tWIDTHxHEIGHT<+->X<+->Y\n"
           "examples:\n"
           "\t100x100+10+10 - width=100, height=100 at x=10, y=10\n"
           "\t150x50+5+6    - width=150, height=50 at x=5, y=6\n",
           progname);
}
int
main(int argc, char *argv[])
{
   Ecore_Evas *ee;
   int i;
   if (argc < 2)
     {
        usage(argv[0]);
        return -2;
     }
   input_count = 0;
   maxw = 0;
   maxh = 0;
   for (i = 1; i < argc; i++)
     {
        char sx, sy;
        if (sscanf(argv[i], "%dx%d%c%d%c%d",
                   &(r->
w), &(r->
h), &sx, &(r->
x), &sy, &(r->
y)) == 6)
          {
             if (sx == 
'-') r->
x *= -1;
              if (sy == 
'-') r->
y *= -1;
              if (maxw < r->x + r->
w) maxw = r->
x + r->
w;
              if (maxh < r->y + r->
h) maxh = r->
y + r->
h;
              input_count++;
          }
        else
          fprintf(stderr, "ERROR: invalid rectangle ignored: %s\n", argv[i]);
     }
   if (input_count == 0)
     {
        fputs("ERROR: Could not find any valid rectangle. Exit!\n", stderr);
        usage(argv[0]);
        free(input_rects);
        return -3;
     }
   if ((maxw == 0) || (maxh == 0))
     {
        fputs("ERROR: All rectangles with size 0x0. Exit!\n", stderr);
        usage(argv[0]);
        free(input_rects);
        return -3;
     }
   winw = 2 * maxw + 3 * WINDOW_PAD;
   winh = maxh + 2 * WINDOW_PAD;
   if (!ee)
     {
        fputs("ERROR: Could not create window. Check ecore-evas install.\n",
              stderr);
        goto end;
     }
   evas_object_resize(o, winw, winh);
   add_text("Input", WINDOW_PAD, 0, maxw);
   evas_object_move(o, WINDOW_PAD, WINDOW_PAD);
   evas_object_resize(o, maxw, maxh);
   add_text("Output", maxw + 2 * WINDOW_PAD, 0, maxw);
   evas_object_move(o, maxw + 2 * WINDOW_PAD, WINDOW_PAD);
   evas_object_resize(o, maxw, maxh);
 end:
   free(input_rects);
   return 0;
}