Synergy-core 编译 使用 教程

Synergy 是一款非常棒的跨平台 鼠标、键盘、剪切板共享软件,不过GUI版本1.8.8停留在x32位,且不免费。所以转投Synergy-core 开源版,在此分享一下编译x64位,以及使用心得

Synergy

Synergy是一个跨平台的 鼠标、键盘、剪切板共享软件,支持Windows,Mac,Linux主流桌面平台。今天我就手把手把这个软件分享给大家。

如果你在用两台电脑,你是如何在他们之间传输文件的,又是如何切换键盘鼠标的,诚然我知道Windows官方出的鼠标有 无界 功能。今天我介绍的synergy软件,你用了之后,你就再也不用在微信的 文件传输助手 上互传文件了,直接在这个电脑复制,在另一个电脑上粘贴!不受硬件限制,只要两台电脑ip互联互通。

无界鼠标由于是微软出品的,所以只支持 Windows 平台,安装包也是 MSI 文件格式。

Synergy-core 是开源的cli程序.

官网:https://symless.com/synergy

开源地址:https://github.com/symless/synergy-core

下载

GUI版程序只能官网付费上下载,没有登陆App Store, 还有点需要注意1.8.8及以前的版本都是32位的程序,所以这次来折腾下64位的开源版。

Mac OS Cotalina 开始,就完全不能运行32位的程序了

安装

如果是官网下载Synergy GUI版,那双击就完事了。

编译

这里说下github上开源的core版本,编译,安装。

Mac OS 环境

安装依赖,编译软件,编译,安装:

# Install Homebrew

## 安装相关编译软件
$ brew install cmake
$ brew install qt
$ brew install openssh
$ brew install git

## 检查qt 安装信息,以及目录,等下需要用到 /usr/local/Cellar/qt/5.15.0/Frameworks/
$ brew info qt

## 添加一个环境变量
$ export CMAKE_PREFIX_PATH="/usr/local/Cellar/qt/5.15.0/Frameworks/"

# 下载源码并进入源码目录
$ git clone https://github.com/symless/synergy-core.git
$ cd synergy-core
$ mkdir build
$ cd build
## 准备编译参数
$ cmake -DCMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk  -DCMAKE_OSX_DEPLOYMENT_TARGET=10.15 -DCMAKE_OSX_ARCHITECTURES=x86_64 
## 编译
make

Windows 编译

这里我在Windows 环境安装了 最新Qt,和 Visual Studio community 2019.

Add C:\Qt\Tools\QtCreator\bin to the system PATH

Set CMAKE_PREFIX_PATH environment variable
C:\Qt\5.12.5\msvc2017_64

$ cd Projects\synergy
$ mkdir build
$ cd build
$ call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"
$ cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Debug ..
$ msbuild synergy-core.sln /p:Platform="x64" /p:Configuration=Debug /m
$ cd ..
$ copy ext\openssl\windows\x64\bin\* build\
$ cd Projects\synergy-core
$ mkdir build
$ cd build
$ call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64
$ cmake -G "Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=Debug ..
$ msbuild synergy-core.sln /p:Platform="x64" /p:Configuration=Debug /m

Linux 编译

$ cd Projects/synergy
$ mkdir build
$ cd build
$ cmake ..
$ make

编译成功,会生成3个可执行文件。编辑后产物在:

./synergy-core/build/bin/

可执行文件

synergy-core

命令行程序:synergy-core, synergyc, synergys.

Synergy Core v1.x

对于1.x版本,client,server是两个程序,也就是分别对应:synergyc,synergys。

OS Command
Windows synergyc [server IP]
macOS ./synergyc [server IP]
Linux ./synergyc [server IP]
OS Command
Windows synergys -c [path to config file]
macOS ./synergys -c [path to config file]
Linux ./synergys -c [path to config file]

Synergy Core v2.x

v2.x,server,client 都是一个程序了,通过不同的命令来确定自己的职责。

OS Command
Windows synergy-core --client [server IP]
macOS ./synergy-core --client [server IP]
Linux ./synergy-core --client [server IP]
OS Command
Windows synergy-core --server -c [path to config file]
macOS ./synergy-core --server -c [path to config file]
Linux ./synergy-core --server -c [path to config file]

