a small clock for X11 that displays time as pixels

add -left, -right, -top, -bottom to set position change -width to -size so it makes sense with the position

jcs 53e85258 c3756ec9

+86 -32
+86 -32
pixelclock.c
··· 1 1 /* vim:ts=8 2 - * $Id: pixelclock.c,v 1.7 2008/11/26 16:51:48 jcs Exp $ 2 + * $Id: pixelclock.c,v 1.8 2009/03/09 06:35:26 jcs Exp $ 3 3 * 4 4 * pixelclock 5 5 * a different way of looking at time 6 6 * 7 - * Copyright (c) 2005,2008 joshua stein <jcs@jcs.org> 7 + * Copyright (c) 2005,2008-2009 joshua stein <jcs@jcs.org> 8 8 * Copyright (c) 2005 Federico G. Schwindt 9 9 * 10 10 * Redistribution and use in source and binary forms, with or without ··· 44 44 #include <X11/Xlib.h> 45 45 #include <X11/Xutil.h> 46 46 47 - /* default clock width */ 48 - #define DEFWIDTH 3 47 + /* default clock size */ 48 + #define DEFSIZE 3 49 + 50 + /* default position is along the right side */ 51 + #define DEFPOS 'r' 49 52 50 53 /* so our window manager knows us */ 51 54 char* win_name = "pixelclock"; ··· 58 61 int dpy_width, dpy_height; 59 62 int screen; 60 63 Window win; 61 - int width; 62 - int onleft; 64 + int size; 65 + char position; 63 66 GC gc; 64 67 Colormap win_colormap; 65 68 } x; 66 69 67 70 const struct option longopts[] = { 68 71 { "display", required_argument, NULL, 'd' }, 69 - { "width", required_argument, NULL, 'w' }, 72 + { "size", required_argument, NULL, 's' }, 70 73 { "left", no_argument, NULL, 'l' }, 74 + { "right", no_argument, NULL, 'r' }, 75 + { "top", no_argument, NULL, 't' }, 76 + { "bottom", no_argument, NULL, 'b' }, 71 77 72 78 { NULL, 0, NULL, 0 } 73 79 }; ··· 95 101 XEvent event; 96 102 97 103 bzero(&x, sizeof(struct xinfo)); 98 - x.width = DEFWIDTH; 99 - x.onleft = 0; 104 + x.size = DEFSIZE; 105 + x.position = NULL; 100 106 101 107 while ((c = getopt_long_only(argc, argv, "", longopts, NULL)) != -1) { 102 108 switch (c) { ··· 104 110 display = optarg; 105 111 break; 106 112 113 + case 'b': 114 + case 't': 107 115 case 'l': 108 - x.onleft = 1; 116 + case 'r': 117 + if (x.position) 118 + errx(1, "only one of -top, -bottom, -left, " 119 + "-right allowed"); 120 + /* NOTREACHED */ 121 + 122 + x.position = c; 109 123 break; 110 124 111 - case 'w': 112 - x.width = strtol(optarg, &p, 10); 113 - if (*p || x.width < 1) 125 + case 's': 126 + x.size = strtol(optarg, &p, 10); 127 + if (*p || x.size < 1) 114 128 errx(1, "illegal value -- %s", optarg); 115 129 /* NOTREACHED */ 116 130 break; ··· 120 134 /* NOTREACHED */ 121 135 } 122 136 } 137 + 138 + if (!x.position) 139 + x.position = DEFPOS; 123 140 124 141 argc -= optind; 125 142 argv += optind; ··· 145 162 /* parse times like 14:12 */ 146 163 h = atoi(p); 147 164 if ((p = strchr(p, ':')) == NULL) 148 - errx(1, "Invalid time %s", argv[i]); 165 + errx(1, "invalid time %s", argv[i]); 149 166 m = atoi(p + 1); 150 167 151 168 if (h > 23 || h < 0 || m > 59 || m < 0) ··· 161 178 signal(SIGTERM, handler); 162 179 163 180 /* each hour will be this many pixels away */ 164 - hourtick = x.dpy_height / 24; 181 + hourtick = ((x.position == 'b' || x.position == 't') ? x.dpy_width : 182 + x.dpy_height) / 24; 165 183 166 184 for (;;) { 167 185 if (gettimeofday(&tv[0], NULL)) ··· 187 205 188 206 /* draw the current time */ 189 207 XSetForeground(x.dpy, x.gc, getcolor("yellow")); 190 - XFillRectangle(x.dpy, x.win, x.gc, 191 - 0, newpos, 192 - x.width, 6); 208 + if (x.position == 'b' || x.position == 't') 209 + XFillRectangle(x.dpy, x.win, x.gc, 210 + newpos, 0, 6, x.size); 211 + else 212 + XFillRectangle(x.dpy, x.win, x.gc, 213 + 0, newpos, x.size, 6); 193 214 194 215 /* draw the hour ticks */ 195 216 XSetForeground(x.dpy, x.gc, getcolor("blue")); 196 - for (y = 1; y <= 23; y++) { 197 - XFillRectangle(x.dpy, x.win, x.gc, 198 - 0, (y * hourtick), 199 - x.width, 2); 200 - } 217 + for (y = 1; y <= 23; y++) 218 + if (x.position == 'b' || x.position == 't') 219 + XFillRectangle(x.dpy, x.win, x.gc, 220 + (y * hourtick), 0, 2, x.size); 221 + else 222 + XFillRectangle(x.dpy, x.win, x.gc, 223 + 0, (y * hourtick), x.size, 2); 201 224 202 225 /* highlight requested times */ 203 226 XSetForeground(x.dpy, x.gc, getcolor("green")); 204 - for (i = 0; i < nhihours; i++) { 205 - XFillRectangle(x.dpy, x.win, x.gc, 206 - 0, (hihours[i] * hourtick), 207 - x.width, 2); 208 - } 227 + for (i = 0; i < nhihours; i++) 228 + if (x.position == 'b' || x.position == 't') 229 + XFillRectangle(x.dpy, x.win, x.gc, 230 + (hihours[i] * hourtick), 0, 231 + 2, x.size); 232 + else 233 + XFillRectangle(x.dpy, x.win, x.gc, 234 + 0, (hihours[i] * hourtick), 235 + x.size, 2); 209 236 210 237 lastpos = newpos; 211 238 ··· 222 249 init_x(const char *display) 223 250 { 224 251 int rc; 252 + int left = 0, top = 0, width = 0, height = 0; 225 253 XGCValues values; 226 254 XSetWindowAttributes attributes; 227 255 XTextProperty win_name_prop; 228 256 229 257 if (!(x.dpy = XOpenDisplay(display))) 230 - errx(1, "Unable to open display %s", XDisplayName(display)); 258 + errx(1, "unable to open display %s", XDisplayName(display)); 231 259 /* NOTREACHED */ 232 260 233 261 x.screen = DefaultScreen(x.dpy); ··· 237 265 238 266 x.win_colormap = DefaultColormap(x.dpy, DefaultScreen(x.dpy)); 239 267 268 + switch (x.position) { 269 + case 'b': 270 + left = 0; 271 + height = x.size; 272 + top = x.dpy_height - height; 273 + width = x.dpy_width; 274 + break; 275 + case 't': 276 + left = 0; 277 + top = 0; 278 + height = x.size; 279 + width = x.dpy_width; 280 + break; 281 + case 'l': 282 + left = 0; 283 + top = 0; 284 + height = x.dpy_height; 285 + width = x.size; 286 + break; 287 + case 'r': 288 + width = x.size; 289 + left = x.dpy_width - width; 290 + top = 0; 291 + height = x.dpy_height; 292 + break; 293 + } 294 + 240 295 x.win = XCreateSimpleWindow(x.dpy, RootWindow(x.dpy, x.screen), 241 - (x.onleft ? 0 : x.dpy_width - x.width), 0, 242 - x.width, x.dpy_height, 296 + left, top, width, height, 243 297 0, 244 298 BlackPixel(x.dpy, x.screen), 245 299 BlackPixel(x.dpy, x.screen)); ··· 295 349 usage(void) 296 350 { 297 351 fprintf(stderr, "usage: %s %s\n", __progname, 298 - "[-display host:dpy] [-left] [-width <pixels>] " 352 + "[-display host:dpy] [-left|-right|-top|-bottom] [-size <pixels>] " 299 353 "[time time2 ...]"); 300 354 exit(1); 301 355 }