dslinux/user/pixil/apps/nanox/misc Makefile nxclock.c nxclock.h nxloadmon.c nxterm.c nxterm.h vertex.c vertex.h

amadeus dslinux_amadeus at user.in-berlin.de
Tue Oct 3 13:42:56 CEST 2006


Update of /cvsroot/dslinux/dslinux/user/pixil/apps/nanox/misc
In directory antilope:/tmp/cvs-serv22795/user/pixil/apps/nanox/misc

Added Files:
	Makefile nxclock.c nxclock.h nxloadmon.c nxterm.c nxterm.h 
	vertex.c vertex.h 
Log Message:
Pixil: add some forgotten files

--- NEW FILE: vertex.h ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */


#ifndef vertex_h
#define vertex_h
#endif

#include "nano-X.h"

typedef short COORD_T;

struct matrix
{
    double a, b, c, d, x, y;
};

void push_matrix();
void pop_matrix();
void mult_matrix(double a, double b, double c, double d, double x, double y);
void scale_xy(double x, double y);
void scale_x(double x);
void translate(double x, double y);
void rotate(double d);
void begin_points();
void begin_line();
void begin_loop();
void begin_polygon();
double transform_x(double x, double y);
double transform_y(double x, double y);
double transform_dx(double x, double y);
double transform_dy(double x, double y);
void transformed_double_vertex(double xf, double yf);
void vertex(double x, double y);
void end_points(GR_WINDOW_ID pmap, GR_GC_ID gc);
void end_line(GR_WINDOW_ID pmap, GR_GC_ID gc);
void end_loop(GR_WINDOW_ID pmap, GR_GC_ID gc);
void end_polygon(GR_WINDOW_ID pmap, GR_GC_ID gc);
void begin_complex_polygon();
void fgap();
void end_complex_polygon(GR_WINDOW_ID pmap, GR_GC_ID gc);
void circle(double x, double y, double r, GR_WINDOW_ID pmap, GR_GC_ID gc,
	    GR_SIZE w, GR_SIZE h);

--- NEW FILE: nxclock.h ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */


#ifndef nxclock_h
#define nxclock_h
#endif

#include "nano-X.h"

#define ROUND_CLOCK 	1

#define BLACK		MWRGB( 0  , 0  , 0   )
#define GRAY		MWRGB( 128, 128, 128 )
#define DKGRAY		MWRGB( 32 , 32 , 32  )
#define LTGRAY		MWRGB( 192, 192, 192 )
#define WHITE		MWRGB( 255, 255, 255 )

#define M_PI            3.14159265358979323846

typedef unsigned char uchar;

struct clock_time
{
    int hour_;
    int minute_;
    int second_;
};

--- NEW FILE: nxterm.c ---
/*                                                                       
  * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
  *                                                                       
  * This file is part of the PIXIL Operating Environment                 
  *                                                                       
  * The use, copying and distribution of this file is governed by one    
  * of two licenses, the PIXIL Commercial License, or the GNU General    
  * Public License, version 2.                                           
  *                                                                       
  * Licensees holding a valid PIXIL Commercial License may use this file 
  * in accordance with the PIXIL Commercial License Agreement provided   
  * with the Software. Others are governed under the terms of the GNU   
  * General Public License version 2.                                    
  *                                                                       
  * This file may be distributed and/or modified under the terms of the  
  * GNU General Public License version 2 as published by the Free        
  * Software Foundation and appearing in the file LICENSE.GPL included   
  * in the packaging of this file.                                      
  *                                                                       
[...1087 lines suppressed...]
	close(STDOUT_FILENO);
	close(tfd);

	setsid();
	pty_name[5] = 't';
	if ((tfd = open(pty_name, O_RDWR)) < 0) {
	    fprintf(stderr, "Child: Can't open pty %s [%s]\n", pty_name,
		    strerror(errno));
	    exit(1);
	}
	close(STDERR_FILENO);
	dup2(tfd, STDIN_FILENO);
	dup2(tfd, STDOUT_FILENO);
	dup2(tfd, STDERR_FILENO);
	execv(nargv[0], nargv);
	exit(1);
    }
    return tfd;
}
#endif /* !FreeBSD */

--- NEW FILE: nxclock.c ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */



#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include "vertex.h"
#include "nxclock.h"
#define MWINCLULDECOLORS
#include <nano-X.h>
#include <wm/nxlib.h>

#define DEF_STYLE	(GR_WM_PROPS_APPWINDOW |\
			 GR_WM_PROPS_NOAUTORESIZE |\
			 GR_WM_PROPS_NOAUTOMOVE)
#define DEF_GEOMETRY	"100x100-0+0"
#define DEF_TITLE	"Clock"

/* application initialization data*/
static nxARGS args[] = {
    nxTITLE(DEF_TITLE),
    nxGEOMETRY(DEF_GEOMETRY),
    nxBACKGROUND(GR_COLOR_WINDOW),
    nxSTYLE(DEF_STYLE),
    nxEND
};

GR_SIZE width;
GR_SIZE height;

static struct clock_time newtime = { 0, 0, 0 };

const float hourhand[4][2] = { {-0.5f, 0}, {0, 1.5f}, {0.5f, 0}, {0, -7.0f} };
const float minhand[4][2] = { {-0.5f, 0}, {0, 1.5f}, {0.5f, 0}, {0, -11.5f} };
const float sechand[4][2] = { {-0.1f, 0}, {0, 2.0f}, {0.1f, 0}, {0, -11.5f} };

static void
drawhand(double ang, const float v[][2], GR_COLOR fill,
	 GR_COLOR line, GR_WINDOW_ID pmap, GR_GC_ID gc)
{
    int i;

    push_matrix();
    rotate(ang);

    GrSetGCForeground(gc, fill);
    begin_polygon();
    for (i = 0; i < 4; i++)
	vertex(v[i][0], v[i][1]);
    end_polygon(pmap, gc);
    GrSetGCForeground(gc, line);
    begin_loop();
    for (i = 0; i < 4; i++)
	vertex(v[i][0], v[i][1]);
    end_loop(pmap, gc);
    pop_matrix();
}

static uchar
type()
{
    return ROUND_CLOCK;
}

void
draw_clock_hands(GR_COLOR fill, GR_COLOR line, GR_WINDOW_ID pmap, GR_GC_ID gc)
{

    drawhand(-360 * (newtime.hour_ + newtime.minute_ / 60.0) / 12, hourhand,
	     fill, line, pmap, gc);
    drawhand(-360 * (newtime.minute_ + newtime.second_ / 60.0) / 60, minhand,
	     fill, line, pmap, gc);
    drawhand(-360 * (newtime.second_ / 60.0), sechand, fill, line, pmap, gc);
}


static void
rect(double x, double y, double w, double h, GR_WINDOW_ID pmap, GR_GC_ID gc)
{
    double r = x + w;
    double t = y + h;
    begin_polygon();
    vertex(x, y);
    vertex(r, y);
    vertex(r, t);
    vertex(x, t);
    end_polygon(pmap, gc);
}

void
reset_values(int h, int m, int s)
{
    if (h != newtime.hour_ || m != newtime.minute_ || s != newtime.second_) {
	newtime.hour_ = h;
	newtime.minute_ = m;
	newtime.second_ = s;
    }
}

static void
tick()
{
    struct timeval t;
    struct tm *timeofday;

    gettimeofday(&t, 0);
    timeofday = localtime((const time_t *) &t.tv_sec);
    reset_values(timeofday->tm_hour, timeofday->tm_min, timeofday->tm_sec);
}

