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