Put X11 and EGL into the kernel leaving ES 2.0 only in driver
authoruser <moby@cilux.org>
Fri, 13 Mar 2009 14:27:33 +0000 (14:27 +0000)
committeruser <moby@cilux.org>
Fri, 13 Mar 2009 14:27:33 +0000 (14:27 +0000)
.gitignore [new file with mode: 0644]
makefile
src/drivers/mid/mid.c
src/include/kernelapi.h
src/platform/container.h
src/platform/kernelapi.c
src/platform/linux/cilux.c
src/platform/linux/kernelplat.c
src/platform/linux/kernelplat.h
src/platform/linux/platform.h

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..6c6e12a
--- /dev/null
@@ -0,0 +1,6 @@
+.*.swp
+*.log
+*.so
+*.o
+pcviewer_es2.cfg
+version.h
index 15c0fff..629fc0a 100644 (file)
--- a/makefile
+++ b/makefile
@@ -62,9 +62,9 @@ mod-mid.so: src/drivers/mid/mid.c src/include/kernelapi.h src/platform/linux/pla
        @echo '--------------------'
 
 install-lin:
-       cp cilux /usr/local/bin
-       cp mod-ni.so mod-np.so /usr/local/lib
-       cp mod-mid.so modules/mid
+       mv cilux /usr/local/bin
+       mv mod-ni.so mod-np.so /usr/local/lib
+       mv mod-mid.so modules/mid
        @echo '--------------------'
 
 # ---------------------------------------------------------------------------- #
index e18f352..a684d80 100644 (file)
@@ -1,5 +1,4 @@
 
-
 /* -------------------------------------------------------------------------- */
 
 #include <kernelapi.h>
 
 /* -------------------------------------------------------------------------- */
 
-EGLNativeDisplayType eglX11Display;
-EGLNativeWindowType  eglX11Window;
-EGLDisplay           eglDisplay = 0;
-EGLConfig            eglConfig  = 0;
-EGLSurface           eglSurface = 0;
-EGLContext           eglContext = 0;
-
-/* -------------------------------------------------------------------------- */
+static GLfloat xco=   0;
+static GLfloat yco=   1;
+static GLfloat zco= -35;
+static GLfloat view_rotx=0.0, view_roty=0.0, view_rotz=0.0;
+static int     shift=0;
 
-#define GLfloat float
-static GLfloat   xco=   0;
-static GLfloat   yco=   1;
-static GLfloat   zco= -35;
-static GLfloat   view_rotx=0.0, view_roty=0.0, view_rotz=0.0;
-static int       shift=0;
+GLuint       program;
+GLuint       texture;
+GLuint       vbo;
+unsigned int numberOfVertices;
+unsigned int posStep;
+unsigned int normStep;
+unsigned int tcStep;
+unsigned int stride;
+float        angle=0.0;
+float        viewAngle = 0.0;
 
 /* -------------------------------------------------------------------------- */
 
@@ -43,9 +43,6 @@ static void reshape(int width, int height);
 static void draw(void);
 static void key(unsigned char k, int down);
 
-static void destroyEGL();
-static int  getX11Display(int windowwidth, int windowheight);
-static int  setUpEGL();
 static int  useTheProgram();
 static int  setUpTnL();
 static int  drawStuff(int width, int height);
@@ -77,7 +74,7 @@ EXPORT int mid_module_event(void* data)
 
 EXPORT int mid_module_tick(void)
 {
-    if(!drawStuff(WINDOW_WIDTH, WINDOW_HEIGHT)) destroyEGL();
+    if(!drawStuff(WINDOW_WIDTH, WINDOW_HEIGHT)) k_gl_end();
     return 1;
 }
 
@@ -96,10 +93,8 @@ void sync_resource(ni_resource* res)
 
 void init_gl(void)
 {
-    if(!getX11Display(WINDOW_WIDTH, WINDOW_HEIGHT)) destroyEGL();
-    if(!setUpEGL())                                 destroyEGL();
-    if(!useTheProgram())                            destroyEGL();
-    if(!setUpTnL())                                 destroyEGL();
+    if(!useTheProgram()) k_gl_end();
+    if(!setUpTnL())      k_gl_end();
 }
 
 void reshape(int width, int height)
