summaryrefslogtreecommitdiffstats
path: root/src/glut/fbdev/cursor.c
blob: 4bb2b7fba0128756a4fc2257fdc641fb8d811d72 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Mesa 3-D graphics library
 * Version:  6.5
 * Copyright (C) 1995-2006  Brian Paul
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Library for glut using mesa fbdev driver
 *
 * Written by Sean D'Epagnier (c) 2006
 */

/* these routines are written to access graphics memory directly, not using mesa
   to render the cursor, this is faster, it would be good to use a hardware
   cursor if it exists instead */

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>

#include <linux/fb.h>

#include <GL/glut.h>

#include "internal.h"
#include "cursors.h"

int CurrentCursor = GLUT_CURSOR_LEFT_ARROW;

static int LastMouseX, LastMouseY;
static unsigned char *MouseBuffer;

void InitializeCursor(void)
{
   if(!MouseBuffer && (MouseBuffer = malloc(CURSOR_WIDTH * CURSOR_HEIGHT
			    * VarInfo.bits_per_pixel / 8)) == NULL) {
      sprintf(exiterror, "malloc failure\n");
      exit(0);
   }

   MouseX = VarInfo.xres / 2;
   MouseY = VarInfo.yres / 2;
}

void EraseCursor(void)
{
   int off = LastMouseY * FixedInfo.line_length 
      + LastMouseX * VarInfo.bits_per_pixel / 8;
   int stride = CURSOR_WIDTH * VarInfo.bits_per_pixel / 8;
   int i;

   unsigned char *src = MouseBuffer;

   if(!MouseVisible || CurrentCursor < 0 || CurrentCursor >= NUM_CURSORS)
      return;

   for(i = 0; i<CURSOR_HEIGHT; i++) {
      memcpy(BackBuffer + off, src, stride);
      src += stride;
      off += FixedInfo.line_length;
   }
}

static void SaveCursor(int x, int y)
{
   int bypp, off, stride, i;
   unsigned char *src = MouseBuffer;

   if(x < 0)
      LastMouseX = 0;
   else
      if(x > (int)VarInfo.xres - CURSOR_WIDTH)
	 LastMouseX = VarInfo.xres - CURSOR_WIDTH;
      else
	 LastMouseX = x;

   if(y < 0)
      LastMouseY = 0;
   else
      if(y > (int)VarInfo.yres - CURSOR_HEIGHT)
	 LastMouseY = VarInfo.yres - CURSOR_HEIGHT;
      else
	 LastMouseY = y;

   bypp = VarInfo.bits_per_pixel / 8;
   off = LastMouseY * FixedInfo.line_length + LastMouseX * bypp;
   stride = CURSOR_WIDTH * bypp;
   for(i = 0; i<CURSOR_HEIGHT; i++) {
      memcpy(src, BackBuffer + off, stride);
      src += stride;
      off += FixedInfo.line_length;
   }
}

