[hackers] [libdraw] initial commit || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Wed, 30 Jun 2010 00:36:23 +0000 (UTC)

changeset: 1:9d7b9be90615
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Wed Jun 30 01:36:14 2010 +0100
files: LICENSE Makefile README cleanupdraw.c config.mk draw.h drawsquare.c drawtext.c eprint.c getcolor.c initfont.c setupdraw.c textnw.c textw.c
description:
initial commit

diff -r 5e5199b03cd8 -r 9d7b9be90615 LICENSE
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LICENSE Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,22 @@
+MIT/X Consortium License
+
+© 2010 Connor Lane Smith <cls_AT_lubutu.com>
+© 2010 Anselm R Garbe <anselm_AT_garbe.us>
+
+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 -r 5e5199b03cd8 -r 9d7b9be90615 Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,53 @@
+# libdraw - dynamic drawing library
+# See LICENSE file for copyright and license details.
+
+include config.mk
+
+SRC = cleanupdraw.c drawsquare.c drawtext.c eprint.c getcolor.c initfont.c \
+ setupdraw.c textnw.c textw.c
+OBJ = ${SRC:.c=.o}
+
+all: options libdraw.a
+
+options:
+ @echo libdraw build options:
+ @echo "CFLAGS = ${CFLAGS}"
+ @echo "CC = ${CC}"
+
+.c.o:
+ @echo CC $<
+ @${CC} -c ${CFLAGS} $<
+
+${OBJ}: config.mk draw.h
+
+libdraw.a: ${OBJ}
+ @echo AR $@
+ @ar cr $@ $+
+
+clean:
+ @echo cleaning
+ @rm -f libdraw.a ${OBJ} libdraw-${VERSION}.tar.gz
+
+dist: clean
+ @echo creating dist tarball
+ @mkdir -p libdraw-${VERSION}
+ @cp -R LICENSE Makefile README config.mk ${SRC} libdraw-${VERSION}
+ @tar -cf libdraw-${VERSION}.tar libdraw-${VERSION}
+ @gzip libdraw-${VERSION}.tar
+ @rm -rf libdraw-${VERSION}
+
+install: all
+ @echo installing header file to ${DESTDIR}${PREFIX}/include
+ @mkdir -p ${DESTDIR}${PREFIX}/include
+ @cp -f draw.h ${DESTDIR}${PREFIX}/include
+ @echo installing library to ${DESTDIR}${PREFIX}/lib
+ @mkdir -p ${DESTDIR}${PREFIX}/lib
+ @cp -f libdraw.a ${DESTDIR}${PREFIX}/lib
+
+uninstall:
+ @echo removing header file from ${DESTDIR}${PREFIX}/include
+ @rm -f ${DESTDIR}${PREFIX}/include/draw.h
+ @echo removing library from ${DESTDIR}${PREFIX}/lib
+ @rm -f ${DESTDIR}${PREFIX}/lib/libdraw.a
+
+.PHONY: all options clean dist install uninstall
diff -r 5e5199b03cd8 -r 9d7b9be90615 README
--- a/README Tue Jun 29 21:33:07 2010 +0000
+++ b/README Wed Jun 30 01:36:14 2010 +0100
@@ -1,2 +1,19 @@
 libdraw
 =======