Command help

通过一下命令:

$ ./synergyc --help
$ ./synergys --help

你能获取v1.x synergy的帮助信息,但是v2.x不知道是不是忘了把这个加上。

$ ./synergy-core --help     ## 不会显示帮助信息

所以我就把它应有的帮助信息放这里了。

Options for synergy-core --client

Usage: synergy-core --client [--yscroll <delta>] [--daemon|--no-daemon] [--name <screen-name>] [--restart|--no-restart] [--debug <level>] <server-address>

Connect to a synergy mouse/keyboard sharing server.

  -d, --debug <level>      filter out log messages with priority below level.
                             level may be: FATAL, ERROR, WARNING, NOTE, INFO,
                             DEBUG, DEBUG1, DEBUG2.
  -n, --name <screen-name> use screen-name instead the hostname to identify
                             this screen in the configuration.
  -1, --no-restart         do not try to restart on failure.
*     --restart            restart the server automatically if it fails.
  -l  --log <file>         write log messages to file.
      --no-tray            disable the system tray icon.
      --enable-drag-drop   enable file drag & drop.
  -f, --no-daemon          run in the foreground.
*     --daemon             run as a daemon.
      --yscroll <delta>    defines the vertical scrolling delta, which is
                             120 by default.
  -h, --help               display this help and exit.
      --version            display version information and exit.

* marks defaults.

The server address is of the form: [<hostname>][:<port>].  The hostname
must be the address or hostname of the server.  The port overrides the
default port, 24800.

Options for synergy-core --server

Usage: synergy-core --server [--address <address>] [--config <pathname>] [--daemon|--no-daemon] [--name <screen-name>] [--restart|--no-restart] [--debug <level>]

Start the synergy mouse/keyboard sharing server.

  -a, --address <address>  listen for clients on the given address.
  -c, --config <pathname>  use the named configuration file instead.
  -d, --debug <level>      filter out log messages with priority below level.
                             level may be: FATAL, ERROR, WARNING, NOTE, INFO,
                             DEBUG, DEBUG1, DEBUG2.
  -n, --name <screen-name> use screen-name instead the hostname to identify
                             this screen in the configuration.
  -1, --no-restart         do not try to restart on failure.
*     --restart            restart the server automatically if it fails.
  -l  --log <file>         write log messages to file.
      --no-tray            disable the system tray icon.
      --enable-drag-drop   enable file drag & drop.
  -f, --no-daemon          run in the foreground.
*     --daemon             run as a daemon.
  -h, --help               display this help and exit.
      --version            display version information and exit.

* marks defaults.

The argument for --address is of the form: [<hostname>][:<port>].  The
hostname must be the address or hostname of an interface on the system.
The default is to listen on all interfaces.  The port overrides the
default port, 24800.

If no configuration file pathname is provided then the first of the
following to load successfully sets the configuration:
  $HOME/.synergy.conf
  /etc/synergy.conf

配置

GUI的配置就不讲了,这里说下cli程序的配置。

原文在这里Synergy Text Config,我这里简单提一下。然后按照我给的模版来改改就能用,需要特殊定制可看看官方wiki,或者留言问我。

Synergy 的配置文件基本格式:

section: ''name''
    ''args''
end

配置类型有4大类:

  • screens
  • aliases
  • links
  • options

aliases

定义 host name 和 屏幕命名的关系。

那么如何获取 host name呢?

mac os 环境下:

$ hostname
## 或者
$ echo $HOSTNAME

screens

定义我们的屏幕,命名,以及在操作各个屏幕时,是否响应一些特殊按键。

Windows,Linux,Mac 都有各自的特殊按键,如win, command, meta按键等,如果不是跨平台,都不需要做特殊处理。

links

定义各个屏幕之间的排列方位,这个很重要。

{left|right|up|down}[<range>] = name[<range>]

关于range参数就比较有意思了,合理配置可是下如下效果。

section: links
     moe:
         right        = larry
         up(50,100)   = curly(0,50)
     larry:
         left         = moe
         up(0,50)     = curly(50,100)
     curly:
         down(0,50)   = moe
         down(50,100) = larry(0,50)
 end

实现了:

