From 3556813c5506d6431602780ca644b2d321a4ab7a Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 27 Apr 2020 11:55:10 +0100 Subject: [PATCH 1/3] libmalcontent-ui: Use library i18n functions Rather than application i18n functions and `#include`s. This ensures that the correct translation domain is used. Signed-off-by: Philip Withnall --- libmalcontent-ui/gs-content-rating.c | 2 +- libmalcontent-ui/restrict-applications-dialog.c | 4 +++- libmalcontent-ui/restrict-applications-selector.c | 4 +++- libmalcontent-ui/user-controls.c | 4 +++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/libmalcontent-ui/gs-content-rating.c b/libmalcontent-ui/gs-content-rating.c index 0e584be..edb99ff 100644 --- a/libmalcontent-ui/gs-content-rating.c +++ b/libmalcontent-ui/gs-content-rating.c @@ -21,7 +21,7 @@ #include "config.h" -#include +#include #include #include "gs-content-rating.h" diff --git a/libmalcontent-ui/restrict-applications-dialog.c b/libmalcontent-ui/restrict-applications-dialog.c index ed70c19..c7c9bc0 100644 --- a/libmalcontent-ui/restrict-applications-dialog.c +++ b/libmalcontent-ui/restrict-applications-dialog.c @@ -19,10 +19,12 @@ * - Philip Withnall */ +#include "config.h" + #include #include #include -#include +#include #include #include "restrict-applications-dialog.h" diff --git a/libmalcontent-ui/restrict-applications-selector.c b/libmalcontent-ui/restrict-applications-selector.c index a38d1ae..c0db4e1 100644 --- a/libmalcontent-ui/restrict-applications-selector.c +++ b/libmalcontent-ui/restrict-applications-selector.c @@ -19,12 +19,14 @@ * - Philip Withnall */ +#include "config.h" + #include #include #include #include #include -#include +#include #include #include diff --git a/libmalcontent-ui/user-controls.c b/libmalcontent-ui/user-controls.c index bec59e7..c727a46 100644 --- a/libmalcontent-ui/user-controls.c +++ b/libmalcontent-ui/user-controls.c @@ -20,11 +20,13 @@ * - Philip Withnall */ +#include "config.h" + #include #include #include #include -#include +#include #include #include "gs-content-rating.h" From 28d496926da9ac45264368809ba63d949c6b5747 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 27 Apr 2020 12:10:58 +0100 Subject: [PATCH 2/3] libmalcontent: Add a constructor to bind the translation domain This ensures that the translation domain is loaded for malcontent as soon as the library is loaded. The same is not needed for libmalcontent-ui, because it always causes libmalcontent to be loaded. Signed-off-by: Philip Withnall --- libmalcontent/gconstructor.h | 122 +++++++++++++++++++++++++++++++++++ libmalcontent/init.c | 51 +++++++++++++++ libmalcontent/meson.build | 2 + 3 files changed, 175 insertions(+) create mode 100644 libmalcontent/gconstructor.h create mode 100644 libmalcontent/init.c diff --git a/libmalcontent/gconstructor.h b/libmalcontent/gconstructor.h new file mode 100644 index 0000000..603c2dd --- /dev/null +++ b/libmalcontent/gconstructor.h @@ -0,0 +1,122 @@ +/* + If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and + destructors, in a sane way, including e.g. on library unload. If not you're on + your own. + + Some compilers need #pragma to handle this, which does not work with macros, + so the way you need to use this is (for constructors): + + #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA + #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor) + #endif + G_DEFINE_CONSTRUCTOR(my_constructor) + static void my_constructor(void) { + ... + } + +*/ + +#ifndef __GTK_DOC_IGNORE__ + +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7) + +#define G_HAS_CONSTRUCTORS 1 + +#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void); +#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void); + +#elif defined (_MSC_VER) && (_MSC_VER >= 1500) +/* Visual studio 2008 and later has _Pragma */ + +#include + +#define G_HAS_CONSTRUCTORS 1 + +/* We do some weird things to avoid the constructors being optimized + * away on VS2015 if WholeProgramOptimization is enabled. First we + * make a reference to the array from the wrapper to make sure its + * references. Then we use a pragma to make sure the wrapper function + * symbol is always included at the link stage. Also, the symbols + * need to be extern (but not dllexport), even though they are not + * really used from another object file. + */ + +/* We need to account for differences between the mangling of symbols + * for Win32 (x86) and x64 programs, as symbols on Win32 are prefixed + * with an underscore but symbols on x64 are not. + */ +#ifdef _WIN64 +#define G_MSVC_SYMBOL_PREFIX "" +#else +#define G_MSVC_SYMBOL_PREFIX "_" +#endif + +#define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX) +#define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX) + +#define G_MSVC_CTOR(_func,_sym_prefix) \ + static void _func(void); \ + extern int (* _array ## _func)(void); \ + int _func ## _wrapper(void) { _func(); g_slist_find (NULL, _array ## _func); return 0; } \ + __pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \ + __pragma(section(".CRT$XCU",read)) \ + __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper; + +#define G_MSVC_DTOR(_func,_sym_prefix) \ + static void _func(void); \ + extern int (* _array ## _func)(void); \ + int _func ## _constructor(void) { atexit (_func); g_slist_find (NULL, _array ## _func); return 0; } \ + __pragma(comment(linker,"/include:" _sym_prefix # _func "_constructor")) \ + __pragma(section(".CRT$XCU",read)) \ + __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _constructor; + +#elif defined (_MSC_VER) + +#define G_HAS_CONSTRUCTORS 1 + +/* Pre Visual studio 2008 must use #pragma section */ +#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 +#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 + +#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ + section(".CRT$XCU",read) +#define G_DEFINE_CONSTRUCTOR(_func) \ + static void _func(void); \ + static int _func ## _wrapper(void) { _func(); return 0; } \ + __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper; + +#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ + section(".CRT$XCU",read) +#define G_DEFINE_DESTRUCTOR(_func) \ + static void _func(void); \ + static int _func ## _constructor(void) { atexit (_func); return 0; } \ + __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor; + +#elif defined(__SUNPRO_C) + +/* This is not tested, but i believe it should work, based on: + * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c + */ + +#define G_HAS_CONSTRUCTORS 1 + +#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 +#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 + +#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ + init(_func) +#define G_DEFINE_CONSTRUCTOR(_func) \ + static void _func(void); + +#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ + fini(_func) +#define G_DEFINE_DESTRUCTOR(_func) \ + static void _func(void); + +#else + +/* constructors not supported for this compiler */ + +#endif + +#endif /* __GTK_DOC_IGNORE__ */ diff --git a/libmalcontent/init.c b/libmalcontent/init.c new file mode 100644 index 0000000..077dfde --- /dev/null +++ b/libmalcontent/init.c @@ -0,0 +1,51 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright © 2020 Endless Mobile, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * - Philip Withnall + */ + +#include "config.h" + +#include +#include "gconstructor.h" + + +static void +mct_init (void) +{ + bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR); + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); +} + +#ifdef G_HAS_CONSTRUCTORS + +#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA +#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(mct_init_ctor) +#endif +G_DEFINE_CONSTRUCTOR(mct_init_ctor) + +static void +mct_init_ctor (void) +{ + mct_init (); +} + +#else +#error Your platform/compiler is missing constructor support +#endif diff --git a/libmalcontent/meson.build b/libmalcontent/meson.build index 9970d40..f68299c 100644 --- a/libmalcontent/meson.build +++ b/libmalcontent/meson.build @@ -2,6 +2,7 @@ libmalcontent_api_version = '0' libmalcontent_api_name = 'malcontent-' + libmalcontent_api_version libmalcontent_sources = [ 'app-filter.c', + 'init.c', 'manager.c', 'session-limits.c', ] @@ -13,6 +14,7 @@ libmalcontent_headers = [ ] libmalcontent_private_headers = [ 'app-filter-private.h', + 'gconstructor.h', 'session-limits-private.h', ] From f856d465713c13be424eeae3ddd31025210623b7 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 27 Apr 2020 12:11:39 +0100 Subject: [PATCH 3/3] libmalcontent-ui: Explicitly state translation domains in UI files This means the strings are always translated using the correct domain, regardless of which program libmalcontent-ui is used in. Signed-off-by: Philip Withnall --- libmalcontent-ui/restrict-applications-dialog.ui | 2 +- libmalcontent-ui/restrict-applications-selector.ui | 2 +- libmalcontent-ui/user-controls.ui | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libmalcontent-ui/restrict-applications-dialog.ui b/libmalcontent-ui/restrict-applications-dialog.ui index e7244ba..4678124 100644 --- a/libmalcontent-ui/restrict-applications-dialog.ui +++ b/libmalcontent-ui/restrict-applications-dialog.ui @@ -1,6 +1,6 @@ - +