Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the active application using C/java?

Tags:

I want to record the active application and save a history of my active applications. I say active application because if I run an application and It's minimized, etc I would not count it as an active application. In order to make my question more clear let's look at this example. I open Firefox and surf the web for 20 minutes. Then I open a text editor and start writing for 5 minutes (Firefox is running but I don't use it and so Firefox should not be counted as an active application). So I should be able to record the following information:

Firefox -> 20 minutes 
Text editor -> 5 minutes

I want to track each used application during the OS (Linux is preferred) is on and make an open source application that says how much you use each application.

UPDATE : the application that I want to record is the application that user see on the display and is working with. For instance you might change the windows size in such a way that you can see both Firefox and Text editor (alongside, in different work-space, cascaded, etc), but you are typing in text editor, therefore, the text editor is the active application. In other word the application is active if you are interacting with or is the last interacted app that you are looking at to read some thing in pdf, text, etc. Interacting with the application: I mean clicking, typing, scrolling, etc.

NARROWING : The only thing that i can not figure out is how to get the active application with these conditions. I think if I get the topest application on the display in the current workspace that would solve the problem, hoever, we have a property to set to a windows (always on top) that might need to get without this property enabled. I have found this question that might help to answer.

Using Fedora 26,

Thanks in advance.

like image 926
Hossein Amiri Avatar asked Oct 08 '17 00:10

Hossein Amiri


2 Answers

  1. Whether or not an application is "minimized" is NOT a property of the Linux process. Rather, it's managed by your "desktop manager: software (e.g. Gnome), which in turn sits on top of X Windows.

  2. In other words, to find which applications are "minimized" and which are not, you generally have to query X Windows. For example:

How can you check if a window is minimized via the terminal in linux

if xwininfo -all -id $windowIdGoHere |grep "Hidden"; then
  echo "is hidden"
fi
  1. Should "xwininfo" work for you ... then you can certainly call it from Java, for example by using Process p = Runtime.getRuntime().exec(...).
like image 53
paulsm4 Avatar answered Oct 15 '22 15:10

paulsm4


Thanks to contributors, I have found a solution to do what I asked. I found this github project which does a similar job in C, and then implemented a solution in java (I prefer it because of maven and JavaFX).

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Main {
    private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("Start :");

        while(true) {
            Date date = new Date();      
            Process proc = Runtime.getRuntime().exec("xdotool getactivewindow getwindowname");
            java.io.InputStream is = proc.getInputStream();
            java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
            String val = "";
            if (s.hasNext()) {
                val = s.next();
            }
            else {
                val = "";
            }
            System.out.print(val +" " + "at:");
            System.out.println(sdf.format(date));
            TimeUnit.SECONDS.sleep(10);
        }
    }
}

It returns the active windows name every 10 second and prints the title of the application which is in use:

Start :
ApplineBuilder - NetBeans IDE 8.2
 at:2017/10/12 02:58:58
ApplineBuilder - NetBeans IDE 8.2
 at:2017/10/12 02:59:08
GoldenDict
 at:2017/10/12 02:59:18
 at:2017/10/12 02:59:28
How to detect the active application using C/java? - Stack Overflow - Mozilla Firefox
 at:2017/10/12 02:59:38
ApplineBuilder - NetBeans IDE 8.2
 at:2017/10/12 02:59:48
Cancel Running Task
 at:2017/10/12 02:59:58
like image 23
Hossein Amiri Avatar answered Oct 15 '22 15:10

Hossein Amiri