extern "C" {
  #include <ubixos/schedule.h>
  #include <sys/video.h>
  #include <vmm/paging.h>
  #include <lib/kmalloc.h>
  #include <lib/bioscall.h>
  }

#include <sde/sde.h>
#include <objgfx/objgfx30.h>
#include <sde/ogDisplay_UbixOS.h>

extern "C" void sdeThread() {
  ogSurface *screen = new ogDisplay_UbixOS();
  struct sdeWindows *tmp = 0x0;
  ogSurface         *buf = 0x0;
  
  screen->ogCreate(640,480,OG_PIXFMT_16BPP);
  screen->ogClear(screen->ogRGB(63,63,63));
  
  while (1) {
    for (tmp = windows;tmp;tmp=tmp->next) {
      switch (tmp->status) {
        case registerWindow:
          buf = (ogSurface *)tmp->buf;
          buf->buffer = mapFromTask(tmp->pid,buf->buffer,buf->bSize);
          buf->lineOfs = (uInt32 *)mapFromTask(tmp->pid,buf->lineOfs,buf->lSize);
          tmp->status = windowReady;
          kprintf("Window Registered!\n");
          break;
        case drawWindow:
          kprintf("Draw Window Routines Here\n");
          buf = (ogSurface *)tmp->buf;
          screen->ogCopy(*buf);
          tmp->status = windowReady;
          break;
        case killWindow:
          kprintf("Killed Window\n");
          if (tmp->next != 0x0) {
            tmp->next->prev = tmp->prev;
            if (tmp->prev != 0x0)
              tmp->prev->next = tmp->next;
            }
          else if (tmp->prev != 0x0) {
            tmp->prev->next = tmp->next;
            if (tmp->next != 0x0)
              tmp->next->prev = tmp->prev;
              }
          else {
            windows = 0x0;
            }
          unmapPages(buf->buffer,buf->bSize);
          unmapPages(buf->lineOfs,buf->lSize);
        //  kfree(tmp->buf);
          kfree(tmp);
          tmp = 0x0;
          break;
        default:
          break;
        }
      }
    sched_yield();
    }
  }