xref: /haiku/src/servers/app/ServerCursor.cpp (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, Haiku, Inc.
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		ServerCursor.cpp
23 //	Author:			DarkWyrm <bpmagic@columbus.rr.com>
24 //					Stephan Aßmus <superstippi@gmx.de>
25 //	Description:	Glorified ServerBitmap used for cursor work.
26 //
27 //------------------------------------------------------------------------------
28 #include "ServerCursor.h"
29 #include <stdio.h>
30 /*!
31 	\brief Constructor
32 	\param r Size of the cursor
33 	\param cspace Color space of the cursor
34 	\param flags ServerBitmap flags. See Bitmap.h.
35 	\param hotspot Hotspot of the cursor
36 	\param bytesperline Bytes per row for the cursor. See ServerBitmap::ServerBitmap()
37 
38 */
39 ServerCursor::ServerCursor(BRect r, color_space format,
40 						   int32 flags, BPoint hotspot,
41 						   int32 bytesPerRow,
42 						   screen_id screen)
43 	: ServerBitmap(r, format, flags, bytesPerRow, screen),
44 	  fHotSpot(hotspot),
45 	  fOwningTeam(-1)
46 {
47 	fHotSpot.ConstrainTo(Bounds());
48 	_AllocateBuffer();
49 }
50 
51 /*!
52 	\brief Constructor
53 	\param data Pointer to 68-byte cursor data array. See BeBook entry for BCursor for details
54 */
55 ServerCursor::ServerCursor(const int8* data)
56 	: ServerBitmap(BRect(0, 0, 15, 15), B_RGBA32, 0),
57 	  fHotSpot(0, 0),
58 	  fOwningTeam(-1)
59 {
60 	// 68-byte array used in R5 for holding cursors.
61 	// This API has serious problems and should be deprecated(but supported) in R2
62 
63 	// Now that we have all the setup, we're going to map (for now) the cursor
64 	// to RGBA32. Eventually, there will be support for 16 and 8-bit depths
65 	if (data) {
66 		_AllocateBuffer();
67 		uint8* buffer = Bits();
68 		if (!buffer)
69 			return;
70 
71 		fInitialized = true;
72 		uint32 black = 0xFF000000;
73 		uint32 white = 0xFFFFFFFF;
74 
75 		uint16* cursorpos = (uint16*)(data + 4);
76 		uint16* maskpos = (uint16*)(data + 36);
77 		fHotSpot.Set(data[3], data[2]);
78 
79 		uint32* bmppos;
80 		uint16 cursorflip;
81 		uint16 maskflip;
82 		uint16 cursorval;
83 		uint16 maskval;
84 		uint16 powval;
85 
86 		// for each row in the cursor data
87 		for (int32 j = 0; j < 16; j++) {
88 			bmppos = (uint32*)(buffer + (j * BytesPerRow()));
89 
90 			// On intel, our bytes end up swapped, so we must swap them back
91 			cursorflip = (cursorpos[j] & 0xFF) << 8;
92 			cursorflip |= (cursorpos[j] & 0xFF00) >> 8;
93 
94 			maskflip = (maskpos[j] & 0xFF) << 8;
95 			maskflip |= (maskpos[j] & 0xFF00) >> 8;
96 
97 			// for each column in each row of cursor data
98 			for (int32 i = 0; i < 16; i++) {
99 				// Get the values and dump them to the bitmap
100 				powval = 1 << (15 - i);
101 				cursorval = cursorflip & powval;
102 				maskval = maskflip & powval;
103 				bmppos[i] = ((cursorval != 0) ? black : white) &
104 							((maskval > 0) ? 0xFFFFFFFF : 0x00FFFFFF);
105 			}
106 		}
107 	} else {
108 		fWidth = 0;
109 		fHeight = 0;
110 		fBytesPerRow = 0;
111 		fSpace = B_NO_COLOR_SPACE;
112 	}
113 }
114 
115 /*!
116 	\brief Constructor
117 	\param data Pointer to bitmap data in memory, the padding bytes should be contained when format less than 32 bpp.
118 */
119 ServerCursor::ServerCursor(const uint8* alreadyPaddedData,
120 						   uint32 width, uint32 height,
121 						   color_space format)
122 	: ServerBitmap(BRect(0, 0, width - 1, height - 1), format, 0),
123 	  fHotSpot(0, 0),
124 	  fOwningTeam(-1)
125 {
126 	_AllocateBuffer();
127 	if (Bits())
128 		memcpy(Bits(), alreadyPaddedData, BitsLength());
129 }
130 
131 /*!
132 	\brief Copy constructor
133 	\param cursor cursor to copy
134 */
135 ServerCursor::ServerCursor(const ServerCursor* cursor)
136 	: ServerBitmap(cursor),
137 	  fHotSpot(0, 0),
138 	  fOwningTeam(-1)
139 {
140 	// TODO: Hm. I don't move this into the if clause,
141 	// because it might break code elsewhere.
142 	_AllocateBuffer();
143 
144 	if (cursor) {
145 		fInitialized = true;
146 		if (Bits() && cursor->Bits())
147 			memcpy(Bits(), cursor->Bits(), BitsLength());
148 		fHotSpot = cursor->fHotSpot;
149 	}
150 }
151 
152 //!	Frees the heap space allocated for the cursor's image data
153 ServerCursor::~ServerCursor()
154 {
155 	_FreeBuffer();
156 }
157 
158 /*!
159 	\brief Sets the cursor's hotspot
160 	\param pt New location of hotspot, constrained to the cursor's boundaries.
161 */
162 void
163 ServerCursor::SetHotSpot(BPoint pt)
164 {
165 	fHotSpot = pt;
166 	fHotSpot.ConstrainTo(Bounds());
167 }
168 
169 // SetAppSignature
170 void
171 ServerCursor::SetAppSignature(const char* signature)
172 {
173 	fAppSignature.SetTo((signature) ? signature : "");
174 }
175