linux窗口透明(全局透明,进程id查找wid,进程名称查找wid)
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)的更多相关文章
- linux与windows查看占用端口的进程ID并杀死进程
有时候tomcat出现端口被占用,需要查出进程ID并杀死进程. 1.查找占用端口的进程ID(windows与linux一样 8005也可以加上引号 grep可以用findstr替换) 6904就 ...
- 进程的基本属性:进程ID、父进程ID、进程组ID、会话和控制终端
摘要:本文主要介绍进程的基本属性,基本属性包含:进程ID.父进程ID.进程组ID.会话和控制终端. 进程基本属性 1.进程ID(PID) 函数定义: #include <sys/typ ...
- C++ Windows 下 根据进程名获取进程ID 以及该进程下所有窗口的句柄
#include <windows.h> #include <stdint.h> #include <tlhelp32.h> #include <stdio. ...
- 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 ...
- 【转】iis解决应用程序池**提供服务的进程意外终止进程ID是**。进程退出代码是'0x80'
转自:http://blog.sina.com.cn/s/blog_56a68d5501013xdd.html 我们公司旗下的红黑互联会遇到这种问题 事件类型: 警告事件来源: W3SVC事件种类: ...
- linux查找进程id和杀死进程以及查看内存??
ps 命令用于查看当前正在运行的进程 ps ax : 显示当前系统进程的列表 ps aux : 显示当前系统进程详细列表以及进程用户 -e 显示所有进程,环境变量 此参数的效果和指定"A&q ...
- linux 端口号、进程id、杀进程、查询tcp的连接(各种状态的)
sudo netstat -antupkill -s 9 50713netstat -n | grep 61616netstat -n | awk '/^tcp/ {++S[$NF]} END {fo ...
- 根据关键词获取进程ID然后杀掉进程
例如需要杀掉监听进程,如下: [oracle@kel ~]$ ps -ef|grep lsnr oracle 4973 1 1 19:40 ? 00:00:00 /home/oracle/produc ...
- windows根据进程id杀死任务进程
然后打开任务管理器找出来结束进程即可
- linux查找进程id端口占用和杀死进程
linux 查找进程id端口占用和杀死进程 ps 命令用于查看当前正在运行的进程 辅助上grep 用于搜索匹配ps -ef | grep java ps ax : 显示当前系统进程的列表 ps aux ...
随机推荐
- git上传大文件!git push 报错 ! [remote rejected] main -> main (pre-receive hook declined) error_ failed to push some refs to 'xxx
前言 今天在用git push项目的时候,出现了一个报错,记录一下解决方案,以后报同样的错误可以回来看. 错误信息 下面是git push的详细报错信息: 20866@DESKTOP-7R0VL04 ...
- C/C++ 创建Socket实现双工通信
点击查看代码 实现简单的Socket通信 服务端代码(Server) #include <stdio.h> #include <winsock2.h> #pragma comm ...
- Matlab转python的索引问题
python 中numpy库可以实现类似matlab多维数组的运算.但两者在索引方式上存在一些差异.这是需要注意的.例如: % 定义一个4*4矩阵 A=1:16; A=reshape(A,[4,4]) ...
- 【Bug记录】[@vue/compiler-sfc] `defineProps` is a compiler macro and no longer needs to be imported.
[Bug记录][@vue/compiler-sfc] defineProps is a compiler macro and no longer needs to be imported. Vue3项 ...
- Thinkphp3.2 PHPMailer 发送邮件
第一步 :下载附件PHPMailer解压到ThinkPHP\Library\Vendor 第二步:在Common文件夹中的公共函数function.php中写一个发送邮件的函数, 这样可以在项目任意位 ...
- PX4 仿真环境开发整理
博客地址:https://www.cnblogs.com/zylyehuo/ (一)PX4 仿真开发 搭建仿真环境 概念介绍及环境建议 MAVROS安装(适用于ROS1.ROS2) Ubuntu安装Q ...
- Java使用多线程处理未知任务数方案
知道任务个数,你可以定义好线程数规则,生成线程数去跑 代码说明: 虚拟线程池: 使用 Executors.newVirtualThreadPerTaskExecutor() 创建虚拟线程池,每个任务将 ...
- SQL Server 中的异常处理
为什么我们需要 SQL Server 中的异常处理? 让我们通过一个示例来了解 SQL Server 中异常处理的必要性.因此,创建一个 SQL Server 存储过程,通过执行以下查询来除以两个数字 ...
- 学习Kotlin语法(一)
简介 Kotlin是一种现代.简洁且功能强大的编程语言,特别适合Android开发.本文将从基础语法开始,逐步掌握Kotlin的核心特性. 目录 变量 基本类型 数字 无符号对应项 布尔值 字符和字符 ...
- 独家推荐!这款端到端AI测试工具 Testim,让效率飙升!(支持移动端、Web端)
在当今快速发展的软件开发时代,确保软件质量成为每个开发团队的首要任务. 随着人工智能(AI)和机器学习(ML)技术的飞速发展,AI测试工具应运而生,为软件测试领域带来了革命性的变化.今天,我要向大家强 ...