-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux_x11.cpp
More file actions
126 lines (108 loc) · 3.47 KB
/
linux_x11.cpp
File metadata and controls
126 lines (108 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Linux/X11 specific code for obtaining information about the frontmost window
*/
#include "linux_x11.h"
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
linux_x11::linux_x11()
{
}
/**
* Returns the window name for a specific window on a display
***/
char *linux_x11::name (Display *disp, Window win) {
// Atom prop = XInternAtom(disp,"WM_NAME",False), type;
Atom prop = XInternAtom(disp,"_NET_WM_NAME",False), type;
int form;
unsigned long remain, len;
unsigned char *list;
if (XGetWindowProperty(disp,win,prop,0,1024,False,AnyPropertyType, &type,&form,&len,&remain,&list) != Success) {
return NULL;
}
// if(strlen((char*)list) == 0) {
// Atom prop2 = XInternAtom(disp,"_NET_WM_NAME",False), type;
// if (XGetWindowProperty(disp,win,prop2,0,1024,False,AnyPropertyType, &type,&form,&len,&remain,&list) != Success)
// return NULL;
// }
return (char*)list;
}
/**
* Returns the pid for a specific window on a display
***/
int* linux_x11::pid(Display *disp, Window win) {
Atom prop = XInternAtom(disp,"_NET_WM_PID",False), type;
int form;
unsigned long remain, len;
unsigned char *list;
if (XGetWindowProperty(disp,win,prop,0,1024,False,AnyPropertyType, &type,&form,&len,&remain,&list) != Success)
return NULL;
return (int*)list;
}
/**
* Returns the active window on a specific display
***/
Window * linux_x11::active (Display *disp, unsigned long *len) {
Atom prop = XInternAtom(disp,"_NET_ACTIVE_WINDOW",False), type;
int form;
unsigned long remain;
unsigned char *list;
if (XGetWindowProperty(disp,XDefaultRootWindow(disp),prop,0,1024,False,XA_WINDOW, &type,&form,len,&remain,&list) != Success)
return NULL;
return (Window*)list;
}
/**
* Returns process name from pid (processes output from /proc/<pid>/status)
***/
QString linux_x11::processName(long pid)
{
// construct command string
QString command = "cat /proc/" + QString("%1").arg(pid) + "/status";
// capture output in a FILE pointer returned from popen
FILE * output = popen(command.toStdString().c_str(), "r");
// initialize a buffer for storing the first line of the output
char buffer[1024];
// put the contents of the buffer into a QString
QString line = QString::fromUtf8(fgets(buffer, sizeof(buffer), output));
// close the process pipe
pclose(output);
// take right substring of line returned to get process name
return line.right(line.length() - 6).replace("\n", "");
}
QList<WindowInfo> linux_x11::getActiveWindows()
{
QList<WindowInfo> windowTitles;
unsigned long len;
Display *disp = XOpenDisplay(NULL);
Window *list;
char *n;
int* p;
list = (Window*)active(disp,&len);
if((int)len > 0)
{
for (int i=0;i<(int)len;i++) {
n = name(disp,list[i]);
p = pid(disp, list[i]);
long p_id = 0;
QString pName;
QString windowTitle;
if(p!=NULL)
{
p_id = *p; // dereference pointer for obtaining pid
pName = processName(p_id);
}
if(n!=NULL)
windowTitle = QString::fromUtf8(n);
WindowInfo wi;
wi.setWindowTitle(windowTitle);
wi.setProcessName(pName);
wi.setPID(p_id);
windowTitles.append(wi);
delete n;
delete p;
}
}
delete list;
XCloseDisplay (disp);
return windowTitles;
}