genode/repos/demo/src/app/scout/main.cc

178 lines
4.9 KiB
C++
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Scout tutorial browser main program
* \date 2005-10-24
* \author Norman Feske <norman.feske@genode-labs.com>
*/
/*
* Copyright (C) 2005-2017 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
2011-12-22 16:19:25 +01:00
*/
#include <base/component.h>
#include <base/heap.h>
#include <scout/platform.h>
#include <scout/tick.h>
#include <scout/user_state.h>
#include <scout/nitpicker_graphics_backend.h>
2011-12-22 16:19:25 +01:00
#include "config.h"
#include "elements.h"
#include "fade_icon.h"
#include "browser_window.h"
extern Scout::Document *create_document();
2011-12-22 16:19:25 +01:00
static Genode::Allocator *_alloc_ptr;
2011-12-22 16:19:25 +01:00
void *operator new (__SIZE_TYPE__ n) { return _alloc_ptr->alloc(n); }
2011-12-22 16:19:25 +01:00
#define POINTER_RGBA _binary_pointer_rgba_start
#define NAV_NEXT_RGBA _binary_nav_next_rgba_start
#define NAV_PREV_RGBA _binary_nav_prev_rgba_start
extern unsigned char POINTER_RGBA[];
extern unsigned char NAV_NEXT_RGBA[];
extern unsigned char NAV_PREV_RGBA[];
static unsigned char *navicons_rgba[] = { NAV_NEXT_RGBA, NAV_PREV_RGBA };
static Scout::Generic_icon **navicons[] = { &Scout::Navbar::next_icon,
&Scout::Navbar::prev_icon };
2011-12-22 16:19:25 +01:00
extern int native_startup(int, char **);
namespace Scout {
struct Main;
using namespace Genode;
}
struct Scout::Main : Scout::Event_handler
{
Env &_env;
Heap _heap { _env.ram(), _env.rm() };
bool const _global_new_initialized = (_alloc_ptr = &_heap, true);
bool const _launcher_initialized = (Launcher::init(_env, _heap), true);
bool const _png_image_initialized = (Png_image::init(_heap), true);
Nitpicker::Connection _nitpicker { _env };
Platform _platform { _env, *_nitpicker.input() };
bool const _event_handler_registered = (_platform.event_handler(*this), true);
Area const _max_size { 530, 620 };
Point const _initial_position { 256, 80 };
Area const _initial_size { 530, 400 };
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Config const _config { };
Nitpicker_graphics_backend
_graphics_backend { _env.rm(), _nitpicker, _heap, _max_size,
_initial_position, _initial_size };
void _init_navicons()
{
for (unsigned int i = 0; i < sizeof(navicons)/sizeof(void *); i++) {
Fade_icon<Pixel_rgb565, 64, 64> *icon = new Fade_icon<Pixel_rgb565, 64, 64>;
icon->rgba(navicons_rgba[i]);
icon->alpha(100);
*navicons[i] = icon;
}
}
bool const _navicons_initialized = (_init_navicons(), true);
Document &_doc = *create_document();
/* create instance of browser window */
Browser_window<Pixel_rgb565> _browser { &_doc, _graphics_backend,
_initial_position, _initial_size,
_max_size, _config };
/* initialize mouse cursor */
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Icon<Pixel_rgb565, 32, 32> _mcursor { };
void _init_mouse_cursor()
{
if (_config.mouse_cursor) {
_mcursor.geometry(Rect(Point(0, 0), Area(32, 32)));
_mcursor.rgba(POINTER_RGBA);
_mcursor.alpha(255);
_mcursor.findable(0);
_browser.append(&_mcursor);
}
}
bool const _mouse_cursor_initialized = (_init_mouse_cursor(), true);
User_state _user_state { &_browser, &_browser,
_initial_position.x(), _initial_position.y() };
bool const _browser_ypos_initialized = (_browser.ypos(0), true);
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Scout::Point _mouse_position { };
unsigned long _old_time = _platform.timer_ticks();
void handle_event(Scout::Event const &event) override
{
using namespace Scout;
Event ev = event;
if (event.type != Event::WHEEL) {
ev.mouse_position = ev.mouse_position - _user_state.view_position();
/* update mouse cursor */
if (_config.mouse_cursor && (ev.mouse_position.x() != _mouse_position.x()
|| ev.mouse_position.y() != _mouse_position.y())) {
int x1 = min(ev.mouse_position.x(), _mouse_position.x());
int y1 = min(ev.mouse_position.y(), _mouse_position.y());
int x2 = max(ev.mouse_position.x() + _mcursor.size().w() - 1,
_mouse_position.x() + _mcursor.size().w() - 1);
int y2 = max(ev.mouse_position.y() + _mcursor.size().h() - 1,
_mouse_position.y() + _mcursor.size().h() - 1);
_mcursor.geometry(Rect(ev.mouse_position, _mcursor.size()));
_browser.redraw_area(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
_mouse_position = ev.mouse_position;
}
}
_user_state.handle_event(ev);
if (event.type == Event::TIMER)
Tick::handle(_platform.timer_ticks());
/* perform periodic redraw */
unsigned long curr_time = _platform.timer_ticks();
if (!_platform.event_pending() && ((curr_time - _old_time > 20)
|| (curr_time < _old_time))) {
_old_time = curr_time;
_browser.process_redraw();
}
}
2011-12-22 16:19:25 +01:00
Main(Env &env) : _env(env) { }
};
2011-12-22 16:19:25 +01:00
void Component::construct(Genode::Env &env)
{
/* XXX execute constructors of global statics */
env.exec_static_constructors();
static Scout::Main main(env);
}