Initial commit
[pyghosd.git] / ghosd.c
blobb452b1e2c1b143181cf41ae36ad3bc45dcbf2a0f
1 /* Fast refactoring of ghosd-0.0.1/examples/text.c.
2 */
4 #include <Python.h>
6 #include <cairo/cairo.h>
7 #include <pango/pangocairo.h>
8 #include <time.h>
9 #include <unistd.h>
11 #include <X11/Xlib.h>
13 #include <ghosd/ghosd.h>
14 #include <ghosd/ghosd-text.h>
16 struct _ghosd_render_info {
17 PangoLayout * layout;
18 float fgr, fgg, fgb, fga,
19 bgr, bgg, bgb, bga;
22 static void
23 render(Ghosd *ghosd, cairo_t *cr, void* data) {
25 struct _ghosd_render_info * ri = data;
27 /* drop shadow! */
28 cairo_set_source_rgba(cr, ri->bgr, ri->bgg, ri->bgb, ri->bga);
29 cairo_move_to(cr, 2, 2);
30 pango_cairo_show_layout(cr, ri->layout);
32 /* and the actual text. */
33 cairo_set_source_rgba(cr, ri->fgr, ri->fgg, ri->fgb, ri->fga);
34 cairo_move_to(cr, 0, 0);
35 pango_cairo_show_layout(cr, ri->layout);
38 static PyObject *
39 ghosd_flash_text(PyObject * self, PyObject * args, PyObject * kw)
41 static char * kwlist[] = {"text", "x", "y",
42 "fade_time", "display_time",
43 "fg_color", "bg_color", NULL};
44 Ghosd *ghosd;
45 PangoContext * context;
46 struct _ghosd_render_info ri;
47 int x = -50, y = -50, fade = 300, dtime = 2000;
48 char * text = NULL;
49 PyObject * fg_color = NULL, * bg_color = NULL;
51 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|iiiiOO",
52 kwlist,
53 &text,
54 &x, &y, &fade, &dtime, &fg_color, &bg_color))
55 return NULL;
57 #define cbcheck(nam, c) ((ri.nam ## c >= 0) && (ri.nam ## c <= 1))
58 #define colcheck(nam) \
59 if (nam ## _color) { \
60 if (!PyTuple_Check(nam ## _color)) { \
61 PyErr_SetString(PyExc_TypeError, #nam "_color should be a tuple"); \
62 return NULL; \
63 } \
64 if (!PyArg_ParseTuple(nam ## _color, "ffff", \
65 & ri.nam ## r, & ri.nam## g, \
66 & ri.nam ## b, & ri.nam ## a)) { \
67 PyErr_SetString(PyExc_TypeError, \
68 #nam "_color should be a 4-tuple of floats"); \
69 return NULL; \
70 } \
71 if (!cbcheck(nam, r) || !cbcheck(nam, g) || \
72 !cbcheck(nam, b) || !cbcheck(nam, a)) { \
73 PyErr_SetString(PyExc_ValueError, \
74 "all components in " #nam \
75 "_color should be between 0 and 1"); \
76 return NULL; \
77 } \
80 ri.fgr = ri.fgg = ri.fgb = ri.fga = 1;
81 ri.bgr = ri.bgg = ri.bgb = 0; ri.bga = .5;
83 colcheck(fg)
84 colcheck(bg)
86 #undef cbcheck
87 #undef colcheck
89 /* Finally, we are there: let's use this ghosd lib!
91 if (!(context = pango_cairo_font_map_create_context(
92 PANGO_CAIRO_FONT_MAP(pango_cairo_font_map_get_default())))) {
93 PyErr_SetString(PyExc_RuntimeError,
94 "could not create pango context");
95 return NULL;
98 if (!(ri.layout = pango_layout_new(context))) {
99 PyErr_SetString(PyExc_RuntimeError,
100 "could not create pango layout");
101 g_object_unref(context);
102 return NULL;
105 pango_layout_set_markup(ri.layout, text, -1);
107 if (!(ghosd = ghosd_new())) {
108 PyErr_SetString(PyExc_RuntimeError,
109 "could not create ghosd object");
110 g_object_unref(ri.layout);
111 g_object_unref(context);
112 return NULL;
115 ghosd_text_set_position(ghosd, x, y, ri.layout);
116 ghosd_set_render(ghosd, render, &ri);
118 ghosd_flash(ghosd, fade, dtime);
120 /* Free the various objects */
121 free(ghosd); /* See ghosd-0.0.1/ghosd/ghosd.c:
122 no API for now, but that's just
123 a calloc'ed struct: no API to close
124 the display unfortunately. */
125 g_object_unref(ri.layout);
126 g_object_unref(context);
128 Py_INCREF(Py_None);
129 return Py_None;
133 static PyMethodDef ghosdMethods[] = {
134 { "flash_text", (PyCFunction)ghosd_flash_text, METH_VARARGS | METH_KEYWORDS ,
135 "flash_text(text, x=-50, y=-50, fade_time=300, display_time=2000, \n\
136 fg_color=(1, 1, 1, 1), bg_color=(0, 0, 0, .5)) => None\n\
138 Flash a given text message onto the screen.\n\
140 - text text to be displayed, in Pango markup (see below)\n\
141 - x, y position onto the screen\n\
142 - fade_time time, in milliseconds, for fading the text in and out\n\
143 - display_time time the text is to be displayed\n\
144 - fg_color RGBA default text color\n\
145 - bg_color RGBA text shadow color\n\
147 A concise description of the Pango Markup Language can be found\n\
148 in PyGTK documentation:\n\
150 http://www.pygtk.org/docs/pygtk/pango-markup-language.html\n\
152 {NULL}
156 PyMODINIT_FUNC
157 init_ghosd(void)
159 g_type_init();
160 Py_InitModule("_ghosd", ghosdMethods);