aboutsummaryrefslogtreecommitdiffstats
path: root/tests/bandwidth-server-many-up
stat options
Period:
Authors:

Commits per author per week (path 'tests/bandwidth-server-many-up')

AuthorW30 2025W31 2025W32 2025W33 2025Total
Total00000
f='#n26'>26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
/* $Id: XWindow.c,v 1.7 2000/03/28 07:22:15 reinelt Exp $
 *
 * X11 Driver for LCD4Linux 
 *
 * (c) 2000 Herbert Rosmanith <herp@widsau.idv.uni-linz.ac.at>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * $Log: XWindow.c,v $
 * Revision 1.7  2000/03/28 07:22:15  reinelt
 *
 * version 0.95 released
 * X11 driver up and running
 * minor bugs fixed
 *
 */

/* 
 *
 * exported fuctions:
 *
 * struct LCD XWindow[]
 *
 */


/*
 *
 * Sun Mar 26 15:28:23 MET 2000 various rewrites
 * Sat Mar 25 23:58:19 MET 2000 use generic pixmap driver
 * Thu Mar 23 01:05:07 MET 2000 multithreading, synchronization
 * Tue Mar 21 22:22:03 MET 2000 initial coding
 *
 */

#include	<X11/Xlib.h>
#include	<X11/Xutil.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<sys/types.h>
#include	<sys/ipc.h>
#include	<sys/sem.h>
#include	<sys/shm.h>
#include	<unistd.h>
#include	<signal.h>

#include	"cfg.h"
#include	"display.h"
#include	"pixmap.h"

#define BARS ( BAR_L | BAR_R | BAR_U | BAR_D | BAR_H2 | BAR_V2 )

static LCD Lcd;
Display *dp;
int sc;
Window w,rw;
Visual *vi;
int dd;
Colormap cm;
GC gc,gcb,gch;
XColor co[3];
XColor db;
Pixmap pmback;

unsigned char *BackupLCDpixmap;
char *rgbfg,*rgbbg,*rgbhg;
int pixel=-1;			/*pointsize in pixel*/
int pgap=0;			/*gap between points */
int rgap=0;			/*row gap between lines*/
int cgap=0;			/*column gap between characters*/
int border=0;			/*window border*/
int rows=-1,cols=-1;		/*rows+cols without background*/
int xres=-1,yres=-1;		/*xres+yres (same as self->...)*/
int dimx,dimy;			/*total window dimension in pixel*/
int boxw,boxh;			/*box width, box height*/
void async_update();		/*PROTO*/
pid_t async_updater_pid=1;
int semid=-1;
int shmid=-1;

void acquire_lock() {
struct sembuf sembuf;
	sembuf.sem_num=0;
	sembuf.sem_op=-1;
	sembuf.sem_flg=0;
	semop(semid,&sembuf,1);		/* get mutex */
}

void release_lock() {
struct sembuf sembuf;
	sembuf.sem_num=0;
	sembuf.sem_op=1;
	sembuf.sem_flg=0;
	semop(semid,&sembuf,1);		/* free mutex */
}

void semcleanup() {
union semun arg;
	if (semid>-1) semctl(semid,0,IPC_RMID,arg);
}

void shmcleanup() {
	if (shmid>-1) shmctl(shmid,IPC_RMID,NULL);
}

void quit(int nsig) {
	printf("pid%d got signal %d\n",getpid(),nsig);
	semcleanup();
	shmcleanup();
	exit(0);
}

void quit_updater() {
	if (async_updater_pid>1)
		kill(async_updater_pid,15);
}

void init_signals()  {
unsigned int oksig=(1<<SIGBUS)|(1<<SIGFPE)|(1<<SIGSEGV)|
		   (1<<SIGTSTP)|(1<<SIGCHLD)|(1<<SIGCONT)|
		   (1<<SIGTTIN)|(1<<SIGWINCH);
int i;
	for(i=0;i<NSIG;i++)
		if (((1<<i)&oksig)==0)
			signal(i,quit);
}

int init_shm(int nbytes,unsigned char **buf) {

	shmid=shmget(IPC_PRIVATE,nbytes,SHM_R|SHM_W);
	if (shmid==-1) {
		perror("X11: shmget() failed");
		return -1;
	}
	*buf=shmat(shmid,NULL,0);
	if (*buf==NULL) {
		perror("X11: shmat() failed");
		return -1;
	}
	return 0;
}

int init_thread(int bufsiz) {
union semun semun;

	semid=semget(IPC_PRIVATE,1,0);
	if (semid==-1) {
		perror("X11: semget() failed");
		return -1;
	}
	semun.val=1;
	semctl(semid,0,SETVAL,semun);

	switch(async_updater_pid=fork()) {
	case -1:
		perror("X11: fork() failed");
		return -1;
	case 0:
		async_update();
		/*notreached*/
		break;
	default:
		break;
	}
	atexit(quit_updater);
	return 0;
}

