Subversion Repositories Kolibri OS

Rev

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