xref: /haiku/src/kits/interface/Screen.cpp (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 //	File name:		Screen.cpp
2 
3 //	Description:	BScreen let you retrieve and change the display settings.
4 
5 //	Author:			Stefano Ceccherini (burton666@libero.it)
6 
7 
8 // System includes
9 #include <Screen.h>
10 #include <Window.h>
11 
12 // Private includes
13 #include <PrivateScreen.h>
14 
15 
16 /*!	\brief Creates a BScreen object which represents the display with the given screen_id
17 	\param id The screen_id of the screen to get.
18 
19 	In the current implementation, there is only one display (B_MAIN_SCREEN_ID).
20 	To be sure that the object was correctly constructed, call IsValid().
21 */
22 BScreen::BScreen(screen_id id)
23 {
24 	screen = BPrivateScreen::CheckOut(id);
25 }
26 
27 
28 /*!	\brief Creates a BScreen object which represents the display which contains
29 	the given BWindow.
30 	\param win A BWindow.
31 */
32 BScreen::BScreen(BWindow *win)
33 {
34 	screen = BPrivateScreen::CheckOut(win);
35 }
36 
37 
38 /*!	\brief Releases the resources allocated by the constructor.
39 */
40 BScreen::~BScreen()
41 {
42 	if (screen != NULL)
43 		BPrivateScreen::Return(screen);
44 }
45 
46 
47 /*! \brief Checks if the BScreen object represents a real screen connected to the computer.
48 	\return \c true if the BScreen object is valid, \c false if not.
49 */
50 bool
51 BScreen::IsValid()
52 {
53 	return (screen != NULL);
54 }
55 
56 
57 /*!	\brief In the current implementation, this function always returns B_ERROR.
58 	\return Always \c B_ERROR.
59 */
60 status_t
61 BScreen::SetToNext()
62 {
63 	if (screen != NULL)
64 		return screen->SetToNext();
65 	return B_ERROR;
66 }
67 
68 
69 /*!	\brief Returns the color space of the screen display.
70 	\return \c B_CMAP8, \c B_RGB15, or \c B_RGB32, or \c B_NO_COLOR_SPACE
71 		if the screen object is invalid.
72 */
73 color_space
74 BScreen::ColorSpace()
75 {
76 	if (screen != NULL)
77 		return screen->ColorSpace();
78 	return B_NO_COLOR_SPACE;
79 }
80 
81 
82 /*!	\brief Returns the rectangle that locates the screen in the screen coordinate system.
83 	\return a BRect that locates the screen in the screen coordinate system.
84 */
85 BRect
86 BScreen::Frame()
87 {
88 	if (screen != NULL)
89 		return screen->Frame();
90 	return BRect(0, 0, 0, 0);
91 }
92 
93 
94 /*!	\brief Returns the identifier for the screen.
95 	\return A screen_id struct that identifies the screen.
96 
97 	In the current implementation, this function always returns \c B_MAIN_SCREEN_ID,
98 	even if the object is invalid.
99 */
100 screen_id
101 BScreen::ID()
102 {
103 	if (screen != NULL)
104 		return screen->ID();
105 	return B_MAIN_SCREEN_ID;
106 }
107 
108 
109 /*!	\brief Blocks until the monitor has finished the current vertical retrace.
110 	\return \c B_OK, or \c B_ERROR if the screen object is invalid.
111 */
112 status_t
113 BScreen::WaitForRetrace()
114 {
115 	if (screen != NULL)
116 		return screen->WaitForRetrace(B_INFINITE_TIMEOUT);
117 	return B_ERROR;
118 }
119 
120 
121 /*!	\brief Blocks until the monitor has finished the current vertical retrace,
122 	or until the given timeout has passed.
123 	\param timeout A bigtime_t which indicates the time to wait before returning.
124 	\return \c B_OK if the monitor has retraced in the given amount of time,
125 		\c B_ERROR otherwise.
126 */
127 status_t
128 BScreen::WaitForRetrace(bigtime_t timeout)
129 {
130 	if (screen != NULL)
131 		return screen->WaitForRetrace(timeout);
132 	return B_ERROR;
133 }
134 
135 
136 /*!	\brief Returns the index of the 8-bit color that,
137 		most closely matches the given 32-bit color.
138 	\param r The red value for a 32-bit color.
139 	\param g The green value for a 32-bit color.
140 	\param b The blue value for a 32-bit color.
141 	\param a The alpha value for a 32-bit color.
142 	\return An index for a 8-bit color in the screen's color map.
143 */
144 uint8
145 BScreen::IndexForColor(uint8 r, uint8 g, uint8 b, uint8 a)
146 {
147 	if (screen != NULL)
148 		return screen->IndexForColor(r, g, b, a);
149 	return 0;
150 }
151 
152 
153 /*!	\brief Returns the 32-bit color representation of a given 8-bit color index.
154 	\param index The 8-bit color index to convert.
155 	\return A rgb_color struct which represents the given 8-bit color index.
156 */
157 rgb_color
158 BScreen::ColorForIndex(const uint8 index)
159 {
160 	if (screen != NULL)
161 		return screen->ColorForIndex(index);
162 	return rgb_color();
163 }
164 
165 
166 /*!	\brief Returns the "inversion" ov the given 8-bit color.
167 	\param index An 8-bit color index.
168 	\return An 8-bit color index that represents the "inversion" of the given color.
169 */
170 uint8
171 BScreen::InvertIndex(uint8 index)
172 {
173 	if (screen != NULL)
174 		return screen->InvertIndex(index);
175 	return 0;
176 }
177 
178 
179 /*!	\brief Returns the color map of the current display.
180 	\return A pointer to the object's color_map.
181 */
182 const color_map*
183 BScreen::ColorMap()
184 {
185 	if (screen != NULL)
186 		return screen->ColorMap();
187 	return NULL;
188 }
189 
190 
191 /*!	\brief Copies the screen's contents into the first argument BBitmap.
192 	\param screen_shot A pointer to a BBitmap pointer, where the function will allocate a BBitmap for you.
193 	\param draw_cursor Specifies if you want the cursor to be drawn.
194 	\param bound Let you specify the area you want copied. If it's NULL, the entire screen is copied.
195 	\return \c B_OK if the operation was succesful, \c B_ERROR on failure.
196 */
197 status_t
198 BScreen::GetBitmap(BBitmap **screen_shot, bool draw_cursor, BRect *bound)
199 {
200 	if (screen != NULL)
201 		return screen->GetBitmap(screen_shot, draw_cursor, bound);
202 	return B_ERROR;
203 }
204 
205 
206 /*!	\brief Copies the screen's contents into the first argument BBitmap.
207 	\param screen_shot A pointer to an allocated BBitmap, where the function will store the screen's content.
208 	\param draw_cursor Specifies if you want the cursor to be drawn.
209 	\param bound Let you specify the area you want copied. If it's NULL, the entire screen is copied.
210 	\return \c B_OK if the operation was succesful, \c B_ERROR on failure.
211 
212 	The only difference between this method and GetBitmap() is that ReadBitmap requires you
213 	to allocate a BBitmap, while the latter will allocate a BBitmap for you.
214 */
215 status_t
216 BScreen::ReadBitmap(BBitmap *buffer, bool draw_cursor, BRect *bound)
217 {
218 	if (screen != NULL)
219 		return screen->ReadBitmap(buffer, draw_cursor, bound);
220 	return B_ERROR;
221 }
222 
223 
224 /*!	\brief Returns the color of the desktop.
225 	\return An rgb_color structure which is the color of the desktop.
226 */
227 rgb_color
228 BScreen::DesktopColor()
229 {
230 	if (screen != NULL)
231 		return screen->DesktopColor(B_ALL_WORKSPACES);
232 	return rgb_color();
233 }
234 
235 
236 /*!	\brief Returns the color of the desktop in the given workspace.
237 	\param workspace The workspace of which you want to have the color.
238 	\return An rgb_color structure which is the color of the desktop in the given workspace.
239 */
240 rgb_color
241 BScreen::DesktopColor(uint32 workspace)
242 {
243 	if (screen != NULL)
244 		return screen->DesktopColor(workspace);
245 	return rgb_color();
246 }
247 
248 
249 /*!	\brief Set the color of the desktop.
250 	\param rgb The color you want to paint the desktop background.
251 	\param stick If you pass \c true here, the color will be maintained across boots.
252 */
253 void
254 BScreen::SetDesktopColor(rgb_color rgb, bool stick)
255 {
256 	if (screen != NULL)
257 		screen->SetDesktopColor(rgb, B_ALL_WORKSPACES, stick);
258 }
259 
260 
261 /*!	\brief Set the color of the desktop in the given workspace.
262 	\param rgb The color you want to paint the desktop background.
263 	\param index The workspace you want to change the color.
264 	\param stick If you pass \c true here, the color will be maintained across boots.
265 */
266 void
267 BScreen::SetDesktopColor(rgb_color rgb, uint32 index, bool stick)
268 {
269 	if (screen != NULL)
270 		screen->SetDesktopColor(rgb, index, stick);
271 }
272 
273 
274 /*!	\brief Attempts to adjust the supplied mode so that it's a supported mode.
275 	\param target The mode you want to be adjusted.
276 	\param low The lower limit you want target to be adjusted.
277 	\param high The higher limit you want target to be adjusted.
278 	\return
279 		- \c B_OK if target (as returned) is supported and falls into the limits.
280 		- \c B_BAD_VALUE if target (as returned) is supported but doesn't fall into the limits.
281 		- \c B_ERROR if target isn't supported.
282 */
283 status_t
284 BScreen::ProposeMode(display_mode *target, const display_mode *low, const display_mode *high)
285 {
286 	if (screen != NULL)
287 		return screen->ProposeMode(target, low, high);
288 	return B_ERROR;
289 }
290 
291 
292 /*!	\brief allocates and returns a list of the display_modes
293 		that the graphics card supports.
294 	\param mode_list A pointer to a mode_list pointer, where the function will
295 		allocate an array of display_mode structures.
296 	\param count A pointer to an integer, where the function will store the amount of
297 		available display modes.
298 	\return \c B_OK.
299 */
300 status_t
301 BScreen::GetModeList(display_mode **mode_list, uint32 *count)
302 {
303 	if (screen != NULL)
304 		return screen->GetModeList(mode_list, count);
305 	return B_ERROR;
306 }
307 
308 
309 /*!	\brief Copies the current display_mode into mode.
310 	\param mode A pointer to a display_mode structure,
311 		where the current display_mode will be copied.
312 	\return \c B_OK if the operation was succesful.
313 */
314 status_t
315 BScreen::GetMode(display_mode *mode)
316 {
317 	if (screen != NULL)
318 		return screen->GetMode(B_ALL_WORKSPACES, mode);
319 	return B_ERROR;
320 }
321 
322 
323 /*!	\brief Copies the current display_mode of the given workspace into mode.
324 	\param mode A pointer to a display_mode structure,
325 		where the current display_mode will be copied.
326 	\return \c B_OK if the operation was succesful.
327 */
328 status_t
329 BScreen::GetMode(uint32 workspace, display_mode *mode)
330 {
331 	if (screen != NULL)
332 		return screen->GetMode(workspace, mode);
333 	return B_ERROR;
334 }
335 
336 
337 /*!	\brief Set the screen to the given mode.
338 	\param mode A pointer to a display_mode.
339 	\param makeDefault If true, the mode becomes the default for the screen.
340 	\return \c B_OK.
341 */
342 status_t
343 BScreen::SetMode(display_mode *mode, bool makeDefault)
344 {
345 	if (screen != NULL)
346 		return screen->SetMode(B_ALL_WORKSPACES, mode, makeDefault);
347 	return B_ERROR;
348 }
349 
350 
351 /*!	\brief Set the given workspace to the given mode.
352 	\param workspace The workspace that you want to change.
353 	\param mode A pointer to a display_mode.
354 	\param makeDefault If true, the mode becomes the default for the workspace.
355 	\return \c B_OK.
356 */
357 status_t
358 BScreen::SetMode(uint32 workspace, display_mode *mode, bool makeDefault)
359 {
360 	if (screen != NULL)
361 		return screen->SetMode(workspace, mode, makeDefault);
362 	return B_ERROR;
363 }
364 
365 
366 /*!	\brief Returns information about the graphics card.
367 	\param info An accelerant_device_info struct where to store the retrieved info.
368 	\return \c B_OK if the operation went fine, otherwise an error code.
369 */
370 status_t
371 BScreen::GetDeviceInfo(accelerant_device_info *info)
372 {
373 	if (screen != NULL)
374 		return screen->GetDeviceInfo(info);
375 	return B_ERROR;
376 }
377 
378 
379 /*!	\brief Returns, in low and high, the minimum and maximum pixel clock rates
380 		that are possible for the given mode.
381 	\param mode A pointer to a display_mode.
382 	\param low A pointer to an int where the function will store the lowest available pixel clock.
383 	\param high A pointer to an int where the function wills tore the highest available pixel clock.
384 	\return \c B_OK if the operation went fine, otherwise an error code.
385 */
386 status_t
387 BScreen::GetPixelClockLimits(display_mode *mode, uint32 *low, uint32 *high)
388 {
389 	if (screen != NULL)
390 		return screen->GetPixelClockLimits(mode, low, high);
391 	return B_ERROR;
392 }
393 
394 
395 /*!	\brief Fills out the dtc structure with the timing constraints of the current display mode.
396 	\param dtc A pointer to a display_timing_constraints structure where the function will store
397 		the timing constraints of the current mode.
398 	\return \c B_OK if the operation went fine, otherwise an error code.
399 */
400 status_t
401 BScreen::GetTimingConstraints(display_timing_constraints *dtc)
402 {
403 	if (screen != NULL)
404 		return screen->GetTimingConstraints(dtc);
405 	return B_ERROR;
406 }
407 
408 
409 /*!	\brief Lets you set the VESA Display Power Management Signaling state for the screen.
410 	\param dpms_state The DPMS state you want to be set.
411 		valid values are:
412 		- \c B_DPMS_ON
413 		- \c B_DPMS_STAND_BY
414 		- \c B_DPMS_SUSPEND
415 		- \c B_DPMS_OFF
416 	\return \c B_OK if the operation went fine, otherwise an error code.
417 */
418 status_t
419 BScreen::SetDPMS(uint32 dpms_state)
420 {
421 	if (screen != NULL)
422 		return screen->SetDPMS(dpms_state);
423 	return B_ERROR;
424 }
425 
426 
427 /*!	\brief Returns the current DPMS state of the screen.
428 */
429 uint32
430 BScreen::DPMSState()
431 {
432 	if (screen != NULL)
433 		return screen->DPMSState();
434 	return B_ERROR;
435 }
436 
437 
438 /*!	\brief Indicates which DPMS modes the monitor supports.
439 */
440 uint32
441 BScreen::DPMSCapabilites()
442 {
443 	if (screen != NULL)
444 		return screen->DPMSCapabilites();
445 	return B_ERROR;
446 }
447 
448 
449 /*!	\brief Returns the BPrivateScreen used by the BScreen object.
450 	\return A pointer to the BPrivateScreen class internally used by the BScreen object.
451 */
452 BPrivateScreen*
453 BScreen::private_screen()
454 {
455 	return screen;
456 }
457 
458 
459 /*----- Private or reserved -----------------------------------------*/
460 
461 /*!	\brief Deprecated, use ProposeMode() instead.
462 */
463 status_t
464 BScreen::ProposeDisplayMode(display_mode *target, const display_mode *low, const display_mode *high)
465 {
466 	return ProposeMode(target, low, high);
467 }
468 
469 
470 /*!	\brief Copy not allowed.
471 */
472 BScreen&
473 BScreen::operator=(const BScreen&)
474 {
475 	return *this;
476 }
477 
478 
479 /*!	\brief Copy not allowed.
480 */
481 BScreen::BScreen(const BScreen &screen)
482 {
483 }
484 
485 
486 /*!	\brief Returns the base address of the framebuffer.
487 */
488 void*
489 BScreen::BaseAddress()
490 {
491 	if (screen != NULL)
492 		return screen->BaseAddress();
493 	return NULL;
494 }
495 
496 
497 /*!	\brief Returns the amount of bytes per row of the framebuffer.
498 */
499 uint32
500 BScreen::BytesPerRow()
501 {
502 	if (screen != NULL)
503 		return screen->BytesPerRow();
504 	return 0;
505 }
506