Commit 71aa4318 authored by Hanspeter Portner's avatar Hanspeter Portner

Add 'pugl/' from commit '0361919d'

git-subtree-dir: pugl
git-subtree-mainline: 22e361a2
git-subtree-split: 0361919d
parents 22e361a2 0361919d
.waf*/
build/
.lock-waf*
Author:
David Robillard <d@drobilla.net>
Original GLX inspiration, portability fixes:
Ben Loftis
Various fixes and improvements:
Robin Gareus <robin@gareus.org>
UTF-8 and Windows event work:
Erik Åldstedt Sund <erikalds@gmail.com>
Copyright 2011-2014 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, 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.
This source diff could not be displayed because it is too large. You can view the blob instead.
PUGL
====
Pugl is a minimal portable API for GUIs which supports embedding and is
suitable for use in plugins. It works on X11, Mac OS X, and Windows. GUIs can
be drawn with OpenGL or Cairo.
Pugl is vaguely similar to GLUT, but with some significant distinctions:
* Minimal in scope, providing only what is necessary to draw and receive
keyboard and mouse input.
* No reliance on static data whatsoever, so the API can be used in plugins or
multiple independent parts of a program.
* Single implementation, which is small, liberally licensed Free / Open Source
Software, and suitable for direct inclusion in programs if avoiding a
library dependency is desired.
* Support for embedding in other windows, so Pugl code can draw to a widget
inside a larger GUI.
* More complete support for keyboard input, including additional "special"
keys, modifiers, and support for detecting individual modifier key presses.
For more information, see <http://drobilla.net/software/pugl>.
-- David Robillard <d@drobilla.net>
prefix=@PREFIX@
exec_prefix=@EXEC_PREFIX@
libdir=@LIBDIR@
includedir=@INCLUDEDIR@
Name: Pugl
Version: @PUGL_VERSION@
Description: Lightweight portable OpenGL API
Libs: -L${libdir} -l@LIB_PUGL@
Cflags: -I${includedir}/pugl-@PUGL_MAJOR_VERSION@
/*
Copyright 2016 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, 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.
*/
#if defined(PUGL_HAVE_GL) && defined(PUGL_HAVE_CAIRO)
#include <cairo.h>
#include <stdint.h>
#include "pugl/gl.h"
typedef struct {
unsigned texture_id;
uint8_t* buffer;
} PuglCairoGL;
static cairo_surface_t*
pugl_cairo_gl_create(PuglCairoGL* ctx, int width, int height, int bpp)
{
free(ctx->buffer);
ctx->buffer = (uint8_t*)calloc(bpp * width * height, sizeof(uint8_t));
if (!ctx->buffer) {
fprintf(stderr, "failed to allocate surface buffer\n");
return NULL;
}
return cairo_image_surface_create_for_data(
ctx->buffer, CAIRO_FORMAT_ARGB32, width, height, bpp * width);
}
static void
pugl_cairo_gl_free(PuglCairoGL* ctx)
{
free(ctx->buffer);
ctx->buffer = NULL;
}
static void
pugl_cairo_gl_configure(PuglCairoGL* ctx, int width, int height)
{
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDeleteTextures(1, &ctx->texture_id);
glGenTextures(1, &ctx->texture_id);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, ctx->texture_id);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
}
static void
pugl_cairo_gl_draw(PuglCairoGL* ctx, int width, int height)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glEnable(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8,
width, height, 0,
GL_BGRA, GL_UNSIGNED_BYTE, ctx->buffer);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, (GLfloat)height);
glVertex2f(-1.0f, -1.0f);
glTexCoord2f((GLfloat)width, (GLfloat)height);
glVertex2f(1.0f, -1.0f);
glTexCoord2f((GLfloat)width, 0.0f);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-1.0f, 1.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glPopMatrix();
}
#endif
/*
Copyright 2012-2014 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, 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.
*/
/**
@file gl.h Portable header wrapper for gl.h.
Unfortunately, GL includes vary across platforms so this header allows for
pure portable programs.
*/
#ifdef __APPLE__
# include "OpenGL/gl.h"
#else
# ifdef _WIN32
# include <windows.h> /* Broken Windows GL headers require this */
# endif
# include "GL/gl.h"
#endif
/*
Copyright 2016 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, 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.
*/
/**
@file gl.h Portable header wrapper for glew.h.
Unfortunately, GL includes vary across platforms so this header allows for
pure portable programs.
*/
#ifdef __APPLE__
# include "OpenGL/glew.h"
#else
# ifdef _WIN32
# include <windows.h> /* Broken Windows GL headers require this */
# endif
# include "GL/glew.h"
#endif
/*
Copyright 2012-2015 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, 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.
*/
/**
@file gl.h Portable header wrapper for glu.h.
Unfortunately, GL includes vary across platforms so this header allows for
pure portable programs.
*/
#ifdef __APPLE__
# include "OpenGL/glu.h"
#else
# ifdef _WIN32
# include <windows.h> /* Broken Windows GL headers require this */
# endif
# include "GL/glu.h"
#endif
This diff is collapsed.
/*
Copyright 2012-2015 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, 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.
*/
/**
@file pugl.hpp C++ API for Pugl, a minimal portable API for OpenGL.
*/
#ifndef PUGL_HPP_INCLUDED
#define PUGL_HPP_INCLUDED
#include "pugl/pugl.h"
/**
@defgroup puglmm Puglmm
C++ API wrapper for Pugl.
@{
*/
namespace pugl {
class View {
public:
View(int* pargc, char** argv)
: _view(puglInit(pargc, argv))
{
puglSetHandle(_view, this);
puglSetEventFunc(_view, _onEvent);
}
virtual ~View() { puglDestroy(_view); }
virtual void initWindowParent(PuglNativeWindow parent) {
puglInitWindowParent(_view, parent);
}
virtual void initWindowSize(int width, int height) {
puglInitWindowSize(_view, width, height);
}
virtual void initWindowMinSize(int width, int height) {
puglInitWindowMinSize(_view, width, height);
}
virtual void initWindowAspectRatio(int min_x, int min_y, int max_x, int max_y) {
puglInitWindowAspectRatio(_view, min_x, min_y, max_x, max_y);
}
virtual void initResizable(bool resizable) {
puglInitResizable(_view, resizable);
}
virtual void initTransientFor(uintptr_t parent) {
puglInitTransientFor(_view, parent);
}
virtual void initContextType(PuglContextType type) {
puglInitContextType(_view, type);
}
virtual void createWindow(const char* title) {
puglCreateWindow(_view, title);
}
virtual void showWindow() { puglShowWindow(_view); }
virtual void hideWindow() { puglHideWindow(_view); }
virtual PuglNativeWindow getNativeWindow() { return puglGetNativeWindow(_view); }
virtual void onEvent(const PuglEvent* event) = 0;
virtual void* getContext() { return puglGetContext(_view); }
virtual void ignoreKeyRepeat(bool ignore) { puglIgnoreKeyRepeat(_view, ignore); }
virtual void grabFocus() { puglGrabFocus(_view); }
virtual PuglStatus waitForEvent() { return puglWaitForEvent(_view); }
virtual PuglStatus processEvents() { return puglProcessEvents(_view); }
virtual void postRedisplay() { puglPostRedisplay(_view); }
PuglView* cobj() { return _view; }
private:
static void _onEvent(PuglView* view, const PuglEvent* event) {
((View*)puglGetHandle(view))->onEvent(event);
}
PuglView* _view;
};
} // namespace pugl
/**
@}
*/
#endif /* PUGL_HPP_INCLUDED */
/*
Copyright 2012-2016 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, 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.
*/
/**
@file pugl_internal.h Private platform-independent definitions.
Note this file contains function definitions, so it must be compiled into
the final binary exactly once. Each platform specific implementation file
including it once should achieve this.
If you are copying the pugl code into your source tree, the following
symbols can be defined to tweak pugl behaviour:
PUGL_HAVE_CAIRO: Include Cairo support code.
PUGL_HAVE_GL: Include OpenGL support code.
*/
#include <stdlib.h>
#include <string.h>
#include "pugl/pugl.h"
typedef struct PuglInternalsImpl PuglInternals;
struct PuglViewImpl {
PuglHandle handle;
PuglEventFunc eventFunc;
PuglInternals* impl;
char* windowClass;
PuglNativeWindow parent;
PuglContextType ctx_type;
uintptr_t transient_parent;
int width;
int height;
int min_width;
int min_height;
int min_aspect_x;
int min_aspect_y;
int max_aspect_x;
int max_aspect_y;
bool ignoreKeyRepeat;
bool redisplay;
bool resizable;
bool visible;
};
PuglInternals* puglInitInternals(void);
PuglView*
puglInit(int* pargc, char** argv)
{
PuglView* view = (PuglView*)calloc(1, sizeof(PuglView));
if (!view) {
return NULL;
}
PuglInternals* impl = puglInitInternals();
if (!impl) {
return NULL;
}
view->ctx_type = PUGL_GL;
view->impl = impl;
view->width = 640;
view->height = 480;
return view;
}
void
puglInitWindowSize(PuglView* view, int width, int height)
{
view->width = width;
view->height = height;
}
void
puglInitWindowMinSize(PuglView* view, int width, int height)
{
view->min_width = width;
view->min_height = height;
}
void
puglInitWindowAspectRatio(PuglView* view,
int min_x,
int min_y,
int max_x,
int max_y)
{
view->min_aspect_x = min_x;
view->min_aspect_y = min_y;
view->max_aspect_x = max_x;
view->max_aspect_y = max_y;
}
void
puglInitWindowClass(PuglView* view, const char* name)
{
const size_t len = strlen(name);
free(view->windowClass);
view->windowClass = (char*)calloc(1, len + 1);
memcpy(view->windowClass, name, len);
}
void
puglInitWindowParent(PuglView* view, PuglNativeWindow parent)
{
view->parent = parent;
}
void
puglInitResizable(PuglView* view, bool resizable)
{
view->resizable = resizable;
}
void
puglInitTransientFor(PuglView* view, uintptr_t parent)
{
view->transient_parent = parent;
}
void
puglInitContextType(PuglView* view, PuglContextType type)
{
view->ctx_type = type;
}
void
puglSetHandle(PuglView* view, PuglHandle handle)
{
view->handle = handle;
}
PuglHandle
puglGetHandle(PuglView* view)
{
return view->handle;
}
bool
puglGetVisible(PuglView* view)
{
return view->visible;
}
void
puglGetSize(PuglView* view, int* width, int* height)
{
*width = view->width;
*height = view->height;
}
void
puglIgnoreKeyRepeat(PuglView* view, bool ignore)
{
view->ignoreKeyRepeat = ignore;
}
void
puglSetEventFunc(PuglView* view, PuglEventFunc eventFunc)
{
view->eventFunc = eventFunc;
}
/** Return the code point for buf, or the replacement character on error. */
static uint32_t
puglDecodeUTF8(const uint8_t* buf)
{
#define FAIL_IF(cond) { if (cond) return 0xFFFD; }
// http://en.wikipedia.org/wiki/UTF-8
if (buf[0] < 0x80) {
return buf[0];
} else if (buf[0] < 0xC2) {
return 0xFFFD;
} else if (buf[0] < 0xE0) {
FAIL_IF((buf[1] & 0xC0) != 0x80);
return (buf[0] << 6) + buf[1] - 0x3080;
} else if (buf[0] < 0xF0) {
FAIL_IF((buf[1] & 0xC0) != 0x80);
FAIL_IF(buf[0] == 0xE0 && buf[1] < 0xA0);
FAIL_IF((buf[2] & 0xC0) != 0x80);
return (buf[0] << 12) + (buf[1] << 6) + buf[2] - 0xE2080;
} else if (buf[0] < 0xF5) {
FAIL_IF((buf[1] & 0xC0) != 0x80);
FAIL_IF(buf[0] == 0xF0 && buf[1] < 0x90);
FAIL_IF(buf[0] == 0xF4 && buf[1] >= 0x90);
FAIL_IF((buf[2] & 0xC0) != 0x80);
FAIL_IF((buf[3] & 0xC0) != 0x80);
return ((buf[0] << 18) +
(buf[1] << 12) +
(buf[2] << 6) +
buf[3] - 0x3C82080);
}
return 0xFFFD;
}
static void
puglDispatchEvent(PuglView* view, const PuglEvent* event)
{
switch (event->type) {
case PUGL_NOTHING:
break;
case PUGL_CONFIGURE:
view->width = event->configure.width;
view->height = event->configure.height;
puglEnterContext(view);
view->eventFunc(view, event);
puglLeaveContext(view, false);
break;
case PUGL_EXPOSE:
if (event->expose.count == 0) {
puglEnterContext(view);
view->eventFunc(view, event);
puglLeaveContext(view, true);
}
break;
default:
view->eventFunc(view, event);
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*
Copyright 2012-2014 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, 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.
*/
/**
@file pugl_cairo_test.c A simple Pugl test that creates a top-level window.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <cairo/cairo.h>
#include "pugl/pugl.h"
static int quit = 0;
static bool entered = false;
typedef struct {
int x;
int y;
int w;
int h;
bool pressed;
const char* label;
} Button;
static Button toggle_button = { 16, 16, 128, 64, false, "Test" };
static void
roundedBox(cairo_t* cr, double x, double y, double w, double h)
{
static const double radius = 10;
static const double degrees = 3.14159265 / 180.0;
cairo_new_sub_path(cr);
cairo_arc(cr,
x + w - radius,
y + radius,
radius, -90 * degrees, 0 * degrees);
cairo_arc(cr,
x + w - radius, y + h - radius,
radius, 0 * degrees, 90 * degrees);
cairo_arc(cr,
x + radius, y + h - radius,
radius, 90 * degrees, 180 * degrees);
cairo_arc(cr,
x + radius, y + radius,
radius, 180 * degrees, 270 * degrees);
cairo_close_path(cr);
}
static void
buttonDraw(cairo_t* cr, const Button* but)
{
// Draw base
if (but->pressed) {
cairo_set_source_rgba(cr, 0.4, 0.9, 0.1, 1);
} else {
cairo_set_source_rgba(cr, 0.3, 0.5, 0.1, 1);
}
roundedBox(cr, but->x, but->y, but->w, but->h);
cairo_fill_preserve(cr);
// Draw border
cairo_set_source_rgba(cr, 0.4, 0.9, 0.1, 1);
cairo_set_line_width(cr, 4.0);
cairo_stroke(cr);
// Draw label
cairo_text_extents_t extents;
cairo_set_font_size(cr, 32.0);
cairo_text_extents(cr, but->label, &extents);
cairo_move_to(cr,
(but->x + but->w / 2) - extents.width / 2,
(but->y + but->h / 2) + extents.height / 2);
cairo_set_source_rgba(cr, 0, 0, 0, 1);
cairo_show_text(cr, but->label);
}
static bool
buttonTouches(const Button* but, double x, double y)
{
return (x >= toggle_button.x && x <= toggle_button.x + toggle_button.w &&
y >= toggle_button.y && y <= toggle_button.y + toggle_button.h);
}