#       +-----------+  
#       |   curly   |  
#       |           | 
#       +-----------+ 
# +----------+ +----------+
# |    moe   | |  larry   | 
# |          | |          | 
# +----------+ +----------+ 

也就是在curly屏幕时,鼠标往左下滑,会滑到moe屏幕,往右下滑,会滑到larray屏幕。

options

其他选项,例如定义心跳间隔,屏幕切换粘连时间,

配置样例

好的还是来一个大而全的配置文件例子吧。

启动一个Synergy-core server,配置文件是必须的!

Example textual configuration file
This example comes from doc/synergy-basic.conf

# sample synergy configuration file
#
# comments begin with the # character and continue to the end of
# line.  comments may appear anywhere the syntax permits.
# +----------+  +---------+ +---------+
# | mac-mini |  | macbook | | windows |
# |          |  |         | |         |
# +----------+  +---------+ +---------+

section: screens
    # three hosts named:  mac-mini, macbook, and windows
    # These are the nice names of the hosts to make it easy to write the config file
    # The aliases section below contain the "actual" names of the hosts (their hostnames)
    mac-mini:
    macbook:
    windows:
end

section: links
    # windows is to the right of macbook
    # mac-mini is to the left of macbook
    macbook:
        right(0,100) = windows # the numbers in parentheses indicate the percentage of the screen's edge to be considered active for switching)
        left  = mac-mini
        # shift = shift (shift, alt, super, meta can be mapped to any of the others)

    # macbook is to the right of mac-mini
    mac-mini:
        right = macbook

    # macbook is to the left of windows
    windows:
        left  = macbook
end

section: aliases
    # The "real" name of windows is John-Smiths-windows-3.local. 
    # If we wanted we could remove this alias and instead use John-Smiths-windows-3.local everywhere windows is above. 
    # Hopefully it should be easy to see why using an alias is nicer
    macbook:
        Pauls-MBP.local
    mac-mini:
        jumei-deMac-xp-mini.local
end

section: options
    switchDelay = 400   # 鼠标滑到边缘时,提留多久才能切换屏幕
    clipboardSharing = true # 共享剪切板
    clipboardSharingSize = 10000    # 剪切板共享字节大小限制,单位:千字节
end

启动

## 启动服务端
$ ./synergy-core --server --address 172.20.50.25:24800 --no-daemon --name macbook --config ./synergy.conf

## 启动其中一个客户端
$ ./synergy-core --client --no-daemon --name mac-mini 172.20.50.25:24801

常见问题

问题1:FATAL: An error occurred: assistive devices does not trust this process, allow it in system settings.

这是应为你运行命令行程序调用了系统的敏感接口,所有报错了。你需要给这个命令行软件授予权限.

也就是说,你用iTerm运行Synergy-core,就给iTerm授权,你用系统terminal运行的就给terminal授权。

我用的Mac,就在 系统偏好设置 > 安全性隐私 > 隐私 为其配置上。

权限授予

如果是第一次授权,一般会有个弹窗,你按照弹框点进去就行了。

问题2: synergy-core[66198:16759205] pid(66198)/euid(501) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!

你是不是还启动 Synergy GUI 程序,把它关掉就好了。

总结

synergy-core程序用着还不错,虽然配置麻烦了一点,不过鼠标,键盘映射都没问题,复制粘贴(图片都能跨屏幕传)也都正常。不过还没试过跨macos,windows使用过。试了再回来

参考

  • https://github.com/symless/synergy-core/wiki
  • https://my.oschina.net/k4nz/blog/4337405
  • https://apple.macx.cn/thread-2174542-1-1.html

MacOS 下 Docker 容器内挂载的文件内外不一致问题

Docker挂载的文件,在外部修改后,容器内不生效问题

问题

MacOS,装了vbox,在里面运行的Docker,拉了一个 openresty-alpine镜像,发现volume挂载的文件,通过tail -n 100 /usr/share/nginx/xx.lua, 和 cat /usr/share/nginx/xx.lua 两个命令展示的结果不一致。

我想大家也会有类似的,在IDE编辑了代码,发现Docker容器内的文件没变化这样的问题。

解决否

已解决

方案

version: "2"
services:
    php:
        image: php
        volumes:
            - /mnt/vbox/compose/bs/php/:/var/www/html/:rw,cached

