1 /**
2 * Copyright (c) 2014, Timothy Stack
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of Timothy Stack nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @file sysclip.cc
30 */
31
32 #include "config.h"
33
34 #include <stdio.h>
35
36 #include "base/injector.hh"
37 #include "base/lnav_log.hh"
38 #include "fmt/format.h"
39 #include "sysclip.hh"
40 #include "sysclip.cfg.hh"
41
42 namespace sysclip {
43
get_commands()44 static nonstd::optional<clipboard> get_commands()
45 {
46 auto& cfg = injector::get<const config&>();
47
48 for (const auto& pair : cfg.c_clipboard_impls) {
49 const auto full_cmd = fmt::format("{} > /dev/null 2>&1",
50 pair.second.c_test_command);
51
52 log_debug("testing clipboard impl %s using: %s",
53 pair.first.c_str(), full_cmd.c_str());
54 if (system(full_cmd.c_str()) == 0) {
55 log_info("detected clipboard: %s", pair.first.c_str());
56 return pair.second;
57 }
58 }
59
60 return nonstd::nullopt;
61 }
62
63 /* XXX For one, this code is kinda crappy. For two, we should probably link
64 * directly with X so we don't need to have xclip installed and it'll work if
65 * we're ssh'd into a box.
66 */
open(type_t type,op_t op)67 FILE *open(type_t type, op_t op)
68 {
69 const char *mode = op == op_t::WRITE ? "w" : "r";
70 static const auto clip_opt = sysclip::get_commands();
71
72 if (!clip_opt) {
73 log_error("unable to detect clipboard implementation");
74 return nullptr;
75 }
76
77 auto cmd = clip_opt.value().select(type).select(op);
78
79 if (cmd.empty()) {
80 log_error("clipboard does not support type/op");
81 return nullptr;
82 }
83
84 switch (op) {
85 case op_t::WRITE:
86 cmd = fmt::format("{} > /dev/null 2>&1", cmd);
87 break;
88 case op_t::READ:
89 cmd = fmt::format("{} < /dev/null 2>/dev/null", cmd);
90 break;
91 }
92
93 return popen(cmd.c_str(), mode);
94 }
95
96 }
97