void
draw_clock(int x, int y, int w, int h, GR_WINDOW_ID pmap, GR_GC_ID gc,
	   GR_WINDOW_ID window)
{
    int i;

    GrSetGCForeground(gc, GrGetSysColor(GR_COLOR_WINDOW));
    GrFillRect(pmap, gc, 0, 0, w, h);

    tick();
    push_matrix();
    translate(x + w / 2.0 - .5, y + h / 2.0 - .5);
    scale_xy((w - 1) / 28.0, (h - 1) / 28.0);
    if (type() == ROUND_CLOCK) {
	GrSetGCForeground(gc, BLACK);
	begin_polygon();
	circle(0, 0, 14, pmap, gc, w, h);
	end_polygon(pmap, gc);
	GrSetGCForeground(gc, BLACK);
	begin_loop();
	circle(0, 0, 14, pmap, gc, w, h);
	end_loop(pmap, gc);
    }
    //draw the shadows
    push_matrix();
    translate(0.60, 0.60);
    draw_clock_hands(LTGRAY, LTGRAY, pmap, gc);
    pop_matrix();
    //draw the tick marks
    push_matrix();
    GrSetGCForeground(gc, BLACK);
    for (i = 0; i < 12; i++) {
	if (6 == i)
	    rect(-0.5, 9, 1, 2, pmap, gc);
	else if (3 == i || 0 == i || 9 == i)
	    rect(-0.5, 9.5, 1, 1, pmap, gc);
	else
	    rect(-0.25, 9.5, .5, 1, pmap, gc);
	rotate(-30);
    }
    pop_matrix();
    //draw the hands
    draw_clock_hands(GRAY, BLACK, pmap, gc);
    pop_matrix();
    GrCopyArea(window, gc, 0, 0, w, h, pmap, 0, 0, MWROP_SRCCOPY);
}

GR_WINDOW_ID
resize(GR_SIZE w, GR_SIZE h, GR_WINDOW_ID pmap)
{
    width = w;
    height = h;
    if (pmap)
	GrDestroyWindow(pmap);
    return GrNewPixmap(w, h, NULL);
}

int
main(int ac, char **av)
{
    GR_EVENT event;
    GR_GC_ID gc;
    GR_WINDOW_ID pmap;
    GR_WINDOW_ID window;
    GR_WINDOW_INFO info;

    if (GrOpen() < 0) {
	fprintf(stderr, "cannot open graphics\n");
	exit(1);
    }

    window = nxCreateAppWindow(&ac, &av, args);
    gc = GrNewGC();
    GrSetGCForeground(gc, GrGetSysColor(GR_COLOR_WINDOW));
    GrSetGCBackground(gc, GrGetSysColor(GR_COLOR_WINDOWTEXT));

    GrSelectEvents(window, GR_EVENT_MASK_EXPOSURE |
		   GR_EVENT_MASK_UPDATE | GR_EVENT_MASK_CLOSE_REQ);

    GrGetWindowInfo(window, &info);
    pmap = resize(info.width, info.height, 0);

    GrMapWindow(window);

    while (1) {
	GrGetNextEventTimeout(&event, 500L);

	switch (event.type) {
	case GR_EVENT_TYPE_EXPOSURE:
	case GR_EVENT_TYPE_TIMEOUT:
	    draw_clock(0, 0, width, height, pmap, gc, window);
	    break;

	case GR_EVENT_TYPE_CLOSE_REQ:
	    GrClose();
	    exit(0);

	case GR_EVENT_TYPE_UPDATE:
	    switch (event.update.utype) {
	    case GR_UPDATE_SIZE:
		pmap = resize(event.update.width, event.update.height, pmap);
		break;
	    }
	}
    }
    return 0;
}

--- NEW FILE: vertex.c ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "vertex.h"
#define MWINCLULDECOLORS
#include <nano-X.h>
#include <wm/nxlib.h>

static struct matrix m = { 1, 0, 0, 1, 0, 0 };
static struct matrix stack[10];
static int sptr = 0;
static GR_POINT *p = (GR_POINT *) 0;
static int gap;
static int p_size;
static int n;
static int what;

enum
{ LINE, LOOP, POLYGON, POINT_ };

void
push_matrix()
{
    stack[sptr++] = m;
}

void
pop_matrix()
{
    m = stack[--sptr];
}

void
mult_matrix(double a, double b, double c, double d, double x, double y)
{
    struct matrix o;
    o.a = a * m.a + b * m.c;
    o.b = a * m.b + b * m.d;
    o.c = c * m.a + d * m.c;
    o.d = c * m.b + d * m.d;
    o.x = x * m.a + y * m.c + m.x;
    o.y = x * m.b + y * m.d + m.y;
    m = o;
}

void
scale_xy(double x, double y)
{
    mult_matrix(x, 0, 0, y, 0, 0);
}

void
scale_x(double x)
{
    mult_matrix(x, 0, 0, x, 0, 0);
}

void
translate(double x, double y)
{
    mult_matrix(1, 0, 0, 1, x, y);
}

