Slow gtkmm compile for tiny program - g++

I am learning gtkmm and extremely simple sample projects (i.e. a window with a button that opens a dialog box) are taking ~3 seconds to compile with g++ on reasonable hardware.
$ time g++ examplewindow.cpp main.cpp `pkg-config gtkmm-3.0 --cflags --libs`
real 0m4.011s
user 0m3.630s
sys 0m0.374s
Is there anything I can do to cut down this time?
I am on Linux using gtkmm version 3.24.0-2 and version 8.3.0 of g++.
Edit: Code
examplewindow.h
#pragma once
#include <gtkmm/window.h>
#include <gtkmm/buttonbox.h>
#include <gtkmm/button.h>
class ExampleWindow : public Gtk::Window {
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
void on_button_info_clicked();
void on_button_question_clicked();
Gtk::ButtonBox m_ButtonBox;
Gtk::Button m_Button_Info, m_Button_Question;
};
examplewindow.cpp
#include <iostream>
#include <gtkmm/messagedialog.h>
#include "examplewindow.h"
ExampleWindow::ExampleWindow()
:m_ButtonBox( Gtk::ORIENTATION_VERTICAL ),
m_Button_Info( "Show info box" ),
m_Button_Question( "Show question box" )
{
add( m_ButtonBox );
m_Button_Info.signal_clicked().connect( sigc::mem_fun( *this, &ExampleWindow::on_button_info_clicked) );
m_Button_Question.signal_clicked().connect( sigc::mem_fun( *this, &ExampleWindow::on_button_question_clicked) );
m_ButtonBox.pack_start( m_Button_Info );
m_ButtonBox.pack_start( m_Button_Question );
show_all_children();
}
ExampleWindow::~ExampleWindow() {
}
void ExampleWindow::on_button_info_clicked() {
Gtk::MessageDialog dialog( *this, "This is an INFO MessageDialog", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_OK_CANCEL);
dialog.set_secondary_text( "This is the secondary text" );
dialog.run();
}
void ExampleWindow::on_button_question_clicked() {
Gtk::MessageDialog dialog( *this, "This is a QUESTION MessageDialog", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_OK_CANCEL);
dialog.set_secondary_text( "This is the other text" );
int result = dialog.run();
switch( result ) {
case( Gtk::RESPONSE_OK ):
std::cout << "Ok clicked" << std::endl;
break;
case( Gtk::RESPONSE_CANCEL ):
std::cout << "Cancel clicked" << std::endl;
break;
default:
std::cout << "Unexpected button clicked" << std::endl;
break;
}
}
main.cpp
#include <gtkmm/application.h>
#include "examplewindow.h"
int main( int argc, char **argv ) {
auto app = Gtk::Application::create( argc, argv, "org.gtkmm.example" );
ExampleWindow window;
return app->run( window );
}

Related

How to co_await for a change in a variable using boost coroutine ts?