int init_x(int rows,int cols,int xres,int yres) {
XSetWindowAttributes wa;
XSizeHints size_hints;
XColor co_dummy;
XEvent ev;

	if ((dp=XOpenDisplay(NULL))==NULL) {
		fprintf(stderr,"can't open display\n");
		return -1;
	}
	sc=DefaultScreen(dp);
	gc=DefaultGC(dp,sc);
	vi=DefaultVisual(dp,sc);
	dd=DefaultDepth(dp,sc);
	rw=DefaultRootWindow(dp);
	cm=DefaultColormap(dp,sc);

	if (XAllocNamedColor(dp,cm,rgbfg,&co[0],&co_dummy)==False) {
		fprintf(stderr,"can't alloc foreground color '%s'\n",
			rgbfg);
		return -1;
	}
	if (XAllocNamedColor(dp,cm,rgbbg,&co[1],&co_dummy)==False) {
		fprintf(stderr,"can't alloc background color '%s'\n",
			rgbbg);
		return -1;
	}
	if (XAllocNamedColor(dp,cm,rgbhg,&co[2],&co_dummy)==False) {
		fprintf(stderr,"can't alloc halfground color '%s'\n",
			rgbhg);
		return -1;
	}
	if (XAllocNamedColor(dp,cm,"#e0e0e0",&db,&co_dummy)==False) {
		fprintf(stderr,"can't alloc db color '%s'\n",
			"#0000ff");
		return -1;
	}
	boxw=xres*(pixel+pgap)+cgap;
	boxh=yres*(pixel+pgap)+rgap;
	dimx=(cols-1)*cgap+cols*xres*(pixel+pgap);
	dimy=(rows-1)*rgap+rows*yres*(pixel+pgap);
	wa.event_mask=ExposureMask|ButtonPressMask|ButtonReleaseMask;
	w=XCreateWindow(dp,rw,0,0,dimx+2*border,dimy+2*border,0,0,
		InputOutput,vi,CWEventMask,&wa);
	pmback=XCreatePixmap(dp,w,dimx,dimy,dd);
	size_hints.min_width=size_hints.max_width=dimx+2*border;
	size_hints.min_height=size_hints.max_height=dimy+2*border;
	size_hints.flags=PPosition|PSize|PMinSize|PMaxSize;
	XSetWMProperties(dp,w,NULL,NULL,NULL,0,&size_hints,NULL,NULL);
	XSetForeground(dp,gc,co[0].pixel);
	XSetBackground(dp,gc,co[1].pixel);
	gcb=XCreateGC(dp,w,0,NULL);
	XSetForeground(dp,gcb,co[1].pixel);
	XSetBackground(dp,gcb,co[0].pixel);
	gch=XCreateGC(dp,w,0,NULL);
	XSetForeground(dp,gch,co[2].pixel);
	XSetBackground(dp,gch,co[0].pixel);
	XFillRectangle(dp,pmback,gcb,0,0,dimx,dimy);
	XSetWindowBackground(dp,w,co[1].pixel);
	XClearWindow(dp,w);
	XStoreName(dp,w,"XLCD4Linux");
	XMapWindow(dp,w);
	XFlush(dp);
	for(;;) {
		XNextEvent(dp,&ev);
		if (ev.type==Expose && ev.xexpose.count==0)
			break;
	}
	XChangeWindowAttributes(dp,w,0,NULL);
	return 0;
}

int xlcdinit(LCD *Self) {
char *s;

	if (sscanf(s=cfg_get("size")?:"20x4","%dx%d",&cols,&rows)!=2
		|| rows<1 || cols<1) {
		fprintf(stderr,"X11: bad size '%s'\n",s);
		return -1;
	}
	if (sscanf(s=cfg_get("font")?:"5x8","%dx%d",&xres,&yres)!=2
		|| xres<5 || yres>10) {
    		fprintf(stderr,"X11: bad font '%s'\n",s);
    		return -1;
	}
	if (sscanf(s=cfg_get("pixel")?:"4+1","%d+%d",&pixel,&pgap)!=2
		|| pixel<1 || pgap<0) {
		fprintf(stderr,"X11: bad pixel '%s'\n",s);
		return -1;
	}
	if (sscanf(s=cfg_get("gap")?:"3x3","%dx%d",&cgap,&rgap)!=2
		|| cgap<0 || rgap<0) {
		fprintf(stderr,"X11: bad gap '%s'\n",s);
		return -1;
	}
	border=atoi(cfg_get("border")?:"0");
	rgbfg=cfg_get("foreground")?:"#000000";
	rgbbg=cfg_get("background")?:"#64b17a";
	rgbhg=cfg_get("halfground")?:"#44915a";

	if (pix_init(rows,cols,xres,yres)==-1) return -1;
	if (init_x(rows,cols,xres,yres)==-1) return -1;
	init_signals();
	if (init_shm(rows*cols*xres*yres,&BackupLCDpixmap)==-1) return -1;
	memset(BackupLCDpixmap,0xff,rows*yres*cols*xres);
	if (init_thread(rows*cols*xres*yres)==-1) return -1;
	Self->rows=rows;
	Self->cols=cols;
	Self->xres=xres;
	Self->yres=yres;
	Lcd=*Self;

	pix_clear();
	return 0;
}

