6ab953008c4b306bd0edf9bd6d4ba0767b4b026c
[src/window-management-client-grpc.git] / src / main.cpp
1 /*
2  * Copyright 2023 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 #include <QGuiApplication>
26 #include <QCommandLineParser>
27 #include <QtCore/QUrlQuery>
28 #include <QtCore/QObject>
29 #include <QObject>
30 #include <QQmlEngine>
31 #include <QtGui/QGuiApplication>
32 #include <QtQml/QQmlApplicationEngine>
33 #include <QtQml/QQmlContext>
34 #include <QtQml/QQmlComponent>
35 #include <QtQml/qqml.h>
36 #include <QQuickWindow>
37 #include <QTimer>
38
39 #include "AglShellGrpcClient.h"
40
41 const QString myname = QString("window-management-client-rpc");
42
43 enum mode {
44         NONE = 0,
45         FLOAT = 1,
46         FULLSCREEN = 2,
47         FULLSCREEN_QT = 3,      //  rather than use rpc, use Qt API
48         ON_OTHER_OUTPUTS = 4,
49         SET_FLOAT_POS   = 5,
50         SCALE   = 6,
51 };
52
53 static QWindow *
54 create_window(QQmlComponent *comp)
55 {
56         QObject *obj = comp->create();
57         obj->setParent(nullptr);
58
59         QWindow *win = qobject_cast<QWindow *>(obj);
60
61         return win;
62 }
63
64 int main(int argc, char *argv[])
65 {
66         enum mode mmode = NONE;
67
68         QGuiApplication app(argc, argv);
69         QQmlApplicationEngine engine;
70
71         // necessary to identify correctly by app_id
72         app.setDesktopFileName(myname);
73
74         QQmlComponent main_comp(&engine, QUrl("qrc:/Main.qml"));
75
76         if (argc >= 2) {
77                 const char *output_name = nullptr;
78                 if (strcmp(argv[1], "float") == 0)
79                         mmode = FLOAT;
80                 else if (strcmp(argv[1], "full") == 0)
81                         mmode = FULLSCREEN;
82                 else if (strcmp(argv[1], "on_output") == 0)
83                         mmode = ON_OTHER_OUTPUTS;
84                 else if (strcmp(argv[1], "full_qt") == 0)
85                         mmode = FULLSCREEN_QT;
86                 else if (strcmp(argv[1], "position") == 0)
87                         mmode = SET_FLOAT_POS;
88                 else if (strcmp(argv[1], "scale") == 0)
89                         mmode = SCALE;
90                 else
91                         assert(!"Invalid mode");
92
93                 if (mmode != FLOAT && mmode != FULLSCREEN && 
94                     mmode != ON_OTHER_OUTPUTS && mmode != SET_FLOAT_POS &&
95                     mmode != SCALE) {
96                         fprintf(stderr, "Will not use rpc\n");
97                         goto skip;
98                 }
99
100                 if (mmode == ON_OTHER_OUTPUTS)
101                        output_name = argv[2];
102
103                 // start grpc connection
104                 GrpcClient *client = new GrpcClient();
105
106                 // note that these are setting up the window state by using
107                 // another communication channel (rpc), and need to happen
108                 // before the client does the initial commit (without a buffer
109                 // attached)
110                 switch (mmode) {
111                 case FLOAT:
112                         fprintf(stderr, "Setting the application as float\n");
113                         client->SetAppFloat(myname.toStdString(), 40, 300);
114                         break;
115                 case FULLSCREEN:
116                         fprintf(stderr, "Setting the application as fullscreen\n");
117                         client->SetAppFullscreen(myname.toStdString());
118                         break;
119                 case ON_OTHER_OUTPUTS:
120                         fprintf(stderr, "Setting application '%s' on output '%s'\n", 
121                                         myname.toStdString().c_str(), output_name);
122                         if (!output_name) {
123                                 fprintf(stderr, "Output name is not set!\n");
124                                 exit(EXIT_FAILURE);
125                         }
126                         client->SetAppOnOutput(myname.toStdString(),
127                                                std::string(output_name));
128                         break;
129                 case SET_FLOAT_POS:
130                         // this assumes the window is already running and
131                         // floating; uses the same application so this needs
132                         // to be first started as float.
133                         client->SetAppPosition(myname.toStdString(), 550, 550);
134                         exit(EXIT_SUCCESS);
135                         break;
136                 case SCALE:
137                         // this assumes the window is already running and
138                         // floating; uses the same application so this needs
139                         // to be first started as float.
140                         client->SetAppScale(myname.toStdString(), 200, 200);
141                         exit(EXIT_SUCCESS);
142                         break;
143                 default:
144                         break;
145                 }
146         }
147
148 skip:
149         // this would allow call any of the QWindow methods
150         QWindow *win = create_window(&main_comp);
151
152         // alternatively do the load directly
153         //engine.load(QUrl(QStringLiteral("qrc:/Main.qml")));
154
155         return app.exec();
156 }