git.y1.nz

fbui

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

libfbui/Clock/main.c

      1 
      2 /*=========================================================================
      3  *
      4  * fbclock, an analog clock 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  * Sept 26, 2004: responds to geometry changes.
     30  * Sept 30, 2004: lib changes.
     31  */
     32 
     33 #include <stdio.h>
     34 #include <time.h>
     35 #include <unistd.h>
     36 #include <math.h>
     37 
     38 #include "libfbui.h"
     39 
     40 
     41 static short w = 200;
     42 static short h = 200;
     43 
     44 int getpos (int deg, int *xp, int *yp, int border)
     45 {
     46 	double radians;
     47 	double x,y,xradius,yradius;
     48 
     49 	radians = deg;
     50 	radians /= 360.0;
     51 	radians *= 2.0 * 3.14159265;
     52 
     53 	xradius = w / 2 - border;
     54 	yradius = h / 2 - border;
     55 	x = sin (radians) * xradius;
     56 	y = cos (radians) * yradius;
     57 	x += w/2;
     58 	y = h/2 - y;
     59 
     60 	*xp = x;
     61 	*yp = y;
     62 }
     63 
     64 int
     65 main(int argc, char** argv)
     66 {
     67 	int i;
     68 	Display *dpy;
     69 	Window *win;
     70 
     71 	unsigned long markercolor = 0x00ff00;
     72 	unsigned long centercolor = 0xffff00;
     73 	unsigned long handcolor = 0x00ffff;
     74 	unsigned long gemcolor = 0xffffff;
     75 	unsigned long bgcolor = 0x2000;
     76 
     77 	dpy = fbui_display_open ();
     78 	if (!dpy)
     79 		FATAL("cannot open display");
     80 
     81 	w = 120;
     82 	h = 120;
     83 	win = fbui_window_open (dpy, w,h, &w, &h, 9999,9999, 0, -1, 
     84 		&handcolor, 
     85 		&bgcolor,
     86 		"fbclock", "", 
     87 		FBUI_PROGTYPE_TOOL, false, false, -1, false,false,false, argc,argv);
     88 	if (!win) 
     89 		FATAL ("cannot create window");
     90 
     91 printf ("our window is id %d\n", win->id);
     92 
     93 	time_t t0 = time(0);
     94 	while(1) {
     95 		time_t t;
     96 		int need=0, mustclear=0;
     97 		int size;
     98 		usleep (500000);
     99 
    100 		t = time(0);
    101 		int tdiff = t - t0;
    102 		if (tdiff >= 1) {
    103 			need=1;
    104 			mustclear=1;
    105 			t0 = t;
    106 		}
    107 
    108 		if (!need) {
    109 			Event ev;
    110 			Window *win2;
    111 			int err;
    112 
    113 			if (err = fbui_poll_event (dpy, &ev,
    114 			     		     FBUI_EVENTMASK_ALL & 
    115 					     ~(FBUI_EVENTMASK_KEY | FBUI_EVENTMASK_MOTION)))
    116 			{
    117 				if (err != FBUI_ERR_NOEVENT)
    118 					fbui_print_error (err);
    119 				continue;
    120 			}
    121 
    122 			int num= ev.type;
    123 printf ("%s got event %s\n", argv[0], fbui_get_event_name (ev.type));
    124 
    125 			win2 = ev.win;
    126 			if (win2 != win) {
    127 printf ("ev.win=%08lx\n", (unsigned long)win2);
    128 				FATAL ("event's window is not ours");
    129 			}
    130 
    131 			if (num == FBUI_EVENT_EXPOSE)
    132 				need=1;
    133 			else
    134 			if (num == FBUI_EVENT_ENTER) {
    135 				printf ("fbclock got Enter\n");
    136 				continue;
    137 			}
    138 			else
    139 			if (num == FBUI_EVENT_LEAVE) {
    140 				printf ("fbclock got Leave\n");
    141 				continue;
    142 			}
    143 			else
    144 			if (num == FBUI_EVENT_MOVE_RESIZE) {
    145 				w = ev.width;
    146 				h = ev.height;
    147 				need=1;
    148 			}
    149 		}
    150 
    151 		if (!need) 
    152 			continue;
    153 
    154 		struct tm *tmp = localtime (&t);
    155 		int hour = tmp->tm_hour;
    156 		int minute = tmp->tm_min;
    157 		int second = tmp->tm_sec;
    158 
    159 		if (mustclear)
    160 			fbui_clear (dpy, win);
    161 
    162 		int shorter_side = w < h ? w : h;
    163 
    164 		int deg;
    165 		for (deg=0; deg < 360; deg += 6) {
    166 			int x,y;
    167 			getpos (deg, &x,&y, 5);
    168 			
    169 			int size=0;
    170 			if ((deg % 90) == 0)
    171 				size=2;
    172 			else
    173 			if ((deg % 30) == 0)
    174 				size=1;
    175 			fbui_fill_area (dpy, win, x-size, y-size, x+size, y+size, markercolor);
    176 		}
    177 
    178 		int x,y;
    179 		float p = minute;
    180 		p /= 60.0;
    181 		p *= 30;
    182 		getpos ((hour%12)*30 + (int)p, &x,&y, shorter_side/4);
    183 		fbui_draw_line (dpy, win, w/2,h/2,x,y, handcolor);
    184 		fbui_draw_line (dpy, win, w/2+1,h/2,x+1,y, handcolor);
    185 		fbui_draw_line (dpy, win, w/2,h/2+1,x+1,y+1, handcolor);
    186 		
    187 		getpos (minute*6, &x,&y, 15);
    188 		fbui_draw_line (dpy, win, w/2,h/2,x,y, handcolor);
    189 		
    190 		getpos (second*6, &x,&y, 10);
    191 		fbui_draw_point (dpy, win, x,y-2, gemcolor);
    192 		fbui_draw_hline (dpy, win, x-1,x+1,y-1, gemcolor);
    193 		fbui_draw_hline (dpy, win, x-2,x+2,y, gemcolor);
    194 		fbui_draw_hline (dpy, win, x-1,x+1,y+1, gemcolor);
    195 		fbui_draw_point (dpy, win, x,y+2, gemcolor);
    196 		
    197 		size=3;
    198 		fbui_fill_area (dpy, win, w/2-size, h/2-size, w/2+size, h/2+size, centercolor);
    199 
    200 		fbui_flush (dpy, win);
    201 	}
    202 
    203 	fbui_window_close(dpy, win);
    204 	fbui_display_close(dpy);
    205 	return 0;
    206 }

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