~ubuntu-branches/ubuntu/maverick/libaosd/maverick

« back to all changes in this revision

Viewing changes to examples/animation/animation.c

  • Committer: Bazaar Package Importer
  • Author(s): William Pitcock
  • Date: 2007-11-30 07:42:21 UTC
  • Revision ID: james.westby@ubuntu.com-20071130074221-8yt6spn1ns6rfw7e
Tags: upstream-0.1.3
ImportĀ upstreamĀ versionĀ 0.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* aosd -- OSD with fake transparency, cairo, and pango.
 
2
 *
 
3
 * Copyright (C) 2006 Evan Martin <martine@danga.com>
 
4
 */
 
5
 
 
6
#include <stdio.h>
 
7
#include <sys/time.h>
 
8
#include <sys/poll.h>
 
9
#include <time.h>
 
10
 
 
11
#include <cairo/cairo.h>
 
12
 
 
13
#include <libaosd/aosd.h>
 
14
 
 
15
Bool clicked = False;
 
16
 
 
17
typedef struct {
 
18
  cairo_surface_t* foot;
 
19
  float alpha;
 
20
} RenderData;
 
21
 
 
22
static void
 
23
round_rect(cairo_t* cr, int x, int y, int w, int h, int r)
 
24
{
 
25
  cairo_move_to(cr, x+r, y);
 
26
  cairo_line_to(cr, x+w-r, y); /* top edge */
 
27
  cairo_curve_to(cr, x+w, y, x+w, y, x+w, y+r);
 
28
  cairo_line_to(cr, x+w, y+h-r); /* right edge */
 
29
  cairo_curve_to(cr, x+w, y+h, x+w, y+h, x+w-r, y+h);
 
30
  cairo_line_to(cr, x+r, y+h); /* bottom edge */
 
31
  cairo_curve_to(cr, x, y+h, x, y+h, x, y+h-r);
 
32
  cairo_line_to(cr, x, y+r); /* left edge */
 
33
  cairo_curve_to(cr, x, y, x, y, x+r, y);
 
34
  cairo_close_path(cr);
 
35
}
 
36
 
 
37
#define RADIUS 40
 
38
 
 
39
static void
 
40
render(Aosd* aosd, cairo_t* cr, void* data)
 
41
{
 
42
  RenderData* rdata = data;
 
43
 
 
44
  cairo_set_source_rgba(cr, rdata->alpha, 0, 0, 0.7);
 
45
  cairo_new_path(cr);
 
46
  round_rect(cr, 0, 0, 180, 230, RADIUS);
 
47
  cairo_fill(cr);
 
48
 
 
49
  cairo_set_source_rgba(cr, 1, 1, 1, 1.0);
 
50
  cairo_new_path(cr);
 
51
  round_rect(cr, 10, 10, 160, 210, RADIUS);
 
52
  cairo_stroke(cr);
 
53
 
 
54
  cairo_save(cr);
 
55
  cairo_set_source_rgba(cr, 1, 1, 1, 1.0);
 
56
  cairo_set_source_surface(cr, rdata->foot, 20, 20);
 
57
  cairo_paint(cr);
 
58
  cairo_restore(cr);
 
59
}
 
60
 
 
61
static void
 
62
mouse_callback(Aosd* aosd, AosdMouseEvent* event, void* user_data)
 
63
{
 
64
  clicked = True;
 
65
}
 
66
 
 
67
int main(int argc, char* argv[])
 
68
{
 
69
  Aosd* aosd;
 
70
  RenderData data = {0};
 
71
 
 
72
  const char* image = "/usr/share/pixmaps/gnome-background-image.png";
 
73
  data.foot = cairo_image_surface_create_from_png(image);
 
74
  data.alpha = 0.5;
 
75
 
 
76
  aosd = aosd_new();
 
77
  aosd_set_transparency(aosd, TRANSPARENCY_COMPOSITE);
 
78
  aosd_set_mouse_event_cb(aosd, mouse_callback, NULL);
 
79
  aosd_set_geometry(aosd, 50, 50, 180, 230);
 
80
  aosd_set_renderer(aosd, render, &data, NULL);
 
81
 
 
82
  aosd_show(aosd);
 
83
 
 
84
  aosd_main_iterations(aosd);
 
85
 
 
86
  const int STEP = 100;
 
87
  float dalpha = 0.05;
 
88
 
 
89
  struct timeval tv_nextupdate;
 
90
 
 
91
  do
 
92
  {
 
93
    gettimeofday(&tv_nextupdate, NULL);
 
94
    tv_nextupdate.tv_usec += STEP * 1000;
 
95
 
 
96
    aosd_main_until(aosd, &tv_nextupdate);
 
97
 
 
98
    data.alpha += dalpha;
 
99
    if (data.alpha >= 1.0)
 
100
    {
 
101
      data.alpha = 1.0;
 
102
      dalpha = -dalpha;
 
103
    }
 
104
    else if (data.alpha <= 0.0)
 
105
    {
 
106
      data.alpha = 0.0;
 
107
      dalpha = -dalpha;
 
108
    }
 
109
    aosd_render(aosd);
 
110
  } while (!clicked);
 
111
 
 
112
  cairo_surface_destroy(data.foot);
 
113
  aosd_destroy(aosd);
 
114
 
 
115
  return 0;
 
116
}
 
117
 
 
118
/* vim: set ts=2 sw=2 et : */