void DrawCursor(void)
{
   int i, j, px, py, xoff, xlen, yoff, ylen, bypp, cstride, dstride;
   unsigned char *c;
   const unsigned char *d;

   if(!MouseVisible || CurrentCursor < 0 || CurrentCursor >= NUM_CURSORS)
      return;

   px = MouseX - CursorsXOffset[CurrentCursor];
   py = MouseY - CursorsYOffset[CurrentCursor];

   SaveCursor(px, py);

   xoff = 0;
   if(px < 0)
      xoff = -px;

   xlen = CURSOR_WIDTH;
   if(px + CURSOR_WIDTH > VarInfo.xres)
      xlen = VarInfo.xres - px;

   yoff = 0;
   if(py < 0)
      yoff = -py;

   ylen = CURSOR_HEIGHT;
   if(py + CURSOR_HEIGHT > VarInfo.yres)
      ylen = VarInfo.yres - py;

   bypp = VarInfo.bits_per_pixel / 8;

   c = BackBuffer + FixedInfo.line_length * (py + yoff) + (px + xoff) * bypp;
   cstride = FixedInfo.line_length - bypp * (xlen - xoff);

   d = Cursors[CurrentCursor] + (CURSOR_WIDTH * yoff + xoff)*4;
   dstride = (CURSOR_WIDTH - xlen + xoff) * 4;

   switch(bypp) {
   case 1:
      {
	 const int shift = 8 - REVERSECMAPSIZELOG;
	 for(i = yoff; i < ylen; i++) {
	    for(j = xoff; j < xlen; j++) {
	       if(d[3] < 220)
		  *c = ReverseColorMap
		     [(d[0]+(((int)(RedColorMap[c[0]]>>8)*d[3])>>8))>>shift]
		     [(d[1]+(((int)(GreenColorMap[c[0]]>>8)*d[3])>>8))>>shift]
		     [(d[2]+(((int)(BlueColorMap[c[0]]>>8)*d[3])>>8))>>shift];
	       c++;
	       d+=4;
	    }
	    d += dstride;
	    c += cstride;
	 } 
      } break;
   case 2:
      {
	 uint16_t *e = (void*)c;
	 cstride /= 2;
	 for(i = yoff; i < ylen; i++) {
	    for(j = xoff; j < xlen; j++) {
	       if(d[3] < 220)
		  e[0] = ((((d[0] + (((int)(((e[0] >> 8) & 0xf8) 
		     	 | ((c[0] >> 11) & 0x7)) * d[3]) >> 8)) & 0xf8) << 8)
		     | (((d[1] + (((int)(((e[0] >> 3) & 0xfc)
	   		 | ((e[0] >> 5) & 0x3)) * d[3]) >> 8)) & 0xfc) << 3)
		     | ((d[2] + (((int)(((e[0] << 3) & 0xf8)
		      	 | (e[0] & 0x7)) * d[3]) >> 8)) >> 3));
		
	       e++;
	       d+=4;
	    }
	    d += dstride;
	    e += cstride;
	 }
      } break;
   case 3:
   case 4:
      for(i = yoff; i < ylen; i++) {
	 for(j = xoff; j < xlen; j++) {
	    if(d[3] < 220) {
	       c[0] = d[0] + (((int)c[0] * d[3]) >> 8);
	       c[1] = d[1] + (((int)c[1] * d[3]) >> 8);
	       c[2] = d[2] + (((int)c[2] * d[3]) >> 8);
	    }
		
	    c+=bypp;
	    d+=4;
	 }
	 d += dstride;
	 c += cstride;
      } break;
   }
}

#define MIN(x, y) x < y ? x : y
void SwapCursor(void)
{
   int px = MouseX - CursorsXOffset[CurrentCursor];
   int py = MouseY - CursorsYOffset[CurrentCursor];

   int minx = MIN(px, LastMouseX);
   int sizex = abs(px - LastMouseX);

   int miny = MIN(py, LastMouseY);
   int sizey = abs(py - LastMouseY);

   if(MouseVisible)
      DrawCursor();

   /* now update the portion of the screen that has changed, this is also
      used to hide the mouse if MouseVisible is 0 */
   if(DisplayMode & GLUT_DOUBLE && ((sizex || sizey) || !MouseVisible)) {
      int off, stride, i;
      if(minx < 0)
	 minx = 0;
      if(miny < 0)
	 miny = 0;
	
      if(minx + sizex > VarInfo.xres - CURSOR_WIDTH)
	 sizex = VarInfo.xres - CURSOR_WIDTH - minx;
      if(miny + sizey > VarInfo.yres - CURSOR_HEIGHT)
	 sizey = VarInfo.yres - CURSOR_HEIGHT - miny;
      off = FixedInfo.line_length * miny
	 + minx * VarInfo.bits_per_pixel / 8;
      stride = (sizex + CURSOR_WIDTH) * VarInfo.bits_per_pixel / 8;

      for(i = 0; i < sizey + CURSOR_HEIGHT; i++) {
	 memcpy(FrameBuffer+off, BackBuffer+off, stride);
	 off += FixedInfo.line_length;
      }
   }
}

void glutWarpPointer(int x, int y) 
{
   if(x < 0)
      x = 0;
   if(x >= VarInfo.xres) 
      x = VarInfo.xres - 1;
   MouseX = x;

   if(y < 0)
      y = 0;
   if(y >= VarInfo.yres) 
      y = VarInfo.yres - 1;
   MouseY = y;

   EraseCursor();
   SwapCursor();
}

void glutSetCursor(int cursor)
{
   if(cursor == GLUT_CURSOR_FULL_CROSSHAIR)
      cursor = GLUT_CURSOR_CROSSHAIR;

   EraseCursor();
   MouseVisible = 1;
   CurrentCursor = cursor;
   SwapCursor();
}