| 1 |
/* |
| 2 |
Copyright 2022 Guido Berhoerster <[email protected]> |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
// Package focusinfo retrieves information whether an app is focused using |
| 18 |
// liblomiri-app-launch and the Lomiri Shell DBus API |
| 19 |
package focusinfo |
| 20 |
|
| 21 |
/* |
| 22 |
#cgo pkg-config: lomiri-app-launch-0 |
| 23 |
|
| 24 |
#include <lomiri-app-launch.h> |
| 25 |
#include <glib.h> |
| 26 |
*/ |
| 27 |
import "C" |
| 28 |
|
| 29 |
import ( |
| 30 |
"unsafe" |
| 31 |
|
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 33 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 34 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 35 |
) |
| 36 |
|
| 37 |
var BusAddress bus.Address = bus.Address{ |
| 38 |
Interface: "com.lomiri.Shell.FocusInfo", |
| 39 |
Path: "/com/lomiri/Shell/FocusInfo", |
| 40 |
Name: "com.lomiri.Shell.FocusInfo", |
| 41 |
} |
| 42 |
|
| 43 |
// wrapper for lomiri_app_launch_get_primary_pid() |
| 44 |
func getPrimaryPID(appId *click.AppId) int { |
| 45 |
var pid C.GPid |
| 46 |
|
| 47 |
app_id := C.CString(appId.String()) |
| 48 |
defer C.free(unsafe.Pointer(app_id)) |
| 49 |
pid = C.lomiri_app_launch_get_primary_pid(app_id) |
| 50 |
return (int)(pid) |
| 51 |
} |
| 52 |
|
| 53 |
var GetPrimaryPID = getPrimaryPID |
| 54 |
|
| 55 |
type FocusInfo struct { |
| 56 |
bus bus.Endpoint |
| 57 |
log logger.Logger |
| 58 |
} |
| 59 |
|
| 60 |
func New(endp bus.Endpoint, log logger.Logger) *FocusInfo { |
| 61 |
return &FocusInfo{endp, log} |
| 62 |
} |
| 63 |
|
| 64 |
func (fi *FocusInfo) isPIDFocused(pid int) (isFocused bool) { |
| 65 |
if err := fi.bus.Call("isPidFocused", bus.Args(uint32(pid)), &isFocused); err != nil { |
| 66 |
fi.log.Errorf("isPidFocused call returned %v", err) |
| 67 |
} |
| 68 |
return |
| 69 |
} |
| 70 |
|
| 71 |
func (fi *FocusInfo) IsAppFocused(appId *click.AppId) bool { |
| 72 |
pid := GetPrimaryPID(appId) |
| 73 |
if pid == 0 { |
| 74 |
return false |
| 75 |
} |
| 76 |
return fi.isPIDFocused(pid) |
| 77 |
} |