~3v1n0/unity/input-monitor-menus-scrubbing-triangolation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2012 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Tim Penhey <tim.penhey@canonical.com>
 */

#include <iostream>
#include <vector>

#include <NuxCore/Logger.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <signal.h>

#include "unity-shared/ApplicationManager.h"
#include "UnityCore/GLibSource.h"

using namespace std;
using namespace unity;

GMainLoop *loop;

std::ostream& operator<<(std::ostream &os, AppType at)
{
  switch (at)
  {
    case AppType::NORMAL:
      return os << "NORMAL";
    case AppType::WEBAPP:
      return os << "WEBAPP";
    case AppType::MOCK:
      return os << "MOCK";
    case AppType::UNKNOWN:
      return os << "UNKNOWN";
  }

  return os;
}

std::ostream& operator<<(std::ostream &os, WindowType wt)
{
  switch (wt)
  {
    case WindowType::NORMAL:
      return os << "NORMAL";
    case WindowType::DESKTOP:
      return os << "DESKTOP";
    case WindowType::DOCK:
      return os << "DOCK";
    case WindowType::DIALOG:
      return os << "DIALOG";
    case WindowType::TOOLBAR:
      return os << "TOOLBAR";
    case WindowType::MENU:
      return os << "MENU";
    case WindowType::UTILITY:
      return os << "UTILITY";
    case WindowType::SPLASHSCREEN:
      return os << "SPLASHSCREEN";
    case WindowType::TAB:
      return os << "TAB";
    case WindowType::MOCK:
      return os << "MOCK";
    case WindowType::UNKNOWN:
      return os << "UNKNOWN";
  }

  return os;
}

void connect_window_events(ApplicationWindowPtr const& win)
{
  win->title.changed.connect([win] (std::string const& t) {
    std::cout << "Window "<< win->window_id()<< " title changed to "<< t << endl;
  });
  win->maximized.changed.connect([win] (bool m) {
    std::cout << "Window "<< win->window_id()<< " maximized changed to "<< m << endl;
  });
  win->monitor.changed.connect([win] (int m) {
    std::cout << "Window "<< win->window_id()<< " monitor changed to "<< m << endl;
  });
  win->active.changed.connect([win] (bool a) {
    std::cout << "Window "<< win->window_id()<< " active changed to "<< a << endl;
  });
}

void dump_app(ApplicationPtr const& app, std::string const& prefix = "")
{
  if (app)
  {
    cout << prefix << "Application: " << app->title()
         << ", seen: " << (app->seen() ? "yes" : "no")
         << ", sticky: " << (app->sticky() ? "yes" : "no")
         << ", visible: " << (app->visible() ? "yes" : "no")
         << ", active: " << (app->active() ? "yes" : "no")
         << ", running: " << (app->running() ? "yes" : "no")
         << ", urgent: " << (app->urgent() ? "yes" : "no")
         << ", repr: " << app->repr()
         << "\n  icon: \"" << app->icon() << "\""
         << "\n  desktop file: \"" << app->desktop_file() << "\""
         << "\n  type: \"" << app->type() << "\""
         << endl;

    for (auto win : app->GetWindows())
    {
      std::cout << "  Window: " << win->title()
                << ", window_id: " << win->window_id()
                << ", monitor: " << win->monitor()
                << ", maximized: " << win->maximized()
                << ", type: " << win->type()
                << endl;
    }
  }
  else
  {
    cout << "App ptr is null" << endl;
  }
}

std::vector<std::string> names;

void connect_events(ApplicationPtr const& app)
{
  if (app->seen())
  {
    cout << "Already seen " << app->title() << ", skipping event connection.\n";
    return;
  }

  auto idx = names.empty() ? 0 : names.size()-1;
  names.push_back(app->title());
  app->title.changed.connect([idx](std::string const& value) {
    cout << names[idx] << " changed name to: " << value << endl;
    names[idx] = value;
  });
  app->icon.changed.connect([idx](std::string const& value) {
    cout << names[idx] << " icon changed: " << value << endl;
  });
  app->desktop_file.changed.connect([idx](std::string const& value) {
    cout << names[idx] << " desktop file changed: " << value << endl;
  });
  app->visible.changed.connect([idx](bool value) {
    cout << names[idx] << " visibility changed: " << (value ? "yes" : "no") << endl;
  });
  app->running.changed.connect([idx](bool value) {
    cout << names[idx] << " running changed: " << (value ? "yes" : "no") << endl;
  });
  app->active.changed.connect([idx](bool value) {
    cout << names[idx] << " active changed: " << (value ? "yes" : "no") << endl;
  });
  app->urgent.changed.connect([idx](bool value) {
    cout << names[idx] << " urgent changed: " << (value ? "yes" : "no") << endl;
  });
  app->closed.connect([idx]() {
    cout << names[idx] << " closed." << endl;
  });
  app->window_opened.connect([idx](ApplicationWindowPtr const& window) {
    cout << "** " << names[idx] << " window opened: " << window->title() << endl;
    connect_window_events(window);
  });
  app->window_closed.connect([idx](ApplicationWindowPtr const& window) {
    cout << "** " << names[idx] << " window closed: " << window->title() << endl;
  });
  app->window_moved.connect([idx](ApplicationWindowPtr const& window) {
    cout << "** " << names[idx] << " window moved: " << window->title() << endl;
  });
  app->seen = true;

  for (auto win : app->GetWindows())
    connect_window_events(win);
}