+libdraw is a simple dynamic drawing library for suckless projects.
+
+
+Requirements
+------------
+In order to build libdraw you need the Xlib header files.
+
+
+Installation
+------------
+Edit config.mk to match your local setup (libdraw is installed into
+the /usr/local namespace by default).
+
+Afterwards enter the following command to build and install (if
+necessary as root):
+
+ make clean install
diff -r 5e5199b03cd8 -r 9d7b9be90615 cleanupdraw.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/cleanupdraw.c Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,13 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include <draw.h>
+
+void
+cleanupdraw(DC *dc) {
+ if(dc->font.set)
+ XFreeFontSet(dc->dpy, dc->font.set);
+ else
+ XFreeFont(dc->dpy, dc->font.xfont);
+ XFreePixmap(dc->dpy, dc->drawable);
+ XFreeGC(dc->dpy, dc->gc);
+}
diff -r 5e5199b03cd8 -r 9d7b9be90615 config.mk
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/config.mk Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,22 @@
+# libdraw version
+VERSION = 1.0
+
+# Customize below to fit your system
+
+# paths
+PREFIX = /usr/local
+
+X11INC = /usr/X11R6/include
+
+# includes
+INCS = -I. -I/usr/include -I${X11INC}
+
+# flags
+CPPFLAGS = -D_BSD_SOURCE
+CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
+
+# Solaris
+#CFLAGS = -fast ${INCS}
+
+# compiler
+CC = cc
diff -r 5e5199b03cd8 -r 9d7b9be90615 draw.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/draw.h Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,39 @@
+/* See LICENSE file for copyright and license details. */
+#ifndef DRAW_H
+#define DRAW_H
+
+#include <X11/Xlib.h>
+
+/* enums */
+enum { ColBorder, ColFG, ColBG, ColLast };
+
+/* typedefs */
+typedef struct {
+ int x, y, w, h;
+ Drawable drawable;
+ Display *dpy;
+ GC gc;
+ struct {
+ XFontStruct *xfont;
+ XFontSet set;
+ int ascent;
+ int descent;
+ int height;
+ } font;
+} DC; /* draw context */
+
+/* forward declarations */
+void cleanupdraw(DC *dc);
+void drawsquare(DC *dc, Bool filled, unsigned long col[ColLast], Bool invert);
+void drawtext(DC *dc, const char *text, unsigned long col[ColLast], Bool invert);
+void eprint(const char *fmt, ...);
+unsigned long getcolor(DC *dc, const char *colstr);
+void initfont(DC *dc, const char *fontstr);
+void setupdraw(DC *dc, Window w);
+int textnw(DC *dc, const char *text, unsigned int len);
+int textw(DC *dc, const char *text);
+
+/* variables */
+extern const char *progname;
+
+#endif
diff -r 5e5199b03cd8 -r 9d7b9be90615 drawsquare.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/drawsquare.c Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,19 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include <draw.h>
+
+void
+drawsquare(DC *dc, Bool filled, unsigned long col[ColLast], Bool invert) {
+ int n;
+ XRectangle r = { dc->x, dc->y, dc->w, dc->h };
+
+ XSetForeground(dc->dpy, dc->gc, col[invert ? ColBG : ColFG]);
+ n = ((dc->font.ascent + dc->font.descent + 2) / 4) + (filled ? 1 : 0);
+ r.width = r.height = n;
+ r.x = dc->x + 1;
+ r.y = dc->y + 1;
+ if(filled)
+ XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1);
+ else
+ XDrawRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1);
+}
diff -r 5e5199b03cd8 -r 9d7b9be90615 drawtext.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/drawtext.c Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,34 @@
+/* See LICENSE file for copyright and license details. */
+#include <string.h>
+#include <X11/Xlib.h>
+#include <draw.h>
+
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
+void
+drawtext(DC *dc, const char *text, unsigned long col[ColLast], Bool invert) {
+ char buf[256];
+ int i, x, y, h, len, olen;
+ XRectangle r = { dc->x, dc->y, dc->w, dc->h };
+
+ XSetForeground(dc->dpy, dc->gc, col[invert ? ColFG : ColBG]);
+ XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1);
+ if(!text)
+ return;
+ olen = strlen(text);
+ h = dc->font.height;
+ y = dc->y + ((h+2) / 2) - (h / 2) + dc->font.ascent;
+ x = dc->x + (h / 2);
+ /* shorten text if necessary */
+ for(len = MIN(olen, sizeof buf); len && textnw(dc, text, len) > dc->w - h; len--);
+ if(!len)
+ return;
+ memcpy(buf, text, len);
+ if(len < olen)
+ for(i = len; i && i > len - 3; buf[--i] = '.');
+ XSetForeground(dc->dpy, dc->gc, col[invert ? ColBG : ColFG]);
+ if(dc->font.set)
+ XmbDrawString(dc->dpy, dc->drawable, dc->font.set, dc->gc, x, y, buf, len);
+ else
+ XDrawString(dc->dpy, dc->drawable, dc->gc, x, y, buf, len);
+}
diff -r 5e5199b03cd8 -r 9d7b9be90615 eprint.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/eprint.c Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,18 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <draw.h>
+
+const char *progname;
+
+void
+eprint(const char *fmt, ...) {
+ va_list ap;
+
+ fprintf(stderr, "%s: ", progname);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ exit(EXIT_FAILURE);
+}
diff -r 5e5199b03cd8 -r 9d7b9be90615 getcolor.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/getcolor.c Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,13 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include <draw.h>
+
+unsigned long
+getcolor(DC *dc, const char *colstr) {
+ Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
+ XColor color;
+
+ if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
+ eprint("cannot allocate color '%s'\n", colstr);
+ return color.pixel;
+}
diff -r 5e5199b03cd8 -r 9d7b9be90615 initfont.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/initfont.c Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,36 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include <draw.h>
+
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+
+void
+initfont(DC *dc, const char *fontstr) {
+ char *def, **missing = NULL;
+ int i, n;
+
+ if(!fontstr || !*fontstr)
+ eprint("cannot load null font\n");
+ dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def);
+ if(missing)
+ XFreeStringList(missing);
+ if(dc->font.set) {
+ XFontStruct **xfonts;
+ char **font_names;
+ dc->font.ascent = dc->font.descent = 0;
+ n = XFontsOfFontSet(dc->font.set, &xfonts, &font_names);
+ for(i = 0; i < n; i++) {
+ dc->font.ascent = MAX(dc->font.ascent, (*xfonts)->ascent);
+ dc->font.descent = MAX(dc->font.descent, (*xfonts)->descent);
+ xfonts++;
+ }
+ }
+ else {
+ if(!(dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))
+ && !(dc->font.xfont = XLoadQueryFont(dc->dpy, "fixed")))
+ eprint("cannot load font '%s'\n", fontstr);
+ dc->font.ascent = dc->font.xfont->ascent;
+ dc->font.descent = dc->font.xfont->descent;
+ }
+ dc->font.height = dc->font.ascent + dc->font.descent;
+}
diff -r 5e5199b03cd8 -r 9d7b9be90615 setupdraw.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/setupdraw.c Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,16 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include <draw.h>
+
+void
+setupdraw(DC *dc, Window w) {
+ XWindowAttributes wa;
+
+ XGetWindowAttributes(dc->dpy, w, &wa);
+ dc->drawable = XCreatePixmap(dc->dpy, w, wa.width, wa.height,
+ DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
+ dc->gc = XCreateGC(dc->dpy, w, 0, NULL);
+ XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
+ if(!dc->font.set)
+ XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
+}
diff -r 5e5199b03cd8 -r 9d7b9be90615 textnw.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/textnw.c Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,14 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include <draw.h>
+
+int
+textnw(DC *dc, const char *text, unsigned int len) {
+ XRectangle r;
+
+ if(dc->font.set) {
+ XmbTextExtents(dc->font.set, text, len, NULL, &r);
+ return r.width;
+ }
+ return XTextWidth(dc->font.xfont, text, len);
+}
diff -r 5e5199b03cd8 -r 9d7b9be90615 textw.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/textw.c Wed Jun 30 01:36:14 2010 +0100
@@ -0,0 +1,9 @@
+/* See LICENSE file for copyright and license details. */
+#include <string.h>
+#include <X11/Xlib.h>
+#include <draw.h>
+
+int
+textw(DC *dc, const char *text) {
+ return textnw(dc, text, strlen(text)) + dc->font.height;
+}
Received on Wed Jun 30 2010 - 00:36:23 UTC

This archive was generated by hypermail 2.2.0 : Wed Jun 30 2010 - 00:48:04 UTC