Back to index

unity  6.0.0
UScreen.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
00017  */
00018 
00019 #include "UScreen.h"
00020 #include <NuxCore/Logger.h>
00021 
00022 namespace unity
00023 {
00024 
00025 namespace
00026 {
00027 static UScreen* default_screen_ = nullptr;
00028 nux::logging::Logger logger("unity.screen");
00029 }
00030 
00031 UScreen::UScreen()
00032   : screen_(gdk_screen_get_default(), glib::AddRef())
00033   , proxy_("org.freedesktop.UPower",
00034            "/org/freedesktop/UPower",
00035            "org.freedesktop.UPower",
00036            G_BUS_TYPE_SYSTEM)
00037   , primary_(0)
00038 {
00039   size_changed_signal_.Connect(screen_, "size-changed", sigc::mem_fun(this, &UScreen::Changed));
00040   monitors_changed_signal_.Connect(screen_, "monitors-changed", sigc::mem_fun(this, &UScreen::Changed));
00041   proxy_.Connect("Resuming", [&] (GVariant* data) { resuming.emit(); });
00042 
00043   Refresh();
00044 }
00045 
00046 UScreen::~UScreen()
00047 {
00048   if (default_screen_ == this)
00049     default_screen_ = nullptr;
00050 }
00051 
00052 UScreen* UScreen::GetDefault()
00053 {
00054   if (G_UNLIKELY(!default_screen_))
00055     default_screen_ = new UScreen();
00056 
00057   return default_screen_;
00058 }
00059 
00060 int UScreen::GetMonitorWithMouse()
00061 {
00062   GdkDevice* device;
00063   GdkDisplay *display;
00064   int x;
00065   int y;
00066 
00067   display = gdk_display_get_default();
00068   device = gdk_device_manager_get_client_pointer(gdk_display_get_device_manager(display));
00069 
00070   gdk_device_get_position(device, nullptr, &x, &y);
00071 
00072   return GetMonitorAtPosition(x, y);
00073 }
00074 
00075 int UScreen::GetPrimaryMonitor()
00076 {
00077   return primary_;
00078 }
00079 
00080 int UScreen::GetMonitorAtPosition(int x, int y)
00081 {
00082   return gdk_screen_get_monitor_at_point(screen_, x, y);
00083 }
00084 
00085 nux::Geometry& UScreen::GetMonitorGeometry(int monitor)
00086 {
00087   return monitors_[monitor];
00088 }
00089 
00090 std::vector<nux::Geometry>& UScreen::GetMonitors()
00091 {
00092   return monitors_;
00093 }
00094 
00095 void UScreen::Changed(GdkScreen* screen)
00096 {
00097   if (refresh_idle_)
00098     return;
00099 
00100   refresh_idle_.reset(new glib::Idle([&] () {
00101     Refresh();
00102     refresh_idle_.reset();
00103 
00104     return false;
00105   }));
00106 }
00107 
00108 void UScreen::Refresh()
00109 {
00110   LOG_DEBUG(logger) << "Screen geometry changed";
00111 
00112   nux::Geometry last_geo;
00113   monitors_.clear();
00114   primary_ = gdk_screen_get_primary_monitor(screen_);
00115   int monitors = gdk_screen_get_n_monitors(screen_);
00116 
00117   for (int i = 0; i < monitors; ++i)
00118   {
00119     GdkRectangle rect = { 0 };
00120     gdk_screen_get_monitor_geometry(screen_, i, &rect);
00121     nux::Geometry geo(rect.x, rect.y, rect.width, rect.height);
00122 
00123     // Check for mirrored displays
00124     if (geo == last_geo)
00125       continue;
00126 
00127     last_geo = geo;
00128     monitors_.push_back(geo);
00129 
00130     LOG_DEBUG(logger) << "Monitor " << i << " has geometry " << geo.x << "x"
00131                       << geo.y << "x" << geo.width << "x" << geo.height;
00132   }
00133 
00134   changed.emit(primary_, monitors_);
00135 }
00136 
00137 } // Namespace