summaryrefslogtreecommitdiffstats
path: root/abs/mv-core/ghosd/ghosd-0.0.1/examples/animation.c
blob: 8c1a2c50fc1b6859855fa59a351c6980c2860126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* ghosd -- OSD with fake transparency, cairo, and pango.
 * Copyright (C) 2006 Evan Martin <martine@danga.com>
 */

#include <stdio.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <time.h>

#include <cairo/cairo.h>

#include <ghosd/ghosd.h>

typedef struct {
  cairo_surface_t* foot;
  float alpha;
} RenderData;

static void
round_rect(cairo_t *cr, int x, int y, int w, int h, int r) {
  cairo_move_to(cr, x+r, y);
  cairo_line_to(cr, x+w-r, y); /* top edge */
  cairo_curve_to(cr, x+w, y, x+w, y, x+w, y+r);
  cairo_line_to(cr, x+w, y+h-r); /* right edge */
  cairo_curve_to(cr, x+w, y+h, x+w, y+h, x+w-r, y+h);
  cairo_line_to(cr, x+r, y+h); /* bottom edge */
  cairo_curve_to(cr, x, y+h, x, y+h, x, y+h-r);
  cairo_line_to(cr, x, y+r); /* left edge */
  cairo_curve_to(cr, x, y, x, y, x+r, y);
  cairo_close_path(cr);
}

#define RADIUS 20

static void
render(Ghosd *ghosd, cairo_t *cr, void* data) {
  RenderData *rdata = data;

  cairo_set_source_rgba(cr, rdata->alpha, 0, 0, 0.5);
  cairo_new_path(cr);
  round_rect(cr, 10, 10, 180, 230, RADIUS);
  cairo_fill(cr);

  cairo_set_source_rgba(cr, 1, 1, 1, 1.0);
  cairo_new_path(cr);
  round_rect(cr, 20, 20, 160, 210, RADIUS);
  cairo_stroke(cr);

  cairo_save(cr);
  cairo_set_source_rgba(cr, 1, 1, 1, 1.0);
  cairo_set_source_surface(cr, rdata->foot, 30, 30);
  cairo_paint(cr);
  cairo_restore(cr);
}

int main(int argc, char* argv[]) {
  Ghosd *ghosd;
  RenderData data = {0};

  const char *image = "/usr/share/pixmaps/gnome-background-image.png";
  data.foot = cairo_image_surface_create_from_png(image);
  data.alpha = 0.5;

  ghosd = ghosd_new();
  ghosd_set_position(ghosd, 50, 50, 200, 240);
  ghosd_set_render(ghosd, render, &data);
  ghosd_render(ghosd);
  ghosd_show(ghosd);

  ghosd_main_iterations(ghosd);

  const int STEP = 50;
  float dalpha = 0.05;

  struct timeval tv_nextupdate;

  for (;;) {
    gettimeofday(&tv_nextupdate, NULL);
    tv_nextupdate.tv_usec += STEP*1000;

    ghosd_main_until(ghosd, &tv_nextupdate);

    data.alpha += dalpha;
    if (data.alpha >= 1.0) {
      data.alpha = 1.0;
      dalpha = -dalpha;
    } else if (data.alpha <= 0.0) {
      data.alpha = 0.0;
      dalpha = -dalpha;
    }
    ghosd_render(ghosd);
  }

  return 0;
}

/* vim: set ts=2 sw=2 et cino=(0 : */