linux窗口透明

使用到了qt xcb-ewmh x11-xcb

效果图

如何实现

控制全部窗口透明
1.遍历WID树,的到全部窗口得wid
2.区别窗口属性,桌面和dock窗口不设置透明,其他窗口设置透明(透明度随着滑动条)
3.监听x11时间,新的窗口创建就设置透明度感觉话滑动条

核心代码 通过名称找到该程序的wid

#ifndef SETDESKTOP_H
#define SETDESKTOP_H // Attempt to identify a window by name or attribute.
// by Adam Pierce <adam@doctort.org> #include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <iostream>
#include <list>
#include <stdlib.h> #include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h> int find_pid_by_name(char *ProcName, int *foundpid)
{
DIR *dir;
struct dirent *d;
int pid, i;
char *s;
int pnlen; i = 0;
foundpid[0] = 0;
pnlen = strlen(ProcName); /* Open the /proc directory. */
dir = opendir("/proc");
if (!dir) {
printf("cannot open /proc");
return -1;
} /* Walk through the directory. */
while ((d = readdir(dir)) != NULL) { char exe [PATH_MAX + 1];
char path[PATH_MAX + 1];
int len;
int namelen; /* See if this is a process */
if ((pid = atoi(d->d_name)) == 0) continue; snprintf(exe, sizeof(exe), "/proc/%s/exe", d->d_name);
if ((len = readlink(exe, path, PATH_MAX)) < 0)
continue;
path[len] = '\0'; /* Find ProcName */
s = strrchr(path, '/');
if (s == NULL) continue;
s++; /* we don't need small name len */
namelen = strlen(s);
if (namelen < pnlen) continue; if (!strncmp(ProcName, s, pnlen)) {
/* to avoid subname like search proc tao but proc taolinke matched */
if (s[pnlen] == ' ' || s[pnlen] == '\0') {
foundpid[i] = pid;
i++;
}
}
} foundpid[i] = 0;
closedir(dir); return 0; } using namespace std; class WindowsMatchingPid
{
public:
WindowsMatchingPid(Display *display, Window wRoot, unsigned long pid)
: _display(display)
, _pid(pid)
{
// Get the PID property atom.
_atomPID = XInternAtom(display, "_NET_WM_PID", True);
if (_atomPID == None) {
cout << "No such atom" << endl;
return;
} search(wRoot);
} const list<Window> &result() const { return _result; } const list<Window> &Allresult() const { return _allResult; } private:
unsigned long _pid;
Atom _atomPID;
Display *_display;
list<Window> _result;
list<Window> _allResult;
void search(Window w)
{
// Get the PID for the current Window.
Atom type;
int format;
unsigned long nItems;
unsigned long bytesAfter;
unsigned char *propPID = 0;
if (Success == XGetWindowProperty(_display, w, _atomPID, 0, 1, False, XA_CARDINAL,
&type, &format, &nItems, &bytesAfter, &propPID)) {
if (propPID != 0) {
// If the PID matches, add this window to the result set.
if (_pid == *((unsigned long *)propPID))
_result.push_back(w); XFree(propPID);
}
} // Recurse into child windows.
Window wRoot;
Window wParent;
Window *wChild;
unsigned nChildren;
if (0 != XQueryTree(_display, w, &wRoot, &wParent, &wChild, &nChildren)) {
for (unsigned i = 0; i < nChildren; i++) {
search(wChild[i]);
_allResult.push_back(wChild[i]);
}
}
}
};
#endif // SETDESKTOP_H
//通过名称寻找改名称的wid的list
QList<unsigned long> MainWindow::searchWindowid(const QString &name)
{
QList<Window> wlist; char *str = NULL;
QByteArray ba = name.toLatin1();
str = (char *)malloc(ba.length() + 1);
memset(str, 0, ba.length());
memcpy(str, ba.data(), ba.length()); str[ba.length()] = '\0';
//设置desktop透明
int pid_t[128];
find_pid_by_name(str, pid_t);
int pid = pid_t[0]; Display *display = XOpenDisplay(0);
WindowsMatchingPid match(display, XDefaultRootWindow(display), pid);
const list<Window> &result = match.result();
for (Window id : result) {
wlist.push_back(id);
}
return wlist;
}

通过wid查找窗口属性{我这里是为了判断是否是desktop和dock属性}

//初始化

    xcb_ewmh_connection_t m_ewmh_connection;
