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,输出typeuint32_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();
//获得全部窗口widconst 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和dockif (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

参考博客

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. win32根据进程名获取进程ID或者终止进程

    第一部分: 根据进程名获取进程ID. 首先我们需要了解三个windows API函数: HANDLE WINAPI CreateToolhelp32Snapshot(DWORD dwFlags,DWO ...

  3. linux如何查询进程ID和关闭进程

    1.查看端口占用的进程号 (1)比如要查询8080 端口 lsof -i:8080 COMMAND:进行成名,如上说明8080被一个server进程占用 PID:进程的ID,一般杀掉进程时就是指定进程 ...

  4. 为应用程序池**提供服务的进程意外终止。进程ID是**。进程退出代码是'0x80'

    2010-8-26 11:39  事件类型: 警告  事件来源: W3SVC  事件种类: 无  事件 ID: 7034  日期: 2010-8-XX  事件: XX:XX:XX  用户: XX  计 ...

  5. 通过进程ID获得该进程主窗口的句柄

    一个进程可以拥有很多主窗口,也可以不拥有主窗口,所以这样的函数是不存在的,所幸的是,相反的函数是有的.所以我们可以调用EnumWindows来判断所有的窗口是否属于这个进程. typedef stru ...

  6. 使用cat /proc/进程id/maps 查看进程内存映射

    proc/<PID>/maps 查看进程的虚拟地址空间是如何使用的. 该文件有6列,分别为: 地址:库在进程里地址范围 权限:虚拟内存的权限,r=读,w=写,x=,s=共享,p=私有: 偏 ...

  7. Kibana停止kibana的方法命令:找到5601端口对应的进程ID 把此进程进行kill即可

    报错 {"type": "log","@timestamp": "2018-11-21T06:32:59Z"," ...

  8. Linux 内核进程管理之进程ID

    Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数据和结构,Linux 内核所有涉及到进程和程序的所有算法都是围绕该数据结构建立的,是内核中最重要的数据结构之一.该数据结构 ...

  9. 详解窗口句柄,进程ID,进程句柄,窗口与进程之间的关系

    窗口句柄: 在Windows或Linux等操作系统下窗口句柄对应着每个窗口的钥匙,例如,你要对某一个窗口进行操作,你必须知道你要操作哪一个窗口,这里就拿Windows做举例,Windows下几乎会创建 ...

最新文章

  1. LR需要理解的一些内容
  2. OpenLayers 3+Geoserver+PostGIS实现点击查询
  3. Codeforces Round #354 (Div. 2)-A
  4. 网民关注iPhone、Google、微软和安全
  5. 【bzoj2850】巧克力王国 KD-tree
  6. 一、TensorFlow的简介和安装和一些基本概念
  7. P3512 [POI2010]PIL-Pilots
  8. 计算几何——多边形面积
  9. org.postgresql.util.PSQLException: 不支援 10 验证类型
  10. 从单目视觉信息学习深度信息(一)
  11. 域名过户操作流程及常见问题
  12. 干货 I 用数据分析进行“无死角”的复盘?
  13. Android_线程_多线程下载
  14. TMS28027输出PWM波例程解析
  15. 奇虎360 2013校园招聘笔试题
  16. 桥接,NAT,Host Only的区别
  17. 配置git send-mail发邮件 (126邮箱适用)
  18. IOS开发UI-------button
  19. 今天获取的云蹦迪直播软件源码全开源
  20. Java自学视频整理(持续更新中...)

热门文章

  1. 英特尔杯作品 2010年一等奖作品摘要
  2. 法令纹出卖年龄 该如何消除?
  3. 学记笔记 $\times$ 巩固 · 期望泛做$Junior$
  4. java时间类型处理manman
  5. 微信JSSDK中的初始化方法调用遇到的问题
  6. 数据库原理第四章课后题答案(第四版)
  7. 【shader学习历程】翻页特效
  8. AI行业态势感知(第二期)
  9. 「DP Rec.」[USACO21JAN] Uddered but not Herd G
  10. 剪枝论文二(Filters Pruning)