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