Context
I build a webserver using boost coroutine ts, boost asio and boost beast.
There is a coroutine for reading and one for writing.
There is a message_to_send queue where messages get pushed to send to the user.
The writing coroutine checks if there is something in the message_to_send queue and sends it.
After sending the writing coroutine suspends itself for 100 milliseconds and checks again for something to write.
Problem
The writing coroutine is polling the message queue every 100 milliseconds. I like to find a solution without polling after some timer has fired.
Posible solution
Maybe ther is a solution to co_await the change of a variable. Maybe creating a async_wait_for_callback with "async_initiate"?
Code example
You can clone the project. Or use the complete example code posted here:
#include <algorithm>
#include <boost/asio.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/system_timer.hpp>
#include <boost/beast.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/bind/bind.hpp>
#include <boost/optional.hpp>
#include <chrono>
#include <cstddef>
#include <deque>
#include <exception>
#include <iostream>
#include <list>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>
// TODO use cmake to find out if the compiler is gcc or clang
#include <coroutine> // enable if build with gcc
// #include <experimental/coroutine> //enable if build with clang
using namespace boost::beast;
using namespace boost::asio;
typedef boost::asio::use_awaitable_t<>::as_default_on_t<boost::asio::basic_waitable_timer<boost::asio::chrono::system_clock>> CoroTimer;
typedef boost::beast::websocket::stream<boost::beast::tcp_stream> Websocket;
using namespace boost::beast;
using namespace boost::asio;
using boost::asio::ip::tcp;
using tcp_acceptor = use_awaitable_t<>::as_default_on_t<tcp::acceptor>;
struct User
{
boost::asio::awaitable<void> writeToClient (std::weak_ptr<Websocket> &connection);
std::deque<std::string> msgQueue{};
std::shared_ptr<CoroTimer> timer{};
};
void
handleMessage (std::string const &msg, std::list<std::shared_ptr<User>> &users, std::shared_ptr<User> user)
{
std::cout << "please implement handle message" << std::endl;
user->msgQueue.push_back ("please implement handle message");
user->timer->cancel ();
}
boost::asio::awaitable<void>
User::writeToClient (std::weak_ptr<Websocket> &connection)
{
try
{
while (not connection.expired ())
{
timer = std::make_shared<CoroTimer> (CoroTimer{ co_await this_coro::executor });
timer->expires_after (std::chrono::system_clock::time_point::max () - std::chrono::system_clock::now ());
try
{
co_await timer->async_wait ();
}
catch (boost::system::system_error &e)
{
using namespace boost::system::errc;
if (operation_canceled == e.code ())
{
// swallow cancel
}
else
{
std::cout << "error in timer boost::system::errc: " << e.code () << std::endl;
abort ();
}
}
while (not msgQueue.empty () && not connection.expired ())
{
auto tmpMsg = std::move (msgQueue.front ());
std::cout << " msg: " << tmpMsg << std::endl;
msgQueue.pop_front ();
co_await connection.lock ()->async_write (buffer (tmpMsg), use_awaitable);
}
}
}
catch (std::exception &e)
{
std::cout << "write Exception: " << e.what () << std::endl;
}
}
class Server
{
public:
Server (boost::asio::ip::tcp::endpoint const &endpoint);
boost::asio::awaitable<void> listener ();
private:
void removeUser (std::list<std::shared_ptr<User>>::iterator user);
boost::asio::awaitable<std::string> my_read (Websocket &ws_);
boost::asio::awaitable<void> readFromClient (std::list<std::shared_ptr<User>>::iterator user, Websocket &connection);
boost::asio::ip::tcp::endpoint _endpoint{};
std::list<std::shared_ptr<User>> users{};
};
namespace this_coro = boost::asio::this_coro;
Server::Server (boost::asio::ip::tcp::endpoint const &endpoint) : _endpoint{ endpoint } {}
awaitable<std::string>
Server::my_read (Websocket &ws_)
{
std::cout << "read" << std::endl;
flat_buffer buffer;
co_await ws_.async_read (buffer, use_awaitable);
auto msg = buffers_to_string (buffer.data ());
std::cout << "number of letters '" << msg.size () << "' msg: '" << msg << "'" << std::endl;
co_return msg;
}
awaitable<void>
Server::readFromClient (std::list<std::shared_ptr<User>>::iterator user, Websocket &connection)
{
try
{
for (;;)
{
auto readResult = co_await my_read (connection);
handleMessage (readResult, users, *user);
}
}
catch (std::exception &e)
{
removeUser (user);
std::cout << "read Exception: " << e.what () << std::endl;
}
}
void
Server::removeUser (std::list<std::shared_ptr<User>>::iterator user)
{
users.erase (user);
}
awaitable<void>
Server::listener ()
{
auto executor = co_await this_coro::executor;
tcp_acceptor acceptor (executor, _endpoint);
for (;;)
{
try
{
auto socket = co_await acceptor.async_accept ();
auto connection = std::make_shared<Websocket> (std::move (socket));
users.emplace_back (std::make_shared<User> ());
std::list<std::shared_ptr<User>>::iterator user = std::next (users.end (), -1);
connection->set_option (websocket::stream_base::timeout::suggested (role_type::server));
connection->set_option (websocket::stream_base::decorator ([] (websocket::response_type &res) { res.set (http::field::server, std::string (BOOST_BEAST_VERSION_STRING) + " websocket-server-async"); }));
co_await connection->async_accept (use_awaitable);
co_spawn (
executor, [connection, this, &user] () mutable { return readFromClient (user, *connection); }, detached);
co_spawn (
executor, [connectionWeakPointer = std::weak_ptr<Websocket>{ connection }, &user] () mutable { return user->get ()->writeToClient (connectionWeakPointer); }, detached);
}
catch (std::exception &e)
{
std::cout << "Server::listener () connect Exception : " << e.what () << std::endl;
}
}
}
auto const DEFAULT_PORT = u_int16_t{ 55555 };
int
main ()
{
try
{
using namespace boost::asio;
io_context io_context (1);
signal_set signals (io_context, SIGINT, SIGTERM);
signals.async_wait ([&] (auto, auto) { io_context.stop (); });
auto server = Server{ { ip::tcp::v4 (), DEFAULT_PORT } };
co_spawn (
io_context, [&server] { return server.listener (); }, detached);
io_context.run ();
}
catch (std::exception &e)
{
std::printf ("Exception: %s\n", e.what ());
}
return 0;
}
EDIT: updated code based on sehe's idea which is marked as answer.
The classical threading solution would be a condition variable. Of course, that's not what you want - I see you even explicitly disabled ASIO threading. Good.
One way - short of providing an Asio service to implement this behaviour - would be to use timers to emulate condition variables. You could use timer that "never" expires (deadline is at timepoint::max()) and manually reset it to timepoint::min() (canceling any async_wait) or any time in the past to signify the condition. Then you can use Timer::async_wait with use_awaitable like you already know how.
Note that you still need to "manually" signal the change. This is what you want because anything else requires kernel process tracing support/hardware debugger facilities which require massive priviliges and tend to be very slow.
You might want to know about associating the use_awaitable as the default completion token for the executor bound to your timer. See e.g. the examples: https://www.boost.org/doc/libs/1_78_0/doc/html/boost_asio/example/cpp17/coroutines_ts/echo_server_with_default.cpp (the HTML docs do NOT link these examples)

