Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / unx / gtk3_kde5 / gtk3_kde5_filepicker_ipc.hxx
blobe24fb92de0235141f82817d87ceb5132a4c97fde
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <osl/process.h>
24 #include "filepicker_ipc_commands.hxx"
26 #include <functional>
27 #include <mutex>
28 #include <thread>
29 #include <sstream>
31 OUString getResString(const char* pResId);
33 class Gtk3KDE5FilePickerIpc
35 protected:
36 oslProcess m_process;
37 oslFileHandle m_inputWrite;
38 oslFileHandle m_outputRead;
39 // simple multiplexing: every command gets its own ID that can be used to
40 // read the corresponding response
41 uint64_t m_msgId = 1;
42 std::mutex m_mutex;
43 uint64_t m_incomingResponse = 0;
44 std::string m_responseBuffer;
45 std::stringstream m_responseStream;
47 public:
48 explicit Gtk3KDE5FilePickerIpc();
49 ~Gtk3KDE5FilePickerIpc();
51 sal_Int16 execute();
53 void writeResponseLine(const std::string& line);
55 template <typename... Args> uint64_t sendCommand(Commands command, const Args&... args)
57 auto id = m_msgId;
58 ++m_msgId;
59 std::stringstream stream;
60 sendIpcArgs(stream, id, command, args...);
61 writeResponseLine(stream.str());
62 return id;
65 std::string readResponseLine();
67 // workaround gcc <= 4.8 bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55914
68 template <int...> struct seq
71 template <int N, int... S> struct gens : gens<N - 1, N - 1, S...>
74 template <int... S> struct gens<0, S...>
76 typedef seq<S...> type;
78 template <typename... Args> struct ArgsReader
80 ArgsReader(Args&... args)
81 : m_args(args...)
85 void operator()(std::istream& stream)
87 callFunc(stream, typename gens<sizeof...(Args)>::type());
90 private:
91 template <int... S> void callFunc(std::istream& stream, seq<S...>)
93 readIpcArgs(stream, std::get<S>(m_args)...);
96 std::tuple<Args&...> m_args;
99 template <typename... Args> void readResponse(uint64_t id, Args&... args)
101 ArgsReader<Args...> argsReader(args...);
102 while (true)
104 // only let one thread read at any given time
105 std::scoped_lock<std::mutex> lock(m_mutex);
107 // check if we need to read (and potentially wait) a response ID
108 if (m_incomingResponse == 0)
110 m_responseStream.clear();
111 m_responseStream.str(readResponseLine());
112 readIpcArgs(m_responseStream, m_incomingResponse);
115 if (m_incomingResponse == id)
117 // the response we are waiting for came in
118 argsReader(m_responseStream);
119 m_incomingResponse = 0;
120 break;
122 else
124 // the next response answers some other request, yield
125 std::this_thread::yield();
130 private:
131 std::function<void()> blockMainWindow();
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */