2015-09-16 23:15:17 +02:00
|
|
|
/*
|
|
|
|
* \brief Decorator configuration handling
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2015-09-17
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Genode Labs GmbH
|
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _CONFIG_H_
|
|
|
|
#define _CONFIG_H_
|
|
|
|
|
|
|
|
/* Genode includes */
|
2016-12-01 17:37:08 +01:00
|
|
|
#include <util/reconstructible.h>
|
2015-09-16 23:15:17 +02:00
|
|
|
#include <os/session_policy.h>
|
|
|
|
#include <util/color.h>
|
|
|
|
|
|
|
|
/* decorator includes */
|
|
|
|
#include <decorator/types.h>
|
|
|
|
|
|
|
|
namespace Decorator {
|
|
|
|
|
|
|
|
class Config;
|
|
|
|
typedef Genode::String<200> Window_title;
|
2016-11-15 18:01:28 +01:00
|
|
|
|
|
|
|
using Genode::Allocator;
|
2016-12-01 17:37:08 +01:00
|
|
|
using Genode::Reconstructible;
|
2015-09-16 23:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Decorator::Config
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
class Window_control
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum Type { TYPE_CLOSER, TYPE_TITLE, TYPE_MAXIMIZER,
|
|
|
|
TYPE_MINIMIZER, TYPE_UNMAXIMIZER, TYPE_UNDEFINED };
|
|
|
|
|
|
|
|
enum Align { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT };
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
Type _type = TYPE_UNDEFINED;
|
|
|
|
Align _align = ALIGN_CENTER;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Window_control() { }
|
|
|
|
|
|
|
|
Window_control(Type type, Align align)
|
|
|
|
: _type(type), _align(align) { }
|
|
|
|
|
|
|
|
Type type() const { return _type; }
|
|
|
|
Align align() const { return _align; }
|
|
|
|
|
|
|
|
static char const *type_name(Type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case TYPE_CLOSER: return "closer";
|
|
|
|
case TYPE_TITLE: return "title";
|
|
|
|
case TYPE_MAXIMIZER: return "maximizer";
|
|
|
|
case TYPE_MINIMIZER: return "minimizer";
|
|
|
|
case TYPE_UNMAXIMIZER: return "unmaximizer";
|
|
|
|
case TYPE_UNDEFINED: return "undefined";
|
|
|
|
};
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator != (Window_control const &other) const
|
|
|
|
{
|
|
|
|
return _type != other._type || _align != other._align;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2016-11-15 18:01:28 +01:00
|
|
|
struct Buffered_xml
|
|
|
|
{
|
|
|
|
Allocator &_alloc;
|
|
|
|
char const * const _ptr; /* pointer to dynamically allocated buffer */
|
|
|
|
Xml_node const _xml; /* referring to buffer of '_ptr' */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \throw Allocator::Out_of_memory
|
|
|
|
*/
|
|
|
|
static char const *_init_ptr(Allocator &alloc, Xml_node node)
|
|
|
|
{
|
|
|
|
char *ptr = (char *)alloc.alloc(node.size());
|
|
|
|
Genode::memcpy(ptr, node.addr(), node.size());
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* \throw Allocator::Out_of_memory
|
|
|
|
*/
|
|
|
|
Buffered_xml(Allocator &alloc, Xml_node node)
|
|
|
|
:
|
|
|
|
_alloc(alloc), _ptr(_init_ptr(alloc, node)), _xml(_ptr, node.size())
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~Buffered_xml() { _alloc.free(const_cast<char *>(_ptr), _xml.size()); }
|
|
|
|
|
|
|
|
Xml_node xml() const { return _xml; }
|
|
|
|
};
|
|
|
|
|
2015-09-16 23:15:17 +02:00
|
|
|
Genode::Allocator &_alloc;
|
|
|
|
|
2016-12-01 17:37:08 +01:00
|
|
|
Reconstructible<Buffered_xml> _buffered_config;
|
2016-11-15 18:01:28 +01:00
|
|
|
|
2015-09-16 23:15:17 +02:00
|
|
|
/**
|
|
|
|
* Maximum number of configured window controls
|
|
|
|
*/
|
|
|
|
enum { MAX_WINDOW_CONTROLS = 10 };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of window elements
|
|
|
|
*/
|
|
|
|
Window_control *_window_controls[MAX_WINDOW_CONTROLS];
|
|
|
|
|
|
|
|
unsigned _num_window_controls = 0;
|
|
|
|
|
|
|
|
void _reset_window_controls()
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < MAX_WINDOW_CONTROLS; i++) {
|
|
|
|
if (_window_controls[i]) {
|
|
|
|
Genode::destroy(_alloc, _window_controls[i]);
|
|
|
|
_window_controls[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_num_window_controls = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-11-15 18:01:28 +01:00
|
|
|
Config(Genode::Allocator &alloc, Xml_node config)
|
|
|
|
:
|
|
|
|
_alloc(alloc), _buffered_config(_alloc, config)
|
2015-09-16 23:15:17 +02:00
|
|
|
{
|
|
|
|
_reset_window_controls();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exception type
|
|
|
|
*/
|
|
|
|
class Index_out_of_range { };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return information about the Nth window control
|
|
|
|
*
|
|
|
|
* The index 'n' denotes the position of the window control from left
|
|
|
|
* to right.
|
|
|
|
*
|
|
|
|
* \throw Index_out_of_range
|
|
|
|
*/
|
|
|
|
Window_control window_control(unsigned n) const
|
|
|
|
{
|
|
|
|
/* return title of no controls are configured */
|
|
|
|
if (_num_window_controls == 0 && n == 0)
|
|
|
|
return Window_control(Window_control::TYPE_TITLE,
|
|
|
|
Window_control::ALIGN_CENTER);
|
|
|
|
|
|
|
|
if (n >= MAX_WINDOW_CONTROLS || !_window_controls[n])
|
|
|
|
throw Index_out_of_range();
|
|
|
|
|
|
|
|
return *_window_controls[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned num_window_controls() const
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We always report at least one window control. Even if none
|
|
|
|
* was configured, we present a title.
|
|
|
|
*/
|
|
|
|
return Genode::max(_num_window_controls, 1UL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the base color of the window with the specified title
|
|
|
|
*/
|
|
|
|
Color base_color(Window_title const &title) const
|
|
|
|
{
|
|
|
|
Color result(68, 75, 95);
|
|
|
|
|
|
|
|
try {
|
2016-11-15 18:01:28 +01:00
|
|
|
Genode::Session_policy policy(title, _buffered_config->xml());
|
2015-09-16 23:15:17 +02:00
|
|
|
result = policy.attribute_value("color", result);
|
|
|
|
|
|
|
|
} catch (Genode::Session_policy::No_policy_defined) { }
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return gradient intensity in percent
|
|
|
|
*/
|
|
|
|
int gradient_percent(Window_title const &title) const
|
|
|
|
{
|
|
|
|
unsigned long result =
|
2016-11-15 18:01:28 +01:00
|
|
|
_buffered_config->xml().attribute_value("gradient", 32UL);
|
2015-09-16 23:15:17 +02:00
|
|
|
|
|
|
|
try {
|
2016-11-15 18:01:28 +01:00
|
|
|
Genode::Session_policy policy(title, _buffered_config->xml());
|
2015-09-16 23:15:17 +02:00
|
|
|
result = policy.attribute_value("gradient", result);
|
|
|
|
|
|
|
|
} catch (Genode::Session_policy::No_policy_defined) { }
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the internally cached configuration state
|
|
|
|
*/
|
2016-11-15 18:01:28 +01:00
|
|
|
void update(Xml_node config)
|
2015-09-16 23:15:17 +02:00
|
|
|
{
|
2016-11-15 18:01:28 +01:00
|
|
|
_buffered_config.construct(_alloc, config);
|
|
|
|
|
2015-09-16 23:15:17 +02:00
|
|
|
_reset_window_controls();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse configuration of window controls
|
|
|
|
*/
|
|
|
|
auto lambda = [&] (Xml_node node) {
|
|
|
|
|
|
|
|
if (_num_window_controls >= MAX_WINDOW_CONTROLS) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
Genode::warning("number of configured window controls exceeds maximum");
|
2015-09-16 23:15:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Window_control::Type type = Window_control::TYPE_UNDEFINED;
|
|
|
|
Window_control::Align align = Window_control::ALIGN_CENTER;
|
|
|
|
|
|
|
|
if (node.has_type("title")) type = Window_control::TYPE_TITLE;
|
|
|
|
if (node.has_type("closer")) type = Window_control::TYPE_CLOSER;
|
|
|
|
if (node.has_type("maximizer")) type = Window_control::TYPE_MAXIMIZER;
|
|
|
|
if (node.has_type("minimizer")) type = Window_control::TYPE_MINIMIZER;
|
|
|
|
|
|
|
|
if (node.has_attribute("align")) {
|
|
|
|
Genode::Xml_attribute attr = node.attribute("align");
|
|
|
|
if (attr.has_value("left")) align = Window_control::ALIGN_LEFT;
|
|
|
|
if (attr.has_value("right")) align = Window_control::ALIGN_RIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
_window_controls[_num_window_controls++] =
|
|
|
|
new (_alloc) Window_control(type, align);
|
|
|
|
};
|
|
|
|
|
|
|
|
try { config.sub_node("controls").for_each_sub_node(lambda); }
|
|
|
|
catch (Xml_node::Nonexistent_sub_node) { }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _CONFIG_H_ */
|