xcb_intern_atom_cookie_t *m_cookie{nullptr}; m_cookie = xcb_ewmh_init_atoms(QX11Info::connection(), &m_ewmh_connection);
xcb_ewmh_init_atoms_replies(&m_ewmh_connection, m_cookie, NULL);
//传入wid,输出type

    uint32_t searchWindowType(int wid)
{
uint32_t reId = 0;
if (m_cookie) { xcb_get_property_cookie_t cooke = xcb_ewmh_get_wm_window_type(&m_ewmh_connection, wid); xcb_ewmh_get_atoms_reply_t name;
xcb_generic_error_t *error_t = new xcb_generic_error_t;
// xcb_ewmh_get_wm_window_type_reply(&m_ewmh_connection, cooke, &name, NULL);
xcb_ewmh_get_wm_window_type_reply(&m_ewmh_connection, cooke, &name, &error_t);
qDebug() << "ssss";
if (error_t) {
qDebug() << error_t->response_type;
qDebug() << error_t->error_code;
qDebug() << error_t->sequence;
qDebug() << error_t->resource_id;
qDebug() << error_t->minor_code;
qDebug() << error_t->major_code;
delete error_t;
error_t = NULL;
return 0;
} else { }
qDebug() << "eeee";
if (name.atoms && name.atoms_len <= 10) {
reId = name.atoms[0];
} }
return reId;
}

通过 wid转化为QWindow,并设置透明度(获取全部窗口)

void MainWindow::setAllWindows()
{
qDebug() << "xxx1";
char *str = NULL;
QByteArray ba = "";
str = (char *)malloc(ba.length() + 1);
memset(str, 0, ba.length());
memcpy(str, ba.data(), ba.length());
qDebug() << "xxxx2";
str[ba.length()] = '\0';
//设置desktop透明
int pid_t[128];
find_pid_by_name(str, pid_t);
int pid = pid_t[0]; qDebug() << "xxxx3";
Display *display = XOpenDisplay(0);
WindowsMatchingPid match(display, XDefaultRootWindow(display), pid);
// const list<Window> &result = match.result();
//获得全部窗口wid
const list<Window> &allresult = match.Allresult();
qDebug() << "xxxx4";
for (Window id : allresult) {
QWindow *window = QWindow::fromWinId((unsigned long)id);
uint32_t indexId = searchWindowType(id) ; qDebug() << indexId;
//373和374一般都为desktop和dock
if (window != nullptr && !m_noOpacityId.contains(id)
&& (indexId != 373 || indexId == 374)) { //滑动条的值,这里可以自定义
int value = ui->opacitySlider->value();
double a = (double)value;
double o = a / 100.0;
window->setOpacity(o);
static int i = 0;
qDebug() << "ok" << i++;
strucWindow st;
st.window = window;
st.wid = id;
st.name = "name";
st.opacity = o;
m_windowVec.insert(id, st); }
}
}

代码地址:

https://github.com/dependon/x11opacitytool

程序下载地址,appimage程序,在deepin和uos上测试过

https://download.csdn.net/download/qq_43081702/16658009

参考博客

https://blog.csdn.net/nicholas_dlut/article/details/80990289 linux下C++根据进程名字获取进程的进程号PID
http://www.voidcn.com/article/p-dbqsbdxh-bsp.html linux下C++根据进程名字获取进程的进程号PID