GTKmm application: Gdk key press signals not firing

I have (sort of) copied (but with some modifications) the example in the GTKmm tutorials on keyboard events. (link)
https://developer.gnome.org/gtkmm-tutorial/stable/sec-keyboardevents-overview.html.en#keyboardevents-simple-example
Here is my code:
#include <gtkmm/application.h>
#include <gtkmm/window.h>
#include <gtkmm/textview.h>
#include <iostream>
class MainWindow
{
public:
MainWindow()
{
_window_.set_default_size(600, 400);
_window_.add(_textview_);
Glib::RefPtr<Gtk::TextBuffer> textbuffer_rp{_textview_.get_buffer()};
textbuffer_rp->set_text("some text here");
_textview_.set_monospace();
_window_.add_events(Gdk::KEY_PRESS_MASK);
_window_.show_all_children();
}
Gtk::Window &get_window()
{
return _window_;
}
protected:
bool on_key_press_event(GdkEventKey* event)
{
std::cout << "some keypress event" << std::endl;
if
(
(event->keyval == GDK_KEY_H) &&
((event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK) == GDK_CONTROL_MASK))
)
{
std::cout << "Hello world!" << std::endl;
}
if
(
(event->keyval == GDK_KEY_C) &&
((event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK) == GDK_CONTROL_MASK))
)
{
signal_textview_CTRL_C();
}
else if
(
(event->keyval == GDK_KEY_V) &&
((event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK) == GDK_CONTROL_MASK))
)
{
signal_textview_CTRL_V();
}
else
{
std::cout << "unhandled key" << std::endl;
}
return true;
}
void signal_textview_CTRL_C()
{
_text_register_0_ = "some text gets put here";
}
void signal_textview_CTRL_V()
{
Glib::RefPtr<Gtk::TextBuffer> textbuffer_rp{_textview_.get_buffer()};
textbuffer_rp->set_text(_text_register_0_);
}
private:
Gtk::Window _window_;
Gtk::TextView _textview_;
Glib::ustring _text_register_0_;
};
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
MainWindow mainwindow;
return app->run(mainwindow.get_window());
}
However it is not working - none of the signal events appear to fire when I press the key sequences CTRL-C, CTRL-V, CTRL-H.
I think I have stripped all the irrelevant stuff from the code so this should be a working MWE.
Connect your handler as first.
_window_.signal_key_press_event().connect(sigc::mem_fun(*this, &MainWindow::on_key_press_event), false);
Return false to let _textview_ get the key.
Your conditions are contradictory.
GDK_KEY_H is keysym for shift+h. Then you check (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK) == GDK_CONTROL_MASK). You are asking if state (which we know is with GDK_SHIFT_MASK) is ONLY GDK_CONTROL_MASK.
Code:
#include <gtkmm/application.h>
#include <gtkmm/window.h>
#include <gtkmm/textview.h>
#include <iostream>
class MainWindow
{
public:
MainWindow()
{
_window_.set_default_size(600, 400);
_window_.add(_textview_);
Glib::RefPtr<Gtk::TextBuffer> textbuffer_rp{_textview_.get_buffer()};
textbuffer_rp->set_text("some text here");
_textview_.set_monospace();
_window_.add_events(Gdk::KEY_PRESS_MASK);
_window_.signal_key_press_event().connect(sigc::mem_fun(*this, &MainWindow::on_key_press_event), false);
_window_.show_all_children();
}
Gtk::Window &get_window()
{
return _window_;
}
protected:
bool on_key_press_event(GdkEventKey* event)
{
std::cout << "some keypress event " << std::hex<<event->keyval<<" "<<std::hex<<event->state<<std::endl;
if
(
(event->keyval == GDK_KEY_h) &&
(event->state & GDK_CONTROL_MASK)
)
{
std::cout << "Hello world!" << std::endl;
return true;
}
if
(
(event->keyval == GDK_KEY_c) &&
(event->state & GDK_CONTROL_MASK)
)
{
std::cout<<"ctrl c"<<std::endl;
signal_textview_CTRL_C();
return true;
}
else if
(
(event->keyval == GDK_KEY_v) &&
(event->state & GDK_CONTROL_MASK)
)
{
std::cout<<"ctrl v"<<std::endl;
signal_textview_CTRL_V();
return true;
}
else
{
std::cout << "unhandled key" << std::endl;
}
return false;
}
void signal_textview_CTRL_C()
{
_text_register_0_ = "some text gets put here";
}
void signal_textview_CTRL_V()
{
Glib::RefPtr<Gtk::TextBuffer> textbuffer_rp{_textview_.get_buffer()};
textbuffer_rp->set_text(_text_register_0_);
}
private:
Gtk::Window _window_;
Gtk::TextView _textview_;
Glib::ustring _text_register_0_;
};
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
MainWindow mainwindow;
return app->run(mainwindow.get_window());
}