void
rotate(double d)
{
    if (d) {
	double s, c;
	if (0 == d) {
	    s = 0;
	    c = 1;
	} else if (90 == d) {
	    s = 1;
	    c = 0;
	} else if (180 == d) {
	    s = 0;
	    c = -1;
	} else if (270 == d || -90 == d) {
	    s = -1;
	    c = 0;
	} else {
	    s = sin(d * M_PI / 180);
	    c = cos(d * M_PI / 180);
	}
	mult_matrix(c, -s, s, c, 0, 0);
    }
}

void
begin_points()
{
    n = 0;
    what = POINT_;
}

void
begin_line()
{
    n = 0;
    what = LINE;
}

void
begin_loop()
{
    n = 0;
    what = LOOP;
}

void
begin_polygon()
{
    n = 0;
    what = POLYGON;
}

double
transform_x(double x, double y)
{
    return x * m.a + y * m.c + m.x;
}

double
transform_y(double x, double y)
{
    return x * m.b + y * m.d + m.y;
}

double
transform_dx(double x, double y)
{
    return x * m.a + y * m.c;
}

double
transform_dy(double x, double y)
{
    return x * m.b + y * m.d;
}

static void
transformed_coord_vertex(COORD_T x, COORD_T y)
{
    if (!n || x != p[n - 1].x || y != p[n - 1].y) {
	if (n >= p_size) {
	    p_size = p ? 2 * p_size : 16;
	    p = (GR_POINT *) realloc((void *) p, p_size * sizeof(*p));
	}
	p[n].x = x;
	p[n].y = y;
	n++;
    }
}

void
transformed_double_vertex(double xf, double yf)
{
    transformed_coord_vertex((COORD_T) xf + .5, (COORD_T) yf + .5);
}

void
vertex(double x, double y)
{
    transformed_double_vertex(x * m.a + y * m.c + m.x,
			      x * m.b + y * m.d + m.y);
}

void
end_points(GR_WINDOW_ID pmap, GR_GC_ID gc)
{
    if (n > 1)
	GrPoly(pmap, gc, n, p);
}

void
end_line(GR_WINDOW_ID pmap, GR_GC_ID gc)
{
    if (n > 1)
	GrPoly(pmap, gc, n, p);
}

static void
fixloop()
{
    while (n > 2 && p[n - 1].x == p[0].x && p[n - 1].y == p[0].y)
	n--;
}

void
end_loop(GR_WINDOW_ID pmap, GR_GC_ID gc)
{
    fixloop();
    if (n > 2)
	transformed_coord_vertex((COORD_T) p[0].x, (COORD_T) p[0].y);
    end_line(pmap, gc);
}

void
end_polygon(GR_WINDOW_ID pmap, GR_GC_ID gc)
{
    fixloop();
    if (n > 2)
	GrFillPoly(pmap, gc, n, p);
}


void
begin_complex_polygon()
{
    begin_polygon();
    gap = 0;
}

void
fgap()
{
    while (n > gap + 2 && p[n - 1].x == p[gap].x && p[n - 1].y == p[gap].y)
	n--;
    if (n > gap + 2) {
	transformed_coord_vertex((COORD_T) p[gap].x, (COORD_T) p[gap].y);
	gap = n;
    } else {
	n = gap;
    }
}

void
end_complex_polygon(GR_WINDOW_ID pmap, GR_GC_ID gc)
{
    fgap();
    if (n > 2)
	GrFillPoly(pmap, gc, n, p);
}

void
circle(double x, double y, double r, GR_WINDOW_ID pmap, GR_GC_ID gc,
       GR_SIZE w, GR_SIZE h)
{
    GrEllipse(pmap, gc, w / 2 - 1, h / 2 - 1, w / 2 - 1, h / 2 - 1);
}

--- NEW FILE: Makefile ---
#nanox/misc/Makefile

TARGET_EXTRAS=nxloadmon nxterm
INSTALL_EXTRAS=install-apps
CLEAN_EXTRAS=clean-apps

# Just the basic nano-X library for nxterm

NXLIBS=-L$(MWDIR)/lib -lnano-X

# A bit more is needed for nxclock and nxloadmon

WMLIBS= -L$(STAGE_DIR)/lib -L$(MWDIR)/lib -lwm -lnano-X

ifeq ($(CONFIG_COLOSSEUM),y)
WMLIBS+=-lipc
endif

