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. 解决 Dell PowerEdge T630 增加第三方 PCIe 设备后制冷系统异常

    博客链接:解决 Dell PowerEdge T630 增加第三方 PCIe 设备后制冷系统异常 配置 Device: Dell PowerEdge T630 CPU: Intel(R) Xeon(R ...

  2. 本地如何访问vue2 生成的dist代码

    前言 当你使用 Vue CLI 或其他构建工具构建 Vue 2 项目时,它会生成一个 dist 文件夹,这个文件夹包含了你项目的生产环境版本的静态资源文件(HTML.JavaScript 和 CSS) ...

  3. kubernetes安装配置使用vGPU

    前言 AI 落地时,在某些场景下 AI 模型在训练或者是推理时,其算力要求不需要占用整卡的 GPU,比如只需要0.5卡 GPU 即可满足需求. 在这种情况下,可以使用 GPU 虚拟化技术来解决这个问题 ...

  4. kubectl port-forward bind: address already in use unable

    前言 本地的 8080 映射到 Pod 的 80,kubectl 会把这个端口的所有数据都转发给集群内部的 Pod kubectl port-forward wp-pod 8080:80 & ...

  5. CISCN&CCB半决赛_2025_PWN_WP

    CISCN&CCB半决赛_2025_PWN_WP 前言: 记录一下第一次打半决赛国赛,总结来说还是自己太菜了,还有check脚本是真的很shi,正规军白给了... typo break edi ...

  6. BUUCTF---达芬奇的密码

    题目 达芬奇隐藏在蒙娜丽莎中的数字列:1 233 3 2584 1346269 144 5 196418 21 1597 610 377 10946 89 514229 987 8 55 6765 2 ...

  7. VSCode 终端中文乱码解决方案

    问题描述 以下为一个简单的 c++ 文件代码,注意中文部分的显示内容 //测试代码 #include <stdio.h> int main() { int y = 10; int coun ...

  8. 2024睿抗机器人开发者大赛CAIP-编程技能赛-本科组(省赛) RC-u5 工作安排详解

    本文参考 https://www.cnblogs.com/Kescholar/p/18306136 这一题可能对高手来说就能轻而易举的看出是个01背包,但是对于我这种小白还是要经过详细的分析才可以理解 ...

  9. 记录一次ubuntu软件安装未完全的解决

    背景 预想是在ubuntu20.10上去安装android-studio,所以找了个教程,是使用ubuntu-make来进行安装,不过我也不知为何,安装到最后,出现了dpkg的报错并返回,错误提示是让 ...

  10. unity 多层叠加的BillBoard特效转序列帧特效降低overdraw