Empty QComboBox with QSqlQueryModel

I am totally new to Qt and just want to play around a little. I want to try to fill a ComboBox with values from a DB
I have got the following code:
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("LOCALHOST");
db.setDatabaseName("rms32");
if(db.open())
{
QSqlQuery query;
query.prepare("select user_name from T_USER");
if(query.exec())
{
this->model = new QSqlQueryModel();
this->model->setQuery(query);
qDebug() << this->model->rowCount();
qDebug() << this->model->columnCount();
ui->_UsernameCB->setModel(this->model);
//ui->_UsernameCB->setModelColumn(0);
qDebug() << "Last error: " << db.lastError().text();
qDebug() << "Connection opened successfully";
}
db.close();
}
The rowCount and colCount gives me 1, corresponding to my DB, but the user_name is not displayed in the Combobox.
Can anyone point me to the error?
I cannot reproduce your problem. I have the very simply class below inheriting from QComboBox. I went for an sqlite database but otherwise most lines are pretty much exactly as in your case and it correctly displays the one entry in the DB.
If you replace the DB in the code with yours, do you get the entry correctly displayed?
combobox.h
#ifndef MYCOMBOBOX_H
#define MYCOMBOBOX_H
#include <QComboBox>
QT_FORWARD_DECLARE_CLASS(QSqlQueryModel);
class ComboBox : public QComboBox
{
public:
ComboBox(QWidget *widget = 0);
QSqlQueryModel *model;
};
#endif
combobox.cpp
#include "combobox.h"
#include <QSqlDatabase>
#include <QSqlQueryModel>
#include <QSqlQuery>
ComboBox::ComboBox(QWidget *parent) :
QComboBox(parent)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("db.db");
if(db.open())
{
QSqlQuery query;
query.prepare("select * from TEST");
if(query.exec())
{
this->model = new QSqlQueryModel;
this->model->setQuery(query);
this->setModel(this->model);
}
db.close();
}
}
main.cpp
#include <QApplication>
#include <QDialog>
#include <QHBoxLayout>
#include "combobox.h"
int main(int argc, char** argv)
{
QApplication app(argc,argv);
QDialog dialog;
ComboBox box;
QHBoxLayout layout;
layout.addWidget(&box);
dialog.setLayout(&layout);
dialog.show();
return app.exec();
}

