Unsubmodule compositor

This commit is contained in:
Kevin Lange 2013-02-09 20:47:57 -08:00
commit b2c3d4da0e
8 changed files with 2067 additions and 1 deletions

@ -1 +0,0 @@
Subproject commit 1af6e4b81e6f98fdbf40aab227965e147426ca16

62
toaru-compositor/Makefile Normal file
View File

@ -0,0 +1,62 @@
CC = i686-pc-toaru-gcc
CPP = i686-pc-toaru-g++
CFLAGS = -std=c99 -U__STRICT_ANSI__ -O3 -m32 -Wa,--32
CPPFLAGS = -O3 -m32 -Wa,--32
ifeq ($(DEBUG_MODE),1)
STRIP = true
EXTRAFLAGS = -g
else
STRIP = i686-pc-toaru-strip
EXTRAFLAGS = -s
endif
EXECUTABLES = $(patsubst %.c,../hdd/bin/%,$(wildcard *.c))
BEG = ../util/mk-beg
END = ../util/mk-end
INFO = ../util/mk-info
ERRORS = 2>>/tmp/.`whoami`-build-errors || ../util/mk-error
ERRORSS = >>/tmp/.`whoami`-build-errors || ../util/mk-error
BEGRM = ../util/mk-beg-rm
ENDRM = ../util/mk-end-rm
FREETYPE_INC = -I ${TOOLCHAIN}/include/freetype2/
FREETYPE_LIB = ${TOOLCHAIN}/lib/libfreetype.a
LIBPNG = ${TOOLCHAIN}/lib/libpng.a
LIBM = ${TOOLCHAIN}/lib/libm.a
LIBZ = ${TOOLCHAIN}/lib/libz.a
F_INCLUDES = ${FREETYPE_INC}
F_LIBRARIES = ${FREETYPE_LIB} ${LIBPNG} ${LIBM} ${LIBZ}
CAIRO_INC = -I ${TOOLCHAIN}/include/cairo/
CAIRO_LIB = ${TOOLCHAIN}/lib/libcairo.a
PIXMAN_INC = -I ${TOOLCHAIN}/include/pixman-1/
PIXMAN_LIB = ${TOOLCHAIN}/lib/libpixman-1.a
EXTRA_LIB_INCLUDES = ${F_INCLUDES} ${CAIRO_INC} ${PIXMAN_INC}
EXTRA_LIB_LIBRARIES = ${F_LIBRARIES} ${CAIRO_LIB} ${PIXMAN_LIB}
LOCAL_LIBS = $(patsubst %.c,%.o,$(wildcard ../userspace/lib/*.c))
LOCAL_INC = -I ../userspace/
TARGETDIR = ../hdd/bin/
EXTRA_LIB_APPS = compositor2 cairo-demo pixman-demo make-it-snow ttk-demo
EXTRA_LIB_TARGETS = $(EXTRA_LIB_APPS:%=$(TARGETDIR)%)
.PHONY: all clean
all: ${EXECUTABLES}
clean:
@${BEGRM} "RM" "Cleaning userspace full-toolchain applications."
@-rm -f ${EXECUTABLES}
@${ENDRM} "RM" "Cleaned userspace full-toolchain applications."
${EXTRA_LIB_TARGETS}: $(TARGETDIR)% : %.c
@${BEG} "CC" "$< [w/extra libs]"
@${CC} -flto ${CFLAGS} ${EXTRAFLAGS} ${LOCAL_INC} ${EXTRA_LIB_INCLUDES} -o $@ $< ${LOCAL_LIBS} ${EXTRA_LIB_LIBRARIES} ${ERRORS}
@${STRIP} $@
@${END} "CC" "$< [w/extra libs]"

View File

@ -0,0 +1,17 @@
# とある Compositor (Cairo version)
This is a window compositor for [とあるOS](http://github.com/klange/osdev) using Cairo.
The normal compositor that ships with とあるOS uses a built-in alpha blitting mechanism. This one uses Cairo's rendering methods to draw windows, which is faster for alpha-enabled windows than the standard compositor, but slightly slower for non-alpha windows (as they are rendered as alpha-enabled regardless).
## Dependencies
Obviously, this needs Cairo. You may need to tweak some config.h options to get Cairo to build correctly.
Eventually, Cairo will be included in the standard toolchain.
## Installation
As with other external-library applications, clone this into your `osdev` repository, build its dependencies (Cairo, Pixman, etc.), run `make` and then `make clean-disk && make` in `osdev`.
You'll also need to change `init` so it runs `compositor2` instead of `compositor`.

View File

@ -0,0 +1,77 @@
#include <math.h>
#include <stdio.h>
#include <cairo.h>
#include "lib/window.h"
#include "lib/graphics.h"
window_t * window;
gfx_context_t * ctx;
void render() {
draw_fill(ctx, rgba(0,0,0,127));
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, window->width);
cairo_surface_t * surface = cairo_image_surface_create_for_data(ctx->buffer, CAIRO_FORMAT_ARGB32, window->width, window->height, stride);
cairo_t * cr = cairo_create(surface);
cairo_set_line_width (cr, 6);
cairo_rectangle (cr, 12, 12, 232, 70);
cairo_new_sub_path (cr); cairo_arc (cr, 64, 64, 40, 0, 2*M_PI);
cairo_new_sub_path (cr); cairo_arc_negative (cr, 192, 64, 40, 0, -2*M_PI);
cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
cairo_set_source_rgb (cr, 0, 0.7, 0); cairo_fill_preserve (cr);
cairo_set_source_rgb (cr, 0, 0, 0); cairo_stroke (cr);
cairo_translate (cr, 0, 128);
cairo_rectangle (cr, 12, 12, 232, 70);
cairo_new_sub_path (cr); cairo_arc (cr, 64, 64, 40, 0, 2*M_PI);
cairo_new_sub_path (cr); cairo_arc_negative (cr, 192, 64, 40, 0, -2*M_PI);
cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);
cairo_set_source_rgb (cr, 0, 0, 0.9); cairo_fill_preserve (cr);
cairo_set_source_rgb (cr, 0, 0, 0); cairo_stroke (cr);
cairo_surface_flush(surface);
cairo_destroy(cr);
cairo_surface_flush(surface);
cairo_surface_destroy(surface);
}
void resize_callback(window_t * win) {
reinit_graphics_window(ctx, window);
render();
}
int main(int argc, char * argv[]) {
setup_windowing();
int width = 500;
int height = 500;
resize_window_callback = resize_callback;
window = window_create(100,100,500,500);
ctx = init_graphics_window(window);
draw_fill(ctx, rgba(0,0,0,127));
window_enable_alpha(window);
render();
while (1) {
w_keyboard_t * kbd = poll_keyboard();
if (kbd != NULL) {
if (kbd->key == 'q') {
break;
}
free(kbd);
}
}
teardown_windowing();
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,203 @@
/*
* Based on the Wayland/Weston drag and drop demo:
* Copyright © 2010 Kristian Høgsberg
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <math.h>
#include <stdio.h>
#include <cairo.h>
#include "lib/window.h"
#include "lib/graphics.h"
#include "lib/list.h"
window_t * window;
gfx_context_t * ctx;
list_t * snowflakes;
int width, height;
struct snowflake {
int x;
int y;
cairo_surface_t * surface;
};
#define random rand
#define item_width 64
#define item_height 64
int windspeed = 2;
int gravity = 5;
struct snowflake * create_snowflake() {
struct snowflake * item = malloc(sizeof(struct snowflake));
item->x = random() % width;
item->y = random() % height;
const int petal_count = 3 + random() % 5;
const double r1 = 20 + random() % 10;
const double r2 = 5 + random() % 12;
const double u = (10 + random() % 90) / 100.0;
const double v = (random() % 90) / 100.0;
cairo_t *cr;
int i;
double t, dt = 2 * M_PI / (petal_count * 2);
double x1, y1, x2, y2, x3, y3;
item->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, item_width, item_height);
cr = cairo_create(item->surface);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba(cr, 0, 0, 0, 0);
cairo_paint(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_translate(cr, item_width / 2, item_height / 2);
t = random();
cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
for (i = 0; i < petal_count; i++, t += dt * 2) {
x1 = cos(t) * r1;
y1 = sin(t) * r1;
x2 = cos(t + dt) * r2;
y2 = sin(t + dt) * r2;
x3 = cos(t + 2 * dt) * r1;
y3 = sin(t + 2 * dt) * r1;
cairo_curve_to(cr,
x1 - y1 * u, y1 + x1 * u,
x2 + y2 * v, y2 - x2 * v,
x2, y2);
cairo_curve_to(cr,
x2 - y2 * v, y2 + x2 * v,
x3 + y3 * u, y3 - x3 * u,
x3, y3);
}
cairo_close_path(cr);
cairo_set_source_rgba(cr,
0.5 + (random() % 50) / 49.0,
0.5 + (random() % 50) / 49.0,
0.5 + (random() % 50) / 49.0,
0.5 + (random() % 100) / 99.0);
cairo_fill_preserve(cr);
cairo_set_line_width(cr, 1);
cairo_set_source_rgba(cr,
0.5 + (random() % 50) / 49.0,
0.5 + (random() % 50) / 49.0,
0.5 + (random() % 50) / 49.0,
0.5 + (random() % 100) / 99.0);
cairo_stroke(cr);
cairo_destroy(cr);;
return item;
}
void render() {
/* Clear window */
draw_fill(ctx, rgba(0,0,0,0));
/* Set up cairo context */
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, window->width);
cairo_surface_t * surface = cairo_image_surface_create_for_data(ctx->backbuffer, CAIRO_FORMAT_ARGB32, window->width, window->height, stride);
cairo_t * cr = cairo_create(surface);
/* Draw some snowflakes */
foreach(node, snowflakes) {
struct snowflake * item = node->value;
cairo_save(cr);
cairo_set_source_surface(cr, item->surface, item->x, item->y);
cairo_paint(cr);
item->x += windspeed;
item->y += gravity;
if (item->y > height + item_height) {
item->y = -item_height;
}
if (item->x > width + item_width) {
item->x = -item_width;
}
cairo_restore(cr);
}
cairo_surface_flush(surface);
cairo_destroy(cr);
cairo_surface_flush(surface);
cairo_surface_destroy(surface);
flip(ctx);
}
void resize_callback(window_t * win) {
reinit_graphics_window(ctx, window);
render();
}
int main(int argc, char * argv[]) {
setup_windowing();
resize_window_callback = resize_callback;
width = wins_globals->server_width;
height = wins_globals->server_height;
window = window_create(0,0,width,height);
ctx = init_graphics_window_double_buffer(window);
draw_fill(ctx, rgba(0,0,0,0));
flip(ctx);
window_enable_alpha(window);
snowflakes = list_create();
for (int i = 0; i < 100; ++i) {
list_insert(snowflakes, create_snowflake());
}
render();
while (1) {
w_keyboard_t * kbd = poll_keyboard_async();
if (kbd != NULL) {
if (kbd->key == 'q') {
break;
}
free(kbd);
}
render();
}
teardown_windowing();
return 0;
}