@@ -108,16 +103,15 @@ void reshape(int width, int height)
 
 void draw(void)
 {
-    if(!drawStuff(WINDOW_WIDTH, WINDOW_HEIGHT)) destroyEGL();
+    if(!drawStuff(WINDOW_WIDTH, WINDOW_HEIGHT)) k_gl_end();
 }
 
 #define SHIFT 0
 void key(unsigned char k, int down)
 {
-/*
-    if(event.xkey.keycode == 113) viewAngle += 0.1;
-    if(event.xkey.keycode == 114) viewAngle -= 0.1;
-*/
+    if(k == 113) viewAngle += 0.1;
+    if(k == 114) viewAngle -= 0.1;
+
     if(k==SHIFT &&  down){ shift=1; return; }
     if(k==SHIFT && !down){ shift=0; return; }
     if(!down) return;
@@ -189,67 +183,6 @@ void key(unsigned char k, int down)
     draw();
 }
 
-/* -------------------------------------------------------------------------- */
-
-/* need a callback to call this */
-void destroyEGL()
-{
-    eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
-    eglTerminate(eglDisplay);
-}
-
-/* ------------------------------------------------------------- */
-
-int isEGLError(char* where)
-{
-    EGLint err = eglGetError();
-    if(err != EGL_SUCCESS) {
-        printf("EGL failed at %s (%d).\n", where, err);
-        return 1;
-    }
-    return 0;
-}
-
-int setUpEGL()
-{
-    eglDisplay = eglGetDisplay(eglX11Display);
-
-    EGLint iMajorVersion, iMinorVersion;
-    if(!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion)) {
-        printf("Error: eglInitialize() failed.\n");
-        return 0;
-    }
-
-    EGLint pi32ConfigAttribs[5];
-    pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
-    pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
-    pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
-    pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;    
-    pi32ConfigAttribs[4] = EGL_NONE;
-
-    EGLint pi32ContextAttribs[3];
-    pi32ContextAttribs[0] = EGL_CONTEXT_CLIENT_VERSION;
-    pi32ContextAttribs[1] = 2;
-    pi32ContextAttribs[2] = EGL_NONE;
-
-    int iConfigs;
-    if(!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1)) {
-        printf("Error: eglChooseConfig() failed.\n");
-        return 0;
-    }
-
-    eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, eglX11Window, NULL);
-    if(isEGLError("eglCreateWindowSurface")) return 0;
-
-    eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, pi32ContextAttribs);
-    if(isEGLError("eglCreateContext")) return 0;
-
-    eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
-    if(isEGLError("eglMakeCurrent")) return 0;
-
-    return 1;
-}
-
 /* ------------------------------------------------------------- */
 
 GLuint isShaderError(GLuint thing)
@@ -314,17 +247,6 @@ char* file2string(const char *path)
 
 /* ------------------------------------------------------------- */
 