include $(BASE_DIR)/Rules.make

nxloadmon: nxloadmon.o
	$(CC) $(CFLAGS) $(INCLUDES) nxloadmon.o -o $@ $(WMLIBS)

nxterm: nxterm.o
	$(CC) $(CFLAGS) $(INCLUDES) nxterm.o -o $@ $(NXLIBS)

install-apps: $(INSTALL_BINDIR)
	@ cp nxloadmon nxterm $(INSTALL_BINDIR)

clean-apps:
	@ rm -f nxloadmon nxterm


--- NEW FILE: nxterm.h ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */


/* These contained 'pt:re=\\EC' entries.  I deleted them from MiNT and linux
 * because neither of my (linux) termcap nor terminfo manual pages listed
 * them.
 */

#ifdef __MINT__

static char termcap_string[1024] =
    "TERMCAP=wterm|WTerm terminal:al=\\EL:am:bc=\\ED:bl=^G:bs:cd=\\EJ:\
ce=\\EK:cl=\\EE:cm=\\EY%+ %+ :cr=^M:dl=\\EM:do=\\EB:ho=\\EH:is=\\Ev\\Eq:\
l0=F10:le=\\ED:ms:nd=\\EC:rc=\\Ek:rs=\\Ev\\Eq\\EE:sc=\\Ej:sr=\\EI:\
ti=\\Ev\\Ee\\EG:up=\\EA:ve=\\Ee:vi=\\Ef:so=\\Ep:se=\\Eq:mb=\\Ei:md=\\Eg:\
mr=\\Ep:me=\\EG:te=\\EG:us=\\Ei:ue=\\EG:\
kb=^H:kl=\\ED:kr=\\EC:ku=\\EA:kd=\\EB:kI=\\EI:kh=\\EE:kP=\\Ea:kN=\\Eb:k0=\\EY:\
k1=\\EP:k2=\\EQ:k3=\\ER:k4=\\ES:k5=\\ET:k6=\\EU:k7=\\EV:k8=\\EW:k9=\\EX:\
s0=\\Ey:s1=\\Ep:s2=\\Eq:s3=\\Er:s4=\\Es:s5=\\Et:s6=\\Eu:s7=\\Ev:s8=\\Ew:\
s9=\\Ex:";

#elif defined(linux) || defined(__FreeBSD__)

static char termcap_string[1024] =
    "TERMCAP=wterm|WTerm terminal:al=\\EL:am:bc=\\ED:bl=^G:bs:cd=\\EJ:\
ce=\\EK:cl=\\EE:cm=\\EY%+ %+ :cr=^M:dl=\\EM:do=\\EB:ho=\\EH:is=\\Ev\\Eq:\
l0=F10:le=\\ED:ms:nd=\\EC:rc=\\Ek:rs=\\Ev\\Eq\\EE:sc=\\Ej:sr=\\EI:\
ti=\\Ev\\Ee\\EG:up=\\EA:ve=\\Ee:vi=\\Ef:so=\\Ep:se=\\Eq:mb=\\Ei:md=\\Eg:\
mr=\\Ep:me=\\EG:te=\\EG:us=\\Ei:ue=\\EG:\
kb=^H:kl=\\E[D:kr=\\E[C:ku=\\E[A:kd=\\E[B:kI=\\EI:kh=\\EE:kP=\\Ea:kN=\\Eb:\
k0=\\EY:k1=\\EP:k2=\\EQ:k3=\\ER:k4=\\ES:k5=\\ET:k6=\\EU:k7=\\EV:k8=\\EW:\
k9=\\EX:s0=\\Ey:s1=\\Ep:s2=\\Eq:s3=\\Er:s4=\\Es:s5=\\Et:s6=\\Eu:s7=\\Ev:\
s8=\\Ew:s9=\\Ex:";

#elif defined(sun)

  /* only very basic cursor keys so far... */

