changeset: 7:c599c675c202
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Mon Aug 02 14:22:14 2010 +0100
files: Makefile README cleanupdraw.c commitdraw.c config.mk draw.h drawbox.c drawline.c drawrect.c drawsquare.c drawtext.c drawtextn.c eprintf.c freedraw.c getcolor.c initdraw.c initfont.c setcanvas.c setupdraw.c textnw.c textw.c weprintf.c
description:
improved init & fonts, added weprintf, removed square & line
diff -r c6de8835d7d5 -r c599c675c202 Makefile
--- a/Makefile Sat Jul 31 14:54:11 2010 +0100
+++ b/Makefile Mon Aug 02 14:22:14 2010 +0100
@@ -3,8 +3,9 @@
include config.mk
-SRC = cleanupdraw.c commitdraw.c drawbox.c drawline.c drawsquare.c drawtext.c \
- drawtextn.c eprintf.c getcolor.c initfont.c setupdraw.c textnw.c textw.c
+SRC = commitdraw.c drawrect.c drawtext.c drawtextn.c eprintf.c freedraw.c \
+ getcolor.c initdraw.c initfont.c setcanvas.c textnw.c textw.c weprintf.c
+
OBJ = ${SRC:.c=.o}
all: options libdraw.a
diff -r c6de8835d7d5 -r c599c675c202 README
--- a/README Sat Jul 31 14:54:11 2010 +0100
+++ b/README Mon Aug 02 14:22:14 2010 +0100
@@ -1,6 +1,6 @@
libdraw
=======
-libdraw is a simple dynamic drawing library for suckless projects.
+libdraw is a simple dynamic drawing library.
Requirements
@@ -13,7 +13,7 @@
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):
+Afterwards enter the following command to build and install libdraw
+(if necessary as root):
make clean install
diff -r c6de8835d7d5 -r c599c675c202 cleanupdraw.c
--- a/cleanupdraw.c Sat Jul 31 14:54:11 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,13 +0,0 @@
-/* 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 c6de8835d7d5 -r c599c675c202 commitdraw.c
--- a/commitdraw.c Sat Jul 31 14:54:11 2010 +0100
+++ b/commitdraw.c Mon Aug 02 14:22:14 2010 +0100
@@ -1,5 +1,6 @@
+/* See LICENSE file for copyright and license details. */
#include <X11/Xlib.h>
-#include <draw.h>
+#include "draw.h"
void
commitdraw(DC *dc, Window w)
@@ -8,5 +9,5 @@
if(!XGetWindowAttributes(dc->dpy, w, &wa))
eprintf("cannot get window attributes\n");
- XCopyArea(dc->dpy, dc->drawable, w, dc->gc, 0, 0, wa.width, wa.height, 0, 0);
+ XCopyArea(dc->dpy, dc->canvas, w, dc->gc, 0, 0, wa.width, wa.height, 0, 0);
}
diff -r c6de8835d7d5 -r c599c675c202 config.mk
--- a/config.mk Sat Jul 31 14:54:11 2010 +0100
+++ b/config.mk Mon Aug 02 14:22:14 2010 +0100
@@ -6,17 +6,15 @@
# paths
PREFIX = /usr/local
+# Xlib
X11INC = /usr/X11R6/include
# includes
-INCS = -I. -I/usr/include -I${X11INC}
+INCS = -I${X11INC}
# flags
CPPFLAGS = -D_BSD_SOURCE
-CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
-
-# Solaris
-#CFLAGS = -fast ${INCS}
+CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
# compiler
CC = cc
diff -r c6de8835d7d5 -r c599c675c202 draw.h
--- a/draw.h Sat Jul 31 14:54:11 2010 +0100
+++ b/draw.h Mon Aug 02 14:22:14 2010 +0100
@@ -12,11 +12,12 @@
/* typedefs */
typedef struct {
- int x, y, w, h;
+ int x, y;
+ unsigned int w, h;
Bool invert;
Display *dpy;
- Drawable drawable;
GC gc;
+ Pixmap canvas;
struct {
XFontStruct *xfont;
XFontSet set;
@@ -27,20 +28,19 @@
} DC; /* draw context */
/* forward declarations */
-void cleanupdraw(DC *dc);
void commitdraw(DC *dc, Window w);
-void drawbox(DC *dc, unsigned long col[ColLast]);
-void drawline(DC *dc, int x, int y, unsigned int w, unsigned int h,
- unsigned long col[ColLast]);
-void drawsquare(DC *dc, Bool filled, unsigned long col[ColLast]);
+void drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, unsigned long col);
void drawtext(DC *dc, const char *text, unsigned long col[ColLast]);
void drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]);
void eprintf(const char *fmt, ...);
+void freedraw(DC *dc);
unsigned long getcolor(DC *dc, const char *colstr);
+DC *initdraw(void);
void initfont(DC *dc, const char *fontstr);
-void setupdraw(DC *dc, Window w);
+void setcanvas(DC *dc, Window win, unsigned int w, unsigned int h);
int textnw(DC *dc, const char *text, size_t len);
int textw(DC *dc, const char *text);
+void weprintf(const char *fmt, ...);
/* variables */
extern const char *progname;
diff -r c6de8835d7d5 -r c599c675c202 drawbox.c
--- a/drawbox.c Sat Jul 31 14:54:11 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,11 +0,0 @@
-/* See LICENSE file for copyright and license details. */
-#include <X11/Xlib.h>
-#include <draw.h>
-
-void
-drawbox(DC *dc, unsigned long col[ColLast]) {
- XRectangle r = { dc->x, dc->y, dc->w, dc->h };
-
- XSetForeground(dc->dpy, dc->gc, BG(dc, col));
- XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1);
-}
diff -r c6de8835d7d5 -r c599c675c202 drawline.c
--- a/drawline.c Sat Jul 31 14:54:11 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,11 +0,0 @@
-#include <X11/Xlib.h>
-#include <draw.h>
-
-void
-drawline(DC *dc, int x, int y, unsigned int w, unsigned int h,
- unsigned long col[ColLast]) {
- XRectangle r = { dc->x + x, dc->y + y, w, h };
-
- XSetForeground(dc->dpy, dc->gc, FG(dc, col));
- XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1);
-}
diff -r c6de8835d7d5 -r c599c675c202 drawrect.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/drawrect.c Mon Aug 02 14:22:14 2010 +0100
@@ -0,0 +1,12 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include "draw.h"
+
+void
+drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, unsigned long col)
+{
+ XRectangle r = { dc->x + x, dc->y + y, w, h };
+
+ XSetForeground(dc->dpy, dc->gc, col);
+ XFillRectangles(dc->dpy, dc->canvas, dc->gc, &r, 1);
+}
diff -r c6de8835d7d5 -r c599c675c202 drawsquare.c
--- a/drawsquare.c Sat Jul 31 14:54:11 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-/* 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]) {
- int n;
- XRectangle r = { dc->x, dc->y, dc->w, dc->h };
-
- XSetForeground(dc->dpy, dc->gc, FG(dc, col));
- 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 c6de8835d7d5 -r c599c675c202 drawtext.c
--- a/drawtext.c Sat Jul 31 14:54:11 2010 +0100
+++ b/drawtext.c Mon Aug 02 14:22:14 2010 +0100
@@ -1,8 +1,24 @@
/* See LICENSE file for copyright and license details. */
#include <string.h>
-#include <draw.h>
+#include "draw.h"
+
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
void
drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
- drawtextn(dc, text, strlen(text), col);
+ char buf[256];
+ size_t n, mn;
+
+ /* shorten text if necessary */
+ n = strlen(text);
+ for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) > dc->w - dc->font.height/2; mn--)
+ if(mn == 0)
+ return;
+ memcpy(buf, text, mn);
+ if(mn < n)
+ for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.');
+
+ drawrect(dc, 0, 0, dc->w, dc->h, BG(dc, col));
+ drawtextn(dc, buf, mn, col);
}
diff -r c6de8835d7d5 -r c599c675c202 drawtextn.c
--- a/drawtextn.c Sat Jul 31 14:54:11 2010 +0100
+++ b/drawtextn.c Mon Aug 02 14:22:14 2010 +0100
@@ -1,31 +1,19 @@
/* 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))
+#include "draw.h"
void
drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) {
- char buf[256];
- int i, x, y, h;
- size_t mn;
+ int x, y;
- 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(mn = MIN(n, sizeof buf); mn && textnw(dc, text, mn) > dc->w - h; mn--);
- if(!mn)
- return;
- memcpy(buf, text, mn);
- if(mn < n)
- for(i = mn; i && i > mn - 3; buf[--i] = '.');
+ x = dc->x + dc->font.height/2;
+ y = dc->y + dc->font.ascent+1;
+
XSetForeground(dc->dpy, dc->gc, FG(dc, col));
if(dc->font.set)
- XmbDrawString(dc->dpy, dc->drawable, dc->font.set, dc->gc, x, y, buf, mn);
+ XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n);
else {
XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
- XDrawString(dc->dpy, dc->drawable, dc->gc, x, y, buf, mn);
+ XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n);
}
}
diff -r c6de8835d7d5 -r c599c675c202 eprintf.c
--- a/eprintf.c Sat Jul 31 14:54:11 2010 +0100
+++ b/eprintf.c Mon Aug 02 14:22:14 2010 +0100
@@ -2,7 +2,7 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
-#include <draw.h>
+#include "draw.h"
const char *progname;
diff -r c6de8835d7d5 -r c599c675c202 freedraw.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/freedraw.c Mon Aug 02 14:22:14 2010 +0100
@@ -0,0 +1,16 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdlib.h>
+#include <X11/Xlib.h>
+#include "draw.h"
+
+void
+freedraw(DC *dc) {
+ if(dc->font.set)
+ XFreeFontSet(dc->dpy, dc->font.set);
+ else
+ XFreeFont(dc->dpy, dc->font.xfont);
+ XFreePixmap(dc->dpy, dc->canvas);
+ XFreeGC(dc->dpy, dc->gc);
+ XCloseDisplay(dc->dpy);
+ free(dc);
+}
diff -r c6de8835d7d5 -r c599c675c202 getcolor.c
--- a/getcolor.c Sat Jul 31 14:54:11 2010 +0100
+++ b/getcolor.c Mon Aug 02 14:22:14 2010 +0100
@@ -1,6 +1,6 @@
/* See LICENSE file for copyright and license details. */
#include <X11/Xlib.h>
-#include <draw.h>
+#include "draw.h"
unsigned long
getcolor(DC *dc, const char *colstr) {
diff -r c6de8835d7d5 -r c599c675c202 initdraw.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/initdraw.c Mon Aug 02 14:22:14 2010 +0100
@@ -0,0 +1,19 @@
+/* See LICENSE file for copyright and license details. */
+#include <locale.h>
+#include <stdlib.h>
+#include <X11/Xlib.h>
+#include "draw.h"
+
+DC *
+initdraw(void) {
+ DC *dc;
+
+ if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
+ weprintf("no locale support\n");
+ if(!(dc = malloc(sizeof *dc)))
+ eprintf("cannot malloc %u bytes\n", sizeof *dc);
+ if(!(dc->dpy = XOpenDisplay(NULL)))
+ eprintf("cannot open display\n");
+ initfont(dc, NULL);
+ return dc;
+}
diff -r c6de8835d7d5 -r c599c675c202 initfont.c
--- a/initfont.c Sat Jul 31 14:54:11 2010 +0100
+++ b/initfont.c Mon Aug 02 14:22:14 2010 +0100
@@ -1,34 +1,36 @@
/* See LICENSE file for copyright and license details. */
#include <X11/Xlib.h>
-#include <draw.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)
- eprintf("cannot load null font\n");
- dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def);
- if(missing)
- XFreeStringList(missing);
+ dc->font.set = NULL;
+ if(fontstr && *fontstr) {
+ char *def, **missing;
+
+ dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def);
+ if(missing)
+ XFreeStringList(missing);
+ }
if(dc->font.set) {
+ char **names;
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++;
+
+ n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
+ for(i = dc->font.ascent = dc->font.descent = 0; i < n; i++) {
+ dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
+ dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
}
}
else {
- if(!(dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))
- && !(dc->font.xfont = XLoadQueryFont(dc->dpy, "fixed")))
- eprintf("cannot load font '%s'\n", fontstr);
+ if(fontstr)
+ weprintf("cannot load font '%s'\n", fontstr);
+ if(!(dc->font.xfont = XLoadQueryFont(dc->dpy, "fixed")))
+ eprintf("cannot load font 'fixed'\n");
dc->font.ascent = dc->font.xfont->ascent;
dc->font.descent = dc->font.xfont->descent;
}
diff -r c6de8835d7d5 -r c599c675c202 setcanvas.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/setcanvas.c Mon Aug 02 14:22:14 2010 +0100
@@ -0,0 +1,16 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include "draw.h"
+
+void
+setcanvas(DC *dc, Window win, unsigned int w, unsigned int h) {
+ dc->canvas = XCreatePixmap(dc->dpy, win, w, h,
+ DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
+ dc->gc = XCreateGC(dc->dpy, win, 0, NULL);
+ XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
+
+ dc->x = dc->y = 0;
+ dc->w = w;
+ dc->h = h;
+ dc->invert = False;
+}
diff -r c6de8835d7d5 -r c599c675c202 setupdraw.c
--- a/setupdraw.c Sat Jul 31 14:54:11 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,16 +0,0 @@
-/* See LICENSE file for copyright and license details. */
-#include <X11/Xlib.h>
-#include <draw.h>
-
-void
-setupdraw(DC *dc, Window w) {
- XWindowAttributes wa;
-
- if(!XGetWindowAttributes(dc->dpy, w, &wa))
- eprintf("cannot get window attributes");
- 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);
- dc->invert = False;
-}
diff -r c6de8835d7d5 -r c599c675c202 textnw.c
--- a/textnw.c Sat Jul 31 14:54:11 2010 +0100
+++ b/textnw.c Mon Aug 02 14:22:14 2010 +0100
@@ -1,6 +1,6 @@
/* See LICENSE file for copyright and license details. */
#include <X11/Xlib.h>
-#include <draw.h>
+#include "draw.h"
int
textnw(DC *dc, const char *text, size_t len) {
diff -r c6de8835d7d5 -r c599c675c202 textw.c
--- a/textw.c Sat Jul 31 14:54:11 2010 +0100
+++ b/textw.c Mon Aug 02 14:22:14 2010 +0100
@@ -1,7 +1,7 @@
/* See LICENSE file for copyright and license details. */
#include <string.h>
#include <X11/Xlib.h>
-#include <draw.h>
+#include "draw.h"
int
textw(DC *dc, const char *text) {
diff -r c6de8835d7d5 -r c599c675c202 weprintf.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/weprintf.c Mon Aug 02 14:22:14 2010 +0100
@@ -0,0 +1,16 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdarg.h>
+#include <stdio.h>
+#include "draw.h"
+
+const char *progname;
+
+void
+weprintf(const char *fmt, ...) {
+ va_list ap;
+
+ fprintf(stderr, "%s: warning: ", progname);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+}
Received on Mon Aug 02 2010 - 15:23:16 CEST
This archive was generated by hypermail 2.2.0 : Mon Aug 02 2010 - 15:24:04 CEST