检测X11与Wayland的有效方法,最好使用CMake [英] Effective way of detecting X11 vs Wayland, preferrably with CMake

查看:963
本文介绍了检测X11与Wayland的有效方法,最好使用CMake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经做了一些Google搜索,而这方面的知识很少.检测X11或Wayland是否正在使用(最好是在编译时以及与CMake一起使用)的有效且简单的方法是什么?我需要将其应用于我的C ++项目.

解决方案

我假设您要在编译期间(在调用CMake时)而不是在每次编译时都对显示服务器进行评估.这就是CMake的工作方式,应该使用它.缺点是,您必须为每台更改的显示服务器重新运行CMake.

当前没有检测运行中的显示服务器的默认方法.类似地,没有默认的代码片段来通过CMake评估显示服务器.只需选择一种检测显示的方法分别为您或您的环境手动工作的服务器.

从CMake调用此代码,并将结果存储在变量中,并将其用于C ++代码.

例如loginctl show-session $(loginctl | grep $(whoami) | awk '{print $1}') -p Type对我有用.最终的CMake检查是

execute_process(
    "loginctl show-session $(loginctl | grep $(whoami) | awk '{print $1}') -p Type"
    OUTPUT_VARIABLE result_display_server)
if ("${resulting_display_server}" EQUALS "Type=x11")
   set(display_server_x11 TRUE)
else()
   set(display_server_x11 FALSE)
endif()

可能您必须弄弄条件并检查Type=wayland或类似内容,以使其在您的环境中正常工作.

您可以使用display_server_x11将其写入config.h文件以便在C ++代码中使用./p>

So I've done some Google searching and this is something that has very little knowledge out there. What would be an effective and foolproof way of detecting whether X11 or Wayland is in use, preferrably at compile-time and with CMake? I need to apply this to a C++ project of mine.

解决方案

I assume you want to evaluate the display server during compile time, when calling CMake, instead of for every compilation. That's how CMake works and hot it should be used. One downside is, that you have to re-run CMake for every changed display server.

There is currently no default way to detect the running display server. Similar, there is no default code snippet to evaluate the display server by CMake. Just pick one way of detecting the display server that manually works for you or your environment respectively.

Call this code from CMake and store the result in a variable and use it for your C++ code.

For example loginctl show-session $(loginctl | grep $(whoami) | awk '{print $1}') -p Type works for me. The resulting CMake check is

execute_process(
    "loginctl show-session $(loginctl | grep $(whoami) | awk '{print $1}') -p Type"
    OUTPUT_VARIABLE result_display_server)
if ("${resulting_display_server}" EQUALS "Type=x11")
   set(display_server_x11 TRUE)
else()
   set(display_server_x11 FALSE)
endif()

Probably you have to fiddle around with the condition and check for Type=wayland or similar to get it properly working in your environment.

You can use display_server_x11 and write it into a config.h file to use it within C++ code.

这篇关于检测X11与Wayland的有效方法,最好使用CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