View File

@ -0,0 +1,92 @@
#include <stdio.h>
#include <pixman.h>
#include "lib/window.h"
#include "lib/graphics.h"
int main(int argc, char * argv[]) {
setup_windowing();
#define WIDTH 400
#define HEIGHT 400
#define TILE_SIZE 25
window_t * window = window_create(100,100,WIDTH,HEIGHT);
gfx_context_t * ctx = init_graphics_window(window);
window_enable_alpha(window);
draw_fill(ctx, rgba(0,0,0,255));
pixman_image_t *checkerboard;
pixman_image_t *destination;
#define D2F(d) (pixman_double_to_fixed(d))
pixman_transform_t trans = { {
{ D2F (-1.96830), D2F (-1.82250), D2F (512.12250)},
{ D2F (0.00000), D2F (-7.29000), D2F (1458.00000)},
{ D2F (0.00000), D2F (-0.00911), D2F (0.59231)},
}};
int i, j;
checkerboard = pixman_image_create_bits (PIXMAN_a8r8g8b8,
WIDTH, HEIGHT,
NULL, 0);
destination = pixman_image_create_bits (PIXMAN_a8r8g8b8,
WIDTH, HEIGHT,
NULL, 0);
for (i = 0; i < HEIGHT / TILE_SIZE; ++i)
{
for (j = 0; j < WIDTH / TILE_SIZE; ++j)
{
double u = (double)(j + 1) / (WIDTH / TILE_SIZE);
double v = (double)(i + 1) / (HEIGHT / TILE_SIZE);
pixman_color_t black = { 0, 0, 0, 0xffff };
pixman_color_t white = {
v * 0xffff,
u * 0xffff,
(1 - (double)u) * 0xffff,
0xffff };
pixman_color_t *c;
pixman_image_t *fill;
if ((j & 1) != (i & 1))
c = &black;
else
c = &white;
fill = pixman_image_create_solid_fill (c);
pixman_image_composite (PIXMAN_OP_SRC, fill, NULL, checkerboard,
0, 0, 0, 0, j * TILE_SIZE, i * TILE_SIZE,
TILE_SIZE, TILE_SIZE);
}
}
pixman_image_set_transform (checkerboard, &trans);
pixman_image_set_filter (checkerboard, PIXMAN_FILTER_BEST, NULL, 0);
pixman_image_set_repeat (checkerboard, PIXMAN_REPEAT_NONE);
pixman_image_composite (PIXMAN_OP_SRC,
checkerboard, NULL, destination,
0, 0, 0, 0, 0, 0,
WIDTH, HEIGHT);
printf("Going for native draw.\n");
uint32_t * buf = pixman_image_get_data(destination);
memcpy(ctx->buffer,buf,WIDTH*HEIGHT*4);
while (1) {
w_keyboard_t * kbd = poll_keyboard();
if (kbd != NULL) {
if (kbd->key == 'q') {
break;
}
free(kbd);
}
}
teardown_windowing();
return 0;
}