int xlcdclear() {
	return pix_clear();
}

int xlcdput(int row,int col,char *text) {
	return pix_put(row,col,text);
}

int xlcdbar(int type, int row, int col, int max, int len1, int len2) {
	return pix_bar(type,row,col,max,len1,len2);
}

int xlcdflush() {
int dirty;
int i,j,pos;
int x,y;

	acquire_lock();
	dirty=pos=0;
	y=border;
	for(i=0;i<rows*yres;i++) {
		x=border;
		for(j=0;j<cols*xres;j++) {
			if (LCDpixmap[pos]^BackupLCDpixmap[pos]) {
				XFillRectangle(dp,w,
					LCDpixmap[pos]?gc:gch,
					x,y,
					pixel,pixel);
				BackupLCDpixmap[pos]=LCDpixmap[pos];
				dirty=1;
			}
			x+=pixel+pgap;
			if ((j+1)%xres==0) x+=cgap;
			pos++;
		}
		y+=pixel+pgap;
		if ((i+1)%yres==0) y+=rgap;
	}
	if (dirty) XFlush(dp);
	release_lock();
	return 0;
}

/*
 * this one should only be called from the updater-thread
 * no user serviceable parts inside
 */

void update(int x,int y,int width,int height) {
int i,j,pos,wpos;
int xfrom,yfrom;
int xto,yto;
int dx,wx,wy;

	/*
	 * theory of operation:
	 * f:bitpos->pxnr*(pixel+pgap)+(pxnr/xres)*cgap;
	 * f^-1: pxnr -> f^-1(bitpos) = see paper
	 */
	x-=border;
	y-=border;
	if (x>=dimx || y>=dimy || x+width<0 || y+height<0) {
		// printf("border only\n");
		return;	/*border doesnt need update*/
	}
	if (x<0) x=0;
	if (y<0) y=0;
	if (x+width>dimx) width=dimx-x;
	if (y+height>dimy) height=dimy-y;
	/*
	 * now we have to find out where the box starts
	 * with respects to the LCDpixmap coordinates
	 */
	/* upper left corner */
	xfrom=xres*(x/boxw);			/*start at col.no*/
	i=(x%boxw);				/*pixelpos rel. char*/
	if (i>xres*(pixel+pgap))		/*in cgap zone*/
		xfrom+=xres;
	else {
		xfrom+=i/(pixel+pgap);		/*character element*/
		if (i%(pixel+pgap)>=pixel)	/*in pgap zone*/
			xfrom++;
	}
	yfrom=yres*(y/boxh);			/*start at row.no*/
	i=(y%boxh);				/*pixelpos rel. char*/
	if (i>yres*(pixel+pgap))		/*in rgap zone*/
		yfrom+=yres;
	else {
		yfrom+=i/(pixel+pgap);		/*character element*/
		if (i%(pixel+pgap)>=pixel)	/*in pgag zone*/
			yfrom++;
	}
	/*lower right corner*/
	x+=width-1;
	y+=height-1;
	xto=xres*(x/boxw)+(x%boxw)/(pixel+pgap);
	yto=yres*(y/boxh)+(y%boxh)/(pixel+pgap);
	
	pos=yfrom*xres*cols+xfrom;
	wy=border+yfrom*(pixel+pgap)+rgap*(yfrom/yres);
	wx=border+xfrom*(pixel+pgap)+cgap*(xfrom/xres);
	wpos=pos;
	for(i=yfrom;i<=yto;i++) {
		dx=wx;
		for(j=xfrom;j<=xto;j++) {
			XFillRectangle(dp,w,
				BackupLCDpixmap[wpos++]?gc:gch,
				dx,wy,
				pixel,pixel);
			dx+=pixel+pgap;
			if ((j+1)%xres==0) dx+=cgap;
		}
		wy+=pixel+pgap;
		if ((i+1)%yres==0) wy+=rgap;
		pos+=xres*cols;
		wpos=pos;
	}
}

void async_update() {
XEvent ev;

	for(;;) {
		XWindowEvent(dp,w,
			ExposureMask,
			&ev);
		if (ev.type==Expose) {
			acquire_lock();
			update(ev.xexpose.x,ev.xexpose.y,
				ev.xexpose.width,ev.xexpose.height);
			release_lock();
		}
	}
}

LCD XWindow[] = {
	{ "X11", 0, 0, 0, 0, BARS, xlcdinit, xlcdclear, xlcdput, xlcdbar, xlcdflush },
	{ NULL }
};