summaryrefslogtreecommitdiff
path: root/unity-shared
diff options
authorWilliam Hua <william.hua@canonical.com>2014-02-04 16:38:45 +0000
committerWilliam Hua <william.hua@canonical.com>2014-02-04 16:38:45 +0000
commit205ab4657d370b22dd09a83f5f0bb3b014a1eb88 (patch)
tree95b6ae153549c4bba18cf89bf3bd4c5e51683973 /unity-shared
parent0c65eb1dc0f72fa58addd070ec6ac55c64cbd3da (diff)
parent9a8f9c3ce37a10892c8ad64c3ef61aa410ced3fc (diff)
Merge trunk.
(bzr r3608.4.29)
Diffstat (limited to 'unity-shared')
-rw-r--r--unity-shared/CMakeLists.txt1
-rw-r--r--unity-shared/ConfigParser.cpp287
-rw-r--r--unity-shared/ConfigParser.h37
-rw-r--r--unity-shared/EMConverter.cpp5
-rw-r--r--unity-shared/EMConverter.h4
-rw-r--r--unity-shared/UnitySettings.cpp63
-rw-r--r--unity-shared/UnitySettings.h6
7 files changed, 401 insertions, 2 deletions
diff --git a/unity-shared/CMakeLists.txt b/unity-shared/CMakeLists.txt
index a587dfb87..27ed8abc0 100644
--- a/unity-shared/CMakeLists.txt
+++ b/unity-shared/CMakeLists.txt
@@ -19,6 +19,7 @@ include_directories (.. ../services ../UnityCore ${UNITY_SRC} ${CMAKE_BINARY_DIR
set (UNITY_SHARED_SOURCES
ApplicationManager.cpp
BGHash.cpp
+ ConfigParser.cpp
CoverArt.cpp
BackgroundEffectHelper.cpp
DashStyle.cpp
diff --git a/unity-shared/ConfigParser.cpp b/unity-shared/ConfigParser.cpp
new file mode 100644
index 000000000..5cc52f77a
--- /dev/null
+++ b/unity-shared/ConfigParser.cpp
@@ -0,0 +1,287 @@
+/*
+ * Copyright (C) 2014 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: Eleni Maria Stea <elenimaria.stea@canonical.com>
+ */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ConfigParser.h"
+
+static char *strip_whitespace (char *buf);
+
+/* Linked List */
+
+struct Node {
+ char *key;
+ char *value;
+
+ struct Node *next;
+};
+
+/* ConfigString */
+
+struct ConfigString {
+ struct Node *list;
+ char *string;
+};
+
+ConfigString *cfgstr_create (const char *orig_str)
+{
+ char *sptr, *str;
+
+ str = (char*)alloca(strlen(orig_str) + 1);
+ strcpy(str, orig_str);
+
+ ConfigString *cfgstr;
+ if (!(cfgstr = (ConfigString*)malloc(sizeof *cfgstr)))
+ return 0;
+
+ cfgstr->list = NULL;
+ cfgstr->string = NULL;
+
+ sptr = str;
+ while (sptr && *sptr != 0)
+ {
+ char *key, *value, *end, *start, *vstart;
+ struct Node *node;
+
+ if (*sptr == ';')
+ {
+ sptr++;
+ continue;
+ }
+
+ start = sptr;
+ if ((end = strchr(start, ';')))
+ {
+ *end = 0;
+ sptr = end + 1;
+ }
+ else
+ {
+ sptr = NULL;
+ }
+
+ /* parse key/value from the string */
+ if (!(vstart = strchr(start, '=')))
+ {
+ fprintf(stderr, "%s: invalid key-value pair: %s\n", __func__, start);
+ cfgstr_destroy(cfgstr);
+ return 0;
+ }
+ *vstart++ = 0; /* terminate the key part and move the pointer to the start of the value */
+
+ start = strip_whitespace(start);
+ vstart = strip_whitespace(vstart);
+
+ if (!(key = (char*)malloc(strlen(start) + 1)))
+ {
+ cfgstr_destroy(cfgstr);
+ return 0;
+ }
+
+ if (!(value = (char*)malloc(strlen(vstart) + 1)))
+ {
+ free(key);
+ cfgstr_destroy(cfgstr);
+ return 0;
+ }
+
+ strcpy(key, start);
+ strcpy(value, vstart);
+
+ /* create new list node and add to the list */
+ if (!(node = (Node*)malloc(sizeof *node)))
+ {
+ free(key);
+ free(value);
+ cfgstr_destroy(cfgstr);
+ return 0;
+ }
+
+ node->key = key;
+ node->value = value;
+ node->next = cfgstr->list;
+ cfgstr->list = node;
+ }
+
+ return cfgstr;
+}
+
+void cfgstr_destroy (ConfigString *cfgstr)
+{
+ if (!cfgstr)
+ return;
+
+ while (cfgstr->list)
+ {
+ struct Node *node = cfgstr->list;
+ cfgstr->list = cfgstr->list->next;
+ free(node->key);
+ free(node->value);
+ free(node);
+ }
+ free(cfgstr->string);
+ free(cfgstr);
+}
+
+const char *cfgstr_get_string (const ConfigString *cfgstr)
+{
+ int len;
+ const struct Node *node;
+ char *end;
+
+ free(cfgstr->string);
+
+ /* determine the string size */
+ len = 0;
+ node = cfgstr->list;
+ while (node)
+ {
+ len += strlen(node->key) + strlen(node->value) + 2;
+ node = node->next;
+ }
+
+ if (!(((ConfigString*)cfgstr)->string = (char*)malloc(len + 1)))
+ return 0;
+
+ end = cfgstr->string;
+ node = cfgstr->list;
+
+ while (node)
+ {
+ end += sprintf(end, "%s=%s;", node->key, node->value);
+ node = node->next;
+ }
+
+ return cfgstr->string;
+}
+
+static struct Node *find_node(struct Node *node, const char *key)
+{
+ while (node)
+ {
+ if (strcmp(node->key, key) == 0)
+ return node;
+
+ node = node->next;
+ }
+ return 0;
+}
+
+const char *cfgstr_get (const ConfigString *cfgstr, const char *key)
+{
+ struct Node *node = find_node(cfgstr->list, key);
+ if (!node)
+ return 0;
+
+ return node->value;
+}
+
+float cfgstr_get_float (const ConfigString *cfgstr, const char *key)
+{
+ const char *val_str = cfgstr_get(cfgstr, key);
+ return val_str ? atof(val_str) : 0;
+}
+
+int cfgstr_get_int (const ConfigString *cfgstr, const char *key)
+{
+ const char *val_str = cfgstr_get(cfgstr, key);
+ return val_str ? atoi(val_str) : 0;
+}
+
+/*
+ * returns:
+ * -1: on error
+ * 0: successfuly updated value
+ * 1: added new key-value pair
+ * */
+
+int cfgstr_set (ConfigString *cfgstr, const char *key, const char *value)
+{
+ char *new_val;
+ struct Node *node = find_node(cfgstr->list, key);
+ if (!node)
+ {
+ if ((!(node = (Node*)malloc(sizeof *node))))
+ return -1;
+
+ if ((!(node->key = (char*)malloc(strlen(key) + 1))))
+ {
+ free(node);
+ return -1;
+ }
+
+ if ((!(node->value = (char*)malloc(strlen(value) + 1))))
+ {
+ free(node->key);
+ free(node);
+ return -1;
+ }
+
+ strcpy(node->key, key);
+ strcpy(node->value, value);
+ node->next = cfgstr->list;
+ cfgstr->list = node;
+
+ return 1;
+ }
+
+ if ((!(new_val = (char*)malloc(strlen(value) + 1))))
+ return -1;
+
+ strcpy(new_val, value);
+ free(node->value);
+ node->value = new_val;
+ return 0;
+}
+
+int cfgstr_set_float (ConfigString *cfgstr, const char *key, float value)
+{
+ char buf[512];
+ snprintf(buf, sizeof buf, "%f", value);
+ buf[sizeof buf - 1] = 0; /* make sure it's null terminated */
+
+ return cfgstr_set(cfgstr, key, buf);
+}
+
+int cfgstr_set_int (ConfigString *cfgstr, const char *key, int value)
+{
+ char buf[32];
+ snprintf(buf, sizeof buf, "%d", value);
+ buf[sizeof buf - 1] = 0; /* make sure it's null terminated */
+
+ return cfgstr_set(cfgstr, key, buf);
+}
+
+static char *strip_whitespace (char *buf)
+{
+ while (*buf && isspace(*buf))
+ buf++;
+
+ if (!*buf)
+ return 0;
+
+ char *end = buf + strlen(buf) - 1;
+ while (end > buf && isspace(*end))
+ end--;
+
+ end[1] = 0;
+ return buf;
+}
diff --git a/unity-shared/ConfigParser.h b/unity-shared/ConfigParser.h
new file mode 100644
index 000000000..3b0f6bfb0
--- /dev/null
+++ b/unity-shared/ConfigParser.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2014 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: Eleni Maria Stea <elenimaria.stea@canonical.com>
+ */
+
+#ifndef CONFIG_PARSER_H_
+#define CONFIG_PARSER_H_
+
+typedef struct ConfigString ConfigString;
+
+ConfigString *cfgstr_create (const char *str);
+void cfgstr_destroy (ConfigString *cfgstr);
+
+const char *cfgstr_get_string(const ConfigString *cfgstr);
+
+const char *cfgstr_get (const ConfigString *cfgstr, const char *key);
+float cfgstr_get_float (const ConfigString *cfgstr, const char *key);
+int cfgstr_get_int (const ConfigString *cfgstr, const char *key);
+
+int cfgstr_set (ConfigString *cfgstr, const char *key, const char *value);
+int cfgstr_set_float (ConfigString *cfgstr, const char *key, float value);
+int cfgstr_set_int (ConfigString *cfgstr, const char *key, int value);
+
+#endif // CONFIG_PARSER_H_
diff --git a/unity-shared/EMConverter.cpp b/unity-shared/EMConverter.cpp
index fa67834e8..42d50449a 100644
--- a/unity-shared/EMConverter.cpp
+++ b/unity-shared/EMConverter.cpp
@@ -36,6 +36,11 @@ EMConverter::EMConverter(int font_size, double dpi)
UpdateBasePixelsPerEM();
}
+double EMConverter::PtToPx(int pt)
+{
+ return pt * dpi_ / PIXELS_PER_INCH;
+}
+
void EMConverter::UpdatePixelsPerEM()
{
pixels_per_em_ = font_size_ * dpi_ / PIXELS_PER_INCH;
diff --git a/unity-shared/EMConverter.h b/unity-shared/EMConverter.h
index 739ce713a..0c5bc1b06 100644
--- a/unity-shared/EMConverter.h
+++ b/unity-shared/EMConverter.h
@@ -26,7 +26,7 @@ namespace unity
class EMConverter
{
public:
- EMConverter(int font_size, double dpi = 96.0);
+ EMConverter(int font_size = 0, double dpi = 96.0);
void SetFontSize(int font_size);
void SetDPI(double dpi);
@@ -37,6 +37,8 @@ public:
int ConvertPixels(int pixels) const;
double DPIScale() const;
+ double PtToPx(int pt);
+
private:
void UpdatePixelsPerEM();
void UpdateBasePixelsPerEM();
diff --git a/unity-shared/UnitySettings.cpp b/unity-shared/UnitySettings.cpp
index 9d7c578b3..aa0d64b24 100644
--- a/unity-shared/UnitySettings.cpp
+++ b/unity-shared/UnitySettings.cpp
@@ -19,10 +19,13 @@
*/
#include <gdk/gdk.h>
+#include <gtk/gtk.h>
#include <gio/gio.h>
#include <NuxCore/Logger.h>
+#include "DecorationStyle.h"
+#include "MultiMonitor.h"
#include "UnitySettings.h"
#include "UScreen.h"
@@ -50,6 +53,7 @@ public:
, cached_form_factor_(FormFactor::DESKTOP)
, cached_double_click_activate_(true)
, lowGfx_(false)
+ , em_converters_(monitors::MAX)
{
CacheFormFactor();
CacheDoubleClickActivate();
@@ -62,6 +66,8 @@ public:
CacheDoubleClickActivate();
parent_->double_click_activate.changed.emit(cached_double_click_activate_);
});
+
+ UpdateEMConverter();
}
void CacheFormFactor()
@@ -103,6 +109,50 @@ public:
return cached_double_click_activate_;
}
+ int GetFontSize() const
+ {
+ gint font_size;
+ PangoFontDescription* desc;
+
+ desc = pango_font_description_from_string(decoration::Style::Get()->font().c_str());
+ font_size = pango_font_description_get_size(desc);
+ pango_font_description_free(desc);
+
+ return font_size / 1024;
+ }
+
+ // FIXME Add in getting the specific dpi scale from each monitor
+ int GetDPI(int monitor = 0) const
+ {
+ int dpi = 0;
+ g_object_get(gtk_settings_get_default(), "gtk-xft-dpi", &dpi, nullptr);
+
+ return dpi / 1024;
+ }
+
+ void UpdateFontSize()
+ {
+ int font_size = GetFontSize();
+
+ for (auto& em : em_converters_)
+ em.SetFontSize(font_size);
+ }
+
+ void UpdateDPI()
+ {
+ for (int i = 0; i < (int)em_converters_.size(); ++i)
+ {
+ int dpi = GetDPI(i);
+ em_converters_[i].SetDPI(dpi);
+ }
+ }
+
+ void UpdateEMConverter()
+ {
+ UpdateFontSize();
+ UpdateDPI();
+ }
+
Settings* parent_;
glib::Object<GSettings> gsettings_;
FormFactor cached_form_factor_;
@@ -111,6 +161,8 @@ public:
glib::Signal<void, GSettings*, gchar* > form_factor_changed_;
glib::Signal<void, GSettings*, gchar* > double_click_activate_changed_;
+
+ std::vector<EMConverter> em_converters_;
};
//
@@ -163,4 +215,15 @@ void Settings::SetLowGfxMode(const bool low_gfx)
pimpl->lowGfx_ = low_gfx;
}
+EMConverter const& Settings::em(int monitor) const
+{
+ if (monitor < 0 || monitor >= (int)monitors::MAX)
+ {
+ LOG_ERROR(logger) << "Invalid monitor index: " << monitor << ". Returning index 0 monitor instead.";
+ return pimpl->em_converters_[0];
+ }
+
+ return pimpl->em_converters_[monitor];
+}
+
} // namespace unity
diff --git a/unity-shared/UnitySettings.h b/unity-shared/UnitySettings.h
index cc44fb5c4..33f86cb19 100644
--- a/unity-shared/UnitySettings.h
+++ b/unity-shared/UnitySettings.h
@@ -23,7 +23,9 @@
#include <memory>
#include <sigc++/signal.h>
-#include <Nux/Nux.h>
+#include <NuxCore/Property.h>
+
+#include "EMConverter.h"
namespace unity
{
@@ -49,6 +51,8 @@ public:
nux::Property<bool> is_standalone;
nux::ROProperty<bool> double_click_activate;
+ EMConverter const& em(int monitor = 0) const;
+
private:
class Impl;
std::unique_ptr<Impl> pimpl;