summaryrefslogtreecommitdiff
path: root/src/riva_cursor.c
blob: 67f8babc89475e425ffdf5d51a4f20ca8e41c0bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright 1996-1997  David J. McKay
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * DAVID J. MCKAY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* Rewritten with reference from mga driver and 3.3.4 NVIDIA driver by
   Jarno Paananen <jpaana@s2.org> */

/* $XFree86$ */

#include "riva_include.h"

#include "cursorstr.h"

/****************************************************************************\
*                                                                            *
*                          HW Cursor Entrypoints                             *
*                                                                            *
\****************************************************************************/

#define TRANSPARENT_PIXEL   0

#define ConvertToRGB555(c) \
(((c & 0xf80000) >> 9 ) | ((c & 0xf800) >> 6 ) | ((c & 0xf8) >> 3 ) | 0x8000)


static void 
RivaConvertCursor1555(RivaPtr pRiva, CARD32 *src, CARD16 *dst)
{
    CARD32 b, m;
    int i, j;
    
    for ( i = 0; i < 32; i++ ) {
        b = *src++;
        m = *src++;
        for ( j = 0; j < 32; j++ ) {
            if ( m & 1 )
                *dst = ( b & 1) ? pRiva->curFg : pRiva->curBg;
            else
                *dst = TRANSPARENT_PIXEL;
            b >>= 1;
            m >>= 1;
            dst++;
        }
    }
}


static void
RivaTransformCursor (RivaPtr pRiva)
{
    CARD32 *tmp;
    int i, dwords;

    dwords = (32 * 32) >> 1;
    if(!(tmp = ALLOCATE_LOCAL(dwords * 4))) return;
    RivaConvertCursor1555(pRiva, pRiva->curImage, (CARD16*)tmp);

    for(i = 0; i < dwords; i++)
        pRiva->riva.CURSOR[i] = tmp[i];

    DEALLOCATE_LOCAL(tmp);
}

static void
RivaLoadCursorImage( ScrnInfoPtr pScrn, unsigned char *src )
{
    RivaPtr pRiva = RivaPTR(pScrn);

    /* save copy of image for color changes */
    memcpy(pRiva->curImage, src, 256);

    RivaTransformCursor(pRiva);
}

static void
RivaSetCursorPosition(ScrnInfoPtr pScrn, int x, int y)
{
    RivaPtr pRiva = RivaPTR(pScrn);

    pRiva->riva.PRAMDAC[0x0000300/4] = (x & 0xFFFF) | (y << 16);
}

static void
RivaSetCursorColors(ScrnInfoPtr pScrn, int bg, int fg)
{
    RivaPtr pRiva = RivaPTR(pScrn);
    CARD32 fore, back;

    fore = ConvertToRGB555(fg);
    back = ConvertToRGB555(bg);

    if ((pRiva->curFg != fore) || (pRiva->curBg != back)) {
        pRiva->curFg = fore;
        pRiva->curBg = back;
            
        RivaTransformCursor(pRiva);
    }
}


static void 
RivaShowCursor(ScrnInfoPtr pScrn)
{
    RivaPtr pRiva = RivaPTR(pScrn);
    /* Enable cursor - X-Windows mode */
    pRiva->riva.ShowHideCursor(&pRiva->riva, 1);
}

static void
RivaHideCursor(ScrnInfoPtr pScrn)
{
    RivaPtr pRiva = RivaPTR(pScrn);
    /* Disable cursor */
    pRiva->riva.ShowHideCursor(&pRiva->riva, 0);
}

static Bool 
RivaUseHWCursor(ScreenPtr pScreen, CursorPtr pCurs)
{
    return TRUE;
}


Bool 
RivaCursorInit(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    RivaPtr pRiva = RivaPTR(pScrn);
    xf86CursorInfoPtr infoPtr;

    infoPtr = xf86CreateCursorInfoRec();
    if(!infoPtr) return FALSE;
    
    pRiva->CursorInfoRec = infoPtr;

    infoPtr->MaxWidth = infoPtr->MaxHeight = 32;
    infoPtr->Flags = HARDWARE_CURSOR_TRUECOLOR_AT_8BPP |
                     HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_32; 
    infoPtr->SetCursorColors = RivaSetCursorColors;
    infoPtr->SetCursorPosition = RivaSetCursorPosition;
    infoPtr->LoadCursorImage = RivaLoadCursorImage;
    infoPtr->HideCursor = RivaHideCursor;
    infoPtr->ShowCursor = RivaShowCursor;
    infoPtr->UseHWCursor = RivaUseHWCursor;

    return(xf86InitCursor(pScreen, infoPtr));
}