linux窗口透明(全局透明,进程id查找wid,进程名称查找wid)的更多相关文章

  1. linux与windows查看占用端口的进程ID并杀死进程

    有时候tomcat出现端口被占用,需要查出进程ID并杀死进程. 1.查找占用端口的进程ID(windows与linux一样  8005也可以加上引号   grep可以用findstr替换) 6904就 ...

  2. 进程的基本属性:进程ID、父进程ID、进程组ID、会话和控制终端

    摘要:本文主要介绍进程的基本属性,基本属性包含:进程ID.父进程ID.进程组ID.会话和控制终端. 进程基本属性 1.进程ID(PID) 函数定义:      #include <sys/typ ...

  3. C++ Windows 下 根据进程名获取进程ID 以及该进程下所有窗口的句柄

    #include <windows.h> #include <stdint.h> #include <tlhelp32.h> #include <stdio. ...

  4. windows下根据进程ID强制杀死进程

    [windows 进程ID PID]NTSD命令详解 1. ntsd -c q -p PID 2. ntsd -c q -pn ImageName 比如:ntsd -c q -pn qq.exe -c ...

  5. 【转】iis解决应用程序池**提供服务的进程意外终止进程ID是**。进程退出代码是'0x80'

    转自:http://blog.sina.com.cn/s/blog_56a68d5501013xdd.html 我们公司旗下的红黑互联会遇到这种问题 事件类型: 警告事件来源: W3SVC事件种类: ...

  6. linux查找进程id和杀死进程以及查看内存??

    ps 命令用于查看当前正在运行的进程 ps ax : 显示当前系统进程的列表 ps aux : 显示当前系统进程详细列表以及进程用户 -e 显示所有进程,环境变量 此参数的效果和指定"A&q ...

  7. linux 端口号、进程id、杀进程、查询tcp的连接(各种状态的)

    sudo netstat -antupkill -s 9 50713netstat -n | grep 61616netstat -n | awk '/^tcp/ {++S[$NF]} END {fo ...

  8. 根据关键词获取进程ID然后杀掉进程

    例如需要杀掉监听进程,如下: [oracle@kel ~]$ ps -ef|grep lsnr oracle 4973 1 1 19:40 ? 00:00:00 /home/oracle/produc ...

  9. windows根据进程id杀死任务进程

    然后打开任务管理器找出来结束进程即可

  10. linux查找进程id端口占用和杀死进程

    linux 查找进程id端口占用和杀死进程 ps 命令用于查看当前正在运行的进程 辅助上grep 用于搜索匹配ps -ef | grep java ps ax : 显示当前系统进程的列表 ps aux ...

随机推荐

  1. new vue 实例发生了什么呢?

    前言 最近全面栽进vue源码解析中,将出一系列的学习笔记 以及个人的一些总结 第一步准备工作 到GitHub上下载vue的源码(巧妇难为无米之炊) 用自己喜欢的编辑器打开源码 vue主要源码資源在sr ...

  2. winform控件 datagridview分页功能 界面实现需要有上一页下一页等操作控件 dataGridView1 控件的数据绑定方式 如何实现分页中的数据修改然后进行保存 请列出详细例子 特别保存部分

    以下提供一个示例来说明如何在 WinForms 中实现分页功能,并在分页中实现数据修改并保存的操作. 首先,我们需要一个包含数据源的 DataGridView 控件,并添加上一页.下一页等操作控件来实 ...

  3. 一个ABAQUS model需要的Component

    component of abaqus model Abaqus模型由几个不同的组件组成,它们共同描述了要分析的物理问题. a abaqus model 至少要有: discrete goemtry ...

  4. BUGKU_PWN_OVERFLOW2_WP

    WP_OVERFLOW2 拿到程序,首先放到我们的kali里面看看是多少位的程序,然后在看看有没有什么安全属性 64位程序,并且开启了RELRO,NX 也就是说,这道题我们需要使用ROP绕过 使用id ...

  5. Open-Sora 2.0 重磅开源!

    潞晨科技正式推出 Open-Sora 2.0 -- 一款全新开源的 SOTA 视频生成模型,仅 20 万美元(224 张 GPU)成功训练商业级 11B 参数视频生成大模型.开发高性能的视频生成模型通 ...

  6. crontab使用路径的问题

    crontab工具的一个大问题就是不能支持相对路径,会导致文件不能找到,在crontab启用脚本中加入cd指令,使得工作目录切换到运行工具所需的目录,即可 * 定时任务 每天凌晨0点执行 * 00 0 ...

  7. Go语言计算字符串长度——len()和RuneCountInString()

    Go 语言的内建函数 len(),可以用来获取切片.字符串.通道(channel)等的长度.下面的代码可以用 len() 来获取字符串的长度. tip1 := "genji is a nin ...

  8. Git安装与Git GUI的使用

    一.下载安装包 官网:https://git-scm.com/downloads(下载慢) 或 https://pc.qq.com/search.html#!keyword=git,本人使用的是Git ...

  9. oracle修改用户密码的方法

    Oracle用户名及默认密码 修改oracle用户的密码有以下方法: 普通用户 (1)通过alter user语法来进行修改 ,这也是最常见的方式: (2) 第二种方式,是通过password命令来修 ...

  10. Shell语言编程(炼气)

    1. Shell脚本执行方式 执行方式 应用及场景 通过sh或bash 书写脚本后,最常用的方式,在其他非红帽系统中,建议使用bash运行脚本 通过.点或source 加载/生效配置文件(环境变量,别 ...