Subversion Repositories Kolibri OS

Rev

Rev 6347 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <kos32sys.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_events_c.h"
  11. #include "SDL_menuetvideo.h"
  12. #include <string.h>
  13.  
  14. struct process_table_entry
  15. {
  16.  uint32_t cpu_usage;
  17.  uint16_t pos_in_windowing_stack;
  18.  uint16_t win_stack_val_at_ecx;
  19.  uint16_t reserved1;
  20.  char name[12];
  21.  uint32_t memstart;
  22.  uint32_t memused;
  23.  uint32_t pid;
  24.  uint32_t winx_start,winy_start;
  25.  uint32_t winx_size,winy_size;
  26.  uint16_t thread_state;
  27.  uint16_t reserved2;
  28.  uint32_t client_left,client_top,client_width,client_height;
  29.  uint8_t window_state;
  30.  uint8_t reserved3[1024-71];
  31. };
  32.  
  33. static SDL_VideoDevice * vm_suf=NULL;
  34. static int was_initialized=0;
  35.  
  36. static int has_null_cursor=0;
  37. static int null_cursor;
  38.  
  39. inline int get_skinh(void)
  40. {
  41.         int res;
  42.         __asm__ ("int $0x40" : "=a"(res) : "a"(48),"b"(4));
  43.         return res;
  44. }
  45.  
  46. //#define KEEP_OBSOLETE_STYLE3
  47.  
  48. #ifdef KEEP_OBSOLETE_STYLE3
  49. static int IsStyle4Available=0;
  50. #endif
  51.  
  52. void MenuetOS_SDL_RepaintWnd(void)
  53. {
  54.  begin_draw();
  55.  sys_create_window(1,1,vm_suf->hidden->win_size_x+9,vm_suf->hidden->win_size_y+get_skinh()+4, (int)vm_suf->hidden->__title, 0x000000,0x34000000);
  56.  
  57.  if(vm_suf && vm_suf->hidden->__video_buffer)
  58.   put_image(1,1,
  59.    vm_suf->hidden->win_size_x,vm_suf->hidden->win_size_y,
  60.    vm_suf->hidden->__video_buffer);
  61.  
  62.  end_draw();
  63. }
  64.  
  65. static int MenuetOS_AllocHWSurface(_THIS,SDL_Surface * surface)
  66. {
  67.  return -1;
  68. }
  69.  
  70. static void MenuetOS_FreeHWSurface(_THIS,SDL_Surface * surface)
  71. {
  72. }
  73.  
  74. static int MenuetOS_LockHWSurface(_THIS,SDL_Surface * surface)
  75. {
  76.  return 0;
  77. }
  78.  
  79. static void MenuetOS_UnlockHWSurface(_THIS,SDL_Surface * surface)
  80. {
  81. }
  82.  
  83. static void MenuetOS_DirectUpdate(_THIS,int numrects,SDL_Rect * rects)
  84. {
  85.  if(numrects)
  86.  {
  87.   put_image(1,1,
  88.    vm_suf->hidden->win_size_x,vm_suf->hidden->win_size_y,
  89.    this->hidden->__video_buffer);
  90.  }
  91. }
  92.  
  93. int MenuetOS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
  94. {
  95.  return 0;
  96. }
  97.  
  98. void MenuetOS_VideoQuit(_THIS)
  99. {
  100.         if (has_null_cursor)
  101.         {
  102.                 __asm__("int $0x40"::"a"(37),"b"(6),"c"(null_cursor));
  103.                 has_null_cursor = 0;
  104.         }
  105. }
  106.  
  107. void MenuetOS_FinalQuit(void)
  108. {
  109. }
  110.  
  111. void MenuetOS_SetCaption(_THIS,const char * title,const char * icon)
  112. {
  113.  this->hidden->__title=(char *)title;
  114.  if(was_initialized) __asm__("int $0x40"::"a"(71),"b"(1),"c"(title));
  115. }
  116.  
  117. SDL_Surface * MenuetOS_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
  118. {
  119.  int ly;
  120.  char * lx;
  121.  if(bpp!=24) return NULL;
  122.  current->flags=flags;
  123.  current->w=width;
  124.  current->h=height;
  125.  current->pitch=width*(bpp>>3);
  126.  current->pixels=this->hidden->__video_buffer=realloc(this->hidden->__video_buffer,
  127.         current->pitch*current->h);
  128.  this->hidden->__lines=(unsigned char **)realloc(this->hidden->__lines,
  129.                     sizeof(unsigned char *)*current->h);
  130.  for(ly=0,lx=current->pixels;ly<current->h;ly++,lx+=current->pitch)
  131.   this->hidden->__lines[ly]=lx;
  132.  this->UpdateRects=MenuetOS_DirectUpdate;
  133.  this->hidden->win_size_x=width;
  134.  this->hidden->win_size_y=height;
  135.  vm_suf=this;
  136.  if (was_initialized)
  137.  {
  138.   unsigned newheight = height+get_skinh()+4;
  139.   unsigned newwidth = width+9;
  140.   __asm__("int $0x40"::"a"(67),"b"(-1),"c"(-1),"d"(newwidth),"S"(newheight));
  141.  }
  142.  else
  143.  {
  144.   set_wanted_events_mask(0x27);
  145.   was_initialized=1;
  146.   MenuetOS_SDL_RepaintWnd();
  147.  }
  148.  return current;
  149. }
  150.  
  151. /*static SDL_Rect video_mode[4];
  152. static SDL_Rect * SDL_modelist[4]={NULL,NULL,NULL,NULL};*/
  153.  
  154. static SDL_Rect ** MenuetOS_ListModes(_THIS,SDL_PixelFormat * fmt,Uint32 flags)
  155. {
  156. // return (&SDL_modelist[((fmt->BitsPerPixel+7)/8)-1]);
  157.         if (fmt->BitsPerPixel==24)
  158.                 return (SDL_Rect**)-1;
  159.         else
  160.                 return NULL;
  161. }
  162.  
  163. static int MenuetOS_Available(void)
  164. {
  165.  return 1;
  166. }
  167.  
  168. static void MenuetOS_DeleteDevice(_THIS)
  169. {
  170. // free(this->hidden->__video_buffer);  // it will be freed as current->pixels
  171.  free(this->hidden->__lines);
  172. }
  173.  
  174. static int MenuetOS_VideoInit(_THIS,SDL_PixelFormat * vformat)
  175. {
  176. #ifdef KEEP_OBSOLETE_STYLE3
  177.         char buf[16];
  178.         __asm__("int $0x40"::"a"(18),"b"(13),"c"(buf));
  179.         if (buf[5]=='K' && buf[6]=='o' && buf[7]=='l' && buf[8]=='i')
  180.                 /* kernels up to 0.7.0.0 do not support style 4 */;
  181.         else if (*(unsigned*)(buf+5) >= 549)
  182.                 /* window style 4 was introduced in revision 549 */
  183.                 IsStyle4Available = 1;
  184. #endif
  185.  vformat->BitsPerPixel=24;
  186.  vformat->BytesPerPixel=3;
  187.  this->info.wm_available=1;
  188.  this->info.hw_available=0;
  189.  this->info.video_mem=0x200000;
  190. /* video_mode[3].x=0;
  191.  video_mode[3].y=0;
  192.  video_mode[3].w=320;
  193.  video_mode[3].h=200;
  194.  video_mode[2].x=0;
  195.  video_mode[2].y=0;
  196.  video_mode[2].w=640;
  197.  video_mode[2].h=400;
  198.  video_mode[1].x=0;
  199.  video_mode[1].y=0;
  200.  video_mode[1].w=320;
  201.  video_mode[1].h=240;
  202.  video_mode[0].x=0;
  203.  video_mode[0].y=0;
  204.  video_mode[0].w=640;
  205.  video_mode[0].h=480;
  206.  SDL_modelist[2]=video_mode+0;*/
  207.  return 0;
  208. }
  209.  
  210. static int MenuetOS_FlipHWSurface(_THIS,SDL_Surface * surface)
  211. {
  212.  put_image(1,1,surface->w,surface->h, surface->pixels);
  213.  return 0;
  214. }
  215.  
  216. WMcursor* KolibriOS_CreateWMCursor(_THIS,
  217.         Uint8* data, Uint8* mask, int w, int h, int hot_x, int hot_y)
  218. {
  219.         int i,j;
  220.         Uint32* cursor;
  221.         WMcursor* res;
  222.  
  223.         if (w>32 || h>32) return NULL;
  224.         if (w%8 || h%8) return NULL;
  225.         cursor = (Uint32*)malloc(32*32*4);
  226.         if (!cursor) return NULL;
  227.         for (i=0;i<32;i++)
  228.                 for (j=0;j<32;j++)
  229.                 {
  230.                         if (i>=h || j>=w)
  231.                         {
  232.                                 cursor[i*32+j] = 0x00000000;
  233.                                 continue;
  234.                         }
  235.                         if (mask[i*w/8+j/8] & (0x80>>(j&7)))
  236.                                 cursor[i*32+j] = (data[i*w/8+j/8] & (0x80>>(j&7)))?0xFF000000:0xFFFFFFFF;
  237.                         else
  238.                                 cursor[i*32+j] = 0x00000000;
  239.                 }
  240.         __asm__ ("int $0x40" : "=a"(res) : "a"(37),"b"(4),
  241.                 "c"(cursor),"d"((hot_x<<24)+(hot_y<<16)+2));
  242.         free(cursor);
  243.         return res;
  244. }
  245. int KolibriOS_ShowWMCursor(_THIS,WMcursor*cursor)
  246. {
  247.         if (!cursor)
  248.         {
  249.                 if (!has_null_cursor)
  250.                 {
  251.                         unsigned* u = malloc(32*32*4);
  252.                         if (!u) return 1;
  253.                         memset(u,0,32*32*4);
  254.                         __asm__("int $0x40":"=a"(null_cursor):
  255.                                 "a"(37),"b"(4),"c"(u),"d"(2));
  256.                         free(u);
  257.                         has_null_cursor = 1;
  258.                 }
  259.                 cursor = (WMcursor*)null_cursor;
  260.         }
  261.         __asm__("int $0x40" : : "a"(37),"b"(5),"c"(cursor));
  262.         return 1;
  263. }
  264. void KolibriOS_FreeWMCursor(_THIS,WMcursor*cursor)
  265. {
  266.         __asm__("int $0x40" : : "a"(37),"b"(6),"c"(cursor));
  267. }
  268. void KolibriOS_CheckMouseMode(_THIS)
  269. {
  270.         if (this->input_grab == SDL_GRAB_OFF)
  271.                 return;
  272.         struct process_table_entry buf;
  273.         int res;
  274.         __asm__ volatile("int $0x40" : "=a"(res): "a"(9), "b"(&buf), "c"(-1));
  275.         if (res == buf.pos_in_windowing_stack)
  276.         {
  277.                 int x = buf.winx_start + buf.client_left + this->hidden->win_size_x/2;
  278.                 int y = buf.winy_start + buf.client_top + this->hidden->win_size_y/2;
  279.                 __asm__("int $0x40" : : "a"(18),"b"(19),"c"(4),
  280.                         "d"(x*65536+y));
  281.         }
  282. }
  283.  
  284. char def_title[] = "KolibriOS SDL App";
  285. static SDL_VideoDevice * MenuetOS_CreateDevice(int indx)
  286. {
  287.  SDL_VideoDevice * dev;
  288.  dev=(SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
  289.  if(dev)
  290.  {
  291.   memset(dev,0,(sizeof *dev));
  292.   dev->hidden = (struct SDL_PrivateVideoData *)malloc((sizeof *dev->hidden));
  293.  }
  294.  if((dev==NULL) || (dev->hidden==NULL))
  295.  {
  296.   SDL_OutOfMemory();
  297.   if(dev)
  298.   {
  299.    free(dev);
  300.   }
  301.   return(0);
  302.  }
  303.  memset(dev->hidden,0,(sizeof *dev->hidden));
  304.  dev->hidden->__title = def_title;
  305.  dev->VideoInit=MenuetOS_VideoInit;
  306.  dev->ListModes=MenuetOS_ListModes;
  307.  dev->SetVideoMode=MenuetOS_SetVideoMode;
  308.  dev->SetColors=MenuetOS_SetColors;
  309.  dev->UpdateRects=NULL;
  310.  dev->VideoQuit=MenuetOS_VideoQuit;
  311.  dev->AllocHWSurface=MenuetOS_AllocHWSurface;
  312.  dev->CheckHWBlit=NULL;
  313.  dev->FillHWRect=NULL;
  314.  dev->SetHWColorKey=NULL;
  315.  dev->SetHWAlpha=NULL;
  316.  dev->LockHWSurface=MenuetOS_LockHWSurface;
  317.  dev->UnlockHWSurface=MenuetOS_UnlockHWSurface;
  318.  dev->FlipHWSurface=MenuetOS_FlipHWSurface;
  319.  dev->FreeHWSurface=MenuetOS_FreeHWSurface;
  320.  dev->SetCaption=MenuetOS_SetCaption;
  321.  dev->SetIcon=NULL;
  322.  dev->IconifyWindow=NULL;
  323.  dev->GrabInput=NULL;
  324.  dev->GetWMInfo=NULL;
  325.  dev->InitOSKeymap=MenuetOS_InitOSKeymap;
  326.  dev->PumpEvents=MenuetOS_PumpEvents;
  327.  dev->free=MenuetOS_DeleteDevice;
  328.         dev->CreateWMCursor = KolibriOS_CreateWMCursor;
  329.         dev->FreeWMCursor = KolibriOS_FreeWMCursor;
  330.         dev->ShowWMCursor = KolibriOS_ShowWMCursor;
  331.         dev->CheckMouseMode = KolibriOS_CheckMouseMode;
  332.  return dev;
  333. }
  334.  
  335. VideoBootStrap mosvideo_bootstrab={
  336.  "menuetos","MenuetOS Device Driver",
  337.  MenuetOS_Available,MenuetOS_CreateDevice,
  338. };
  339.