457
toaru-compositor/ttk-demo.c Normal file
View File

@ -0,0 +1,457 @@
/*
*
* Toolkit Demo and Development Application
*
*/
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <cairo.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_CACHE_H
#include "lib/window.h"
#include "lib/graphics.h"
#include "lib/decorations.h"
#include "lib/shmemfonts.h"
/* TTK {{{ */
void cairo_rounded_rectangle(cairo_t * cr, double x, double y, double width, double height, double radius) {
double degrees = M_PI / 180.0;
cairo_new_sub_path(cr);
cairo_arc (cr, x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees);
cairo_arc (cr, x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees);
cairo_arc (cr, x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees);
cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
cairo_close_path(cr);
}
typedef struct ttk_window {
window_t * core_window;
gfx_context_t * core_context;
char * title;
cairo_surface_t * cairo_surface;
uint16_t width; /* internal space */
uint16_t height;
uint16_t off_x; /* decor_left_width */
uint16_t off_y; /* decor_top_height */
} ttk_window_t;
#define TTK_BACKGROUND_DEFAULT 204,204,204
#define TTK_DEFAULT_X 300
#define TTK_DEFAULT_Y 300
list_t * ttk_window_list;
void ttk_redraw_borders(ttk_window_t * window) {
render_decorations(window->core_window, window->core_context, window->title);
}
void _ttk_draw_button(cairo_t * cr, int x, int y, int width, int height) {
cairo_save(cr);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
cairo_rounded_rectangle(cr, 2 + x, 2 + y, width - 4, height - 4, 2.0);
cairo_set_source_rgba(cr, 44.0/255.0, 71.0/255.0, 91.0/255.0, 29.0/255.0);
cairo_set_line_width(cr, 4);
cairo_stroke(cr);
cairo_rounded_rectangle(cr, 2 + x, 2 + y, width - 4, height - 4, 2.0);
cairo_set_source_rgba(cr, 158.0/255.0, 169.0/255.0, 177.0/255.0, 1.0);
cairo_set_line_width(cr, 2);
cairo_stroke(cr);
{
cairo_pattern_t * pat = cairo_pattern_create_linear(2 + x, 2 + y, 2 + x, 2 + y + height - 4);
cairo_pattern_add_color_stop_rgba(pat, 0, 1, 1, 1, 1);
cairo_pattern_add_color_stop_rgba(pat, 1, 241.0/255.0, 241.0/255.0, 244.0/255.0, 1);
cairo_rounded_rectangle(cr, 2 + x, 2 + y, width - 4, height - 4, 2.0);
cairo_set_source(cr, pat);
cairo_fill(cr);
cairo_pattern_destroy(pat);
}
{
cairo_pattern_t * pat = cairo_pattern_create_linear(3 + x, 3 + y, 3 + x, 3 + y + height - 4);
cairo_pattern_add_color_stop_rgba(pat, 0, 252.0/255.0, 252.0/255.0, 254.0/255.0, 1);
cairo_pattern_add_color_stop_rgba(pat, 1, 223.0/255.0, 225.0/255.0, 230.0/255.0, 1);
cairo_rounded_rectangle(cr, 3 + x, 3 + y, width - 5, height - 5, 2.0);
cairo_set_source(cr, pat);
cairo_fill(cr);
cairo_pattern_destroy(pat);
}
{
cairo_surface_t * surface = cairo_get_target(cr);
gfx_context_t fake_context = {
.width = cairo_image_surface_get_width(surface),
.height = cairo_image_surface_get_height(surface),
.depth = 32,
.buffer = NULL,
.backbuffer = cairo_image_surface_get_data(surface)
};
set_font_face(FONT_SANS_SERIF);
set_font_size(13);
char * title = "Regular Button";
int str_width = draw_string_width(title);
draw_string(&fake_context, (width - str_width) / 2 + x, y + (height) / 2 + 4, rgb(49,49,49), title);
}
cairo_restore(cr);
}
void _ttk_draw_button_hover(cairo_t * cr, int x, int y, int width, int height) {
cairo_save(cr);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
cairo_rounded_rectangle(cr, 2 + x, 2 + y, width - 4, height - 4, 2.0);
cairo_set_source_rgba(cr, 44.0/255.0, 71.0/255.0, 91.0/255.0, 29.0/255.0);
cairo_set_line_width(cr, 4);
cairo_stroke(cr);
cairo_rounded_rectangle(cr, 2 + x, 2 + y, width - 4, height - 4, 2.0);
cairo_set_source_rgba(cr, 158.0/255.0, 169.0/255.0, 177.0/255.0, 1.0);
cairo_set_line_width(cr, 2);
cairo_stroke(cr);
{
cairo_pattern_t * pat = cairo_pattern_create_linear(2 + x, 2 + y, 2 + x, 2 + y + height - 4);
cairo_pattern_add_color_stop_rgba(pat, 0, 1, 1, 1, 1);
cairo_pattern_add_color_stop_rgba(pat, 1, 229.0/255.0, 229.0/255.0, 246.0/255.0, 1);
cairo_rounded_rectangle(cr, 2 + x, 2 + y, width - 4, height - 4, 2.0);
cairo_set_source(cr, pat);
cairo_fill(cr);
cairo_pattern_destroy(pat);
}
{
cairo_pattern_t * pat = cairo_pattern_create_linear(3 + x, 3 + y, 3 + x, 3 + y + height - 4);
cairo_pattern_add_color_stop_rgba(pat, 0, 252.0/255.0, 252.0/255.0, 254.0/255.0, 1);
cairo_pattern_add_color_stop_rgba(pat, 1, 212.0/255.0, 223.0/255.0, 251.0/255.0, 1);
cairo_rounded_rectangle(cr, 3 + x, 3 + y, width - 5, height - 5, 2.0);
cairo_set_source(cr, pat);
cairo_fill(cr);
cairo_pattern_destroy(pat);
}
{
cairo_surface_t * surface = cairo_get_target(cr);
gfx_context_t fake_context = {
.width = cairo_image_surface_get_width(surface),
.height = cairo_image_surface_get_height(surface),
.depth = 32,
.buffer = NULL,
.backbuffer = cairo_image_surface_get_data(surface)
};
set_font_face(FONT_SANS_SERIF);
set_font_size(13);
char * title = "Button with Hover Highlight";
int str_width = draw_string_width(title);
draw_string(&fake_context, (width - str_width) / 2 + x, y + (height) / 2 + 4, rgb(49,49,49), title);
}
cairo_restore(cr);
}
void _ttk_draw_button_select(cairo_t * cr, int x, int y, int width, int height) {
cairo_save(cr);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
cairo_rounded_rectangle(cr, 2 + x, 2 + y, width - 4, height - 4, 2.0);
cairo_set_source_rgba(cr, 134.0/255.0, 173.0/255.0, 201.0/255.0, 1.0);
cairo_set_line_width(cr, 2);
cairo_stroke(cr);
cairo_rounded_rectangle(cr, 2 + x, 2 + y, width - 4, height - 4, 2.0);
cairo_set_source_rgba(cr, 202.0/255.0, 211.0/255.0, 232.0/255.0, 1.0);
cairo_fill(cr);
{
cairo_surface_t * surface = cairo_get_target(cr);
gfx_context_t fake_context = {
.width = cairo_image_surface_get_width(surface),
.height = cairo_image_surface_get_height(surface),
.depth = 32,
.buffer = NULL,
.backbuffer = cairo_image_surface_get_data(surface)
};
set_font_face(FONT_SANS_SERIF);
set_font_size(13);
char * title = "Selected Button";
int str_width = draw_string_width(title);
draw_string(&fake_context, (width - str_width) / 2 + x, y + (height) / 2 + 4, rgb(49,49,49), title);
}
cairo_restore(cr);
}
void _ttk_draw_button_disabled(cairo_t * cr, int x, int y, int width, int height) {
cairo_save(cr);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
cairo_rounded_rectangle(cr, 2 + x, 2 + y, width - 4, height - 4, 2.0);
cairo_set_source_rgba(cr, 44.0/255.0, 71.0/255.0, 91.0/255.0, 29.0/255.0);
cairo_set_line_width(cr, 4);
cairo_stroke(cr);
cairo_rounded_rectangle(cr, 2 + x, 2 + y, width - 4, height - 4, 2.0);
cairo_set_source_rgba(cr, 152.0/255.0, 152.0/255.0, 152.0/255.0, 1.0);
cairo_set_line_width(cr, 2);
cairo_stroke(cr);
{
cairo_pattern_t * pat = cairo_pattern_create_linear(2 + x, 2 + y, 2 + x, 2 + y + height - 4);
cairo_pattern_add_color_stop_rgba(pat, 0, 229.0/255.0, 229.0/255.0, 229.0/255.0, 1);
cairo_pattern_add_color_stop_rgba(pat, 1, 178.0/255.0, 178.0/255.0, 178.0/255.0, 1);
cairo_rounded_rectangle(cr, 2 + x, 2 + y, width - 4, height - 4, 2.0);
cairo_set_source(cr, pat);
cairo_fill(cr);
cairo_pattern_destroy(pat);
}
{
cairo_pattern_t * pat = cairo_pattern_create_linear(3 + x, 3 + y, 3 + x, 3 + y + height - 4);
cairo_pattern_add_color_stop_rgba(pat, 0, 210.0/255.0, 210.0/255.0, 210.0/255.0, 1);
cairo_pattern_add_color_stop_rgba(pat, 1, 165.0/255.0, 166.0/255.0, 170.0/255.0, 1);
cairo_rounded_rectangle(cr, 3 + x, 3 + y, width - 5, height - 5, 2.0);
cairo_set_source(cr, pat);
cairo_fill(cr);
cairo_pattern_destroy(pat);
}
{
cairo_surface_t * surface = cairo_get_target(cr);
gfx_context_t fake_context = {
.width = cairo_image_surface_get_width(surface),
.height = cairo_image_surface_get_height(surface),
.depth = 32,
.buffer = NULL,
.backbuffer = cairo_image_surface_get_data(surface)
};
set_font_face(FONT_SANS_SERIF);
set_font_size(13);
char * title = "Disabled Button";
int str_width = draw_string_width(title);
draw_string(&fake_context, (width - str_width) / 2 + x, y + (height) / 2 + 4, rgb(100,100,100), title);
}
cairo_restore(cr);
}
#define TTK_MENU_HEIGHT 24
void _ttk_draw_menu(cairo_t * cr, int x, int y, int width) {
cairo_save(cr);
int height = TTK_MENU_HEIGHT;
cairo_set_source_rgba(cr, 59.0/255.0, 59.0/255.0, 59.0/255.0, 1);
cairo_rectangle(cr, x, y, width, height);
cairo_fill(cr);
{
cairo_surface_t * surface = cairo_get_target(cr);
gfx_context_t fake_context = {
.width = cairo_image_surface_get_width(surface),
.height = cairo_image_surface_get_height(surface),
.depth = 32,
.buffer = NULL,
.backbuffer = cairo_image_surface_get_data(surface)
};
set_font_face(FONT_SANS_SERIF);
set_font_size(13);
draw_string(&fake_context, x + 8, y + height - 6, rgb(248,248,248), "File");
}
cairo_restore(cr);
}
void ttk_window_draw(ttk_window_t * window) {
draw_fill(window->core_context, rgb(TTK_BACKGROUND_DEFAULT));
ttk_redraw_borders(window);
/* TODO actual drawing */
{
/* TODO move these surfaces into the ttk_window_t object */
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, window->core_window->width);
cairo_surface_t * core_surface = cairo_image_surface_create_for_data(window->core_context->backbuffer, CAIRO_FORMAT_ARGB32, window->core_window->width, window->core_window->height, stride);
cairo_t * cr_main = cairo_create(core_surface);
/* TODO move this surface to a ttk_frame_t or something; GUIs man, go look at some Qt or GTK APIs! */
cairo_surface_t * internal_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, window->width, window->height);
cairo_t * cr = cairo_create(internal_surface);
_ttk_draw_menu(cr, 0, 0, window->width);
_ttk_draw_button(cr, 4, TTK_MENU_HEIGHT + 4, window->width - 8, 40);
_ttk_draw_button(cr, 4, TTK_MENU_HEIGHT + 48 + 4, (window->width / 2) - 8, 40);
_ttk_draw_button_hover(cr, 4 + (window->width / 2), TTK_MENU_HEIGHT + 48 + 4, (window->width / 2) - 8, 40);
_ttk_draw_button_select(cr, 4, TTK_MENU_HEIGHT + 2 * 48 + 4, (window->width / 2) - 8, 40);
_ttk_draw_button_disabled(cr, 4 + (window->width / 2), TTK_MENU_HEIGHT + 2 * 48 + 4, (window->width / 2) - 8, 40);
_ttk_draw_button(cr, 4, TTK_MENU_HEIGHT + 3 * 48 + 4, window->width - 8, window->height - (3 * 48) - TTK_MENU_HEIGHT - 8);
/* Paint the window's internal surface onto the backbuffer */
cairo_set_source_surface(cr_main, internal_surface, (double)window->off_x, (double)window->off_y);
cairo_paint(cr_main);
cairo_surface_flush(internal_surface);
cairo_destroy(cr);
cairo_surface_destroy(internal_surface);
/* In theory, we don't actually want to destroy much of any of this; maybe the cairo_t */
cairo_surface_flush(core_surface);
cairo_destroy(cr_main);
cairo_surface_destroy(core_surface);
}
flip(window->core_context);
}
void ttk_resize_callback(window_t * window) {
ttk_window_t * window_ttk = NULL;
foreach(node, ttk_window_list) {
ttk_window_t * tmp = (ttk_window_t *)node->value;
if (window->wid == tmp->core_window->wid) {
window_ttk = tmp;
break;
}
}
if (!window_ttk) {
fprintf(stderr, "[ttk] received window callback for a window not registered with TTK, ignoring.\n");
}
/* Update window size */
window_ttk->width = window->width - decor_width();
window_ttk->height = window->height - decor_height();
/* Reinitialize graphics context */
reinit_graphics_window(window_ttk->core_context, window_ttk->core_window);
ttk_window_draw(window_ttk);
}
void ttk_focus_callback(window_t * window) {
ttk_window_t * window_ttk = NULL;
foreach(node, ttk_window_list) {
ttk_window_t * tmp = (ttk_window_t *)node->value;
if (window->wid == tmp->core_window->wid) {
window_ttk = tmp;
break;
}
}
if (!window_ttk) {
fprintf(stderr, "[ttk] received window callback for a window not registered with TTK, ignoring.\n");
}
ttk_window_draw(window_ttk);
}
void ttk_initialize() {
/* Connect to the windowing server */
/* TODO handle errors */
setup_windowing();
/* Set up TTK callbacks */
resize_window_callback = ttk_resize_callback;
focus_changed_callback = ttk_focus_callback;
/* TODO more callbacks, keyboard, mouse */
/* Initialize the decoration library */
init_decorations();
ttk_window_list = list_create();
}
ttk_window_t * ttk_window_new(char * title, uint16_t width, uint16_t height) {
ttk_window_t * new_win = malloc(sizeof(ttk_window_t));
new_win->title = strdup(title);
new_win->width = width;
new_win->height = height;
new_win->off_x = decor_left_width;
new_win->off_y = decor_top_height;
new_win->core_window = window_create(TTK_DEFAULT_X, TTK_DEFAULT_Y, new_win->width + decor_width(), new_win->height + decor_height());
assert(new_win->core_window && "Oh dear, I've failed to allocate a new window from the server. This is terrible.");
new_win->core_context = init_graphics_window_double_buffer(new_win->core_window);
draw_fill(new_win->core_context, rgb(TTK_BACKGROUND_DEFAULT));
ttk_window_draw(new_win);
list_insert(ttk_window_list, new_win);
}
void ttk_quit() {
list_destroy(ttk_window_list);
list_free(ttk_window_list);
free(ttk_window_list);
teardown_windowing();
}
int ttk_run(ttk_window_t * window) {
while (1) {
char ch = 0;
w_keyboard_t * kbd;
while (kbd = poll_keyboard_async()) {
free(kbd);
}
kbd = poll_keyboard();
ch = kbd->key;
free(kbd);
switch (ch) {
case 'q':
goto done;
default:
break;
}
}
done:
ttk_quit();
return 0;
}
/* }}} end TTK */
int main (int argc, char ** argv) {
ttk_initialize();
ttk_window_t * main_window = ttk_window_new("TTK Demo", 500, 500);
return ttk_run(main_window);
}