static char termcap_string[1024] =
    "TERMCAP=wterm|WTerm terminal:al=\\EL:am:bc=\\ED:bl=^G:bs:cd=\\EJ:\
ce=\\EK:cl=\\EE:cm=\\EY%+ %+ :cr=^M:dl=\\EM:do=\\EB:ho=\\EH:is=\\Ev\\Eq:\
l0=F10:le=\\ED:ms:nd=\\EC:pt:re=\\EC:rc=\\Ek:rs=\\Ev\\Eq\\EE:sc=\\Ej:sr=\\EI:\
ti=\\Ev\\Ee\\EG:up=\\EA:ve=\\Ee:vi=\\Ef:so=\\Ep:se=\\Eq:mb=\\Ei:md=\\Eg:\
mr=\\Ep:me=\\EG:te=\\EG:us=\\Ei:ue=\\EG:\
kb=^H:kl=\\E[D:kr=\\E[C:ku=\\E[A:kd=\\E[B:\
k1=\\E[224z:k2=\\E[225z:k3=\\E[226z:k4=\\E[227z:k5=\\E[228z:\
k6=\\E[229z:k7=\\E[230z:k8=\\E[231z:k9=\\E[232z:k0=\\E[233z:\
kN=\\E[222z:kP=\\E[216z:kh=\\E[214z:kH=\\E220z:";

#elif defined(__NetBSD__)

static char termcap_string[1024] =
    "TERMCAP=wterm|WTerm terminal:al=\\EL:am:bc=\\ED:bl=^G:bs:cd=\\EJ:\
ce=\\EK:cl=\\EE:cm=\\EY%+ %+ :cr=^M:dl=\\EM:do=\\EB:ho=\\EH:is=\\Ev\\Eq:\
l0=F10:le=\\ED:ms:nd=\\EC:pt:re=\\EC:rc=\\Ek:rs=\\Ev\\Eq\\EE:sc=\\Ej:sr=\\EI:\
ti=\\Ev\\Ee\\EG:up=\\EA:ve=\\Ee:vi=\\Ef:so=\\Ep:se=\\Eq:mb=\\Ei:md=\\Eg:\
mr=\\Ep:me=\\EG:te=\\EG:us=\\Ei:ue=\\EG:\
kb=^H:kl=\\E[D:kr=\\E[C:ku=\\E[A:kd=\\E[B:\
k1=\\E[224z:k2=\\E[225z:k3=\\E[226z:k4=\\E[227z:k5=\\E[228z:\
k6=\\E[229z:k7=\\E[230z:k8=\\E[231z:k9=\\E[232z:k0=\\E[233z:\
kN=\\E[222z:kP=\\E[216z:kh=\\E[214z:kH=\\E220z:";

#else

#error oops, a new operating system?

#endif

--- NEW FILE: nxloadmon.c ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disclosure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */



/* The obligatory TODO list:
   - Allow the user to update speed via command line 
   - Put in handlers for other Unix like kernels that may not have
   a similar /proc/stat file.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#define MWINCLUDECOLORS
#include <nano-X.h>
#include <wm/nxlib.h>

#define DEF_TITLE 	"CPU"
#define DEF_STYLE	(GR_WM_PROPS_NOAUTOMOVE | \
			GR_WM_PROPS_NOFOCUS | \
			GR_WM_PROPS_BORDER | \
			GR_WM_PROPS_CAPTION | \
			GR_WM_PROPS_CLOSEBOX)
#define DEF_COLOR	GR_COLOR_WINDOW
#define DEF_GEOMETRY	"77x60-0-0"
//#define DEF_GEOMETRY  "100x50-0-0"

#define BACKLOG 	78
#define CTIMEOUT 	500
#define BGCOLOR         MWRGB(0xFF,0xFF,0xFF)

/* application initialization data */
static nxARGS args[] = {
    nxTITLE(DEF_TITLE),
    nxGEOMETRY(DEF_GEOMETRY),
    nxBACKGROUND(DEF_COLOR),
    nxSTYLE(DEF_STYLE),
    nxEND
};

double queue[BACKLOG];
int qhead = 1;
int qtail = 0;

/* Used to hold the delta data from /proc/stat */

struct
{
    unsigned long user;
    unsigned long nice;
    unsigned long sys;
    unsigned long idle;
    unsigned char valid;
}
cpudata;

/* File descriptor */

int loadavg;

static GR_WINDOW_ID mywin;
static GR_GC_ID gc;
static GR_WINDOW_INFO info;
#define XSIZE	info.width
#define YSIZE	info.height

static void expose_points(void);
static void insert_point(double value);

static double
get_load()
{
    unsigned long user, nice, sys, idle;
    double total = 0, busy = 0;

    char str[BUFSIZ];
    char *c;

    /* Very tricky.  We read the first line from /proc/stat */
    /* and parse it up */

    lseek((int) loadavg, 0, SEEK_SET);
    read((int) loadavg, str, BUFSIZ - 1);

    /* Now skip over "cpu" */
    for (c = str; *c != ' '; c++)
	continue;
    c++;

    /* Get the new values */

    user = strtoul(c, &c, 0);
    nice = strtoul(c, &c, 0);
    sys = strtoul(c, &c, 0);
    idle = strtoul(c, &c, 0);

    /* Get the delta with the old values */
    if (cpudata.valid) {
	unsigned long duser, dnice, dsys, didle;

	duser = abs(user - cpudata.user);
	dnice = abs(nice - cpudata.nice);
	dsys = abs(sys - cpudata.sys);
	didle = abs(idle - cpudata.idle);

	busy = (double) duser + dnice + dsys;
	total = (double) busy + didle;
    } else
	total = 0;

    /* And fill up the struct with the new values */
    cpudata.user = user;
    cpudata.nice = nice;
    cpudata.sys = sys;
    cpudata.idle = idle;
    cpudata.valid = 1;

    if (total == 0)
	return (0);

    /* Return the % of cpu use */
    return (busy / total);
}

int
main(int ac, char **av)
{
    GR_EVENT event;

    /* Now open up a handle to our friend, /proc/loadavg */
    loadavg = open("/proc/stat", O_RDONLY);

    if (loadavg == -1) {
	perror("couldn't open /proc/stat");
	exit(1);
    }

    /* Open up the graphics */
    if (GrOpen() < 0) {
	fprintf(stderr, "cannot open graphics\n");
	exit(1);
    }

    /* read arglist and create application window */
    mywin = nxCreateAppWindow(&ac, &av, args);

    GrSelectEvents(mywin, GR_EVENT_MASK_TIMEOUT | GR_EVENT_MASK_EXPOSURE |
		   GR_EVENT_MASK_CLOSE_REQ);
    GrMapWindow(mywin);

    /* Get a graphics context */
    gc = GrNewGC();

    GrGetWindowInfo(mywin, &info);
    memset(&cpudata, sizeof(cpudata), 0);
    get_load();

    while (1) {
	GrGetNextEventTimeout(&event, CTIMEOUT);

	switch (event.type) {
	case GR_EVENT_TYPE_CLOSE_REQ:
	    GrClose();
	    exit(0);

	case GR_EVENT_TYPE_EXPOSURE:
	    expose_points();
	    break;

	case GR_EVENT_TYPE_TIMEOUT:
	    insert_point(get_load());

#ifdef NOTUSED
	    load_array[load_array_head] = get_load();
	    if (++load_array_head == BACKLOG)
		load_array_head = 0;
	    if (++load_array_tail == BACKLOG)
		load_array_tail = 0;
#endif

	    expose_points();
	    break;

	}

    }
}

/* For the example,
   g goes from 221 -- 237
   b goes from 0 to 237
*/

static void
expose_points(void)
{
    int h, x = 0;
    int r = 0x00;
    int g = 0x66;
    int b = 0xCC;

    int rdiff = (204) / 38;
    int gdiff = (204 - 102) / 38;

    int point = qtail;

    while (1) {
	h = (queue[point] * YSIZE);

	GrSetGCForeground(gc, MWRGB(r, g, b));
	GrLine(mywin, gc, x, YSIZE - h, x, YSIZE);

	GrSetGCForeground(gc, BGCOLOR);
	GrLine(mywin, gc, x, 0, x, YSIZE - h - 1);

	if (point == qhead)
	    break;

	if (--point < 0)
	    point = BACKLOG - 1;

	x++;

#ifdef NOTUSED
	if (x < 38) {
	    g -= gdiff;
	    r -= rdiff;
	} else {
	    g += gdiff;
	    r += rdiff;
	}
#endif
    }
}

static void
insert_point(double value)
{
    if (--qhead < 0)
	qhead = (BACKLOG - 1);

    if (--qtail < 0)
	qtail = (BACKLOG - 1);

    queue[qhead] = value;
}




More information about the dslinux-commit mailing list