override signal handler in gtkmm

I struggled with the following code. My signal handler on_button_press_event() is never called but I have no idea why. Could someone have a look on it? Maybe someone is able to run through the gtkmm lib with debug infos. I only have the pre-installed gtkmm packages which could not be used for debugging into the library itself.
#include <iostream>
using namespace std;
#include <gtkmm.h>
#include <goocanvasmm.h>
bool MyExternalHandler( const Glib::RefPtr<Goocanvas::Item>& item, GdkEventButton* ev )
{
cout << "External Handler" << endl;
return false;
}
class MyRect : public Goocanvas::Rect
{
public:
MyRect( double x, double y, double w, double h)
//: Goocanvas::Rect( x,y,w,h)
{
property_x()=x;
property_y()=y;
property_width()=w;
property_height()=h;
}
public:
virtual void nonsens() {}
bool on_button_press_event(const Glib::RefPtr<Item>& target, GdkEventButton* event) override
{
cout << "override handler" << endl;
return false;
}
bool Handler( const Glib::RefPtr<Goocanvas::Item>& item, GdkEventButton* ev )
{
cout << "via mem_fun" << endl;
return false;
}
bool on_enter_notify_event(const Glib::RefPtr<Item>& target, GdkEventCrossing* event) override
{
cout << "override enter notify" << endl;
return false;
}
};
int main(int argc, char* argv[])
{
Gtk::Main app(&argc, &argv);
Goocanvas::init("example", "0.1", argc, argv);
Gtk::Window win;
Goocanvas::Canvas m_canvas;
m_canvas.set_size_request(640, 480);
m_canvas.set_bounds(0, 0, 1000, 1000);
MyRect* ptr;
Glib::RefPtr<MyRect> m_rect_own(ptr=new MyRect(225, 225, 150, 150));
m_rect_own->property_line_width() = 1.0;
m_rect_own->property_stroke_color() = "black";
m_rect_own->property_fill_color_rgba() = 0x555555ff;
Glib::RefPtr<Goocanvas::Item> root = m_canvas.get_root_item();
root->add_child( m_rect_own);
((Glib::RefPtr<Goocanvas::Item>&)m_rect_own)->signal_button_press_event().connect(sigc::ptr_fun(&MyExternalHandler));
((Glib::RefPtr<Goocanvas::Item>&)m_rect_own)->signal_button_press_event().connect(sigc::mem_fun(*ptr, &MyRect::Handler));
win.add(m_canvas);
win.show_all_children();
Gtk::Main::run(win);
return 0;
}
Your on_button_press_event() method is not an override, because it has the wrong parameters:
https://developer.gnome.org/gtkmm/unstable/classGtk_1_1Widget.html#aba72b7f8655d1a0eb1273a26894584e3

DBus client and server in the same process

