Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.     SDL - Simple DirectMedia Layer
  3.     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public
  16.     License along with this library; if not, write to the Free
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     Sam Lantinga
  20.     slouken@devolution.com
  21. */
  22.  
  23.  
  24. /* General cursor handling code for SDL */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30. #include "SDL_mutex.h"
  31. #include "SDL_error.h"
  32. #include "SDL_video.h"
  33. #include "SDL_mouse.h"
  34. #include "SDL_blit.h"
  35. #include "SDL_events_c.h"
  36. #include "SDL_sysvideo.h"
  37. #include "SDL_sysevents.h"
  38. #include "SDL_cursor_c.h"
  39. #include "SDL_pixels_c.h"
  40. #include "default_cursor.h"
  41.  
  42. /* These are static for our cursor handling code */
  43. volatile int SDL_cursorstate = 0;
  44. SDL_Cursor *SDL_cursor = NULL;
  45. static SDL_Cursor *SDL_defcursor = NULL;
  46. SDL_mutex *SDL_cursorlock = NULL;
  47.  
  48. /* Public functions */
  49. void SDL_CursorQuit(void)
  50. {
  51.         if ( SDL_cursor != NULL ) {
  52.                 SDL_Cursor *cursor;
  53.  
  54.                 SDL_cursorstate &= ~CURSOR_VISIBLE;
  55.                 if ( SDL_cursor != SDL_defcursor ) {
  56.                         SDL_FreeCursor(SDL_cursor);
  57.                 }
  58.                 SDL_cursor = NULL;
  59.                 if ( SDL_defcursor != NULL ) {
  60.                         cursor = SDL_defcursor;
  61.                         SDL_defcursor = NULL;
  62.                         SDL_FreeCursor(cursor);
  63.                 }
  64.         }
  65.         if ( SDL_cursorlock != NULL ) {
  66.                 SDL_DestroyMutex(SDL_cursorlock);
  67.                 SDL_cursorlock = NULL;
  68.         }
  69. }
  70. int SDL_CursorInit(Uint32 multithreaded)
  71. {
  72.         /* We don't have mouse focus, and the cursor isn't drawn yet */
  73.         SDL_cursorstate = CURSOR_VISIBLE;
  74.  
  75.         /* Create the default cursor */
  76.         if ( SDL_defcursor == NULL ) {
  77.                 SDL_defcursor = SDL_CreateCursor(default_cdata, default_cmask,
  78.                                         DEFAULT_CWIDTH, DEFAULT_CHEIGHT,
  79.                                                 DEFAULT_CHOTX, DEFAULT_CHOTY);
  80.                 SDL_SetCursor(SDL_defcursor);
  81.         }
  82.  
  83.         /* Create a lock if necessary */
  84.         if ( multithreaded ) {
  85.                 SDL_cursorlock = SDL_CreateMutex();
  86.         }
  87.  
  88.         /* That's it! */
  89.         return(0);
  90. }
  91.  
  92. /* Multi-thread support for cursors */
  93. #ifndef SDL_LockCursor
  94. void SDL_LockCursor(void)
  95. {
  96.         if ( SDL_cursorlock ) {
  97.                 SDL_mutexP(SDL_cursorlock);
  98.         }
  99. }
  100. #endif
  101. #ifndef SDL_UnlockCursor
  102. void SDL_UnlockCursor(void)
  103. {
  104.         if ( SDL_cursorlock ) {
  105.                 SDL_mutexV(SDL_cursorlock);
  106.         }
  107. }
  108. #endif
  109.  
  110. /* Software cursor drawing support */
  111. SDL_Cursor * SDL_CreateCursor (Uint8 *data, Uint8 *mask,
  112.                                         int w, int h, int hot_x, int hot_y)
  113. {
  114.         SDL_VideoDevice *video = current_video;
  115.         int savelen;
  116.         int i;
  117.         SDL_Cursor *cursor;
  118.  
  119.         /* Make sure the width is a multiple of 8 */
  120.         w = ((w+7)&~7);
  121.  
  122.         /* Sanity check the hot spot */
  123.         if ( (hot_x < 0) || (hot_y < 0) || (hot_x >= w) || (hot_y >= h) ) {
  124.                 SDL_SetError("Cursor hot spot doesn't lie within cursor");
  125.                 return(NULL);
  126.         }
  127.  
  128.         /* Allocate memory for the cursor */
  129.         cursor = (SDL_Cursor *)malloc(sizeof *cursor);
  130.         if ( cursor == NULL ) {
  131.                 SDL_OutOfMemory();
  132.                 return(NULL);
  133.         }
  134.         savelen = (w*4)*h;
  135.         cursor->area.x = 0;
  136.         cursor->area.y = 0;
  137.         cursor->area.w = w;
  138.         cursor->area.h = h;
  139.         cursor->hot_x = hot_x;
  140.         cursor->hot_y = hot_y;
  141.         cursor->data = (Uint8 *)malloc((w/8)*h*2);
  142.         cursor->mask = cursor->data+((w/8)*h);
  143.         cursor->save[0] = (Uint8 *)malloc(savelen*2);
  144.         cursor->save[1] = cursor->save[0] + savelen;
  145.         cursor->wm_cursor = NULL;
  146.         if ( ! cursor->data || ! cursor->save[0] ) {
  147.                 SDL_FreeCursor(cursor);
  148.                 SDL_OutOfMemory();
  149.                 return(NULL);
  150.         }
  151.         for ( i=((w/8)*h)-1; i>=0; --i ) {
  152.                 cursor->data[i] = data[i];
  153.                 cursor->mask[i] = mask[i] | data[i];
  154.         }
  155.         memset(cursor->save[0], 0, savelen*2);
  156.  
  157.         /* If the window manager gives us a good cursor, we're done! */
  158.         if ( video->CreateWMCursor ) {
  159.                 cursor->wm_cursor = video->CreateWMCursor(video, data, mask,
  160.                                                         w, h, hot_x, hot_y);
  161.         } else {
  162.                 cursor->wm_cursor = NULL;
  163.         }
  164.         return(cursor);
  165. }
  166.  
  167. /* SDL_SetCursor(NULL) can be used to force the cursor redraw,
  168.    if this is desired for any reason.  This is used when setting
  169.    the video mode and when the SDL window gains the mouse focus.
  170.  */
  171. void SDL_SetCursor (SDL_Cursor *cursor)
  172. {
  173.         SDL_VideoDevice *video = current_video;
  174.         SDL_VideoDevice *this  = current_video;
  175.  
  176.         /* Make sure that the video subsystem has been initialized */
  177.         if ( ! video ) {
  178.                 return;
  179.         }
  180.  
  181.         /* Prevent the event thread from moving the mouse */
  182.         SDL_LockCursor();
  183.  
  184.         /* Set the new cursor */
  185.         if ( cursor && (cursor != SDL_cursor) ) {
  186.                 /* Erase the current mouse position */
  187.                 if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
  188.                         SDL_EraseCursor(SDL_VideoSurface);
  189.                 } else if ( video->MoveWMCursor ) {
  190.                         /* If the video driver is moving the cursor directly,
  191.                            it needs to hide the old cursor before (possibly)
  192.                            showing the new one.  (But don't erase NULL cursor)
  193.                          */
  194.                         if ( SDL_cursor ) {
  195.                                 video->ShowWMCursor(this, NULL);
  196.                         }
  197.                 }
  198.                 SDL_cursor = cursor;
  199.         }
  200.  
  201.         /* Draw the new mouse cursor */
  202.         if ( SDL_cursor && (SDL_cursorstate&CURSOR_VISIBLE) ) {
  203.                 /* Use window manager cursor if possible */
  204.                 if ( SDL_cursor->wm_cursor &&
  205.                      video->ShowWMCursor(this, SDL_cursor->wm_cursor) )
  206.                         SDL_cursorstate &= ~CURSOR_USINGSW;
  207.                 else {
  208.                         SDL_cursorstate |= CURSOR_USINGSW;
  209.                         if ( video->ShowWMCursor ) {
  210.                                 video->ShowWMCursor(this, NULL);
  211.                         }
  212.                         { int x, y;
  213.                                 SDL_GetMouseState(&x, &y);
  214.                                 SDL_cursor->area.x = (x - SDL_cursor->hot_x);
  215.                                 SDL_cursor->area.y = (y - SDL_cursor->hot_y);
  216.                         }
  217.                         SDL_DrawCursor(SDL_VideoSurface);
  218.                 }
  219.         } else {
  220.                 /* Erase window manager mouse (cursor not visible) */
  221.                 if ( SDL_cursor && (SDL_cursorstate & CURSOR_USINGSW) ) {
  222.                         SDL_EraseCursor(SDL_VideoSurface);
  223.                 } else {
  224.                         if ( video ) {
  225.                                 video->ShowWMCursor(this, NULL);
  226.                         }
  227.                 }
  228.         }
  229.         SDL_UnlockCursor();
  230. }
  231.  
  232. SDL_Cursor * SDL_GetCursor (void)
  233. {
  234.         return(SDL_cursor);
  235. }
  236.  
  237. void SDL_FreeCursor (SDL_Cursor *cursor)
  238. {
  239.         if ( cursor ) {
  240.                 if ( cursor == SDL_cursor ) {
  241.                         SDL_SetCursor(SDL_defcursor);
  242.                 }
  243.                 if ( cursor != SDL_defcursor ) {
  244.                         SDL_VideoDevice *video = current_video;
  245.                         SDL_VideoDevice *this  = current_video;
  246.  
  247.                         if ( cursor->data ) {
  248.                                 free(cursor->data);
  249.                         }
  250.                         if ( cursor->save[0] ) {
  251.                                 free(cursor->save[0]);
  252.                         }
  253.                         if ( video && cursor->wm_cursor ) {
  254.                                 video->FreeWMCursor(this, cursor->wm_cursor);
  255.                         }
  256.                         free(cursor);
  257.                 }
  258.         }
  259. }
  260.  
  261. int SDL_ShowCursor (int toggle)
  262. {
  263.         int showing;
  264.  
  265.         showing = (SDL_cursorstate & CURSOR_VISIBLE);
  266.         if ( toggle >= 0 ) {
  267.                 SDL_LockCursor();
  268.                 if ( toggle ) {
  269.                         SDL_cursorstate |= CURSOR_VISIBLE;
  270.                 } else {
  271.                         SDL_cursorstate &= ~CURSOR_VISIBLE;
  272.                 }
  273.                 SDL_UnlockCursor();
  274.                 if ( (SDL_cursorstate & CURSOR_VISIBLE) != showing ) {
  275.                         SDL_VideoDevice *video = current_video;
  276.                         SDL_VideoDevice *this  = current_video;
  277.  
  278.                         SDL_SetCursor(NULL);
  279.                         if ( video && video->CheckMouseMode ) {
  280.                                 video->CheckMouseMode(this);
  281.                         }
  282.                 }
  283.         } else {
  284.                 /* Query current state */ ;
  285.         }
  286.         return(showing ? 1 : 0);
  287. }
  288.  
  289. void SDL_WarpMouse (Uint16 x, Uint16 y)
  290. {
  291.         SDL_VideoDevice *video = current_video;
  292.         SDL_VideoDevice *this  = current_video;
  293.  
  294.         /* This generates a mouse motion event */
  295.         if ( video->WarpWMCursor ) {
  296.                 video->WarpWMCursor(this, x, y);
  297.         } else {
  298.                 x += (this->screen->offset % this->screen->pitch) /
  299.                       this->screen->format->BytesPerPixel;
  300.                 y += (this->screen->offset / this->screen->pitch);
  301.                 SDL_PrivateMouseMotion(0, 0, x, y);
  302.         }
  303. }
  304.  
  305. void SDL_MoveCursor(int x, int y)
  306. {
  307.         SDL_VideoDevice *video = current_video;
  308.  
  309.         /* Erase and update the current mouse position */
  310.         if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
  311.                 /* Erase and redraw mouse cursor in new position */
  312.                 SDL_LockCursor();
  313.                 SDL_EraseCursor(SDL_VideoSurface);
  314.                 SDL_cursor->area.x = (x - SDL_cursor->hot_x);
  315.                 SDL_cursor->area.y = (y - SDL_cursor->hot_y);
  316.                 SDL_DrawCursor(SDL_VideoSurface);
  317.                 SDL_UnlockCursor();
  318.         } else if ( video->MoveWMCursor ) {
  319.                 video->MoveWMCursor(video, x, y);
  320.         }
  321. }
  322.  
  323. /* Keep track of the current cursor colors */
  324. static int palette_changed = 1;
  325. static Uint32 pixels8[2];
  326.  
  327. void SDL_CursorPaletteChanged(void)
  328. {
  329.         palette_changed = 1;
  330. }
  331.  
  332. void SDL_MouseRect(SDL_Rect *area)
  333. {
  334.         int clip_diff;
  335.  
  336.         *area = SDL_cursor->area;
  337.         if ( area->x < 0 ) {
  338.                 area->w += area->x;
  339.                 area->x = 0;
  340.         }
  341.         if ( area->y < 0 ) {
  342.                 area->h += area->y;
  343.                 area->y = 0;
  344.         }
  345.         clip_diff = (area->x+area->w)-SDL_VideoSurface->w;
  346.         if ( clip_diff > 0 ) {
  347.                 area->w = area->w < clip_diff ? 0 : area->w-clip_diff;
  348.         }
  349.         clip_diff = (area->y+area->h)-SDL_VideoSurface->h;
  350.         if ( clip_diff > 0 ) {
  351.                 area->h = area->h < clip_diff ? 0 : area->h-clip_diff;
  352.         }
  353. }
  354.  
  355. static void SDL_DrawCursorFast(SDL_Surface *screen, SDL_Rect *area)
  356. {
  357.         const Uint32 pixels[2] = { 0xFFFFFFFF, 0x00000000 };
  358.         int i, w, h;
  359.         Uint8 *data, datab;
  360.         Uint8 *mask, maskb;
  361.  
  362.         data = SDL_cursor->data + area->y * SDL_cursor->area.w/8;
  363.         mask = SDL_cursor->mask + area->y * SDL_cursor->area.w/8;
  364.         switch (screen->format->BytesPerPixel) {
  365.  
  366.             case 1: {
  367.                 Uint8 *dst;
  368.                 int dstskip;
  369.  
  370.                 if ( palette_changed ) {
  371.                         pixels8[0] = SDL_MapRGB(screen->format, 255, 255, 255);
  372.                         pixels8[1] = SDL_MapRGB(screen->format, 0, 0, 0);
  373.                         palette_changed = 0;
  374.                 }
  375.                 dst = (Uint8 *)screen->pixels +
  376.                        (SDL_cursor->area.y+area->y)*screen->pitch +
  377.                        SDL_cursor->area.x;
  378.                 dstskip = screen->pitch-area->w;
  379.  
  380.                 for ( h=area->h; h; h-- ) {
  381.                         for ( w=area->w/8; w; w-- ) {
  382.                                 maskb = *mask++;
  383.                                 datab = *data++;
  384.                                 for ( i=0; i<8; ++i ) {
  385.                                         if ( maskb & 0x80 ) {
  386.                                                 *dst = pixels8[datab>>7];
  387.                                         }
  388.                                         maskb <<= 1;
  389.                                         datab <<= 1;
  390.                                         dst++;
  391.                                 }
  392.                         }
  393.                         dst += dstskip;
  394.                 }
  395.             }
  396.             break;
  397.  
  398.             case 2: {
  399.                 Uint16 *dst;
  400.                 int dstskip;
  401.  
  402.                 dst = (Uint16 *)screen->pixels +
  403.                        (SDL_cursor->area.y+area->y)*screen->pitch/2 +
  404.                        SDL_cursor->area.x;
  405.                 dstskip = (screen->pitch/2)-area->w;
  406.  
  407.                 for ( h=area->h; h; h-- ) {
  408.                         for ( w=area->w/8; w; w-- ) {
  409.                                 maskb = *mask++;
  410.                                 datab = *data++;
  411.                                 for ( i=0; i<8; ++i ) {
  412.                                         if ( maskb & 0x80 ) {
  413.                                                 *dst = pixels[datab>>7];
  414.                                         }
  415.                                         maskb <<= 1;
  416.                                         datab <<= 1;
  417.                                         dst++;
  418.                                 }
  419.                         }
  420.                         dst += dstskip;
  421.                 }
  422.             }
  423.             break;
  424.  
  425.             case 3: {
  426.                 Uint8 *dst;
  427.                 int dstskip;
  428.  
  429.                 dst = (Uint8 *)screen->pixels +
  430.                        (SDL_cursor->area.y+area->y)*screen->pitch +
  431.                        SDL_cursor->area.x*3;
  432.                 dstskip = screen->pitch-area->w*3;
  433.  
  434.                 for ( h=area->h; h; h-- ) {
  435.                         for ( w=area->w/8; w; w-- ) {
  436.                                 maskb = *mask++;
  437.                                 datab = *data++;
  438.                                 for ( i=0; i<8; ++i ) {
  439.                                         if ( maskb & 0x80 ) {
  440.                                                 memset(dst,pixels[datab>>7],3);
  441.                                         }
  442.                                         maskb <<= 1;
  443.                                         datab <<= 1;
  444.                                         dst += 3;
  445.                                 }
  446.                         }
  447.                         dst += dstskip;
  448.                 }
  449.             }
  450.             break;
  451.  
  452.             case 4: {
  453.                 Uint32 *dst;
  454.                 int dstskip;
  455.  
  456.                 dst = (Uint32 *)screen->pixels +
  457.                        (SDL_cursor->area.y+area->y)*screen->pitch/4 +
  458.                        SDL_cursor->area.x;
  459.                 dstskip = (screen->pitch/4)-area->w;
  460.  
  461.                 for ( h=area->h; h; h-- ) {
  462.                         for ( w=area->w/8; w; w-- ) {
  463.                                 maskb = *mask++;
  464.                                 datab = *data++;
  465.                                 for ( i=0; i<8; ++i ) {
  466.                                         if ( maskb & 0x80 ) {
  467.                                                 *dst = pixels[datab>>7];
  468.                                         }
  469.                                         maskb <<= 1;
  470.                                         datab <<= 1;
  471.                                         dst++;
  472.                                 }
  473.                         }
  474.                         dst += dstskip;
  475.                 }
  476.             }
  477.             break;
  478.         }
  479. }
  480.  
  481. static void SDL_DrawCursorSlow(SDL_Surface *screen, SDL_Rect *area)
  482. {
  483.         const Uint32 pixels[2] = { 0xFFFFFF, 0x000000 };
  484.         int h;
  485.         int x, minx, maxx;
  486.         Uint8 *data, datab = 0;
  487.         Uint8 *mask, maskb = 0;
  488.         Uint8 *dst;
  489.         int dstbpp, dstskip;
  490.  
  491.         data = SDL_cursor->data + area->y * SDL_cursor->area.w/8;
  492.         mask = SDL_cursor->mask + area->y * SDL_cursor->area.w/8;
  493.         dstbpp = screen->format->BytesPerPixel;
  494.         dst = (Uint8 *)screen->pixels +
  495.                        (SDL_cursor->area.y+area->y)*screen->pitch +
  496.                        SDL_cursor->area.x*dstbpp;
  497.         dstskip = screen->pitch-SDL_cursor->area.w*dstbpp;
  498.  
  499.         minx = area->x;
  500.         maxx = area->x+area->w;
  501.         if ( screen->format->BytesPerPixel == 1 ) {
  502.                 if ( palette_changed ) {
  503.                         pixels8[0] = SDL_MapRGB(screen->format, 255, 255, 255);
  504.                         pixels8[1] = SDL_MapRGB(screen->format, 0, 0, 0);
  505.                         palette_changed = 0;
  506.                 }
  507.                 for ( h=area->h; h; h-- ) {
  508.                         for ( x=0; x<SDL_cursor->area.w; ++x ) {
  509.                                 if ( (x%8) == 0 ) {
  510.                                         maskb = *mask++;
  511.                                         datab = *data++;
  512.                                 }
  513.                                 if ( (x >= minx) && (x < maxx) ) {
  514.                                         if ( maskb & 0x80 ) {
  515.                                                 memset(dst, pixels8[datab>>7], dstbpp);
  516.                                         }
  517.                                 }
  518.                                 maskb <<= 1;
  519.                                 datab <<= 1;
  520.                                 dst += dstbpp;
  521.                         }
  522.                         dst += dstskip;
  523.                 }
  524.         } else {
  525.                 for ( h=area->h; h; h-- ) {
  526.                         for ( x=0; x<SDL_cursor->area.w; ++x ) {
  527.                                 if ( (x%8) == 0 ) {
  528.                                         maskb = *mask++;
  529.                                         datab = *data++;
  530.                                 }
  531.                                 if ( (x >= minx) && (x < maxx) ) {
  532.                                         if ( maskb & 0x80 ) {
  533.                                                 memset(dst, pixels[datab>>7], dstbpp);
  534.                                         }
  535.                                 }
  536.                                 maskb <<= 1;
  537.                                 datab <<= 1;
  538.                                 dst += dstbpp;
  539.                         }
  540.                         dst += dstskip;
  541.                 }
  542.         }
  543. }
  544.  
  545. /* This handles the ugly work of converting the saved cursor background from
  546.    the pixel format of the shadow surface to that of the video surface.
  547.    This is only necessary when blitting from a shadow surface of a different
  548.    pixel format than the video surface, and using a software rendered cursor.
  549. */
  550. static void SDL_ConvertCursorSave(SDL_Surface *screen, int w, int h)
  551. {
  552.         SDL_BlitInfo info;
  553.         SDL_loblit RunBlit;
  554.  
  555.         /* Make sure we can steal the blit mapping */
  556.         if ( screen->map->dst != SDL_VideoSurface ) {
  557.                 return;
  558.         }
  559.  
  560.         /* Set up the blit information */
  561.         info.s_pixels = SDL_cursor->save[1];
  562.         info.s_width = w;
  563.         info.s_height = h;
  564.         info.s_skip = 0;
  565.         info.d_pixels = SDL_cursor->save[0];
  566.         info.d_width = w;
  567.         info.d_height = h;
  568.         info.d_skip = 0;
  569.         info.aux_data = screen->map->sw_data->aux_data;
  570.         info.src = screen->format;
  571.         info.table = screen->map->table;
  572.         info.dst = SDL_VideoSurface->format;
  573.         RunBlit = screen->map->sw_data->blit;
  574.  
  575.         /* Run the actual software blit */
  576.         RunBlit(&info);
  577. }
  578.  
  579. void SDL_DrawCursorNoLock(SDL_Surface *screen)
  580. {
  581.         SDL_Rect area;
  582.  
  583.         /* Get the mouse rectangle, clipped to the screen */
  584.         SDL_MouseRect(&area);
  585.         if ( (area.w == 0) || (area.h == 0) ) {
  586.                 return;
  587.         }
  588.  
  589.         /* Copy mouse background */
  590.         { int w, h, screenbpp;
  591.           Uint8 *src, *dst;
  592.  
  593.           /* Set up the copy pointers */
  594.           screenbpp = screen->format->BytesPerPixel;
  595.           if ( (screen == SDL_VideoSurface) ||
  596.                   FORMAT_EQUAL(screen->format, SDL_VideoSurface->format) ) {
  597.                 dst = SDL_cursor->save[0];
  598.           } else {
  599.                 dst = SDL_cursor->save[1];
  600.           }
  601.           src = (Uint8 *)screen->pixels + area.y * screen->pitch +
  602.                                           area.x * screenbpp;
  603.  
  604.           /* Perform the copy */
  605.           w = area.w*screenbpp;
  606.           h = area.h;
  607.           while ( h-- ) {
  608.                   memcpy(dst, src, w);
  609.                   dst += w;
  610.                   src += screen->pitch;
  611.           }
  612.         }
  613.  
  614.         /* Draw the mouse cursor */
  615.         area.x -= SDL_cursor->area.x;
  616.         area.y -= SDL_cursor->area.y;
  617.         if ( (area.x == 0) && (area.w == SDL_cursor->area.w) ) {
  618.                 SDL_DrawCursorFast(screen, &area);
  619.         } else {
  620.                 SDL_DrawCursorSlow(screen, &area);
  621.         }
  622. }
  623.  
  624. void SDL_DrawCursor(SDL_Surface *screen)
  625. {
  626.         /* Lock the screen if necessary */
  627.         if ( screen == NULL ) {
  628.                 return;
  629.         }
  630.         if ( SDL_MUSTLOCK(screen) ) {
  631.                 if ( SDL_LockSurface(screen) < 0 ) {
  632.                         return;
  633.                 }
  634.         }
  635.  
  636.         SDL_DrawCursorNoLock(screen);
  637.  
  638.         /* Unlock the screen and update if necessary */
  639.         if ( SDL_MUSTLOCK(screen) ) {
  640.                 SDL_UnlockSurface(screen);
  641.         }
  642.         if ( (screen == SDL_VideoSurface) &&
  643.              ((screen->flags & SDL_HWSURFACE) != SDL_HWSURFACE) ) {
  644.                 SDL_VideoDevice *video = current_video;
  645.                 SDL_VideoDevice *this  = current_video;
  646.                 SDL_Rect area;
  647.  
  648.                 SDL_MouseRect(&area);
  649.  
  650.                 /* This can be called before a video mode is set */
  651.                 if ( video->UpdateRects ) {
  652.                         video->UpdateRects(this, 1, &area);
  653.                 }
  654.         }
  655. }
  656.  
  657. void SDL_EraseCursorNoLock(SDL_Surface *screen)
  658. {
  659.         SDL_Rect area;
  660.  
  661.         /* Get the mouse rectangle, clipped to the screen */
  662.         SDL_MouseRect(&area);
  663.         if ( (area.w == 0) || (area.h == 0) ) {
  664.                 return;
  665.         }
  666.  
  667.         /* Copy mouse background */
  668.         { int w, h, screenbpp;
  669.           Uint8 *src, *dst;
  670.  
  671.           /* Set up the copy pointers */
  672.           screenbpp = screen->format->BytesPerPixel;
  673.           if ( (screen == SDL_VideoSurface) ||
  674.                   FORMAT_EQUAL(screen->format, SDL_VideoSurface->format) ) {
  675.                 src = SDL_cursor->save[0];
  676.           } else {
  677.                 src = SDL_cursor->save[1];
  678.           }
  679.           dst = (Uint8 *)screen->pixels + area.y * screen->pitch +
  680.                                           area.x * screenbpp;
  681.  
  682.           /* Perform the copy */
  683.           w = area.w*screenbpp;
  684.           h = area.h;
  685.           while ( h-- ) {
  686.                   memcpy(dst, src, w);
  687.                   src += w;
  688.                   dst += screen->pitch;
  689.           }
  690.  
  691.           /* Perform pixel conversion on cursor background */
  692.           if ( src > SDL_cursor->save[1] ) {
  693.                 SDL_ConvertCursorSave(screen, area.w, area.h);
  694.           }
  695.         }
  696. }
  697.  
  698. void SDL_EraseCursor(SDL_Surface *screen)
  699. {
  700.         /* Lock the screen if necessary */
  701.         if ( screen == NULL ) {
  702.                 return;
  703.         }
  704.         if ( SDL_MUSTLOCK(screen) ) {
  705.                 if ( SDL_LockSurface(screen) < 0 ) {
  706.                         return;
  707.                 }
  708.         }
  709.  
  710.         SDL_EraseCursorNoLock(screen);
  711.  
  712.         /* Unlock the screen and update if necessary */
  713.         if ( SDL_MUSTLOCK(screen) ) {
  714.                 SDL_UnlockSurface(screen);
  715.         }
  716.         if ( (screen == SDL_VideoSurface) &&
  717.              ((screen->flags & SDL_HWSURFACE) != SDL_HWSURFACE) ) {
  718.                 SDL_VideoDevice *video = current_video;
  719.                 SDL_VideoDevice *this  = current_video;
  720.                 SDL_Rect area;
  721.  
  722.                 SDL_MouseRect(&area);
  723.                 if ( video->UpdateRects ) {
  724.                         video->UpdateRects(this, 1, &area);
  725.                 }
  726.         }
  727. }
  728.  
  729. /* Reset the cursor on video mode change
  730.    FIXME:  Keep track of all cursors, and reset them all.
  731.  */
  732. void SDL_ResetCursor(void)
  733. {
  734.         int savelen;
  735.  
  736.         if ( SDL_cursor ) {
  737.                 savelen = SDL_cursor->area.w*4*SDL_cursor->area.h;
  738.                 SDL_cursor->area.x = 0;
  739.                 SDL_cursor->area.y = 0;
  740.                 memset(SDL_cursor->save[0], 0, savelen);
  741.         }
  742. }
  743.