nux::logging::Level glog_level_to_nux(GLogLevelFlags log_level)
{
  // For some weird reason, ERROR is more critical than CRITICAL in gnome.
  if (log_level & G_LOG_LEVEL_ERROR)
    return nux::logging::Critical;
  if (log_level & G_LOG_LEVEL_CRITICAL)
    return nux::logging::Error;
  if (log_level & G_LOG_LEVEL_WARNING)
    return nux::logging::Warning;
  if (log_level & G_LOG_LEVEL_MESSAGE ||
      log_level & G_LOG_LEVEL_INFO)
    return nux::logging::Info;
  // default to debug.
  return nux::logging::Debug;
}

void capture_g_log_calls(const gchar* log_domain,
                         GLogLevelFlags log_level,
                         const gchar* message,
                         gpointer user_data)
{
  // If the environment variable is set, we capture the backtrace.
  static bool glog_backtrace = ::getenv("UNITY_LOG_GLOG_BACKTRACE");
  // If nothing else, all log messages from unity should be identified as such
  std::string module("unity");
  if (log_domain)
  {
    module += std::string(".") + log_domain;
  }
  nux::logging::Logger logger(module);
  nux::logging::Level level = glog_level_to_nux(log_level);
  if (level >= logger.GetEffectiveLogLevel())
  {
    std::string backtrace;
    if (glog_backtrace && level >= nux::logging::Warning)
    {
      backtrace = "\n" + nux::logging::Backtrace();
    }
    nux::logging::LogStream(level, logger.module(), "<unknown>", 0).stream()
      << message << backtrace;
  }
}

void print_active_window(ApplicationManager& manager)
{
  ApplicationWindowPtr win = manager.GetActiveWindow();
  if (win)
  {
    ApplicationPtr app = win->application();
    if (app)
      cout << "\n\nActive App: " << app->title();
    else
      cout << "\n\nNo app for window:";
    cout << "\nActive Window: " << win->title() << endl;
  }
  else
    cout << "\n\nNo active window: " << endl;
}

void clean_exit(int sig)
{
  if (loop && g_main_loop_is_running(loop))
    g_main_loop_quit(loop);
}

namespace unity
{
// This function is used by the static Default method on the ApplicationManager
// class, and uses link time to make sure there is a function available.
std::shared_ptr<ApplicationManager> create_application_manager();
}

int main(int argc, char* argv[])
{
  gtk_init(&argc, &argv);
  nux::logging::configure_logging(::getenv("UNITY_APP_LOG_SEVERITY"));
  g_log_set_default_handler(capture_g_log_calls, NULL);

  bool show_active = (argc > 1 && std::string(argv[1]) == "show-active");
  //std::shared_ptr<ApplicationManager> manager_ptr = create_application_manager();
  //ApplicationManager& manager = *manager_ptr;
  ApplicationManager& manager = ApplicationManager::Default();

  ApplicationPtr terminal = manager.GetApplicationForDesktopFile(
    "/usr/share/applications/gnome-terminal.desktop");
  terminal->sticky = true; // this is needed to get the notifications...
  dump_app(terminal);
  connect_events(terminal);

  ApplicationList apps = manager.GetRunningApplications();

  for (auto const& app : apps)
  {
    dump_app(app);
    connect_events(app);
  }

  dump_app(manager.GetApplicationForDesktopFile(
    "/usr/share/applications/gnome-terminal.desktop"));

  // Get some desktop files for checking
  ApplicationPtr pgadmin = manager.GetApplicationForDesktopFile(
    "/usr/share/applications/pgadmin3.desktop");
  dump_app(pgadmin);
  ApplicationPtr gedit = manager.GetApplicationForDesktopFile(
    "/usr/share/applications/gedit.desktop");
  dump_app(gedit);
  // dump new apps
  manager.application_started.connect([&apps](ApplicationPtr const& app) {
    apps.push_back(app);
    dump_app(app, "\nApp started: ");
    connect_events(app);
  });
  manager.active_application_changed.connect([](ApplicationPtr const& app) {
    if (app)
      cout << "Manager::active_application_changed: " << app->title() << endl;
    else
      cout << "Manager::active_application_changed to nothing\n";
  });
  manager.active_window_changed.connect([](ApplicationWindowPtr const& win) {
    cout << "Manager::active_window_changed: " << win->title() << endl;
  });

  shared_ptr<GMainLoop> main_loop(g_main_loop_new(nullptr, FALSE),
                                  g_main_loop_unref);
  loop = main_loop.get();
  signal(SIGINT, clean_exit);
  {
    glib::SourceManager source_manager;
    if (show_active)
      source_manager.AddTimeoutSeconds(5, [&manager]() {
        print_active_window(manager);
        return true;
      });
    g_main_loop_run(loop);
  }
  cout << "After main loop.\n";
}