-GLuint       program;
-GLuint       texture;
-GLuint       vbo;
-unsigned int numberOfVertices;
-unsigned int posStep;
-unsigned int normStep;
-unsigned int tcStep;
-unsigned int stride;
-float        angle=0.0;
-float        viewAngle = 0.0;
-
 int useTheProgram()
 {
     const char *vsSource = file2string("tnl.vert");
@@ -465,11 +387,11 @@ int drawStuff(int width, int height)
 
     glBindBuffer(GL_ARRAY_BUFFER, 0);
 
-    eglSwapBuffers(eglDisplay, eglSurface);
+    k_gl_swap_buffers();
 
     angle += .007f;
 
-    return !isEGLError("drawStuff");
+    return 1;
 }
 
 void deleteStuff(){
@@ -482,78 +404,5 @@ void deleteStuff(){
  
 /* ------------------------------------------------------------- */
 
-#include <X11/Xutil.h>
-
-Window        x11Window   = 0;
-Display*      x11Display  = 0;
-long          x11Screen   = 0;
-XVisualInfo*  x11Visual   = 0;
-Colormap      x11Colormap = 0;
-int getX11Display(int windowwidth, int windowheight)
-{
-    x11Display = XOpenDisplay(0);
-
-    if(!x11Display) {
-        printf("Error: Unable to open X display\n");
-        return 0;
-    }
-
-    x11Screen = XDefaultScreen(x11Display);
-    Window rootWindow = RootWindow(x11Display, x11Screen);
-    int depth = DefaultDepth(x11Display, x11Screen);
-    x11Visual = malloc(sizeof(XVisualInfo));
-    XMatchVisualInfo(x11Display, x11Screen, depth, TrueColor, x11Visual);
-
-    if(!x11Visual) {
-        printf("Error: Unable to acquire visual\n");
-        return 0;
-    }
-
-    x11Colormap = XCreateColormap(x11Display, rootWindow, x11Visual->visual, AllocNone);
-    XSetWindowAttributes XSWA;
-    XSWA.colormap = x11Colormap;
-    XSWA.event_mask = StructureNotifyMask | ExposureMask |
-                      ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
-    unsigned int cwmask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;
-
-    x11Window = XCreateWindow(x11Display, RootWindow(x11Display, x11Screen), 0, 0, windowwidth, windowheight,
-                              0, CopyFromParent, InputOutput, CopyFromParent, cwmask, &XSWA);
-    XMapWindow(x11Display, x11Window);
-    XFlush(x11Display);
-
-    eglX11Display = (EGLNativeDisplayType)x11Display;
-    eglX11Window  = (EGLNativeWindowType) x11Window;
-
-    return 1;
-}
-
-void cleanupX11()
-{
-    if(x11Window)   XDestroyWindow(x11Display, x11Window);
-    if(x11Colormap) XFreeColormap( x11Display, x11Colormap);
-    if(x11Display)  XCloseDisplay( x11Display);
-}
-
-void forKernel()
-{
-        int nm = XPending(x11Display);
-        int m;
-        for(m=0; m< nm; m++) {
-            XEvent event;
-            XNextEvent(x11Display, &event);
-            switch(event.type){
-                case ButtonPress:
-                break;
-                case KeyPress:
-                break;
-                default:
-                break;
-            }
-        }
-}
-
-/* ------------------------------------------------------------- */
-
 
 
index 0425d02..deb9e79 100644 (file)
@@ -39,10 +39,12 @@ PUBLIC int k_event_post(char* module, void* data);
 typedef void (*k_gl_reshape_event)(int width, int height);
 typedef void (*k_gl_draw_event)(void);
 typedef void (*k_gl_key_event)(unsigned char k, int down);
-PUBLIC  void   k_gl_register_reshape(k_gl_reshape_event);
-PUBLIC  void   k_gl_register_draw(   k_gl_draw_event);
-PUBLIC  void   k_gl_register_key(    k_gl_key_event);
-PUBLIC  void gluxSwapBuffers(void);
+
+PUBLIC void k_gl_register_reshape(k_gl_reshape_event);
+PUBLIC void k_gl_register_draw(   k_gl_draw_event);
+PUBLIC void k_gl_register_key(    k_gl_key_event);
+PUBLIC void k_gl_swap_buffers(void);
+PUBLIC void k_gl_end(void);
 
 /* -------------------------------------------------------------------------- */
 
index adb9cce..ea47cc4 100644 (file)
@@ -4,8 +4,7 @@
                   from executable to kernel
 */
 
-PUBLIC void     c_init(CONSOLE console,
-                       char*   version,
+PUBLIC void     c_init(char*   version,
                        char*   ciux,
                        void  (*terminate)(void));
 
index eb2ab0f..746da19 100644 (file)
@@ -153,12 +153,10 @@ static void               init_hashtables(void);
 
 /* -------------------------------------------------------------------------- */
 
-EXPORT void c_init(CONSOLE console,
-                   char*   version,
+EXPORT void c_init(char*   version,
                    char*   ciux,
                    void  (*terminate)(void))
 {
-       k_console  =console;
        k_version  =version;
        k_ciux     =ciux;
        k_terminate=terminate;
index 2c9e301..fa07879 100644 (file)
@@ -1,66 +1,78 @@
 
+/* -------------------------------------------------------------------------- */
+
 #include <kernelplat.h>
 #include <container.h>
 #include <version.h>
 
 /* -------------------------------------------------------------------------- */
 
+static void cilux_terminate(void);
 static void re_exec_as_other();
 
 /* -------------------------------------------------------------------------- */
 
 int main(int argc, char* argv[])
 {
-       c_init(0, cilux_version, cilux_ciux, 0);
-       re_exec_as_other();
-       c_run(0);
+    c_init(cilux_version, cilux_ciux, cilux_terminate);
+
+    re_exec_as_other();
+    c_run(0);
 
-       return 0;
+    return 0;
 }
 
+/* -------------------------------------------------------------------------- */
+
 void re_exec_as_other()
 {
-       char* other="other";
-       uid_t uid=0;
-       gid_t gid=0;
-       if(getuid()==0){
-               struct passwd* pw=getpwnam(other);
-               if(!pw){ printf("fail: getpwnam(\"%s\");\n", other); exit(1); }
-               uid=pw->pw_uid;
-               gid=pw->pw_gid;
-       }
-
-       pid_t pid=fork();
-       switch(pid){
-               case 0:
-                       break;
-               case -1:
-                       printf("Failed to fork\n");
-                       exit(1);
-               default:
-                       printf("pid %d\n", pid);
-                       exit(0);
-       }
-       setsid();
-
-       if(getuid()==0){
-               if(setgroups(0,0)== -1){
-                       printf("fail: setgroups(0,0)\n");
-                       exit(1);
-               }
-               if(setgid(gid)== -1){
-                       printf("fail: setgid(%d)\n", gid);
-                       exit(1);
-               }
-               if(initgroups(other, gid)== -1){
-                       printf("fail: initgroups(\"%s\",%d)\n", other, gid);
-                       exit(1);
-               }
-               if(setuid(uid)== -1){
-                       printf("fail: setuid(%d)\n", uid);
-                       exit(1);
-               }
-       }
+    char* other="other";
+    uid_t uid=0;
+    gid_t gid=0;
+    if(getuid()==0){
+        struct passwd* pw=getpwnam(other);
+        if(!pw){ printf("fail: getpwnam(\"%s\");\n", other); exit(1); }
+        uid=pw->pw_uid;
+        gid=pw->pw_gid;
+    }
+
+    pid_t pid=fork();
+    switch(pid){
+        case 0:
+            break;
+        case -1:
+            printf("Failed to fork\n");
+            exit(1);
+        default:
+            printf("pid %d\n", pid);
+            exit(0);
+    }
+    setsid();
+
+    if(getuid()==0){
+        if(setgroups(0,0)== -1){
+            printf("fail: setgroups(0,0)\n");
+            exit(1);
+        }
+        if(setgid(gid)== -1){
+            printf("fail: setgid(%d)\n", gid);
+            exit(1);
+        }
+        if(initgroups(other, gid)== -1){
+            printf("fail: initgroups(\"%s\",%d)\n", other, gid);
+            exit(1);
+        }
+        if(setuid(uid)== -1){
+            printf("fail: setuid(%d)\n", uid);
+            exit(1);
+        }
+    }
+}
+
+void cilux_terminate(void)
+{
+    c_running(0);
+    exit(0);
 }
 
 /* -------------------------------------------------------------------------- */
index a018dfd..d3f3db4 100644 (file)
@@ -26,247 +26,414 @@ static void set_timeval_ms(struct timeval* t, int ms);
 
 /* -------------------------------------------------------------------------- */
 
+static Window        x11Window   = 0;
+static Display*      x11Display  = 0;
+static long          x11Screen   = 0;
+static XVisualInfo*  x11Visual   = 0;
+static Colormap      x11Colormap = 0;
+static EGLNativeDisplayType eglX11Display = 0;
+static EGLNativeWindowType  eglX11Window  = 0;
+static EGLDisplay           eglDisplay    = 0;
+static EGLConfig            eglConfig     = 0;
+static EGLSurface           eglSurface    = 0;
+static EGLContext           eglContext    = 0;
+
+/* -------------------------------------------------------------------------- */
+
+static int create_gl_window();
+static int getX11Display(int windowwidth, int windowheight);
+static int setUpEGL();
+static void checkX11Events();
+
+/* -------------------------------------------------------------------------- */
+
 void init_thread(void)
 {
-       signal(SIGTERM, signal_terminate);
-       signal(SIGINT,  signal_terminate);
-       signal(SIGQUIT, signal_terminate);
-       signal(SIGALRM, signal_alarm);
-       signal(SIGPIPE, signal_other);
-       signal(SIGCHLD, SIG_IGN);
-       signal(SIGHUP,  SIG_IGN);
-       signal(SIGUSR1, SIG_IGN);
-       signal(SIGUSR2, SIG_IGN);
+    signal(SIGTERM, signal_terminate);
+    signal(SIGINT,  signal_terminate);
+    signal(SIGQUIT, signal_terminate);
+    signal(SIGALRM, signal_alarm);
+    signal(SIGPIPE, signal_other);
+    signal(SIGCHLD, SIG_IGN);
+    signal(SIGHUP,  SIG_IGN);
+    signal(SIGUSR1, SIG_IGN);
+    signal(SIGUSR2, SIG_IGN);
 }
 
 void init_gl(void)
 {
+    create_gl_window();
 }
 
 void init_net(void)
 {
-       FD_ZERO(&rd_fd_set);
-       FD_ZERO(&wr_fd_set);
-       FD_ZERO(&ex_fd_set);
+    FD_ZERO(&rd_fd_set);
+    FD_ZERO(&wr_fd_set);
+    FD_ZERO(&ex_fd_set);
 }
 
 /* -------------------------------------------------------------------------- */
 
 OTHER_THREAD void signal_terminate(int signum)
 {
-       c_running(0);
+    c_running(0);
 }
 
 OTHER_THREAD void signal_alarm(int signum)
 {
-       c_signal(signum);
+    c_signal(signum);
 }
 
 OTHER_THREAD void signal_other(int signum)
 {
-       c_signal(signum);
+    c_signal(signum);
 }
 
 /* -------------------------------------------------------------------------- */
 
 void set_callback(k_channel* chan, int rdwr)
 {
-       if(rdwr & SETCB_RD) FD_SET(chan->priv->SOCK, &rd_fd_set);
-       if(rdwr & SETCB_WR) FD_SET(chan->priv->SOCK, &wr_fd_set);
+    if(rdwr & SETCB_RD) FD_SET(chan->priv->SOCK, &rd_fd_set);
+    if(rdwr & SETCB_WR) FD_SET(chan->priv->SOCK, &wr_fd_set);
 }
 
 void un_set_callback(k_channel* chan, int rdwr)
 {
-       if(rdwr & SETCB_RD) FD_CLR(chan->priv->SOCK, &rd_fd_set);
-       if(rdwr & SETCB_WR) FD_CLR(chan->priv->SOCK, &wr_fd_set);
+    if(rdwr & SETCB_RD) FD_CLR(chan->priv->SOCK, &rd_fd_set);
+    if(rdwr & SETCB_WR) FD_CLR(chan->priv->SOCK, &wr_fd_set);
 }
 
 void poller(int no_block)
 {
-       fd_set rd=rd_fd_set;
-       fd_set wr=wr_fd_set;
-       fd_set ex=ex_fd_set;
-
-       k_channel* chan;
-
-       int highest=0;
-       for(chan=k_channels; chan; chan=chan->next){
-               if(chan->priv->state==CHAN_CLOSE) continue;
-               if(highest < chan->priv->SOCK) highest=chan->priv->SOCK;
-       }
-
-       struct timeval  t;
-       struct timeval* tp=&t;
-       set_timeval_ms(tp, no_block? 0: LOOP_TICK);
-
-       if(highest==0){
-               select(0, 0, 0, 0, tp);
-               next_keys();
-               if(!no_block) do_regular_things();
-               return;
-       }
-
-       int len=select(highest+1, &rd, &wr, &ex, tp);
-       GETERRNO(len);
-
-       next_keys();
-
-       if(len==0){
-               do_regular_things();
-               return;
-       }
-       if(len== -1){
-               if(ERRNO==INTERRUPTED) return;
-               log_net_err("select", ERRNO);
-               sleep(1);
-               do_regular_things();
-               return;
-       }
-
-       for(chan=k_channels; chan; chan=chan->next){
-               if(FD_ISSET(chan->priv->SOCK, &ex)){
-                       exception_socket(chan);
-                       continue;
-               }
-               if(FD_ISSET(chan->priv->SOCK, &wr)){
-                       int err; socklen_t len=sizeof(int);
-                       if(getsockopt(chan->priv->SOCK,
-                                     SOL_SOCKET,
-                                     SO_ERROR, &err, &len) || err){
-                               exception_socket(chan);
-                               continue;
-                       }
-                       else{
-                               writeable_socket(chan);
-                       }
-               }
-               if(FD_ISSET(chan->priv->SOCK, &rd)){
-                       readable_socket(chan);
-               }
-       }
+    fd_set rd=rd_fd_set;
+    fd_set wr=wr_fd_set;
+    fd_set ex=ex_fd_set;
+
+    k_channel* chan;
+
+    int highest=0;
+    for(chan=k_channels; chan; chan=chan->next){
+        if(chan->priv->state==CHAN_CLOSE) continue;
+        if(highest < chan->priv->SOCK) highest=chan->priv->SOCK;
+    }
+
+    struct timeval  t;
+    struct timeval* tp=&t;
+    set_timeval_ms(tp, no_block? 0: LOOP_TICK);
+
+    if(highest==0){
+        select(0, 0, 0, 0, tp);
+        next_keys();
+        if(!no_block) do_regular_things();
+        return;
+    }
+
+    int len=select(highest+1, &rd, &wr, &ex, tp);
+    GETERRNO(len);
+
+    checkX11Events();
+    next_keys();
+
+    if(len==0){
+        do_regular_things();
+        return;
+    }
+    if(len== -1){
+        if(ERRNO==INTERRUPTED) return;
+        log_net_err("select", ERRNO);
+        sleep(1);
+        do_regular_things();
+        return;
+    }
+
+    for(chan=k_channels; chan; chan=chan->next){
+        if(FD_ISSET(chan->priv->SOCK, &ex)){
+            exception_socket(chan);
+            continue;
+        }
+        if(FD_ISSET(chan->priv->SOCK, &wr)){
+            int err; socklen_t len=sizeof(int);
+            if(getsockopt(chan->priv->SOCK,
+                          SOL_SOCKET,
+                          SO_ERROR, &err, &len) || err){
+                exception_socket(chan);
+                continue;
+            }
+            else{
+                writeable_socket(chan);
+            }
+        }
+        if(FD_ISSET(chan->priv->SOCK, &rd)){
+            readable_socket(chan);
+        }
+    }
 }
 
 void set_timeval_ms(struct timeval* t, int ms)
 {
-       t->tv_sec=ms/1000;
-       t->tv_usec=(ms-(t->tv_sec)*1000)*1000;
+    t->tv_sec=ms/1000;
+    t->tv_usec=(ms-(t->tv_sec)*1000)*1000;
 }
 
 char* str_error(int e)
 {
-       return strerror(e);
+    return strerror(e);
 }
 
 /* -------------------------------------------------------------------------- */
 
 void stat_only(char* fullname, k_stat* kstat)
 {
-       kstat->type=0;
-       struct stat s;
-       if(stat(fullname, &s)) return;
-       kstat->type=s.st_mode & 0170000;
-       kstat->size=s.st_size;
-       kstat->time=s.st_mtime;
-       kstat->perm=s.st_mode & 0007777;
+    kstat->type=0;
+    struct stat s;
+    if(stat(fullname, &s)) return;
+    kstat->type=s.st_mode & 0170000;
+    kstat->size=s.st_size;
+    kstat->time=s.st_mtime;
+    kstat->perm=s.st_mode & 0007777;
 }
 
 FILE_T stat_open(char* fullname, k_stat* kstat)
 {
-       stat_only(fullname, kstat);
-       if(!kstat->type) return 0;
-       FILE_T filehandle=open(fullname, O_RDONLY);
-       if(filehandle<0) return 0;
-       return filehandle;
+    stat_only(fullname, kstat);
+    if(!kstat->type) return 0;
+    FILE_T filehandle=open(fullname, O_RDONLY);
+    if(filehandle<0) return 0;
+    return filehandle;
 }
 
 FILE_T open_only(char* fullname, int wr)
 {
-       int rw=wr? O_RDWR|O_CREAT|O_TRUNC: O_RDONLY;
-       FILE_T filehandle=open(fullname, rw, 0644);
-       if(filehandle<0) return 0;
-       return filehandle;
+    int rw=wr? O_RDWR|O_CREAT|O_TRUNC: O_RDONLY;
+    FILE_T filehandle=open(fullname, rw, 0644);
+    if(filehandle<0) return 0;
+    return filehandle;
 }
 
 void* mmap_malloc(void* s, size_t size, int prot, int f, char* fullname, int o)
 {
-       FILE_T fh=open(fullname, O_RDONLY, 0644);
-       if(fh<0) return MAP_FAILED;
-
-       char* data=k_malloc(size);
-       int charsread=0;
-       int len;
-       do{
-               len=read(fh, data+charsread, size-charsread);
-               if(len< 0 && FERRNO(len)==EINTR) continue;
-               if(len<=0) break;
-               charsread+=len;
-       } while(charsread<size);
-
-       close(fh);
-       if(len<0 || charsread!=size){ k_free(data); return MAP_FAILED; }
-       return data;
+    FILE_T fh=open(fullname, O_RDONLY, 0644);
+    if(fh<0) return MAP_FAILED;
+
+    char* data=k_malloc(size);
+    int charsread=0;
+    int len;
+    do{
+        len=read(fh, data+charsread, size-charsread);
+        if(len< 0 && FERRNO(len)==EINTR) continue;
+        if(len<=0) break;
+        charsread+=len;
+    } while(charsread<size);
+
+    close(fh);
+    if(len<0 || charsread!=size){ k_free(data); return MAP_FAILED; }
+    return data;
 }
 
 void* mmap_name(void* s, size_t size, int prot, int f, char* fullname, int o)
 {
-       if(!size) return MAP_FAILED;
-       int w=(prot & PROT_WRITE);
-       FILE_T fh=open(fullname, (w? O_RDWR|O_CREAT: O_RDONLY), 0644);
-       if(fh<0) return MAP_FAILED;
-       return mmap(s, size, prot, f, fh, o);
+    if(!size) return MAP_FAILED;
+    int w=(prot & PROT_WRITE);
+    FILE_T fh=open(fullname, (w? O_RDWR|O_CREAT: O_RDONLY), 0644);
+    if(fh<0) return MAP_FAILED;
+    return mmap(s, size, prot, f, fh, o);
 }
 
 /* -------------------------------------------------------------------------- */
 
 EXPORT void k_random_bytes(char* buf, size_t size)
 {
-       *(short*)buf=getpid();
-       if(size!=2) k_log_err("Linux randomness not implemented yet!");
+    *(short*)buf=getpid();
+    if(size!=2) k_log_err("Linux randomness not implemented yet!");
 }
 
 /* -------------------------------------------------------------------------- */
 
 EXPORT void* k_malloc(size_t size)
 {
-       void* p=malloc(size);
-       if(!p){
-               k_fatal(OOM_STRING);
-       }
-       return p;
+    void* p=malloc(size);
+    if(!p){
+        k_fatal(OOM_STRING);
+    }
+    return p;
 }
 
 EXPORT void* k_realloc(void* o, size_t size)
 {
-       void* p=realloc(o, size);
-       if(!p){
-               k_fatal(OOM_STRING);
-       }
-       return p;
+    void* p=realloc(o, size);
+    if(!p){
+        k_fatal(OOM_STRING);
+    }
+    return p;
 }
 
 EXPORT void k_free(void* o)
 {
-       if(o) free(o);
+    if(o) free(o);
 }
 
 EXPORT char* k_strdup(char* s)
 {
-       if(!s) return 0;
-       char* p=strdup(s);
-       if(!p){
-               k_fatal(OOM_STRING);
-       }
-       return p;
+    if(!s) return 0;
+    char* p=strdup(s);
+    if(!p){
+        k_fatal(OOM_STRING);
+    }
+    return p;
 }
 
 EXPORT void* k_memdup(void* s, size_t size)
 {
-       void* p=malloc(size);
-       if(!p){
-               k_fatal(OOM_STRING);
-       }
-       memcpy(p, s, size);
-       return p;
+    void* p=malloc(size);
+    if(!p){
+        k_fatal(OOM_STRING);
+    }
+    memcpy(p, s, size);
+    return p;
+}
+
+/* ------------------------------------------------------------- */
+
+#define WINDOW_WIDTH  640
+#define WINDOW_HEIGHT 480
+
+int create_gl_window()
+{
+    if(!getX11Display(WINDOW_WIDTH, WINDOW_HEIGHT)) return 0;
+    if(!setUpEGL())                                 return 0;
+    return 1;
+}
+
+/* ------------------------------------------------------------- */
+
+int getX11Display(int windowwidth, int windowheight)
+{
+    x11Display = XOpenDisplay(0);
+
+    if(!x11Display) {
+        printf("Error: Unable to open X display\n");
+        return 0;
+    }
+
+    x11Screen = XDefaultScreen(x11Display);
+    Window rootWindow = RootWindow(x11Display, x11Screen);
+    int depth = DefaultDepth(x11Display, x11Screen);
+    x11Visual = malloc(sizeof(XVisualInfo));
+    XMatchVisualInfo(x11Display, x11Screen, depth, TrueColor, x11Visual);
+
+    if(!x11Visual) {
+        printf("Error: Unable to acquire visual\n");
+        return 0;
+    }
+
+    x11Colormap = XCreateColormap(x11Display, rootWindow, x11Visual->visual, AllocNone);
+    XSetWindowAttributes XSWA;
+    XSWA.colormap = x11Colormap;
+    XSWA.event_mask = StructureNotifyMask | ExposureMask |
+                      ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
+    unsigned int cwmask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;
+
+    x11Window = XCreateWindow(x11Display, RootWindow(x11Display, x11Screen), 0, 0, windowwidth, windowheight,
+                              0, CopyFromParent, InputOutput, CopyFromParent, cwmask, &XSWA);
+    XMapWindow(x11Display, x11Window);
+    XFlush(x11Display);
+
+    eglX11Display = (EGLNativeDisplayType)x11Display;
+    eglX11Window  = (EGLNativeWindowType) x11Window;
+
+    return 1;
+}
+
+void cleanupX11()
+{
+    if(x11Window)   XDestroyWindow(x11Display, x11Window);
+    if(x11Colormap) XFreeColormap( x11Display, x11Colormap);
+    if(x11Display)  XCloseDisplay( x11Display);
+}
+
+void checkX11Events()
+{
+    int nm = XPending(x11Display);
+    int m;
+    for(m=0; m< nm; m++) {
+        XEvent event;
+        XNextEvent(x11Display, &event);
+        switch(event.type){
+            case ButtonPress:
+            break;
+            case KeyPress:
+                c_key((unsigned char)event.xkey.keycode, 1);
+            break;
+            default:
+            break;
+        }
+    }
+}
+
+/* -------------------------------------------------------------------------- */
+
+int isEGLError(char* where)
+{
+    EGLint err = eglGetError();
+    if(err != EGL_SUCCESS) {
+        printf("EGL failed at %s (%d).\n", where, err);
+        return 1;
+    }
+    return 0;
+}
+
+int setUpEGL()
+{
+    eglDisplay = eglGetDisplay(eglX11Display);
+
+    EGLint iMajorVersion, iMinorVersion;
+    if(!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion)) {
+        printf("Error: eglInitialize() failed.\n");
+        return 0;
+    }
+
+    EGLint pi32ConfigAttribs[5];
+    pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
+    pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
+    pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
+    pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;    
+    pi32ConfigAttribs[4] = EGL_NONE;
+
+    EGLint pi32ContextAttribs[3];
+    pi32ContextAttribs[0] = EGL_CONTEXT_CLIENT_VERSION;
+    pi32ContextAttribs[1] = 2;
+    pi32ContextAttribs[2] = EGL_NONE;
+
+    int iConfigs;
+    if(!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1)) {
+        printf("Error: eglChooseConfig() failed.\n");
+        return 0;
+    }
+
+    eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, eglX11Window, NULL);
+    if(isEGLError("eglCreateWindowSurface")) return 0;
+
+    eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, pi32ContextAttribs);
+    if(isEGLError("eglCreateContext")) return 0;
+
+    eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
+    if(isEGLError("eglMakeCurrent")) return 0;
+
+    return 1;
+}
+
+/* -------------------------------------------------------------------------- */
+
+EXPORT void k_gl_swap_buffers(void)
+{
+    eglSwapBuffers(eglDisplay, eglSurface);
+}
+
+EXPORT void k_gl_end(void)
+{
+    eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+    eglTerminate(eglDisplay);
 }
 
 /* -------------------------------------------------------------------------- */
index 2b96446..dd06f41 100644 (file)
@@ -7,6 +7,8 @@
 #include <grp.h>
 #include <netinet/tcp.h>
 #include <arpa/inet.h>
+#include <X11/Xutil.h>
+#include <EGL/egl.h>
 
 #include <platform.h>
 
 #define SLEEP_MS(x)           do{struct timeval t;t.tv_sec=0;t.tv_usec=(x)*1000;select(0,0,0,0,&t);}while(0)
 #define MKTIME                mktime
 
-/* -------------------------------------------------------------------------- */
-
-#define CONSOLE    void*
 #define C_RUN_RV   int
 #define C_RUN_ARG  void*
 
-PUBLIC CONSOLE k_console;
-
 /* -------------------------------------------------------------------------- */
 
index b657cb4..4b5502b 100644 (file)
@@ -7,7 +7,6 @@
 #include <time.h>
 #include <math.h>
 #include <netinet/in.h>
-#include <EGL/egl.h>
 #include <GLES2/gl2.h>
 
 #ifndef PUBLIC