最简单粗暴的就是在rw后面加上cached,至于原因,见下面的官方解释,以及国内大牛讲解。

官方解释:link

国内大佬的总结:blog

Mac环境,安装OpenCV,VScode断点调试C++程序

Mac环境下,配置OpenCV的C/C++环境,并且能够通过VSCode断点调试。

背景

最近在研究图像识别相关代码——OpenCV,为了便于调试,就要在Mac上搭建一个调试c++程序的调试环境。我这跑通了,分享给大家。

环境

  • Mac OS 10.14.5
  • xcode-select v2354
  • Visual Studio code 1.35.1
  • OpenCV 3.4.5
  • Clang 1001.0.46.4
  • CMake 3.14.5

步骤说明

  1. 编译环境准备
  2. 安装OpenCV
  3. 安装VScode 插件
  4. VScode 项目配置
  5. OpenCV HelloWorld

环境准备

Mac 自带有C/C++环境,就不再赘述。

Xcode Command Line Tools是必须的,可通过一下命令行触发安装程序,或者下载安装。

命令安装 xcode-select
 sudo xcode-select --install
下载 xcode-select 安装

Apple Developer Download

选择下载文件注意这里提示的版本号!
命令行安装速度很慢,且失败率很高,建议通过下载dmg文件安装

CMake 准备

编译OpenCV时要用到cmake。同样可以通过homebrew或者下载安装。
下载方式安装的CMake,cmake可执行程序在/Applications/CMake.app/Contents/bin/cmake.

如果你用的homebrew方式安装opencv那么CMake就不是必须的.

pkg-config 安装

编译引用有opencv库的c/c++代码需要附带很多参数来告知include path,libs等,用pkg-config就能精简编译参数。

brew install pkg-config

安装OpenCV

有两种方式安装。

brew install
## 一个命令安装好opencv3
brew install opencv3

安装好之后最好配置上这几个环境变量。(一般命令行会提示你)

export PATH="/usr/local/opt/opencv@3/bin:$PATH"

export LDFLAGS="-L/usr/local/opt/opencv@3/lib"
export CPPFLAGS="-I/usr/local/opt/opencv@3/include"

export PKG_CONFIG_PATH="/usr/local/opt/opencv@3/lib/pkgconfig"

用homebrew安装很方便,就是安装时间很长(我用了一个大白天)。额外还会安装python-opencv。

编译安装

点击下载源代码。这里我选择的是Sources 3.4.5

解压后,进入目录

cd <opencv 解压后目录>
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local 
make
sudo make install

这里创建的release目录会放cmake命令产生的成果。CMAKE_INSTALL_PREFIX配置的目录会存make命令产生的成果。

到这里如果都没报错就算安装成功了。

一般都不会出问题,在虚拟环境不一定能成功。

参考自 编译安装opencv

写个C++ Hello World

先写一个只引用了标准库的C++代码试试看。

“`C++
/// ./main.cpp
#include <stdio.h>
#include

int main(int argc, const char * argv[]) {
std::cout << “Hello, World!\n”;
return 0;
}


结果如下。 ![](http://cdn.itjsz.com/20200710162408.png) 没问题就继续尝试在代码引入`OpenCV`库。 #### pkg-config 环境配置 安装好后,配置一个环境变量就行了。 ```sh PKG_CONFIG_PATH="~/opencv解压目录/release/unix-install"

配置好之后在执行命令:

pkg-config opencv --libs --cflags opencv

显示内容大致如下,这块的内容就是opencv.pc里面的配置。

该环境变量就是为了让pkg-config能够找到opencv.pc这个文件。

写个OpenCV Demo

好,重点来了,大部分人会卡在这里

写个OpenCV HellowWorld来试试,打开一个空的VScode项目,创建
文件test.cpp

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
using namespace cv;

Mat src, dst, src_gray;

char* window_name = "Demo";

/**
 * @主函数
 */
int main( int argc, char** argv )
{

    /// 读取一副图片,不改变图片本身的颜色类型(该读取方式为DOS运行模式)
    src = imread(argv[1], 1 );

    /// 将图片转换成灰度图片
    cvtColor(src, src_gray, CV_RGB2GRAY);

    namedWindow(window_name, CV_WINDOW_AUTOSIZE);

    imshow(window_name, src_gray);

    while(true) {
        int c;
        c = waitKey(20);
        if (27 == (char) c) {
            break;
        }
    }
}

