dslinux/user/pixil/sys/pixilwm/applets Makefile backlight.c battery.c date.c loadmon.c version.c
amadeus
dslinux_amadeus at user.in-berlin.de
Tue Oct 3 13:27:13 CEST 2006
Update of /cvsroot/dslinux/dslinux/user/pixil/sys/pixilwm/applets
In directory antilope:/tmp/cvs-serv11916/sys/pixilwm/applets
Added Files:
Makefile backlight.c battery.c date.c loadmon.c version.c
Log Message:
adding pristine copy of pixil to HEAD so I can branch from it
--- NEW FILE: Makefile ---
MICROWIN_DIR=/user/projects/microwin-0.89.devel/src
CFLAGS=-O0 -g -I$(MICROWIN_DIR)/include -I..
all: version.so date.so loadmon.so
%.so: %.o
gcc -shared -o $@ $<
clean:
rm -f *.so *.o
.SUFFIXES: .c .o
c.o:
$(CC) -c $(CFLAGS) -o $@ $<
--- NEW FILE: battery.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.
*/
/* Battery monitor for those apps with APM support */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <nano-X.h>
#include <nxcolors.h>
#include <pixlib/pixlib.h>
#include "nanowm.h"
#include "applets.h"
static GR_WINDOW_ID wid;
static int g_w = 0, g_h = 0;
static int applet_id;
#define APPICON "battery.gif"
struct battery_t
{
unsigned short percent;
unsigned short ttl;
unsigned char status;
};
static struct battery_t global_battery;
static GR_WINDOW_ID ttl_wid = 0;
static int ttl_shown = 0;
static GR_IMAGE_ID batimage;
static void show_ttlwindow(void) {
char buf[64];
int tw, th, tb;
GR_GC_ID gc;
static GR_FONT_ID fontid = 0;
if (!ttl_wid) {
GR_WINDOW_INFO wi;
GrGetWindowInfo(wid, &wi);
ttl_wid = GrNewWindowEx(GR_WM_PROPS_NODECORATE, NULL, GR_ROOT_WINDOW_ID,
wi.x, wi.y - 10, 45, 15, wm_getColor(WM_DIALOG));
}
GrSelectEvents(ttl_wid, GR_EVENT_MASK_EXPOSURE);
GrMapWindow(ttl_wid);
while(1) {
GR_EVENT event;
GrGetNextEvent(&event);
if (event.type == GR_EVENT_TYPE_EXPOSURE && event.exposure.wid == ttl_wid)
break;
}
if (!fontid)
fontid = GrCreateFont(GR_FONT_GUI_VAR, 0, NULL);
if (global_battery.status || global_battery.ttl == -1)
sprintf(buf, "AC Attached");
else
sprintf(buf, "%d:%02d left", global_battery.ttl / 60,
global_battery.ttl % 60);
gc = GrNewGC();
GrSetGCFont(gc, fontid);
GrGetGCTextSize(gc, buf, -1, GR_TFTOP, &tw, &th, &tb);
GrResizeWindow(ttl_wid, tw + 10, 15);
GrSetGCForeground(gc, GR_COLOR_BLACK);
GrRect(ttl_wid, gc, 0,0, tw + 10, 15);
GrSetGCForeground(gc, wm_getColor(WM_ICONTEXT));
GrSetGCUseBackground(gc, GR_FALSE);
GrText(ttl_wid, gc, 5, 1, buf, -1, GR_TFTOP);
GrDestroyGC(gc);
ttl_shown = 2;
}
static void draw_battery(GR_WINDOW_ID wid) {
GR_GC_ID gc = GrNewGC();
GR_COLOR color;
int perc = global_battery.percent;
int height = 0;
GrSetGCForeground(gc, wm_getColor(WM_TASKBAR));
GrFillRect(wid, gc, 0, 0, 7, 14);
GrSetGCBackground(gc, wm_getColor(WM_TASKBAR));
GrDrawImageToFit(wid, gc, 0, 0, -1, -1, batimage);
/* If we are charging, the percentage is always 100% */
if (global_battery.status) {
perc = 100;
color = GR_COLOR_WHITE;
}
else {
if (perc > 100) perc = 100;
if (perc < 0) perc = 0;
if (perc > 50) color = GR_COLOR_WHITE;
else if (perc <= 50 && perc > 10) color = GR_COLOR_YELLOW;
else color = GR_COLOR_RED;
}
height = (11 * perc) / 100;
GrSetGCForeground(gc, color);
GrFillRect(wid, gc, 1, 12 - height, 5, height);
GrDestroyGC(gc);
}
static int
get_battery_info(struct battery_t *bptr) {
bptr->percent = pix_pwr_getbat(PWR_BAT_PERCENT);
bptr->ttl = pix_pwr_getbat(PWR_BAT_SECONDS);
/* Returns 1 if charging, 0 if not, or -1 if unknown */
bptr->status = pix_pwr_isCharging();
return 0;
}
static void event_callback(GR_WINDOW_ID window, GR_EVENT *event) {
switch(event->type) {
#ifdef NOTUSED
case GR_EVENT_TYPE_BUTTON_DOWN:
show_ttlwindow();
break;
#endif
case GR_EVENT_TYPE_EXPOSURE:
draw_battery(window);
break;
}
}
static void timeout_callback(void) {
int pper, psta;
/* If the TTL window is showing, then hide it */
if (ttl_shown) {
ttl_shown--;
if (!ttl_shown)
GrUnmapWindow(ttl_wid);
}
/* Only redraw the battery if there is a change */
pper = global_battery.percent;
psta = global_battery.status;
get_battery_info(&global_battery);
if (global_battery.percent != pper || global_battery.status != psta)
draw_battery(wid);
}
int applet_init(int id, int *x, int y, int h) {
GR_IMAGE_INFO iinfo;
int tid;
applet_id = id;
/* Get the battery info to start things off */
get_battery_info(&global_battery);
/* Load the image */
batimage = loadIconImage(APPICON, 0, 0);
if (!batimage) return -1;
GrGetImageInfo(batimage, &iinfo);
/* Create the main window */
wid = GrNewWindowEx(GR_WM_PROPS_NODECORATE, 0, GR_ROOT_WINDOW_ID,
*x, y, iinfo.width, h, wm_getColor(WM_TASKBAR));
if (!wid) return -1;
/* Register the applet */
wm_applet_register(id, wid,
GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP |
GR_EVENT_MASK_EXPOSURE, event_callback);
/* Register the timer */
tid = wm_applet_add_timer(id, APPLET_TIMER_PERIODIC, 1000, timeout_callback);
GrMapWindow(wid);
/* Update the coordinates */
g_w = iinfo.width;
g_h = h;
*x += iinfo.width;
return 0;
}
int applet_close(void) {
wm_applet_del_timer(applet_id, 0);
return 0;
}
--- NEW FILE: backlight.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 <par/par.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <nano-X.h>
#include "nanowm.h"
#include "nxdraw.h"
#include "powerman.h"
#include "applets.h"
#define BLOFFICON "bloff.gif"
#define BLONICON "blon.gif"
static GR_IMAGE_ID idON, idOFF;
static GR_WINDOW_ID wid;
static int g_w = 0, g_h = 0;
static int applet_id;
static void
draw_backlight(GR_WINDOW_ID id) {
GR_GC_ID gc = GrNewGC();
GrSetGCForeground(gc, wm_getColor(WM_TASKBAR));
GrFillRect(id, gc, 0, 0, 10, 16);
GrDrawImageToFit(id, gc, 0, 0, -1, -1, pm_get_bl_state() ? idON : idOFF);
GrDestroyGC(gc);
}
static void
event_callback(GR_WINDOW_ID window, GR_EVENT *event) {
switch(event->type) {
case GR_EVENT_TYPE_BUTTON_DOWN:
pm_bl_toggle(); /* Toggle the backlight status */
break;
}
draw_backlight(window);
}
/* This is called when there is a change to the status of the backlight */
static void backlight_callback(int type, void *data) {
draw_backlight(wid);
}
int applet_init(int id, int *x, int y, int h) {
GR_IMAGE_INFO iinfo;
int iw;
applet_id = id;
/* Create the local images */
idON = loadIconImage(BLONICON, 0, 0);
idOFF = loadIconImage(BLOFFICON, 0, 0);
if (!idON || !idOFF) return -1;
GrGetImageInfo(idON, &iinfo);
iw = iinfo.width;
GrGetImageInfo(idOFF, &iinfo);
iw = (iinfo.width > iw) ? iinfo.width : iw;
/* Make a new window */
wid = GrNewWindowEx(GR_WM_PROPS_NODECORATE, 0, GR_ROOT_WINDOW_ID,
*x, y, iw, h, wm_getColor(WM_TASKBAR));
if (!wid) return -1;
/* Register for some events */
wm_applet_register(id, wid,
GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP |
GR_EVENT_MASK_EXPOSURE, event_callback);
/* Register for a power managment status change */
pm_register_callback(PM_CALLBACK_BL, backlight_callback);
/* Map the window */
GrMapWindow(wid);
/* And update the coordinates */
g_w = iw;
g_h = h;
*x += iw;
return 0;
}
int applet_close(void) {
pm_unregister_callback(PM_CALLBACK_BL, backlight_callback);
return 0;
}
--- NEW FILE: loadmon.c ---
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <nano-X.h>
#include <applets.h>
static GR_WINDOW_ID g_wid = 0;
static int g_height = 0;
static int g_applet = 0;
static int loadavg;
struct {
unsigned long user;
unsigned long nice;
unsigned long sys;
unsigned long idle;
unsigned char valid;
} cpudata;
static void draw_applet(void);
static int get_load(void)
{
unsigned long user, nice, sys, idle;
double total = 0, busy = 0;
char str[BUFSIZ];
char *c;
char dummy[3];
/* 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 * 100) / total;
}
static int vals[30];
static int ptr = 0;
static void timeout_callback(void) {
vals[ptr] = get_load();
ptr = (ptr + 1 == 30) ? 0 : ptr + 1;
draw_applet();
}
#define START_RED 170
#define START_GREEN 170
#define START_BLUE 170
#define END_RED 0
#define END_GREEN 0x33
#define END_BLUE 0x80
static void draw_applet(void) {
GR_GC_ID gc=GrNewGC();
int start = (ptr + 1 == 30) ? 0 : ptr + 1;
int x = 1;
int r = START_RED, g = START_GREEN, b = START_BLUE;
while(start != ptr) {
int i = (vals[start] * g_height) / 100;
GrSetGCForeground(gc, MWRGB(r, g, b));
if (i)
GrLine(g_wid, gc, x, g_height - i, x, g_height);
GrSetGCForeground(gc, MWRGB(0xFF, 0xFF, 0xFF));
GrLine(g_wid, gc, x, 0, x, (g_height - i));
x += 1;
if (x < 15) {
r -= (START_RED - END_RED) / 15;
g -= (START_GREEN - END_GREEN) / 15;
b -= (START_BLUE - END_BLUE) / 15;
}
else {
r += (START_RED - END_RED) / 15;
g += (START_GREEN - END_GREEN) / 15;
b += (START_BLUE - END_BLUE) / 15;
}
start = (start + 1 == 30) ? 0 : start + 1;
}
GrDestroyGC(gc);
}
static void event_callback(GR_WINDOW_ID wid, GR_EVENT *event) {
draw_applet();
}
int applet_init(int id, int *x, int y, int h) {
int ret;
g_applet = id;
loadavg = open("/proc/stat", O_RDONLY);
if (loadavg == -1) {
printf("oops\n");
return -1;
}
g_wid = GrNewWindowEx(GR_WM_PROPS_NODECORATE, 0, GR_ROOT_WINDOW_ID,
*x, y, 31, h, 0xFFFFFF);
wm_applet_register(id, g_wid, GR_EVENT_MASK_EXPOSURE, event_callback);
wm_applet_add_timer(id, APPLET_TIMER_PERIODIC, 1000, timeout_callback);
GrMapWindow(g_wid);
*x += 30;
if (ret) *x += 13;
g_height = h - 2;
return ret ? 0 : -1;
}
int applet_close(void) {
wm_applet_del_timer(g_applet, 0);
}
--- NEW FILE: date.c ---
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <nano-X.h>
#include "nanowm.h"
#include "applets.h"
static GR_WINDOW_ID wid;
static GR_FONT_ID fontid;
static int g_w = 0, g_h = 0;
static int dtoggle = 0;
static int applet_id;
static void draw_date(void) {
char buffer[256];
time_t t = time(0);
struct tm *tv = localtime(&t);
GR_GC_ID gc = GrNewGC();
if (!fontid) fontid = GrCreateFont(GR_FONT_GUI_VAR, 0, NULL);
GrSetGCForeground(gc, wm_getColor(WM_TASKBAR));
GrFillRect(wid, gc, 0, 0, g_w, g_h);
if (dtoggle)
strftime(buffer, sizeof(buffer) - 1, "%m/%d/%y", tv);
else
strftime(buffer, sizeof(buffer) - 1, "%I:%M %p\n", tv);
GrSetGCBackground(gc, wm_getColor(WM_TASKBAR));
GrSetGCForeground(gc, wm_getColor(WM_BGCOLOR));
GrSetGCFont(gc, fontid);
GrText(wid, gc, 0, 0, buffer, -1, GR_TFTOP);
GrDestroyGC(gc);
}
static void event_callback(GR_WINDOW_ID wid, GR_EVENT *event) {
switch(event->type) {
case GR_EVENT_TYPE_BUTTON_DOWN:
dtoggle = 1;
break;
case GR_EVENT_TYPE_BUTTON_UP:
dtoggle = 0;
break;
}
draw_date();
}
static void timeout_callback(void) {
draw_date();
}
int applet_init(int id, int *x, int y, int h) {
int tid;
applet_id = id;
wid = GrNewWindowEx(GR_WM_PROPS_NODECORATE, 0, GR_ROOT_WINDOW_ID,
*x, y, 50, h, wm_getColor(WM_TASKBAR));
if (!wid) return -1;
wm_applet_register(id, wid,
GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP |
GR_EVENT_MASK_EXPOSURE, event_callback);
tid = wm_applet_add_timer(id, APPLET_TIMER_PERIODIC, 1000,
timeout_callback);
GrMapWindow(wid);
g_w = 50;
g_h = h;
*x += 50;
return 0;
}
int applet_close(void) {
wm_applet_del_timer(applet_id, 0);
return 0;
}
--- NEW FILE: version.c ---
/* A version applet */
#include <stdio.h>
#include <nano-X.h>
#include <applets.h>
#define TERMICON "term.gif"
static int myid = 0;
void draw_version(GR_WINDOW_ID wid) {
GR_WINDOW_INFO info;
GR_GC_ID gc = GrNewGC();
GR_FONT_ID font = GrCreateFont(GR_FONT_GUI_VAR, 0, NULL);
GrSetGCFont(gc, font);
GrGetWindowInfo(wid, &info);
GrSetGCForeground(gc, MWRGB(0x00, 0x66, 0xcc));
GrSetGCBackground(gc, MWRGB(0x00, 0x66, 0xcc));
GrFillRect(wid, gc, 0, 0, info.width, info.height);
GrSetGCForeground(gc, MWRGB(255,255,255));
GrRect(wid, gc, 2, 2, info.width - 4, info.height - 4);
GrText(wid, gc, 5, 5, "Pixil Window Manager", -1, GR_TFASCII | GR_TFTOP);
GrText(wid, gc, 5, 20, "Version 0.1", -1, GR_TFASCII | GR_TFTOP);
GrText(wid, gc, 5, 35, "Press button to close", -1, GR_TFASCII | GR_TFTOP);
GrDestroyFont(font);
GrDestroyGC(gc);
}
static void show_version(void) {
GR_WINDOW_ID wid;
GR_SCREEN_INFO si;
GrGetScreenInfo(&si);
wid = GrNewWindowEx(GR_WM_PROPS_NODECORATE, NULL, GR_ROOT_WINDOW_ID,
(si.cols - 200) / 2 , (si.rows - 55) / 2,
200, 55, 0xFFFFFF);
if (!wid) return;
GrSelectEvents(wid, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN);
GrMapWindow(wid);
while(1) {
GR_EVENT event;
GrGetNextEvent(&event);
if (((GR_EVENT_GENERAL *) &event)->wid != wid) continue;
if (event.type == GR_EVENT_TYPE_EXPOSURE) draw_version(wid);
else break;
}
GrDestroyWindow(wid);
}
/* Entry points */
int applet_init(int id, int *x, int y, int h) {
int ret;
myid = id;
ret = wm_create_icon_applet(id, *x, y, 13, h, TERMICON, show_version);
if (ret) *x += 13;
return ret ? 0 : -1;
}
int applet_close(void) {
wm_close_icon_applet(myid);
}
More information about the dslinux-commit
mailing list