xref: /haiku/src/kits/interface/InterfaceDefs.cpp (revision d67f94ca9dc693a2734456fa13111d2036bc7297)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2005, Haiku
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:		InterfaceDefs.cpp
23 //	Author:			DarkWyrm <bpmagic@columbus.rr.com>
24 //					Caz <turok2@currantbun.com>
25 //					Axel Dörfler <axeld@pinc-software.de>
26 //	Description:	Global functions and variables for the Interface Kit
27 //
28 //------------------------------------------------------------------------------
29 
30 #include <AppServerLink.h>
31 #include <InterfaceDefs.h>
32 #include <ServerProtocol.h>
33 #include <ScrollBar.h>
34 #include <Screen.h>
35 #include <Roster.h>
36 #include <Menu.h>
37 #include <stdlib.h>
38 #include <TextView.h>
39 
40 #include <WidthBuffer.h>
41 
42 // Private definitions not placed in public headers
43 extern "C" void _init_global_fonts();
44 extern "C" status_t _fini_interface_kit_();
45 
46 #include <InputServerTypes.h>
47 #include <input_globals.h>
48 
49 using namespace BPrivate;
50 
51 
52 _IMPEXP_BE const color_map *
53 system_colors()
54 {
55 	return BScreen(B_MAIN_SCREEN_ID).ColorMap();
56 }
57 
58 #ifndef COMPILE_FOR_R5
59 
60 _IMPEXP_BE status_t
61 set_screen_space(int32 index, uint32 res, bool stick)
62 {
63 	BAppServerLink link;
64 	int32 code = SERVER_FALSE;
65 
66 	link.StartMessage(AS_SET_SCREEN_MODE);
67 	link.Attach<int32>(index);
68 	link.Attach<int32>((int32)res);
69 	link.Attach<bool>(stick);
70 	link.FlushWithReply(&code);
71 
72 	return ((code==SERVER_TRUE)?B_OK:B_ERROR);
73 }
74 
75 
76 _IMPEXP_BE status_t
77 get_scroll_bar_info(scroll_bar_info *info)
78 {
79 	if (info == NULL)
80 		return B_BAD_VALUE;
81 
82 	BAppServerLink link;
83 	int32 code;
84 	link.StartMessage(AS_GET_SCROLLBAR_INFO);
85 	link.FlushWithReply(&code);
86 	link.Read<scroll_bar_info>(info);
87 
88 	return B_OK;
89 }
90 
91 
92 _IMPEXP_BE status_t
93 set_scroll_bar_info(scroll_bar_info *info)
94 {
95 	if (info == NULL)
96 		return B_BAD_VALUE;
97 
98 	BAppServerLink link;
99 	int32 code;
100 
101 	link.StartMessage(AS_SET_SCROLLBAR_INFO);
102 	link.Attach<scroll_bar_info>(*info);
103 	link.FlushWithReply(&code);
104 	return B_OK;
105 }
106 
107 #endif // COMPILE_FOR_R5
108 
109 _IMPEXP_BE status_t
110 get_mouse_type(int32 *type)
111 {
112 	BMessage command(IS_GET_MOUSE_TYPE);
113 	BMessage reply;
114 
115 	_control_input_server_(&command, &reply);
116 
117 	if(reply.FindInt32("mouse_type", type) != B_OK)
118 		return B_ERROR;
119 
120 	return B_OK;
121 }
122 
123 
124 _IMPEXP_BE status_t
125 set_mouse_type(int32 type)
126 {
127 	BMessage command(IS_SET_MOUSE_TYPE);
128 	BMessage reply;
129 
130 	command.AddInt32("mouse_type", type);
131 	return _control_input_server_(&command, &reply);
132 }
133 
134 
135 _IMPEXP_BE status_t
136 get_mouse_map(mouse_map *map)
137 {
138 	BMessage command(IS_GET_MOUSE_MAP);
139 	BMessage reply;
140 	const void *data = 0;
141 	ssize_t count;
142 
143 	_control_input_server_(&command, &reply);
144 
145 	if(reply.FindData("mousemap", B_ANY_TYPE, &data, &count) != B_OK)
146 		return B_ERROR;
147 
148 	memcpy(map, data, count);
149 
150 	return B_OK;
151 }
152 
153 
154 _IMPEXP_BE status_t
155 set_mouse_map(mouse_map *map)
156 {
157 	BMessage command(IS_SET_MOUSE_MAP);
158 	BMessage reply;
159 
160 	command.AddData("mousemap", B_ANY_TYPE, map, sizeof(mouse_map));
161 	return _control_input_server_(&command, &reply);
162 }
163 
164 
165 _IMPEXP_BE status_t
166 get_click_speed(bigtime_t *speed)
167 {
168 	BMessage command(IS_GET_CLICK_SPEED);
169 	BMessage reply;
170 
171 	_control_input_server_(&command, &reply);
172 
173 	if(reply.FindInt64("speed", speed) != B_OK)
174 		return B_ERROR;
175 
176 	return B_OK;
177 }
178 
179 
180 _IMPEXP_BE status_t
181 set_click_speed(bigtime_t speed)
182 {
183 	BMessage command(IS_SET_CLICK_SPEED);
184 	BMessage reply;
185 	command.AddInt64("speed", speed);
186 	return _control_input_server_(&command, &reply);
187 }
188 
189 
190 _IMPEXP_BE status_t
191 get_mouse_speed(int32 *speed)
192 {
193 	BMessage command(IS_GET_MOUSE_SPEED);
194 	BMessage reply;
195 
196 	_control_input_server_(&command, &reply);
197 
198 	if(reply.FindInt32("speed", speed) != B_OK)
199 		return B_ERROR;
200 
201 	return B_OK;
202 }
203 
204 
205 _IMPEXP_BE status_t
206 set_mouse_speed(int32 speed)
207 {
208 	BMessage command(IS_SET_MOUSE_SPEED);
209 	BMessage reply;
210 	command.AddInt32("speed", speed);
211 	return _control_input_server_(&command, &reply);
212 }
213 
214 
215 _IMPEXP_BE status_t
216 get_mouse_acceleration(int32 *speed)
217 {
218 	BMessage command(IS_GET_MOUSE_ACCELERATION);
219 	BMessage reply;
220 
221 	_control_input_server_(&command, &reply);
222 
223 	if(reply.FindInt32("speed", speed) != B_OK)
224 		return B_ERROR;
225 
226 	return B_OK;
227 }
228 
229 
230 _IMPEXP_BE status_t
231 set_mouse_acceleration(int32 speed)
232 {
233 	BMessage command(IS_SET_MOUSE_ACCELERATION);
234 	BMessage reply;
235 	command.AddInt32("speed", speed);
236 	return _control_input_server_(&command, &reply);
237 }
238 
239 
240 _IMPEXP_BE status_t
241 get_key_repeat_rate(int32 *rate)
242 {
243 	BMessage command(IS_GET_KEY_REPEAT_RATE);
244 	BMessage reply;
245 
246 	_control_input_server_(&command, &reply);
247 
248 	if(reply.FindInt32("rate", rate) != B_OK)
249 		return B_ERROR;
250 
251 	return B_OK;
252 }
253 
254 
255 _IMPEXP_BE status_t
256 set_key_repeat_rate(int32 rate)
257 {
258 	BMessage command(IS_SET_KEY_REPEAT_RATE);
259 	BMessage reply;
260 	command.AddInt32("rate", rate);
261 	return _control_input_server_(&command, &reply);
262 }
263 
264 
265 _IMPEXP_BE status_t
266 get_key_repeat_delay(bigtime_t *delay)
267 {
268 	BMessage command(IS_GET_KEY_REPEAT_DELAY);
269 	BMessage reply;
270 
271 	_control_input_server_(&command, &reply);
272 
273 	if(reply.FindInt64("delay", delay) != B_OK)
274 		return B_ERROR;
275 
276 	return B_OK;
277 }
278 
279 
280 _IMPEXP_BE status_t
281 set_key_repeat_delay(bigtime_t  delay)
282 {
283 	BMessage command(IS_SET_KEY_REPEAT_DELAY);
284 	BMessage reply;
285 	command.AddInt64("delay", delay);
286 	return _control_input_server_(&command, &reply);
287 }
288 
289 
290 _IMPEXP_BE uint32
291 modifiers()
292 {
293 	BMessage command(IS_GET_MODIFIERS);
294 	BMessage reply;
295 	int32 err, modifier;
296 
297 	_control_input_server_(&command, &reply);
298 
299 	if(reply.FindInt32("status", &err) != B_OK)
300 		return 0;
301 
302 	if(reply.FindInt32("modifiers", &modifier) != B_OK)
303 		return 0;
304 
305 	return modifier;
306 }
307 
308 
309 _IMPEXP_BE status_t
310 get_key_info(key_info *info)
311 {
312 	BMessage command(IS_GET_KEY_INFO);
313 	BMessage reply;
314 	const void *data = 0;
315 	int32 err;
316 	ssize_t count;
317 
318 	_control_input_server_(&command, &reply);
319 
320 	if(reply.FindInt32("status", &err) != B_OK)
321 		return B_ERROR;
322 
323 	if(reply.FindData("key_info", B_ANY_TYPE, &data, &count) != B_OK)
324 		return B_ERROR;
325 
326 	memcpy(info, data, count);
327 	return B_OK;
328 }
329 
330 
331 _IMPEXP_BE void
332 get_key_map(key_map **map, char **key_buffer)
333 {
334 	BMessage command(IS_GET_KEY_MAP);
335 	BMessage reply;
336 	ssize_t map_count, key_count;
337 	const void *map_array = 0, *key_array = 0;
338 
339 	_control_input_server_(&command, &reply);
340 
341 	if(reply.FindData("keymap", B_ANY_TYPE, &map_array, &map_count) != B_OK)
342 	{
343 		*map = 0; *key_buffer = 0;
344 		return;
345 	}
346 
347 	if(reply.FindData("key_buffer", B_ANY_TYPE, &key_array, &key_count) != B_OK)
348 	{
349 		*map = 0; *key_buffer = 0;
350 		return;
351 	}
352 
353 	*map = (key_map *)malloc(map_count);
354 	memcpy(*map, map_array, map_count);
355 	*key_buffer = (char *)malloc(key_count);
356 	memcpy(*key_buffer, key_array, key_count);
357 }
358 
359 
360 _IMPEXP_BE status_t
361 get_keyboard_id(uint16 *id)
362 {
363 	BMessage command(IS_GET_KEYBOARD_ID);
364 	BMessage reply;
365 	uint16 kid;
366 
367 	_control_input_server_(&command, &reply);
368 
369 	reply.FindInt16("id", (int16 *)&kid);
370 	*id = kid;
371 
372 	return B_OK;
373 }
374 
375 
376 _IMPEXP_BE void
377 set_modifier_key(uint32 modifier, uint32 key)
378 {
379 	BMessage command(IS_SET_MODIFIER_KEY);
380 	BMessage reply;
381 
382 	command.AddInt32("modifier", modifier);
383 	command.AddInt32("key", key);
384 	_control_input_server_(&command, &reply);
385 }
386 
387 
388 _IMPEXP_BE void
389 set_keyboard_locks(uint32 modifiers)
390 {
391 	BMessage command(IS_SET_KEYBOARD_LOCKS);
392 	BMessage reply;
393 
394 	command.AddInt32("locks", modifiers);
395 	_control_input_server_(&command, &reply);
396 }
397 
398 
399 _IMPEXP_BE status_t
400 _restore_key_map_()
401 {
402 	BMessage message(IS_RESTORE_KEY_MAP);
403 	BMessage reply;
404 
405 	return _control_input_server_(&message, &reply);
406 }
407 
408 
409 _IMPEXP_BE rgb_color
410 keyboard_navigation_color()
411 {
412 	// Queries the app_server
413 	return ui_color(B_KEYBOARD_NAVIGATION_COLOR);
414 }
415 
416 
417 #ifndef COMPILE_FOR_R5
418 
419 _IMPEXP_BE int32
420 count_workspaces()
421 {
422 	int32 count;
423 
424 	BAppServerLink link;
425 	int32 code;
426 	link.StartMessage(AS_COUNT_WORKSPACES);
427 	link.FlushWithReply(&code);
428 	link.Read<int32>(&count);
429 	return count;
430 }
431 
432 
433 _IMPEXP_BE void
434 set_workspace_count(int32 count)
435 {
436 	BAppServerLink link;
437 	link.StartMessage(AS_SET_WORKSPACE_COUNT);
438 	link.Attach<int32>(count);
439 	link.Flush();
440 }
441 
442 
443 _IMPEXP_BE int32
444 current_workspace()
445 {
446 	int32 index;
447 
448 	BAppServerLink link;
449 	int32 code;
450 	link.StartMessage(AS_CURRENT_WORKSPACE);
451 	link.FlushWithReply(&code);
452 	link.Read<int32>(&index);
453 
454 	return index;
455 }
456 
457 
458 _IMPEXP_BE void
459 activate_workspace(int32 workspace)
460 {
461 	BAppServerLink link;
462 	link.StartMessage(AS_ACTIVATE_WORKSPACE);
463 	link.Attach<int32>(workspace);
464 	link.Flush();
465 }
466 
467 
468 _IMPEXP_BE bigtime_t
469 idle_time()
470 {
471 	bigtime_t idletime;
472 
473 	BAppServerLink link;
474 	int32 code;
475 	link.StartMessage(AS_IDLE_TIME);
476 	link.FlushWithReply(&code);
477 	link.Read<int64>(&idletime);
478 
479 	return idletime;
480 }
481 
482 
483 _IMPEXP_BE void
484 run_select_printer_panel()
485 {
486 	// Launches the Printer prefs app via the Roster
487 	be_roster->Launch("application/x-vnd.Be-PRNT");
488 }
489 
490 
491 _IMPEXP_BE void
492 run_add_printer_panel()
493 {
494 	// Launches the Printer prefs app via the Roster and asks it to
495 	// add a printer
496 	// TODO: Implement
497 }
498 
499 
500 _IMPEXP_BE void
501 run_be_about()
502 {
503 	// Unsure about how to implement this.
504 	// TODO: Implement
505 }
506 
507 
508 _IMPEXP_BE void
509 set_focus_follows_mouse(bool follow)
510 {
511 	BAppServerLink link;
512 	link.StartMessage(AS_SET_FOCUS_FOLLOWS_MOUSE);
513 	link.Attach<bool>(follow);
514 	link.Flush();
515 }
516 
517 
518 _IMPEXP_BE bool
519 focus_follows_mouse()
520 {
521 	bool ffm;
522 
523 	BAppServerLink link;
524 	int32 code;
525 	link.StartMessage(AS_FOCUS_FOLLOWS_MOUSE);
526 	link.FlushWithReply(&code);
527 	link.Read<bool>(&ffm);
528 	return ffm;
529 }
530 
531 
532 _IMPEXP_BE void
533 set_mouse_mode(mode_mouse mode)
534 {
535 	BAppServerLink link;
536 	link.StartMessage(AS_SET_MOUSE_MODE);
537 	link.Attach<mode_mouse>(mode);
538 	link.Flush();
539 }
540 
541 
542 _IMPEXP_BE mode_mouse
543 mouse_mode()
544 {
545 	mode_mouse mode;
546 
547 	BAppServerLink link;
548 	int32 code;
549 	link.StartMessage(AS_GET_MOUSE_MODE);
550 	link.FlushWithReply(&code);
551 	link.Read<mode_mouse>(&mode);
552 	return mode;
553 }
554 
555 
556 _IMPEXP_BE rgb_color
557 ui_color(color_which which)
558 {
559 	rgb_color color;
560 
561 	BAppServerLink link;
562 	int32 code;
563 	link.StartMessage(AS_GET_UI_COLOR);
564 	link.Attach<color_which>(which);
565 	link.FlushWithReply(&code);
566 	if(code==SERVER_TRUE)
567 		link.Read<rgb_color>(&color);
568 	return color;
569 }
570 
571 
572 _IMPEXP_BE rgb_color
573 tint_color(rgb_color color, float tint)
574 {
575 	rgb_color result;
576 
577 	#define LIGHTEN(x) ((uint8)(255.0f - (255.0f - x) * tint))
578 	#define DARKEN(x)  ((uint8)(x * (2 - tint)))
579 
580 	if (tint < 1.0f) {
581 		result.red   = LIGHTEN(color.red);
582 		result.green = LIGHTEN(color.green);
583 		result.blue  = LIGHTEN(color.blue);
584 		result.alpha = color.alpha;
585 	} else {
586 		result.red   = DARKEN(color.red);
587 		result.green = DARKEN(color.green);
588 		result.blue  = DARKEN(color.blue);
589 		result.alpha = color.alpha;
590 	}
591 
592 	#undef LIGHTEN
593 	#undef DARKEN
594 
595 	return result;
596 }
597 
598 
599 static status_t
600 load_menu_settings(menu_info &into)
601 {
602 	// TODO: Load settings from the settings file,
603 	// and only if it fails, fallback to the defaults
604 
605 	into.font_size = be_plain_font->Size();
606 	be_plain_font->GetFamilyAndStyle(&into.f_family, &into.f_style);
607 	into.background_color = ui_color(B_MENU_BACKGROUND_COLOR);
608 	into.separator = 0;
609 	into.click_to_open = false;
610 	into.triggers_always_shown = false;
611 
612 	return B_OK;
613 }
614 
615 
616 extern "C" status_t
617 _init_interface_kit_()
618 {
619 	sem_id widthSem = create_sem(0, "BTextView WidthBuffer Sem");
620 	if (widthSem < 0)
621 		return widthSem;
622 	BTextView::sWidthSem = widthSem;
623 	BTextView::sWidthAtom = 0;
624 	BTextView::sWidths = new _BWidthBuffer_;
625 
626 	status_t status = load_menu_settings(BMenu::sMenuInfo);
627 
628 	// TODO: fill the other static members
629 
630 	return status;
631 }
632 
633 
634 extern "C" status_t
635 _fini_interface_kit_()
636 {
637 	//TODO: Implement ?
638 
639 	return B_OK;
640 }
641 
642 /*!
643 	\brief private function used by Tracker to set window decor
644 	\param theme The theme to choose
645 
646 	- \c 0: BeOS
647 	- \c 1: AmigaOS
648 	- \c 2: Win95
649 	- \c 3: MacOS
650 */
651 void
652 __set_window_decor(int32 theme)
653 {
654 	BAppServerLink link;
655 	link.StartMessage(AS_R5_SET_DECORATOR);
656 	link.Attach<int32>(theme);
657 	link.Flush();
658 }
659 
660 #endif // COMPILE_FOR_R5
661