Subversion Repositories Kolibri OS

Rev

Rev 9288 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/ksys.h>
  4. #include "SDL.h"
  5. #include "SDL_error.h"
  6. #include "SDL_video.h"
  7. #include "SDL_mouse.h"
  8. #include "SDL_sysvideo.h"
  9. #include "SDL_pixels_c.h"
  10. #include "SDL_menuetvideo.h"
  11. #include <string.h>
  12.  
  13. static SDL_VideoDevice * vm_suf=NULL;
  14. static int was_initialized=0;
  15.  
  16. static int has_null_cursor=0;
  17. static void* null_cursor;
  18.  
  19. #define WINDOW_BORDER_H 4
  20. #define WINDOW_BORDER_W 9
  21.  
  22. void kos_SDL_RepaintWnd(void)
  23. {
  24.     _ksys_start_draw();
  25.     _ksys_create_window(1, 1, vm_suf->hidden->win_size_x+WINDOW_BORDER_W,
  26.                               vm_suf->hidden->win_size_y+_ksys_get_skin_height()+ WINDOW_BORDER_H,
  27.                               vm_suf->hidden->__title, 0, 0x34);
  28.  
  29.      if (vm_suf && vm_suf->hidden->__video_buffer) {
  30.           _ksys_draw_bitmap(vm_suf->hidden->__video_buffer, 0, 0,
  31.                              vm_suf->hidden->win_size_x, vm_suf->hidden->win_size_y);
  32.     }
  33.     _ksys_end_draw();
  34. }
  35.  
  36. static int kos_AllocHWSurface(_THIS,SDL_Surface * surface)
  37. {
  38.     return -1;
  39. }
  40.  
  41. static void kos_FreeHWSurface(_THIS,SDL_Surface * surface) {/*STUB*/}
  42.  
  43. static int kos_LockHWSurface(_THIS,SDL_Surface * surface)
  44. {
  45.     return 0;
  46. }
  47.  
  48. static void kos_UnlockHWSurface(_THIS,SDL_Surface * surface) {/*STUB*/}
  49.  
  50. static void kos_DirectUpdate(_THIS,int numrects,SDL_Rect * rects)
  51. {
  52.     if (numrects) {
  53.           _ksys_draw_bitmap(this->hidden->__video_buffer, 0,0,
  54.                             vm_suf->hidden->win_size_x,vm_suf->hidden->win_size_y);
  55.      }
  56. }
  57.  
  58. int kos_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
  59. {
  60.     return 0;
  61. }
  62.  
  63. void kos_VideoQuit(_THIS)
  64. {
  65.     if (has_null_cursor) {
  66.         _ksys_delete_cursor(null_cursor);
  67.         has_null_cursor = 0;
  68.     }
  69. }
  70.  
  71. void kos_FinalQuit(void) {/*STUB*/}
  72.  
  73. void kos_SetCaption(_THIS,const char * title, const char * icon)
  74. {
  75.      this->hidden->__title=(char *)title;
  76.      if (was_initialized) _ksys_set_window_title(title);
  77. }
  78.  
  79. SDL_Surface *kos_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
  80. {
  81.     int ly;
  82.      unsigned char* lx;
  83.      if (bpp!=24) return NULL;
  84.  
  85.      current->flags=flags;
  86.      current->w=width;
  87.      current->h=height;
  88.      current->pitch=width*(bpp>>3);
  89.  
  90.      char info[100];
  91.      sprintf(info, "width = %d, height = %d, pitch = %d, bpp = %d\n", current->w, current->h, current->pitch, bpp);
  92.     _ksys_debug_puts(info);
  93.  
  94.     current->pixels=this->hidden->__video_buffer=realloc(this->hidden->__video_buffer, current->pitch*current->h);
  95.      this->hidden->__lines=(unsigned char **)realloc(this->hidden->__lines, sizeof(unsigned char *)*current->h);
  96.  
  97.      for (ly=0, lx=current->pixels; ly<current->h; ly++, lx+=current->pitch)
  98.            this->hidden->__lines[ly]=lx;
  99.  
  100.      this->UpdateRects=kos_DirectUpdate;
  101.      this->hidden->win_size_x=width;
  102.      this->hidden->win_size_y=height;
  103.      vm_suf=this;
  104.  
  105.      if (was_initialized) {
  106.           unsigned newheight = height+_ksys_get_skin_height()+WINDOW_BORDER_H;
  107.           unsigned newwidth  = width+WINDOW_BORDER_W;
  108.           _ksys_change_window(-1, -1, newwidth, newheight);
  109.      } else {
  110.         _ksys_set_event_mask(0x27);
  111.           was_initialized=1;
  112.           kos_SDL_RepaintWnd();
  113.      }
  114.      return current;
  115. }
  116.  
  117. /*static SDL_Rect video_mode[4];
  118. static SDL_Rect * SDL_modelist[4]={NULL,NULL,NULL,NULL};*/
  119.  
  120. static SDL_Rect** kos_ListModes(_THIS,SDL_PixelFormat * fmt,Uint32 flags)
  121. {
  122. // return (&SDL_modelist[((fmt->BitsPerPixel+7)/8)-1]);
  123.     if (fmt->BitsPerPixel==24)
  124.         return (SDL_Rect**)-1;
  125.     else
  126.         return NULL;
  127. }
  128.  
  129. static int kos_Available(void)
  130. {
  131.      return 1;
  132. }
  133.  
  134. static void kos_DeleteDevice(_THIS)
  135. {
  136. //  free(this->hidden->__video_buffer); // it will be freed as current->pixels
  137.      free(this->hidden->__lines);
  138. }
  139.  
  140. static int kos_VideoInit(_THIS,SDL_PixelFormat * vformat)
  141. {
  142.     vformat->BitsPerPixel = 24;
  143.      vformat->BytesPerPixel = 3;
  144.      this->info.wm_available = 1;
  145.      this->info.hw_available = 0;
  146.      this->info.video_mem = 0x200000;
  147.      return 0;
  148. }
  149.  
  150. static int kos_FlipHWSurface(_THIS,SDL_Surface * surface)
  151. {
  152.     _ksys_draw_bitmap(surface->pixels, 0, 0, surface->w,surface->h);
  153.      return 0;
  154. }
  155.  
  156. WMcursor* kos_CreateWMCursor(_THIS, Uint8* data, Uint8* mask, int w, int h, int hot_x, int hot_y)
  157. {
  158.     int i,j;
  159.     Uint32* cursor;
  160.     WMcursor* res;
  161.  
  162.     if (w>32 || h>32) return NULL;
  163.     if (w%8 || h%8) return NULL;
  164.     cursor = (Uint32*)malloc(32*32*4);
  165.     if (!cursor) return NULL;
  166.     for (i=0;i<32;i++) {
  167.         for (j=0;j<32;j++) {
  168.             if (i>=h || j>=w) {
  169.                 cursor[i*32+j] = 0x00000000;
  170.                 continue;
  171.             }
  172.             if (mask[i*w/8+j/8] & (0x80>>(j&7)))
  173.                 cursor[i*32+j] = (data[i*w/8+j/8] & (0x80>>(j&7)))?0xFF000000:0xFFFFFFFF;
  174.             else
  175.                 cursor[i*32+j] = 0x00000000;
  176.         }
  177.     }
  178.     res = _ksys_load_cursor(cursor, (hot_x<<24)+(hot_y<<16)+KSYS_CURSOR_INDIRECT);
  179.     free(cursor);
  180.     return res;
  181. }
  182.  
  183. int kos_ShowWMCursor(_THIS, WMcursor* cursor)
  184. {
  185.     if (!cursor) {
  186.         if (!has_null_cursor) {
  187.             unsigned* u = malloc(32*32*4);
  188.             if (!u) return 1;
  189.             memset(u,0,32*32*4);
  190.             null_cursor = _ksys_load_cursor(u, KSYS_CURSOR_INDIRECT);
  191.             free(u);
  192.             has_null_cursor = 1;
  193.         }
  194.         cursor = (WMcursor*)null_cursor;
  195.     }
  196.     _ksys_set_cursor(cursor);
  197.     return 1;
  198. }
  199. void kos_FreeWMCursor(_THIS, WMcursor* cursor)
  200. {
  201.     _ksys_delete_cursor(cursor);
  202. }
  203. void kos_CheckMouseMode(_THIS)
  204. {
  205.     if (this->input_grab == SDL_GRAB_OFF)
  206.         return;
  207.     ksys_thread_t thread_info;
  208.     int res = _ksys_thread_info(&thread_info, -1);
  209.     if (res == thread_info.window_pos_info) {
  210.         int x = thread_info.winx_start + thread_info.clientx + this->hidden->win_size_x/2;
  211.         int y = thread_info.winy_start + thread_info.clienty + this->hidden->win_size_y/2;
  212.         _ksys_set_mouse_pos(x, y);
  213.     }
  214. }
  215.  
  216. char def_title[] = "KolibriOS SDL App";
  217.  
  218. static SDL_VideoDevice *kos_CreateDevice(int indx)
  219. {
  220.     SDL_VideoDevice * dev;
  221.      dev = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
  222.      if (dev) {
  223.           memset(dev,0,(sizeof *dev));
  224.           dev->hidden = (struct SDL_PrivateVideoData*)malloc((sizeof *dev->hidden));
  225.      }
  226.      if ((dev==NULL) || (dev->hidden==NULL)) {
  227.           SDL_OutOfMemory();
  228.           if(dev) {
  229.                free(dev);
  230.           }
  231.           return(0);
  232.      }
  233.  
  234.     memset(dev->hidden, 0, (sizeof *dev->hidden));
  235.     dev->hidden->__title = def_title;
  236.     dev->VideoInit = kos_VideoInit;
  237.      dev->ListModes = kos_ListModes;
  238.     dev->SetVideoMode = kos_SetVideoMode;
  239.     dev->SetColors = kos_SetColors;
  240.      dev->UpdateRects = NULL;
  241.      dev->VideoQuit = kos_VideoQuit;
  242.      dev->AllocHWSurface=kos_AllocHWSurface;
  243.      dev->CheckHWBlit = NULL;
  244.      dev->FillHWRect = NULL;
  245.      dev->SetHWColorKey = NULL;
  246.      dev->SetHWAlpha = NULL;
  247.      dev->LockHWSurface = kos_LockHWSurface;
  248.      dev->UnlockHWSurface = kos_UnlockHWSurface;
  249.      dev->FlipHWSurface = kos_FlipHWSurface;
  250.      dev->FreeHWSurface = kos_FreeHWSurface;
  251.      dev->SetCaption = kos_SetCaption;
  252.      dev->SetIcon = NULL;
  253.      dev->IconifyWindow = NULL;
  254.      dev->GrabInput = NULL;
  255.      dev->GetWMInfo = NULL;
  256.      dev->InitOSKeymap = MenuetOS_InitOSKeymap;
  257.      dev->PumpEvents    = MenuetOS_PumpEvents;
  258.      dev->free = kos_DeleteDevice;
  259.     dev->CreateWMCursor = kos_CreateWMCursor;
  260.     dev->FreeWMCursor = kos_FreeWMCursor;
  261.     dev->ShowWMCursor = kos_ShowWMCursor;
  262.     dev->CheckMouseMode = kos_CheckMouseMode;
  263.     return dev;
  264. }
  265.  
  266. VideoBootStrap mosvideo_bootstrab={
  267.      "kolibrios", "KolibriOS Device Driver",
  268.      kos_Available, kos_CreateDevice,
  269. };
  270.