git.y1.nz

fbui

Framebuffer-based graphical environment
download: https://git.y1.nz/archives/fbui.tar.gz
README | Files | Log | Refs

libfbui/ToDo/main.c

      1 
      2 /*=========================================================================
      3  *
      4  * fbtodo, a "To Do List" for FBUI (in-kernel framebuffer UI)
      5  * Copyright (C) 2004 Zachary T Smith, fbui@comcast.net
      6  *
      7  * This module is free software; you can redistribute it and/or modify
      8  * it under the terms of the GNU General Public License as published by
      9  * the Free Software Foundation; either version 2 of the License, or
     10  * (at your option) any later version.
     11  *
     12  * This module is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  * GNU General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU General Public License
     18  * along with this module; if not, write to the Free Software
     19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     20  *
     21  * (See the file COPYING in the main directory of this archive for
     22  * more details.)
     23  *
     24  *=======================================================================*/
     25 
     26 
     27 /* Changes
     28  *
     29  */
     30 
     31 #include <stdio.h>
     32 #include <sys/param.h>
     33 #include <time.h>
     34 #include <unistd.h>
     35 #include <stdlib.h>
     36 #include <sys/types.h>
     37 #include <sys/stat.h>
     38 #include <string.h>
     39 
     40 
     41 #include "libfbui.h"
     42 
     43 
     44 Font *titlefont, *listfont;
     45 short titlefontheight, listfontheight;
     46 
     47 
     48 short total_items;
     49 struct item {
     50 	char *s;
     51 	struct item *next;
     52 };
     53 struct item *items;
     54 
     55 struct item *item_new(char *s) 
     56 {
     57 	if (!s) return NULL;
     58 
     59 	struct item *nu = (struct item*)malloc(sizeof(struct item));
     60 	if (!nu)
     61 		FATAL("cannot allocate list item");
     62 
     63 	nu->next=NULL;
     64 	nu->s = strdup(s);
     65 	if (!nu->s)
     66 		FATAL("cannot allocate string for item");
     67 	return nu;
     68 }
     69 
     70 struct item *append_item (char *s) 
     71 {
     72 	struct item *nu = item_new (s);
     73 	if (nu) {
     74 		struct item *i = items;
     75 		if (!i) {
     76 			items = nu;
     77 		} else {
     78 			while (i->next)
     79 				i = i->next;
     80 			i->next = nu;
     81 		}
     82 		++total_items;
     83 	}
     84 	return nu;
     85 }
     86 
     87 int readline (FILE *f, char *line)
     88 {
     89 	int ix=0;
     90 	short ch;
     91 	while (EOF != (ch = fgetc (f))) {
     92 		if (ch == '\r')
     93 			continue;
     94 		if (ch == '\n')
     95 			break;
     96 		line[ix++] = ch;
     97 		if (ix==249) break;
     98 	}
     99 	line[ix]=0;
    100 	if (!ix && ch == EOF)
    101 		return -1;
    102 	else
    103 		return ix;
    104 }
    105 
    106 
    107 int readfile (char *path)
    108 {
    109 	short ch;
    110 	char line[250];
    111 	if (!path) 
    112 		return 0;
    113 	FILE *f = fopen (path, "r");
    114 	if (!f) 
    115 		return 0;
    116 
    117 	int count;
    118 	if (readline (f, line) < 1)
    119 		return 0;
    120 
    121 	count = atoi (line);
    122 
    123 	while(count--) {
    124 		if (-1 == readline (f, line))
    125 			break;
    126 
    127 		if (line[0])
    128 			append_item (line);
    129 	}
    130 
    131 	fclose (f);
    132 	return 1;
    133 }
    134 
    135 
    136 static short w = 200;
    137 static short h = 200;
    138 
    139 int
    140 main(int argc, char** argv)
    141 {
    142 	int i;
    143 	Display *dpy;
    144 	Window *win;
    145 	char path[MAXPATHLEN];
    146 	struct stat st;
    147 	FILE *f;
    148 	int highlight_which = 0;
    149 
    150 	total_items = 0;
    151 	items=NULL;
    152 
    153 	strcpy (path, getenv ("HOME"));
    154 	strcat (path, "/.todo");
    155 	i = stat (path, &st);
    156 	if (i) {
    157 		WARNING ("can't open ~/.todo; creating...");
    158 		f = fopen (path, "w");
    159 		fprintf (f, "4\n");
    160 		fprintf (f, "Get lunch\n");
    161 		fprintf (f, "GregPalast.org\n");
    162 		fprintf (f, "VoteCobb.org\n");
    163 		fprintf (f, "VotersUnite.org\n");
    164 		fclose (f);
    165 	}
    166 
    167 	f = fopen (path, "r");
    168 	if (!f)
    169 		FATAL ("can't open ~/.todo");
    170 	fclose (f);
    171 
    172 	if (!readfile (path))
    173 		FATAL ("error reading ~/.todo");
    174 
    175         titlefont = font_new ();
    176         listfont = font_new ();
    177 	if (!titlefont || !listfont)
    178 		FATAL ("cannot allocate Font");
    179 
    180         if (!pcf_read (titlefont, "helvR12.pcf"))
    181 		FATAL ("cannot read Helv bold 12 point");
    182         if (!pcf_read (listfont, "timR10.pcf"))
    183 		FATAL ("cannot read Times 12 point");
    184 
    185 	short indent,a,d;
    186 	font_string_dims (listfont, "88. ", &indent, &a, &d);
    187 
    188 	listfontheight = listfont->ascent + listfont->descent;
    189 	titlefontheight = titlefont->ascent + titlefont->descent;
    190 
    191 	unsigned long titlecolor = RGB_RED;
    192 	unsigned long listcolor = RGB_BROWN;
    193 	unsigned long titlefontcolor = RGB_YELLOW;
    194 	unsigned long listfontcolor = RGB_ORANGE;
    195 	unsigned long sepcolor = RGB_WHITE;
    196 	unsigned long listhighlightcolor = RGB_LTBROWN;
    197 
    198 	dpy = fbui_display_open ();
    199 	if (!dpy)
    200 		FATAL("cannot open display");
    201 
    202 	w = 150;
    203 	h = 150;
    204 	win = fbui_window_open (dpy, w,h, &w, &h, 9999,9999, 0, -1, 
    205 		&listfontcolor, 
    206 		&listcolor,
    207 		"fbtodo", "", 
    208 		FBUI_PROGTYPE_TOOL, false,false, -1, 
    209 		false, false,false, 
    210 		argc,argv);
    211 	if (!win) 
    212 		FATAL ("cannot create window");
    213 
    214 	short x, y;
    215 
    216 	char done=0;
    217 	while(!done) {
    218 		char needdraw=0;
    219 		char fulldraw=0;
    220 		Event ev;
    221 		int err;
    222 
    223 		if (err = fbui_wait_event (dpy, &ev, FBUI_EVENTMASK_ALL)) {
    224 			fbui_print_error (err);
    225 			continue;
    226 		}
    227 
    228 		int type = ev.type;
    229 		Window *win2 = ev.win;
    230 //printf ("fbtodo got event %s\n", fbui_get_event_name (ev.type));
    231 
    232 		if (win2 != win)
    233 			FATAL ("event's window is not ours");
    234 
    235 		switch(type) {
    236 		case FBUI_EVENT_MOTION: 
    237 			x = ev.x;
    238 			y = ev.y;
    239 			if (y >= (titlefontheight + 1)) {
    240 				int which = (y - titlefontheight - 1) / listfontheight;
    241 				if (which > total_items)
    242 					which = total_items;
    243 				if (which != highlight_which) {
    244 					highlight_which = which;
    245 					needdraw = 1;
    246 				}
    247 			}
    248 			break;
    249 	
    250 		case FBUI_EVENT_EXPOSE:
    251 			needdraw=1;
    252 			fulldraw=1;
    253 			break;
    254 
    255 		case FBUI_EVENT_ENTER:
    256 			break;
    257 
    258 		case FBUI_EVENT_BUTTON:
    259 			if (x >= w-10 && y < 10 && (ev.key & FBUI_BUTTON_LEFT)) {
    260 				done=1;
    261 				continue;
    262 			}
    263 			break;
    264 
    265 		case FBUI_EVENT_LEAVE:
    266 			if (highlight_which != -1) {
    267 				highlight_which = -1;
    268 				needdraw=1;
    269 			}
    270 			break;
    271 		case FBUI_EVENT_MOVE_RESIZE:
    272 			w = ev.width;
    273 			h = ev.height;
    274 			break;
    275 		}
    276 		
    277 		if (!needdraw) 
    278 			continue;
    279 
    280 		if (fulldraw) {
    281 			char title[200];
    282 			sprintf (title, "To do: %d item%s\n", total_items, 
    283 				total_items > 1 ? "s" : "");
    284 
    285 			short wid,a,d;
    286 			font_string_dims (titlefont, title, &wid,&a,&d);
    287 			short title_pos = (w - wid) / 2;
    288 
    289 			fbui_fill_area (dpy, win, 0, 0, w-1, titlefontheight, titlecolor);
    290 			fbui_draw_hline (dpy, win, 0, w-1, titlefontheight, sepcolor);
    291 			fbui_draw_string (dpy, win, titlefont, title_pos, 0, 
    292 				title, titlefontcolor);
    293 		}
    294 
    295 		fbui_fill_area (dpy, win, 0, titlefontheight+1, w-1, h, listcolor);
    296 
    297 #if 0
    298 		fbui_draw_line (dpy, win, w-10, 2, w-3, 9, RGB_WHITE);
    299 		fbui_draw_line (dpy, win, w-10, 9, w-3, 2, RGB_WHITE);
    300 #endif
    301 
    302 		struct item *im = items;
    303 		int ix=0;
    304 		while (im) {
    305 			char numstr[5];
    306 			sprintf (numstr, "%d.", ix+1);
    307 
    308 			short y = titlefontheight+1+ix*listfontheight;
    309 
    310 			if (ix == highlight_which) 
    311 				fbui_fill_area (dpy, win, 0, y, w, y + listfontheight,
    312 					listhighlightcolor);
    313 
    314 			fbui_draw_string (dpy, win, listfont, 
    315 				0, titlefontheight+1+ix*listfontheight, 
    316 				numstr, listfontcolor);
    317 	
    318 			fbui_draw_string (dpy, win, listfont, 
    319 				indent, y, im->s, listfontcolor);
    320 
    321 			im = im->next;
    322 			ix++;
    323 		}
    324 
    325 		fbui_flush (dpy, win);
    326 	}
    327 
    328 	fbui_window_close (dpy, win);
    329 	fbui_display_close (dpy);
    330 	return 0;
    331 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.