When I create a D-Bus server (via g_bus_own_name()) and the client to it (using g_dbus_proxy_new()) in the same process and then call g_dbus_proxy_call_sync(), it never returns. However, if server and client are in separate processes, everything is ok.
The following code illustrates my problem (I am using giomm C++ bindings here):
file main.cc:
#include <giomm.h>
#include <thread>
int server_main();
int client_main();
int main() {
Gio::init();
std::thread thr_server([](){ server_main(); });
sleep(1); // give some time to server to register
std::thread thr_client([](){ client_main(); });
sleep(10); // wait for the client to finish
}
file server.cc:
#include <giomm.h>
#include <iostream>
namespace {
static Glib::RefPtr<Gio::DBus::NodeInfo> introspection_data;
static Glib::ustring introspection_xml =
"<node name='/org/glibmm/DBusExample'>"
" <interface name='org.glibmm.DBusExample'>"
" <method name='Method'>"
" </method>"
" </interface>"
"</node>";
guint registered_id = 0;
}
static void on_method_call(const Glib::RefPtr<Gio::DBus::Connection>& /* connection */,
const Glib::ustring& /* sender */, const Glib::ustring& /* object_path */,
const Glib::ustring& /* interface_name */, const Glib::ustring& method_name,
const Glib::VariantContainerBase& parameters,
const Glib::RefPtr<Gio::DBus::MethodInvocation>& invocation)
{
if(method_name == "Method") {
std::cout << "Method was called\n";
}
}
const Gio::DBus::InterfaceVTable interface_vtable(sigc::ptr_fun(&on_method_call));
void on_bus_acquired(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& /* name */)
{
std::cout << "on_bus_acquired\n";
try {
registered_id = connection->register_object("/org/glibmm/DBusExample",
introspection_data->lookup_interface(),
interface_vtable);
}
catch(const Glib::Error& ex) {
std::cerr << "Registration of object failed." << std::endl;
}
return;
}
void on_name_acquired(const Glib::RefPtr<Gio::DBus::Connection>& /* connection */, const Glib::ustring& /* name */)
{}
void on_name_lost(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& /* name */) {
connection->unregister_object(registered_id);
}
int server_main()
{
try {
introspection_data = Gio::DBus::NodeInfo::create_for_xml(introspection_xml);
}
catch(const Glib::Error& ex) {
std::cerr << "Unable to create introspection data: " << ex.what() <<
"." << std::endl;
return 1;
}
const guint id = Gio::DBus::own_name(Gio::DBus::BUS_TYPE_SESSION,
"org.glibmm.DBusExample",
sigc::ptr_fun(&on_bus_acquired),
sigc::ptr_fun(&on_name_acquired),
sigc::ptr_fun(&on_name_lost));
//Keep the service running
auto loop = Glib::MainLoop::create();
loop->run();
Gio::DBus::unown_name(id);
return EXIT_SUCCESS;
}
file client.cc:
#include <giomm.h>
#include <iostream>
Glib::RefPtr<Glib::MainLoop> loop;
// A main loop idle callback to quit when the main loop is idle.
bool on_main_loop_idle() {
std::cout << "loop_idle\n";
loop->quit();
return false;
}
void on_dbus_proxy_available(Glib::RefPtr<Gio::AsyncResult>& result)
{
auto proxy = Gio::DBus::Proxy::create_finish(result);
if(!proxy) {
std::cerr << "The proxy to the user's session bus was not successfully "
"created." << std::endl;
loop->quit();
return;
}
try {
std::cout << "Calling...\n";
proxy->call_sync("Method");
std::cout << "It works!\n";
}
catch(const Glib::Error& error) {
std::cerr << "Got an error: '" << error.what() << "'." << std::endl;
}
// Connect an idle callback to the main loop to quit when the main loop is
// idle now that the method call is finished.
Glib::signal_idle().connect(sigc::ptr_fun(&on_main_loop_idle));
}
int client_main() {
loop = Glib::MainLoop::create();
auto connection =
Gio::DBus::Connection::get_sync(Gio::DBus::BUS_TYPE_SESSION);
if(!connection) {
std::cerr << "The user's session bus is not available." << std::endl;
return 1;
}
// Create the proxy to the bus asynchronously.
Gio::DBus::Proxy::create(connection, "org.glibmm.DBusExample",
"/org/glibmm/DBusExample", "org.glibmm.DBusExample",
sigc::ptr_fun(&on_dbus_proxy_available));
loop->run();
return EXIT_SUCCESS;
}
I compile the test with g++ -O2 -std=c++0x main.cc server.cc client.cc -o test $(pkg-config --cflags --libs giomm-2.4) and run:
./test
on_bus_acquired
Calling...
<it hangs>
However, when I change main.cc:
#include <giomm.h>
int server_main();
int client_main();
int main() {
Gio::init();
auto childid = fork();
if (childid == 0) {
server_main();
} else {
sleep(1);
client_main();
}
}
I get:
./test
on_bus_acquired
Calling...
Method was called
It works!
So call_sync() returns successfully.
I tried to exclude loops from server and client, and use a single-threaded main.cc:
#include <giomm.h>
#include <thread>
int server_main();
int client_main();
int main() {
Gio::init();
server_main();
client_main();
auto loop = Glib::MainLoop::create();
loop->run();
}
Nothing helps. The question is, what am I doing wrong? I want to use my d-bus server and client in one process.
I figured it out, the trick is to execute
Glib::VariantContainerBase result;
invocation->return_value(result);
in the end of on_method_call.