Newer
Older
Import / research / embedded / src / library / screen.c
/*
  Copyright (c) 2007-2013, John Ryland
 */
#include <screen.h>
#include <corelayer.h>

#define USE_X11
#ifdef USE_X11
#  include <stdio.h>
#  include <stdlib.h>
#  include <X11/Xlib.h>
void sysExit(int);
#else
#  include <oslayer.h>
#endif

//#include <string.h>


/* 
// X11 event loop
int main(void) {
   while (1) {
      XEvent e;
      XNextEvent(d, &e);
      if (e.type == Expose) {
  	 char *msg = "Hello, World!";
         XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
         XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg));
      }
      if (e.type == KeyPress)
         break;
   }
   return 0;
}
*/


Display *d;
Window w;
GC gc;
void* pixels;
XImage* img;

Image openScreen()
{
	d = XOpenDisplay(getenv("DISPLAY"));
	if (d == NULL) {
		strPrint("Cannot open display\n");
		sysExit(1);
	}
	int s = DefaultScreen(d);
	Window rootWindow = RootWindow(d, s);
	w = XCreateSimpleWindow(d, rootWindow, 0, 0, 320, 240, 0, 0, 0);
	//XSelectInput(d, w, ExposureMask | KeyPressMask);
	XMapWindow(d, w);
	img = XGetImage(d, rootWindow, 0, 0, 320, 240, AllPlanes, ZPixmap);
	img->data = (void*)malloc(320*240*4);
	gc = XCreateGC(d, w, 0, 0);
	XFlush(d);

	Image data;
	data.width = 320;
	data.height = 240;
	data.bytes_per_pixel = 2;
	data.pixel_data = pixels = (void*)malloc(320*240*2);
	return data;
}

void closeScreen(Image screen)
{
	getchar();
   	XCloseDisplay(d);
}

void updateScreen()
{
	int i = 320*240;
	unsigned int* outPtr = (unsigned int*)img->data;
	unsigned short* inPtr = (unsigned short*)pixels;
	do {
		unsigned int col = *inPtr++;
		unsigned int red = ((col & (0x1F<<11)) << 8) >> 16;
		unsigned int green = ((col & (0x3F<<5)) << 5) >> 8;
		unsigned int blue = ((col & 0x1F) << 3);
		*outPtr++ = (blue << 16) | (green << 8) | (red);
	} while (--i);
	XPutImage(d, w, gc, img, 0, 0, 0, 0, 320, 240);
	XFlush(d);
}

void fillImage(Image image, unsigned short col)
{
	int x, y, i = 0;
	unsigned col2 = col << 16 | col;
	unsigned *fbptr = (unsigned *)image.pixel_data;
	for (y = 0; y < image.height; y++) {
		for (x = 0; x < (image.width>>1); x+=4) {
			fbptr[i+0] = col2;
			fbptr[i+1] = col2;
			fbptr[i+2] = col2;
			fbptr[i+3] = col2;
			i += 4;
		}
	}
	updateScreen();
}

void blitScreen(unsigned char *fbptr, unsigned char *buf)
{
	memCopy((char*)fbptr, (char*)buf, 320*240*2);
/*
	int i;
	for (i = 0; i < 240; i++)
		memCopy((char*)fbptr + i*1280*2 + 1280-320 + 1280*20, (char*)buf + i*320*2, 320*2);
*/
	updateScreen();
}