[hackers] [libdraw] added dc_ prefix || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Thu, 19 Aug 2010 15:43:33 +0000 (UTC)

changeset: 20:ea5665a88106
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Thu Aug 19 16:43:29 2010 +0100
files: Makefile color.c draw.h drawrect.c drawtext.c drawtextn.c font.c free.c freedraw.c getcolor.c init.c initdraw.c initfont.c map.c mapdraw.c resize.c resizedraw.c textnw.c textw.c
description:
added dc_ prefix

diff -r 80b8cded8a20 -r ea5665a88106 Makefile
--- a/Makefile Thu Aug 19 16:17:48 2010 +0100
+++ b/Makefile Thu Aug 19 16:43:29 2010 +0100
@@ -3,8 +3,8 @@
 
 include config.mk
 
-SRC = drawrect.c drawtext.c drawtextn.c eprintf.c freedraw.c getcolor.c \
- initdraw.c initfont.c mapdraw.c resizedraw.c textnw.c textw.c weprintf.c
+SRC = color.c drawrect.c drawtext.c drawtextn.c eprintf.c free.c init.c font.c \
+ map.c resize.c textnw.c textw.c weprintf.c
 
 OBJ = ${SRC:.c=.o}
 
diff -r 80b8cded8a20 -r ea5665a88106 color.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/color.c Thu Aug 19 16:43:29 2010 +0100
@@ -0,0 +1,13 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include "draw.h"
+
+unsigned long
+dc_color(DC *dc, const char *colstr) {
+ Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
+ XColor color;
+
+ if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
+ eprintf("cannot allocate color '%s'\n", colstr);
+ return color.pixel;
+}
diff -r 80b8cded8a20 -r ea5665a88106 draw.h
--- a/draw.h Thu Aug 19 16:17:48 2010 +0100
+++ b/draw.h Thu Aug 19 16:43:29 2010 +0100
@@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+
 #define FG(dc, col) ((col)[(dc)->invert ? ColBG : ColFG])
 #define BG(dc, col) ((col)[(dc)->invert ? ColFG : ColBG])
 
@@ -19,18 +20,18 @@
         } font;
 } DC; /* draw context */
 
-void drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color);
-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]);
+unsigned long dc_color(DC *dc, const char *colstr);
+void dc_drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color);
+void dc_drawtext(DC *dc, const char *text, unsigned long col[ColLast]);
+void dc_drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]);
+void dc_font(DC *dc, const char *fontstr);
+void dc_free(DC *dc);
+DC *dc_init(void);
+void dc_map(DC *dc, Window win, unsigned int w, unsigned int h);
+void dc_resize(DC *dc, unsigned int w, unsigned int h);
+int dc_textnw(DC *dc, const char *text, size_t len);
+int dc_textw(DC *dc, const char *text);
 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 mapdraw(DC *dc, Window win, unsigned int w, unsigned int h);
-void resizedraw(DC *dc, 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, ...);
 
 const char *progname;
diff -r 80b8cded8a20 -r ea5665a88106 drawrect.c
--- a/drawrect.c Thu Aug 19 16:17:48 2010 +0100
+++ b/drawrect.c Thu Aug 19 16:43:29 2010 +0100
@@ -3,7 +3,7 @@
 #include "draw.h"
 
 void
-drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
+dc_drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
         XRectangle r = { dc->x + x, dc->y + y, w, h };
 
         if(!fill) {
diff -r 80b8cded8a20 -r ea5665a88106 drawtext.c
--- a/drawtext.c Thu Aug 19 16:17:48 2010 +0100
+++ b/drawtext.c Thu Aug 19 16:43:29 2010 +0100
@@ -7,19 +7,19 @@
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 
 void
-drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
+dc_drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
         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--)
+ for(mn = MIN(n, sizeof buf); dc_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, True, BG(dc, col));
- drawtextn(dc, buf, mn, col);
+ dc_drawrect(dc, 0, 0, dc->w, dc->h, True, BG(dc, col));
+ dc_drawtextn(dc, buf, mn, col);
 }
diff -r 80b8cded8a20 -r ea5665a88106 drawtextn.c
--- a/drawtextn.c Thu Aug 19 16:17:48 2010 +0100
+++ b/drawtextn.c Thu Aug 19 16:43:29 2010 +0100
@@ -3,7 +3,7 @@
 #include "draw.h"
 
 void
-drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) {
+dc_drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) {
         int x, y;
 
         x = dc->x + dc->font.height/2;
diff -r 80b8cded8a20 -r ea5665a88106 font.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/font.c Thu Aug 19 16:43:29 2010 +0100
@@ -0,0 +1,45 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include "draw.h"
+
+#define DEFAULT "fixed"
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+
+static Bool loadfont(DC *dc, const char *fontstr);
+
+void
+dc_font(DC *dc, const char *fontstr) {
+ if(!loadfont(dc, fontstr ? fontstr : DEFAULT)) {
+ if(fontstr != NULL)
+ weprintf("cannot load font '%s'\n", fontstr);
+ if(fontstr == NULL || !loadfont(dc, DEFAULT))
+ eprintf("cannot load font '%s'\n", DEFAULT);
+ }
+ dc->font.height = dc->font.ascent + dc->font.descent;
+}
+
+Bool
+loadfont(DC *dc, const char *fontstr) {
+ if(!*fontstr)
+ return False;
+ if(XSupportsLocale()) {
+ char *def, **missing, **names;
+ int i, n;
+ XFontStruct **xfonts;
+
+ if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
+ 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);
+ }
+ }
+ if(missing)
+ XFreeStringList(missing);
+ }
+ else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
+ dc->font.ascent = dc->font.xfont->ascent;
+ dc->font.descent = dc->font.xfont->descent;
+ }
+ return (dc->font.set != NULL || dc->font.xfont != NULL);
+}
diff -r 80b8cded8a20 -r ea5665a88106 free.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/free.c Thu Aug 19 16:43:29 2010 +0100
@@ -0,0 +1,17 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdlib.h>
+#include <X11/Xlib.h>
+#include "draw.h"
+
+void
+dc_free(DC *dc) {
+ if(dc->font.set)
+ XFreeFontSet(dc->dpy, dc->font.set);
+ if(dc->font.xfont)
+ XFreeFont(dc->dpy, dc->font.xfont);
+ if(dc->canvas)
+ XFreePixmap(dc->dpy, dc->canvas);
+ XFreeGC(dc->dpy, dc->gc);
+ XCloseDisplay(dc->dpy);
+ free(dc);
+}
diff -r 80b8cded8a20 -r ea5665a88106 freedraw.c
--- a/freedraw.c Thu Aug 19 16:17:48 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-/* 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);
- if(dc->font.xfont)
- XFreeFont(dc->dpy, dc->font.xfont);
- if(dc->canvas)
- XFreePixmap(dc->dpy, dc->canvas);
- XFreeGC(dc->dpy, dc->gc);
- XCloseDisplay(dc->dpy);
- free(dc);
-}
diff -r 80b8cded8a20 -r ea5665a88106 getcolor.c
--- a/getcolor.c Thu Aug 19 16:17:48 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"
-
-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))
- eprintf("cannot allocate color '%s'\n", colstr);
- return color.pixel;
-}
diff -r 80b8cded8a20 -r ea5665a88106 init.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/init.c Thu Aug 19 16:43:29 2010 +0100
@@ -0,0 +1,24 @@
+/* See LICENSE file for copyright and license details. */
+#include <locale.h>
+#include <stdlib.h>
+#include <X11/Xlib.h>
+#include "draw.h"
+
+DC *
+dc_init(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");
+
+ dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL);
+ XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
+ dc->font.xfont = NULL;
+ dc->font.set = NULL;
+ dc->canvas = None;
+ return dc;
+}
diff -r 80b8cded8a20 -r ea5665a88106 initdraw.c
--- a/initdraw.c Thu Aug 19 16:17:48 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-/* 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");
-
- dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL);
- XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
- dc->font.xfont = NULL;
- dc->font.set = NULL;
- dc->canvas = None;
- return dc;
-}
diff -r 80b8cded8a20 -r ea5665a88106 initfont.c
--- a/initfont.c Thu Aug 19 16:17:48 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,45 +0,0 @@
-/* See LICENSE file for copyright and license details. */
-#include <X11/Xlib.h>
-#include "draw.h"
-
-#define DEFAULT "fixed"
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-
-static Bool loadfont(DC *dc, const char *fontstr);
-
-void
-initfont(DC *dc, const char *fontstr) {
- if(!loadfont(dc, fontstr ? fontstr : DEFAULT)) {
- if(fontstr != NULL)
- weprintf("cannot load font '%s'\n", fontstr);
- if(fontstr == NULL || !loadfont(dc, DEFAULT))
- eprintf("cannot load font '%s'\n", DEFAULT);
- }
- dc->font.height = dc->font.ascent + dc->font.descent;
-}
-
-Bool
-loadfont(DC *dc, const char *fontstr) {
- if(!*fontstr)
- return False;
- if(XSupportsLocale()) {
- char *def, **missing, **names;
- int i, n;
- XFontStruct **xfonts;
-
- if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
- 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);
- }
- }
- if(missing)
- XFreeStringList(missing);
- }
- else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
- dc->font.ascent = dc->font.xfont->ascent;
- dc->font.descent = dc->font.xfont->descent;
- }
- return (dc->font.set != NULL || dc->font.xfont != NULL);
-}
diff -r 80b8cded8a20 -r ea5665a88106 map.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/map.c Thu Aug 19 16:43:29 2010 +0100
@@ -0,0 +1,8 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include "draw.h"
+
+void
+dc_map(DC *dc, Window win, unsigned int w, unsigned int h) {
+ XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0);
+}
diff -r 80b8cded8a20 -r ea5665a88106 mapdraw.c
--- a/mapdraw.c Thu Aug 19 16:17:48 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-/* See LICENSE file for copyright and license details. */
-#include <X11/Xlib.h>
-#include "draw.h"
-
-void
-mapdraw(DC *dc, Window win, unsigned int w, unsigned int h) {
- XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0);
-}
diff -r 80b8cded8a20 -r ea5665a88106 resize.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/resize.c Thu Aug 19 16:43:29 2010 +0100
@@ -0,0 +1,15 @@
+/* See LICENSE file for copyright and license details. */
+#include <X11/Xlib.h>
+#include "draw.h"
+
+void
+dc_resize(DC *dc, unsigned int w, unsigned int h) {
+ if(dc->canvas)
+ XFreePixmap(dc->dpy, dc->canvas);
+ dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
+ DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
+ dc->x = dc->y = 0;
+ dc->w = w;
+ dc->h = h;
+ dc->invert = False;
+}
diff -r 80b8cded8a20 -r ea5665a88106 resizedraw.c
--- a/resizedraw.c Thu Aug 19 16:17:48 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-/* See LICENSE file for copyright and license details. */
-#include <X11/Xlib.h>
-#include "draw.h"
-
-void
-resizedraw(DC *dc, unsigned int w, unsigned int h) {
- if(dc->canvas)
- XFreePixmap(dc->dpy, dc->canvas);
- dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
- DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
- dc->x = dc->y = 0;
- dc->w = w;
- dc->h = h;
- dc->invert = False;
-}
diff -r 80b8cded8a20 -r ea5665a88106 textnw.c
--- a/textnw.c Thu Aug 19 16:17:48 2010 +0100
+++ b/textnw.c Thu Aug 19 16:43:29 2010 +0100
@@ -3,7 +3,7 @@
 #include "draw.h"
 
 int
-textnw(DC *dc, const char *text, size_t len) {
+dc_textnw(DC *dc, const char *text, size_t len) {
         if(dc->font.set) {
                 XRectangle r;
 
diff -r 80b8cded8a20 -r ea5665a88106 textw.c
--- a/textw.c Thu Aug 19 16:17:48 2010 +0100
+++ b/textw.c Thu Aug 19 16:43:29 2010 +0100
@@ -4,6 +4,6 @@
 #include "draw.h"
 
 int
-textw(DC *dc, const char *text) {
- return textnw(dc, text, strlen(text)) + dc->font.height;
+dc_textw(DC *dc, const char *text) {
+ return dc_textnw(dc, text, strlen(text)) + dc->font.height;
 }
Received on Thu Aug 19 2010 - 17:43:33 CEST

This archive was generated by hypermail 2.2.0 : Thu Aug 19 2010 - 17:48:04 CEST