#include "ch.h"
#include "hal.h"
#include "gfx.h"
#ifdef GFX_USE_GDISP /* || defined(__DOXYGEN__) */
#include <stdio.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#ifndef GDISP_SCREEN_WIDTH
#define GDISP_SCREEN_WIDTH 640
#endif
#ifndef GDISP_SCREEN_HEIGHT
#define GDISP_SCREEN_HEIGHT 480
#endif
Display *dpy;
Window win;
XVisualInfo *vi;
XSetWindowAttributes swa;
GLXContext cx;
XEvent event;
int attributeList[] = { GLX_RGBA, None };
/* the memory for our thread */
static WORKING_AREA(myThreadWorkingArea, 10240);
/* the actual thread */
static msg_t myThread(void *arg)
{
(void)arg;
chRegSetThreadName("X11 Thread");
while ( 1 ) {
do {
char buf[31];
KeySym keysym;
XNextEvent(dpy, &event);
switch(event.type)
{
case Expose:
/* redraw the screen here? */
break;
}
} while (XPending(dpy));
}
return 0;
}
bool_t gdisp_lld_init(void)
{
/* open the new display but don't show it up yet */
dpy = XOpenDisplay(NULL);
if(dpy == NULL) {
fprintf(stdout, "Couldn't connect to the X server\r\n");
return false;
}
/* get an appropriate visual */
vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
if(vi == NULL) {
fprintf(stdout, "No appropriate visual found. Check your attributes!\r\n");
return false;
}
/* create a GLX context */
cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
/*create a colormap */
swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
/* create a window */
swa.border_pixel = 0;
swa.event_mask = ExposureMask;
win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, GDISP_SCREEN_WIDTH, GDISP_SCREEN_HEIGHT, 0,
vi->depth, InputOutput, vi->visual, CWBorderPixel|CWColormap|CWEventMask, &swa);
XStoreName(dpy, win, "ChibiOS/GFX");
XMapWindow(dpy, win);
/* connect the context to the window */
glXMakeCurrent(dpy, win, cx);
/* create the OpenGL thread */
chThdCreateStatic(myThreadWorkingArea, sizeof(myThreadWorkingArea), NORMALPRIO, myThread, NULL);
return 0;
}
void gdisp_lld_clear(color_t color) {
glClearColor( (float)RED_OF(color), (float)GREEN_OF(color), (float)BLUE_OF(color), 1.0f );
glClearColor( 1.0f, 0.0f, 1.0f, 1.0f);
glClear( GL_COLOR_BUFFER_BIT );
}
void gdisp_lld_draw_pixel(coord_t x, coord_t y, color_t color) {
glBegin( GL_POINTS );
glVertex2i( 10, 10 );
glVertex2i( 50, 50 );
glEnd();
}
void gdisp_lld_draw_line(coord_t x0, coord_t y0, coord_t x1, coord_t y1, color_t color) {
/* ToD o*/
}
#endif /* GFX_USE_GDISP */