File: animation.c

package info (click to toggle)
libaosd 0.2.7-1.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch, trixie
  • size: 512 kB
  • sloc: sh: 2,859; ansic: 1,731; makefile: 82
file content (97 lines) | stat: -rw-r--r-- 2,153 bytes parent folder | download | duplicates (3)
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
/* aosd -- OSD with fake transparency, cairo, and pango.
 *
 * Copyright (C) 2006 Evan Martin <martine@danga.com>
 */

#include <aosd.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 40

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

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

  cairo_set_source_rgba(cr, 1, 1, 1, 1.0);
  cairo_new_path(cr);
  round_rect(cr, 10, 10, 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, 20, 20);
  cairo_paint(cr);
  cairo_restore(cr);
}

int main(int argc, char* argv[])
{
  Aosd* aosd;
  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;

  aosd = aosd_new();
  aosd_set_transparency(aosd, TRANSPARENCY_COMPOSITE);
  aosd_set_hide_upon_mouse_event(aosd, True);
  aosd_set_geometry(aosd, 50, 50, 180, 230);
  aosd_set_renderer(aosd, render, &data);

  aosd_show(aosd);

  aosd_loop_once(aosd);

  float dalpha = 0.05;

  do
  {
    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;
    }

    aosd_render(aosd);
    aosd_loop_for(aosd, 100);
  } while (aosd_get_is_shown(aosd));

  cairo_surface_destroy(data.foot);
  aosd_destroy(aosd);

  return 0;
}

/* vim: set ts=2 sw=2 et : */