| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#pragma once |
| 21 |
|
| 22 |
#include <osl/process.h> |
| 23 |
#include <unotools/resmgr.hxx> |
| 24 |
|
| 25 |
#include "filepicker_ipc_commands.hxx" |
| 26 |
|
| 27 |
#include <functional> |
| 28 |
#include <mutex> |
| 29 |
#include <thread> |
| 30 |
#include <sstream> |
| 31 |
|
| 32 |
OUString getResString(TranslateId pResId); |
| 33 |
|
| 34 |
class Gtk3KDE5FilePickerIpc |
| 35 |
{ |
| 36 |
protected: |
| 37 |
oslProcess m_process; |
| 38 |
oslFileHandle m_inputWrite; |
| 39 |
oslFileHandle m_outputRead; |
| 40 |
// simple multiplexing: every command gets its own ID that can be used to |
| 41 |
// read the corresponding response |
| 42 |
uint64_t m_msgId = 1; |
| 43 |
std::mutex m_mutex; |
| 44 |
uint64_t m_incomingResponse = 0; |
| 45 |
std::string m_responseBuffer; |
| 46 |
std::stringstream m_responseStream; |
| 47 |
|
| 48 |
public: |
| 49 |
explicit Gtk3KDE5FilePickerIpc(); |
| 50 |
~Gtk3KDE5FilePickerIpc(); |
| 51 |
|
| 52 |
sal_Int16 execute(); |
| 53 |
|
| 54 |
void writeResponseLine(const std::string& line); |
| 55 |
|
| 56 |
template <typename... Args> uint64_t sendCommand(Commands command, const Args&... args) |
| 57 |
{ |
| 58 |
auto id = m_msgId; |
| 59 |
++m_msgId; |
| 60 |
std::stringstream stream; |
| 61 |
sendIpcArgs(stream, id, command, args...); |
| 62 |
writeResponseLine(stream.str()); |
| 63 |
return id; |
| 64 |
} |
| 65 |
|
| 66 |
std::string readResponseLine(); |
| 67 |
|
| 68 |
// workaround gcc <= 4.8 bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55914 |
| 69 |
template <int...> struct seq |
| 70 |
{ |
| 71 |
}; |
| 72 |
template <int N, int... S> struct gens : gens<N - 1, N - 1, S...> |
| 73 |
{ |
| 74 |
}; |
| 75 |
template <int... S> struct gens<0, S...> |
| 76 |
{ |
| 77 |
typedef seq<S...> type; |
| 78 |
}; |
| 79 |
template <typename... Args> struct ArgsReader |
| 80 |
{ |
| 81 |
ArgsReader(Args&... args) |
| 82 |
: m_args(args...) |
| 83 |
{ |
| 84 |
} |
| 85 |
|
| 86 |
void operator()(std::istream& stream) |
| 87 |
{ |
| 88 |
callFunc(stream, typename gens<sizeof...(Args)>::type()); |
| 89 |
} |
| 90 |
|
| 91 |
private: |
| 92 |
template <int... S> void callFunc(std::istream& stream, seq<S...>) |
| 93 |
{ |
| 94 |
readIpcArgs(stream, std::get<S>(m_args)...); |
| 95 |
} |
| 96 |
|
| 97 |
std::tuple<Args&...> m_args; |
| 98 |
}; |
| 99 |
|
| 100 |
template <typename... Args> void readResponse(uint64_t id, Args&... args) |
| 101 |
{ |
| 102 |
ArgsReader<Args...> argsReader(args...); |
| 103 |
while (true) |
| 104 |
{ |
| 105 |
// only let one thread read at any given time |
| 106 |
std::scoped_lock<std::mutex> lock(m_mutex); |
| 107 |
|
| 108 |
// check if we need to read (and potentially wait) a response ID |
| 109 |
if (m_incomingResponse == 0) |
| 110 |
{ |
| 111 |
m_responseStream.clear(); |
| 112 |
m_responseStream.str(readResponseLine()); |
| 113 |
readIpcArgs(m_responseStream, m_incomingResponse); |
| 114 |
} |
| 115 |
|
| 116 |
if (m_incomingResponse == id) |
| 117 |
{ |
| 118 |
// the response we are waiting for came in |
| 119 |
argsReader(m_responseStream); |
| 120 |
m_incomingResponse = 0; |
| 121 |
break; |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
// the next response answers some other request, yield |
| 126 |
std::this_thread::yield(); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
private: |
| 132 |
std::function<void()> blockMainWindow(); |
| 133 |
}; |
| 134 |
|
| 135 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |