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