git.y1.nz

fbui

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

libfbui/Launcher/main.c

      1 
      2 /*=========================================================================
      3  *
      4  * fblauncher, a launcher 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 #define VERSION "0.1"
     45 
     46 
     47 Font *titlefont, *listfont;
     48 short titlefontheight, listfontheight;
     49 
     50 
     51 short total_items;
     52 typedef struct item {
     53 	char *s1, *s2;
     54 	struct item *next;
     55 } Item;
     56 
     57 Item *items;
     58 
     59 Item *Item_new(char *s1, char *s2) 
     60 {
     61 	if (!s1 || !s2) return NULL;
     62 
     63 	struct item *nu = (struct item*)malloc(sizeof(struct item));
     64 	if (!nu)
     65 		FATAL("cannot allocate list item");
     66 
     67 	nu->next=NULL;
     68 	nu->s1 = strdup(s1);
     69 	nu->s2 = strdup(s2);
     70 	if (!nu->s1 || !nu->s2)
     71 		FATAL("cannot allocate string for item");
     72 	return nu;
     73 }
     74 
     75 Item *Item_append_pair (char *s1, char *s2) 
     76 {
     77 	struct item *nu = Item_new (s1,s2);
     78 	if (nu) {
     79 		struct item *i = items;
     80 		if (!i) {
     81 			items = nu;
     82 		} else {
     83 			while (i->next)
     84 				i = i->next;
     85 			i->next = nu;
     86 		}
     87 		++total_items;
     88 	}
     89 	return nu;
     90 }
     91 
     92 int readline (FILE *f, char *line)
     93 {
     94 	int ix=0;
     95 	short ch;
     96 	while (EOF != (ch = fgetc (f))) {
     97 		if (ch == '\r')
     98 			continue;
     99 		if (ch == '\n')
    100 			break;
    101 		line[ix++] = ch;
    102 		if (ix==249) break;
    103 	}
    104 	line[ix]=0;
    105 	if (!ix && ch == EOF)
    106 		return -1;
    107 	else
    108 		return ix;
    109 }
    110 
    111 
    112 int readfile (char *path)
    113 {
    114 	short ch;
    115 	char line[250];
    116 	if (!path) 
    117 		return 0;
    118 	FILE *f = fopen (path, "r");
    119 	if (!f) 
    120 		return 0;
    121 
    122 	int count;
    123 	if (readline (f, line) < 1)
    124 		return 0;
    125 
    126 	count = atoi (line);
    127 
    128 	while(count--) {
    129 		if (-1 == readline (f, line))
    130 			break;
    131 
    132 		char *s = strchr (line, ' ');
    133 		if (s) {
    134 			*s = 0;
    135 			s++;
    136 
    137 			if (line[0])
    138 				Item_append_pair (line, s);
    139 		}
    140 	}
    141 
    142 	fclose (f);
    143 	return 1;
    144 }
    145 
    146 char *Item_lookup_n (int n)
    147 {
    148 	Item *im = items;
    149 
    150 	while (im && n--)
    151 		im = im->next;
    152 
    153 	return im? im->s2 : NULL;
    154 }
    155 
    156 
    157 static short w = 200;
    158 static short h = 200;
    159 
    160 int
    161 main(int argc, char** argv)
    162 {
    163 	int i;
    164 	Display *dpy;
    165 	Window *win;
    166 	char path[MAXPATHLEN];
    167 	struct stat st;
    168 	FILE *f;
    169 	int highlight_which = 0;
    170 
    171 	total_items = 0;
    172 	items=NULL;
    173 
    174 	strcpy (path, getenv ("HOME"));
    175 	strcat (path, "/.launcher");
    176 	i = stat (path, &st);
    177 	if (i) {
    178 		WARNING ("can't open ~/.launcher; creating...");
    179 		f = fopen (path, "w");
    180 		fprintf (f, "3\n");
    181 		fprintf (f, "Terminal fbterm\n");
    182 		fprintf (f, "Calculator fbcalc\n");
    183 		fprintf (f, "Scribble fbscribble\n");
    184 		fclose (f);
    185 	}
    186 
    187 	f = fopen (path, "r");
    188 	if (!f)
    189 		FATAL ("can't open ~/.launcher");
    190 	fclose (f);
    191 
    192 	if (!readfile (path))
    193 		FATAL ("error reading ~/.launcher");
    194 
    195         titlefont = font_new ();
    196         listfont = font_new ();
    197 	if (!titlefont || !listfont)
    198 		FATAL ("cannot allocate Font");
    199 
    200         if (!pcf_read (titlefont, "helvR12.pcf"))
    201 		FATAL ("cannot read Helv bold 12 point");
    202         if (!pcf_read (listfont, "timR10.pcf"))
    203 		FATAL ("cannot read Times 12 point");
    204 
    205 	short indent,a,d;
    206 	font_string_dims (listfont, "88. ", &indent, &a, &d);
    207 
    208 	listfontheight = listfont->ascent + listfont->descent;
    209 	titlefontheight = titlefont->ascent + titlefont->descent;
    210 
    211 	unsigned long titlecolor = RGB_RED;
    212 	unsigned long listcolor = RGB_BROWN;
    213 	unsigned long titlefontcolor = RGB_YELLOW;
    214 	unsigned long listfontcolor = RGB_ORANGE;
    215 	unsigned long sepcolor = RGB_WHITE;
    216 	unsigned long listhighlightcolor = RGB_LTBROWN;
    217 
    218 	dpy = fbui_display_open ();
    219 	if (!dpy)
    220 		FATAL("cannot open display");
    221 
    222 	w = 150;
    223 	h = 150;
    224 	win = fbui_window_open (dpy, w,h, &w, &h, 9999,9999, 0, -50, 
    225 		&listfontcolor, 
    226 		&listcolor,
    227 		"fblauncher", VERSION, 
    228 		FBUI_PROGTYPE_EPHEMERAL, 
    229 		false,false, 
    230 		-1, 
    231                 true, // need keys
    232 		false,
    233 		false, 
    234 		argc,argv);
    235 	if (!win) 
    236 		FATAL ("cannot create window");
    237 
    238 	short x=-1, y=-1;
    239 	short whichlaunch = -1;
    240 
    241 	char done=0;
    242 	while(!done) {
    243 		char needdraw=0;
    244 		char fulldraw=0;
    245 		Event ev;
    246 		int err;
    247 
    248 		if (err = fbui_wait_event (dpy, &ev, FBUI_EVENTMASK_ALL)) {
    249 			fbui_print_error (err);
    250 			continue;
    251 		}
    252 
    253 		int type = ev.type;
    254 		Window *win2 = ev.win;
    255 
    256 		if (win2 != win)
    257 			FATAL ("event's window is not ours");
    258 
    259 		switch(type) {
    260 		case FBUI_EVENT_MOTION: 
    261 			x = ev.x;
    262 			y = ev.y;
    263 			if (y >= (titlefontheight + 1)) {
    264 				int which = (y - titlefontheight - 1) / listfontheight;
    265 				if (which > total_items)
    266 					which = total_items;
    267 				if (which != highlight_which) {
    268 					highlight_which = which;
    269 					needdraw = 1;
    270 				}
    271 			}
    272 			break;
    273 	
    274 		case FBUI_EVENT_EXPOSE:
    275 			needdraw=1;
    276 			fulldraw=1;
    277 			break;
    278 
    279 		case FBUI_EVENT_ENTER:
    280 			break;
    281 
    282 		case FBUI_EVENT_BUTTON:
    283 			if (y < titlefontheight) {
    284 				if (x >= (w - 10))
    285 					done=true;
    286 				continue;
    287 			}
    288 
    289 			if ((ev.key & FBUI_BUTTON_LEFT) && (ev.key & 1)) {
    290 				whichlaunch = (y - titlefontheight) / listfontheight;
    291 				done=true;
    292 				continue;
    293 			}
    294 			break;
    295 
    296 		case FBUI_EVENT_LEAVE:
    297 			if (highlight_which != -1) {
    298 				highlight_which = -1;
    299 				needdraw=1;
    300 			}
    301 			break;
    302 		case FBUI_EVENT_MOVE_RESIZE:
    303 			w = ev.width;
    304 			h = ev.height;
    305 			break;
    306 		}
    307 		
    308 		if (!needdraw) 
    309 			continue;
    310 
    311 		if (fulldraw) {
    312 			char title[200];
    313 			sprintf (title, "Launcher %s",VERSION);
    314 
    315 			short wid,a,d;
    316 			font_string_dims (titlefont, title, &wid,&a,&d);
    317 			short title_pos = (w - wid) / 2;
    318 
    319 			fbui_fill_area (dpy, win, 0, 0, w-1, titlefontheight, titlecolor);
    320 			fbui_draw_hline (dpy, win, 0, w-1, titlefontheight, sepcolor);
    321 			fbui_draw_string (dpy, win, titlefont, title_pos, 0, 
    322 				title, titlefontcolor);
    323 		}
    324 
    325 		fbui_fill_area (dpy, win, 0, titlefontheight+1, w-1, h, listcolor);
    326 
    327 		fbui_draw_line (dpy, win, w-10, 2, w-3, 9, RGB_WHITE);
    328 		fbui_draw_line (dpy, win, w-10, 9, w-3, 2, RGB_WHITE);
    329 
    330 		struct item *im = items;
    331 		int ix=0;
    332 		while (im) {
    333 			char numstr[5];
    334 			sprintf (numstr, "%d.", ix+1);
    335 
    336 			short y = titlefontheight+1+ix*listfontheight;
    337 
    338 			if (ix == highlight_which) 
    339 				fbui_fill_area (dpy, win, 0, y, w, y + listfontheight,
    340 					listhighlightcolor);
    341 
    342 			fbui_draw_string (dpy, win, listfont, 
    343 				0, titlefontheight+1+ix*listfontheight, 
    344 				numstr, listfontcolor);
    345 	
    346 			fbui_draw_string (dpy, win, listfont, 
    347 				indent, y, im->s1, listfontcolor);
    348 
    349 			im = im->next;
    350 			ix++;
    351 		}
    352 
    353 		fbui_flush (dpy, win);
    354 	}
    355 
    356 	fbui_window_close (dpy, win);
    357 	fbui_display_close (dpy);
    358 
    359 	if (-1 != whichlaunch) {
    360 		char *launch = Item_lookup_n (whichlaunch);
    361 		if (launch)
    362 			system (launch);
    363 	}
    364 
    365 	return 0;
    366 }

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