homescreenhandler: Add the ability to specify the output based on the
[apps/launcher.git] / launcher / src / shell-desktop.cpp
1 /*
2  * Copyright (c) 2020 Collabora Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <QGuiApplication>
18 #include <QDebug>
19 #include "shell-desktop.h"
20 #include <qpa/qplatformnativeinterface.h>
21 #include <stdio.h>
22
23 static struct wl_output *
24 getWlOutput(QScreen *screen)
25 {
26         QPlatformNativeInterface *native = qApp->platformNativeInterface();
27         void *output = native->nativeResourceForScreen("output", screen);
28         return static_cast<struct ::wl_output*>(output);
29 }
30
31 void
32 Shell::activate_app(QWindow *win, const QString &app_id,
33                     const QString &app_data)
34 {
35         QScreen *screen = nullptr;
36         struct wl_output *output;
37
38         if (!win || !win->screen()) {
39                 screen = qApp->screens().first();
40         } else {
41                 screen = win->screen();
42         }
43
44         if (!screen)
45                 return;
46
47         output = getWlOutput(screen);
48         qDebug() << "will activate app: " << app_id;
49         agl_shell_desktop_activate_app(this->shell.get(),
50                                        app_id.toStdString().c_str(),
51                                        app_data.toStdString().c_str(), output);
52 }
53
54 void
55 Shell::activate_app_by_screen(const QString &screen_name, const QString &app_id,
56                               const QString &app_data)
57 {
58         QScreen *qscreen_to_put = nullptr;
59         for (auto &ss: qApp->screens()) {
60                 if (ss->name() == screen_name) {
61                         qscreen_to_put = ss;
62                         break;
63                 }
64         }
65
66         /* use the primary one */
67         if (!qscreen_to_put) {
68                 qscreen_to_put = qApp->screens().first();
69         }
70
71         struct wl_output *output = getWlOutput(qscreen_to_put);
72         qDebug() << "will activate app: " << app_id << " on output " <<
73                 qscreen_to_put->name();
74         agl_shell_desktop_activate_app(this->shell.get(),
75                                        app_id.toStdString().c_str(),
76                                        app_data.toStdString().c_str(), output);
77 }
78
79 void
80 Shell::deactivate_app(const QString &app_id)
81 {
82         agl_shell_desktop_deactivate_app(this->shell.get(), 
83                                          app_id.toStdString().c_str());
84 }
85
86 void
87 Shell::set_window_props(QWindow *win, const QString &app_id,
88                         uint32_t props, int x, int y)
89 {
90         QScreen *screen = nullptr;
91         struct wl_output *output;
92
93         if (!win || !win->screen()) {
94                 screen = qApp->screens().first();
95         } else {
96                 screen = win->screen();
97         }
98
99         if (!screen) {
100                 return;
101         }
102
103         output = getWlOutput(screen);
104         agl_shell_desktop_set_app_property(this->shell.get(),
105                                            app_id.toStdString().c_str(),
106                                            props, x, y, output);
107 }