From 31113b4c009fb7251ef3924a7562efd7f037bd65 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 23 Oct 2007 03:03:52 -0400 Subject: [PATCH] Initial commit --- LICENSE | 20 ++++++++ MANIFEST | 5 ++ MANIFEST.in | 2 + README | 37 ++++++++++++++ ghosd.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ghosd.py | 23 +++++++++ setup.py | 26 ++++++++++ 7 files changed, 274 insertions(+) create mode 100644 LICENSE create mode 100644 MANIFEST create mode 100644 MANIFEST.in create mode 100644 README create mode 100644 ghosd.c create mode 100644 ghosd.py create mode 100644 setup.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e124c00 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2007 Sylvain Fourmanoit + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..537c862 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,5 @@ +LICENSE +README +ghosd.c +ghosd.py +setup.py diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..9575a1c --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include LICENSE +include README diff --git a/README b/README new file mode 100644 index 0000000..9761a36 --- /dev/null +++ b/README @@ -0,0 +1,37 @@ +pyghosd +======= +Sylvain Fourmanoit + +What is it? +----------- +It is a way to use the Ghosd library to flash text on screen from Python with +minimum fuss; unlike what the name might suggests, this does not contains Python +bindings to Ghosd. + +Requirements +------------ +- Ghosd 0.0.1 and its dependencies (i.e. if you can build Ghosd, you are fine) +- Python (tested on 2.4.4 and 2.5.1), including the development headers. + +Compilation and Installation +---------------------------- +pyghosd comes with a distutils script. In most cases, it should be enough to +perform, with proper privileges: + +----------------------- +python setup.py install +----------------------- + +See `python setup.py --help` for details. + +Usage +----- +See `pydoc ghosd` once pyghosd has been installed. + +To Do +----- +- Add proper Unicode support. + +Legalese +-------- +See LICENSE. diff --git a/ghosd.c b/ghosd.c new file mode 100644 index 0000000..b452b1e --- /dev/null +++ b/ghosd.c @@ -0,0 +1,161 @@ +/* Fast refactoring of ghosd-0.0.1/examples/text.c. + */ + +#include + +#include +#include +#include +#include + +#include + +#include +#include + +struct _ghosd_render_info { + PangoLayout * layout; + float fgr, fgg, fgb, fga, + bgr, bgg, bgb, bga; +}; + +static void +render(Ghosd *ghosd, cairo_t *cr, void* data) { + + struct _ghosd_render_info * ri = data; + + /* drop shadow! */ + cairo_set_source_rgba(cr, ri->bgr, ri->bgg, ri->bgb, ri->bga); + cairo_move_to(cr, 2, 2); + pango_cairo_show_layout(cr, ri->layout); + + /* and the actual text. */ + cairo_set_source_rgba(cr, ri->fgr, ri->fgg, ri->fgb, ri->fga); + cairo_move_to(cr, 0, 0); + pango_cairo_show_layout(cr, ri->layout); +} + +static PyObject * +ghosd_flash_text(PyObject * self, PyObject * args, PyObject * kw) +{ + static char * kwlist[] = {"text", "x", "y", + "fade_time", "display_time", + "fg_color", "bg_color", NULL}; + Ghosd *ghosd; + PangoContext * context; + struct _ghosd_render_info ri; + int x = -50, y = -50, fade = 300, dtime = 2000; + char * text = NULL; + PyObject * fg_color = NULL, * bg_color = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kw, "s|iiiiOO", + kwlist, + &text, + &x, &y, &fade, &dtime, &fg_color, &bg_color)) + return NULL; + +#define cbcheck(nam, c) ((ri.nam ## c >= 0) && (ri.nam ## c <= 1)) +#define colcheck(nam) \ + if (nam ## _color) { \ + if (!PyTuple_Check(nam ## _color)) { \ + PyErr_SetString(PyExc_TypeError, #nam "_color should be a tuple"); \ + return NULL; \ + } \ + if (!PyArg_ParseTuple(nam ## _color, "ffff", \ + & ri.nam ## r, & ri.nam## g, \ + & ri.nam ## b, & ri.nam ## a)) { \ + PyErr_SetString(PyExc_TypeError, \ + #nam "_color should be a 4-tuple of floats"); \ + return NULL; \ + } \ + if (!cbcheck(nam, r) || !cbcheck(nam, g) || \ + !cbcheck(nam, b) || !cbcheck(nam, a)) { \ + PyErr_SetString(PyExc_ValueError, \ + "all components in " #nam \ + "_color should be between 0 and 1"); \ + return NULL; \ + } \ + } + + ri.fgr = ri.fgg = ri.fgb = ri.fga = 1; + ri.bgr = ri.bgg = ri.bgb = 0; ri.bga = .5; + + colcheck(fg) + colcheck(bg) + +#undef cbcheck +#undef colcheck + + /* Finally, we are there: let's use this ghosd lib! + */ + if (!(context = pango_cairo_font_map_create_context( + PANGO_CAIRO_FONT_MAP(pango_cairo_font_map_get_default())))) { + PyErr_SetString(PyExc_RuntimeError, + "could not create pango context"); + return NULL; + } + + if (!(ri.layout = pango_layout_new(context))) { + PyErr_SetString(PyExc_RuntimeError, + "could not create pango layout"); + g_object_unref(context); + return NULL; + } + + pango_layout_set_markup(ri.layout, text, -1); + + if (!(ghosd = ghosd_new())) { + PyErr_SetString(PyExc_RuntimeError, + "could not create ghosd object"); + g_object_unref(ri.layout); + g_object_unref(context); + return NULL; + } + + ghosd_text_set_position(ghosd, x, y, ri.layout); + ghosd_set_render(ghosd, render, &ri); + + ghosd_flash(ghosd, fade, dtime); + + /* Free the various objects */ + free(ghosd); /* See ghosd-0.0.1/ghosd/ghosd.c: + no API for now, but that's just + a calloc'ed struct: no API to close + the display unfortunately. */ + g_object_unref(ri.layout); + g_object_unref(context); + + Py_INCREF(Py_None); + return Py_None; + +} + +static PyMethodDef ghosdMethods[] = { + { "flash_text", (PyCFunction)ghosd_flash_text, METH_VARARGS | METH_KEYWORDS , +"flash_text(text, x=-50, y=-50, fade_time=300, display_time=2000, \n\ +fg_color=(1, 1, 1, 1), bg_color=(0, 0, 0, .5)) => None\n\ +\n\ +Flash a given text message onto the screen.\n\ +\n\ +- text text to be displayed, in Pango markup (see below)\n\ +- x, y position onto the screen\n\ +- fade_time time, in milliseconds, for fading the text in and out\n\ +- display_time time the text is to be displayed\n\ +- fg_color RGBA default text color\n\ +- bg_color RGBA text shadow color\n\ +\n\ +A concise description of the Pango Markup Language can be found\n\ +in PyGTK documentation:\n\ +\n\ +http://www.pygtk.org/docs/pygtk/pango-markup-language.html\n\ +"}, + {NULL} +}; + + +PyMODINIT_FUNC +init_ghosd(void) +{ + g_type_init(); + Py_InitModule("_ghosd", ghosdMethods); +} diff --git a/ghosd.py b/ghosd.py new file mode 100644 index 0000000..9e606f2 --- /dev/null +++ b/ghosd.py @@ -0,0 +1,23 @@ +""" +ghosd for Python + +Right now, this does not contain bindings for ghosd, but a simple hack +to bring parametrable ghosd'ed text display facility to Python. It +doesn't rely on any high-level Python bindings (PyGTK, Pycairo, ...) +either. Usage is pretty simple: + +>>> from ghosd import flash_text +>>> flash_text(\"Hello, world!\") +""" +# We don't really need this frontend right now, but who knows... +# +from _ghosd import * + +# An example? All right: here is a copycat of ghosd-0.0.1/examples/text.c +# +if __name__ == '__main__': + # It doesn't get easier than that. ;-D + # + flash_text("" + "some sample text using pyghosd" + "") diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..6643c8b --- /dev/null +++ b/setup.py @@ -0,0 +1,26 @@ +from distutils.core import setup, Extension +from distutils.command import sdist +from os import popen + +class Sdist(sdist.sdist): + default_format = {'posix': 'bztar'} + +setup(name='pyghosd', + version='0.0.1', + description='Crude ghosd text hack', + author='Sylvain Fourmanoit', + author_email='syfou@users.berlios.de', + license='BSD', + ext_modules=[Extension('_ghosd', + sources=['ghosd.c'], + libraries = ['X11'], + extra_compile_args = + popen("pkg-config --cflags ghosd").read().split(), + extra_link_args = + popen("pkg-config --libs ghosd").read().split() + ) + + ], + py_modules=['ghosd'], + cmdclass = {'sdist' : Sdist} + ) -- 2.11.4.GIT