~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to examples/tooltip.c

  • Committer: Package Import Robot
  • Author(s): CI Train Bot
  • Date: 2015-05-12 13:12:55 UTC
  • mto: This revision was merged to the branch mainline in revision 96.
  • Revision ID: package-import@ubuntu.com-20150512131255-y7z12i8n4pbvo70x
Tags: upstream-0.13.0+15.10.20150512
Import upstream version 0.13.0+15.10.20150512

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 * Copyright © 2015 Canonical Ltd.
 
4
 *
 
5
 * This program is free software: you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License version 3 as
 
7
 * published by the Free Software Foundation.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 * Author: Alan Griffiths <alan@octopull.co.uk>
 
18
 */
 
19
 
 
20
#include "eglapp.h"
 
21
#include <mir_toolkit/mir_client_library.h>
 
22
 
 
23
#include <stdio.h>
 
24
#include <unistd.h>
 
25
#include <GLES2/gl2.h>
 
26
 
 
27
///\example tooltip.c
 
28
/// A simple orange client surface with a simple grey tooltip
 
29
 
 
30
static MirPixelFormat select_pixel_format(MirConnection* connection);
 
31
static MirSurface* create_tooltip(MirConnection* const connection, MirSurface* const parent, const MirPixelFormat format);
 
32
 
 
33
typedef struct Color
 
34
{
 
35
    GLfloat r, g, b, a;
 
36
} Color;
 
37
 
 
38
int main(int argc, char *argv[])
 
39
{
 
40
    float const opacity = mir_eglapp_background_opacity;
 
41
    Color const orange = {0.866666667f, 0.282352941f, 0.141414141f, opacity};
 
42
 
 
43
    unsigned int width = 300, height = 200;
 
44
 
 
45
    if (!mir_eglapp_init(argc, argv, &width, &height))
 
46
        return 1;
 
47
 
 
48
    glClearColor(orange.r, orange.g, orange.b, orange.a);
 
49
    glClear(GL_COLOR_BUFFER_BIT);
 
50
    mir_eglapp_swap_buffers();
 
51
 
 
52
    MirConnection* const connection = mir_eglapp_native_connection();
 
53
    MirSurface* const parent = mir_eglapp_native_surface();
 
54
 
 
55
    MirSurfaceSpec* const spec = mir_connection_create_spec_for_changes(connection);
 
56
    mir_surface_spec_set_name(spec, "tooltip example");
 
57
    mir_surface_spec_set_min_width(spec, width/2);
 
58
    mir_surface_spec_set_max_width(spec, width*2);
 
59
    mir_surface_spec_set_min_height(spec, height/2);
 
60
    mir_surface_spec_set_max_height(spec, height*2);
 
61
    mir_surface_apply_spec(parent, spec);
 
62
    mir_surface_spec_release(spec);
 
63
 
 
64
    MirSurface* tooltip = create_tooltip(connection, parent, select_pixel_format(connection));
 
65
    while (mir_eglapp_running())
 
66
    {
 
67
    }
 
68
 
 
69
    mir_surface_release_sync(tooltip);
 
70
    mir_eglapp_shutdown();
 
71
 
 
72
    return 0;
 
73
}
 
74
 
 
75
static MirPixelFormat select_pixel_format(MirConnection* connection)
 
76
{
 
77
    unsigned int format[mir_pixel_formats];
 
78
    unsigned int nformats;
 
79
 
 
80
    mir_connection_get_available_surface_formats(connection,
 
81
        format, mir_pixel_formats, &nformats);
 
82
 
 
83
    MirPixelFormat pixel_format = format[0];
 
84
    for (unsigned int f = 0; f < nformats; f++)
 
85
    {
 
86
        const bool opaque = (format[f] == mir_pixel_format_xbgr_8888 ||
 
87
                            format[f] == mir_pixel_format_xrgb_8888 ||
 
88
                            format[f] == mir_pixel_format_bgr_888);
 
89
 
 
90
        if (opaque)
 
91
        {
 
92
            pixel_format = format[f];
 
93
            break;
 
94
        }
 
95
    }
 
96
 
 
97
    return pixel_format;
 
98
}
 
99
 
 
100
static MirSurface* create_tooltip(MirConnection* const connection, MirSurface* const parent, const MirPixelFormat format)
 
101
{
 
102
    MirRectangle zone = { 0, 0, 10, 10 };
 
103
    int const width = 50;
 
104
    int const height = 20;
 
105
    MirSurfaceSpec* const spec = mir_connection_create_spec_for_tooltip(
 
106
        connection, width, height, format, parent, &zone);
 
107
 
 
108
    mir_surface_spec_set_buffer_usage(spec, mir_buffer_usage_software);
 
109
    mir_surface_spec_set_name(spec, "tooltip");
 
110
    mir_surface_spec_set_min_width(spec, width);
 
111
    mir_surface_spec_set_max_width(spec, width);
 
112
    mir_surface_spec_set_min_height(spec, height);
 
113
    mir_surface_spec_set_max_height(spec, height);
 
114
 
 
115
    MirSurface* tooltip = mir_surface_create_sync(spec);
 
116
    mir_surface_spec_release(spec);
 
117
 
 
118
    MirBufferStream* const bs = mir_surface_get_buffer_stream(tooltip);
 
119
    MirGraphicsRegion buffer;
 
120
    mir_buffer_stream_get_graphics_region(bs, &buffer);
 
121
 
 
122
    for (int y = 0; y != buffer.height; ++y)
 
123
    {
 
124
        for (int n = 0; n != buffer.width; ++n)
 
125
            switch (format)
 
126
            {
 
127
            case mir_pixel_format_abgr_8888:
 
128
            case mir_pixel_format_argb_8888:
 
129
            {
 
130
                uint32_t* const pixel = (uint32_t*) (buffer.vaddr + y * buffer.stride);
 
131
                pixel[n] = 0xff7f7f7f;
 
132
                break;
 
133
            }
 
134
            case mir_pixel_format_xbgr_8888:
 
135
            case mir_pixel_format_xrgb_8888:
 
136
            {
 
137
                uint32_t* const pixel = (uint32_t*) (buffer.vaddr + y * buffer.stride);
 
138
                pixel[n] = 0x007f7f7f;
 
139
                break;
 
140
            }
 
141
            case mir_pixel_format_bgr_888:
 
142
            {
 
143
                uint8_t* const subpixel = (uint8_t*) (buffer.vaddr + y * buffer.stride);
 
144
                subpixel[3 * n + 0] = 0x7f;
 
145
                subpixel[3 * n + 1] = 0x7f;
 
146
                subpixel[3 * n + 2] = 0x7f;
 
147
                break;
 
148
            }
 
149
            default:
 
150
                break;
 
151
            }
 
152
    }
 
153
 
 
154
    mir_buffer_stream_swap_buffers_sync(bs);
 
155
 
 
156
    return tooltip;
 
157
}