- Improved stability of kintox11, removed unneeded functions

This commit is contained in:
Ben Reaves
2020-01-29 21:29:59 -06:00
parent b167c0a90f
commit 69697ce905
2 changed files with 14 additions and 103 deletions

Binary file not shown.

View File

@@ -6,7 +6,7 @@
http://k-ui.jp/blog/2012/05/07/get-active-window-on-x-window-system/
*/
// To compile
// gcc active_window.c -lX11 -lXmu
// gcc kintox11.c -lX11 -lXmu
//
//
@@ -61,82 +61,8 @@ Window get_focus_window(Display* d){
return w;
}
// get the top window.
// a top window have the following specifications.
// * the start window is contained the descendent windows.
// * the parent window is the root window.
Window get_top_window(Display* d, Window start){
Window w = start;
Window parent = start;
Window root = None;
Window *children;
unsigned int nchildren;
Status s;
while (parent != root) {
w = parent;
s = XQueryTree(d, w, &root, &parent, &children, &nchildren); // see man
if (s)
XFree(children);
if(xerror){
printf("failed to get top window\n");
exit(1);
}
}
return w;
}
// search a named window (that has a WM_STATE prop)
// on the descendent windows of the argment Window.
Window get_named_window(Display* d, Window start){
Window w;
w = XmuClientWindow(d, start); // see man
if(w == start)
return w;
}
// (XFetchName cannot get a name with multi-byte chars)
void print_window_name(Display* d, Window w){
XTextProperty prop;
Status s;
s = XGetWMName(d, w, &prop); // see man
if(!xerror && s){
int count = 0, result;
char **list = NULL;
result = XmbTextPropertyToTextList(d, &prop, &list, &count); // see man
if(result == Success){
printf("\t%s\n", list[0]);
}else{
// printf("ERROR: XmbTextPropertyToTextList\n");
}
}else{
// printf("ERROR: XGetWMName\n");
}
}
void print_window_class(Display* d, Window w){
Status s;
XClassHint* class;
class = XAllocClassHint(); // see man
if(xerror){
// printf("ERROR: XAllocClassHint\n");
}
s = XGetClassHint(d, w, class); // see man
if(xerror || s){
// printf("\tname: %s\n\tclass: %s\n", class->res_name, class->res_class);
printf("%s\n", class->res_class);
}else{
printf("ERROR: XGetClassHint\n");
}
}
const char * str_window_class(Display* d, Window w){
const char * str_window_class(Display* d, Window w, char *prior_app ){
Status s;
XClassHint* class;
@@ -153,17 +79,13 @@ const char * str_window_class(Display* d, Window w){
// printf("\tname: %s\n\tclass: %s\n", class->res_name, class->res_class);
return app_class;
}else{
char * error_msg;
error_msg = malloc(sizeof(char)*50);
strcpy(error_msg, "ERROR: XGetClassHint");
return error_msg;
// char * error_msg;
// error_msg = malloc(sizeof(char)*50);
// strcpy(error_msg, "ERROR: XGetClassHint");
return prior_app;
}
}
void print_window_info(Display* d, Window w){
print_window_class(d, w);
}
int main(void){
char line[RSIZ][LSIZ];
@@ -181,13 +103,6 @@ int main(void){
}
tot = i;
// printf("\n The content of the file %s are : \n","./appnames.csv");
// for(i = 0; i < tot; ++i)
// {
// printf("%s\n", line[i]);
// }
// printf("\n");
Display* d;
Window w;
@@ -206,46 +121,42 @@ int main(void){
// get active window
w = get_focus_window(d);
w = get_top_window(d, w);
w = get_named_window(d, w);
for (;;)
{
if(strcmp(str_window_class(d, w),prior_app)){
if(strcmp(str_window_class(d, w,prior_app),prior_app)){
int len = sizeof(line)/sizeof(line[0]);
// printf("length: %d\n",len);
int i;
for(i = 0; i < len; ++i){
// printf("i: %d\n",i);
// printf(strcicmp(line[i], str_window_class(d, w)));
if((strcicmp(line[i], str_window_class(d, w)) == 0 && (remap_bool == 1 || remap_bool == 2))) {
// printf(strcicmp(line[i], str_window_class(d, w, prior_app)));
if((strcicmp(line[i], str_window_class(d, w,prior_app)) == 0 && (remap_bool == 1 || remap_bool == 2))) {
// printf("Gotcha!\n");
// printf("%s - prior app %s\n",str_window_class(d, w),prior_app);
// printf("%s - prior app %s\n",str_window_class(d, w, prior_app),prior_app);
printf("%s\n","term");
remap_bool = 0;
fflush(stdout);
break;
}
else if((strcicmp(line[i], str_window_class(d, w)) == 0 && remap_bool == 0)){
else if((strcicmp(line[i], str_window_class(d, w,prior_app)) == 0 && remap_bool == 0)){
break;
}
else if (i == 49 && (remap_bool == 0 || remap_bool == 2)){
printf("%s\n","gui");
// printf("no match - %s - prior app %s\n",str_window_class(d, w),prior_app);
// printf("no match - %s - prior app %s\n",str_window_class(d, w, prior_app),prior_app);
remap_bool = 1;
fflush(stdout);
break;
}
}
}
strcpy(prior_app,str_window_class(d, w));
strcpy(prior_app,str_window_class(d, w, prior_app));
XEvent e;
XNextEvent(d, &e);
// get active window
w = get_focus_window(d);
w = get_top_window(d, w);
w = get_named_window(d, w);
}
}