进入到命令行环境

## 编译测试程序
g++ `pkg-config opencv --libs --cflags opencv` ./test.cpp -o ./test.o
## 运行测试程序
./test.o ~/Downloads/opencv-logo.png

pkg-config 这条命令是为编译opencv测试代码引入类库。

能看到如下运行结果,恭喜你成功了。

可参考该链接最后: 编译自测程序

实际调试代码不停编译运行会敲很多命令,我们尝试把这些工作交给VScode。先为VScode安装一些插件

  • C/C++
  • C++ Intellisense
  • C++ Clang Command Adapter

在根目录下创建一个.vocode文件夹,再创建三个文件。

  • .vscode/launch.json
  • .vscode/tasks.json
  • .vscode/c_cpp_properties.json

文件.vscode/tasks.json

{
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.o",
                "`pkg-config",
                "--libs",
                "--cflags",
                "opencv`"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": []
        }
    ],
    "version": "2.0.0"
}

文件.vscode/launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.o",
            "args": [
                "~/Downloads/opencv-logo.png"
            ],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment":  [
                {"name": "PKG_CONFIG_PATH", "value": "opencv解压目录/release/unix-install"},   // 这是opencv解压码后创建的release目录下的unix-install, 要保证该目录下下有opencv.pc文件
                {"name": "DYLD_LIBRARY_PATH", "value": "/usr/local/opencv/build/lib"}   // 这个是你在编译时,opencv make时`CMAKE_INSTALL_PREFIX`指定的目录
                // {"DYLD_LIBRARY_PATH": "/usr/local/opencv/build/lib"}    // 这样的配置是错误的!
            ],
            "externalConsole": true,         // 这个开关打开表示,新开一个命令行窗口并运行程序。
            "MIMode": "lldb",
            "preLaunchTask": "g++ build active file"         // 这一行的内容要和上一个文件`.vscode/tasks.json`的`tasks`->`label`字段的内容要一模一样!
        }
    ]
}

文件.vscode/c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "/usr/local/include",
                "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include",
                "/usr/local/opencv/include",        // 这个是你在编译时,opencv make时`CMAKE_INSTALL_PREFIX`指定的目录
                "${workspaceFolder}"
            ],
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "/usr/local/include",
                    "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include",
                    "/usr/local/opencv/include",     // 这个是你在编译时,opencv make时`CMAKE_INSTALL_PREFIX`指定的目录
                    "${workspaceFolder}"
                ]
            },
            "intelliSenseMode": "clang-x64",
            "macFrameworkPath": [
                "/System/Library/Frameworks",
                "/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

点击VScode菜单栏Debug > Start Debugging运行结果如下:

为了便于调试,我把launch.jsonexternalConsole设置为true,这样会在vscode之外新开一个终端运行程序,可能会弹框要授权,同意即可。

常见问题

  1. pkg-config 配置不正确导致的报错
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
test.cpp:1:10: fatal error: 'opencv2/imgproc/imgproc.hpp' file not found
#include "opencv2/imgproc/imgproc.hpp"
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
[Finished in 0.4s with exit code 1]

解决方案:

运行一下命令添加一条path

export PKG_CONFIG_PATH="/opencv解压目录/release/unix-install":"$PKG_CONFIG_PATH"
  1. dyld 包依赖问题
dyld: Library not loaded: @rpath/libopencv_dnn.3.4.dylib
  Referenced from: ~/App/test
  Reason: image not found
bash: line 1: 18912 Abort trap: 6           ~/App/test
[Finished in 1.0s with exit code 134]

解决方案:

if you want to solve error on terminal you can use export DYLD_LIBRARY_PATH=your/lib:$DYLD_LIBRARY_PATH if you want to solve error on Xcode in build page , go to "Runpath Search Paths" add you lib path

运行一下命令添加一条path

export DYLD_LIBRARY_PATH=/usr/include/opencv/build/lib:$DYLD_LIBRARY_PATH

遇到问题,欢迎留言

原文链接。不支持转载!