File: xapp-util.c

package info (click to toggle)
xapp 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,688 kB
  • sloc: ansic: 11,268; python: 1,463; xml: 258; sh: 22; makefile: 9
file content (277 lines) | stat: -rw-r--r-- 8,138 bytes parent folder | download | duplicates (3)
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

#include "config.h"

#include <glib.h>
#include <gio/gio.h>
#include <pango/pango.h>

#include "xapp-util.h"

#define PRIME_SUPPORTED_TEST_FILE "/var/lib/ubuntu-drivers-common/requires_offloading"
#define PRIME_MODE_FILE "/etc/prime-discrete"

/**
 * xapp_util_gpu_offload_supported:
 *
 * Performs a check to see if on-demand mode for discrete graphics
 * is supported.
 *
 * Returns: %TRUE if supported.
 *
 * Since: 1.8
 */
gboolean
xapp_util_gpu_offload_supported (void)
{
    g_autoptr(GFile) modefile = NULL;
    g_autofree gchar *contents = NULL;

    if (!g_file_test (PRIME_SUPPORTED_TEST_FILE, G_FILE_TEST_EXISTS))
    {
        return FALSE;
    }

    modefile = g_file_new_for_path (PRIME_MODE_FILE);

    if (!g_file_load_contents (modefile,
                               NULL,
                               &contents,
                               NULL,
                               NULL,
                               NULL))
    {
        return FALSE;
    }

    return g_strstr_len (contents, -1, "on-demand") != NULL;
}

// Copied from cinnamon:main.c
/**
 * xapp_util_get_session_is_running:
 *
 * Check if the Session Manager is currently in the "Running" phase.
 *
 * Returns: %TRUE if the session is running.
 *
 * Since: 2.0
 */
gboolean
xapp_util_get_session_is_running (void)
{
    GDBusConnection *session_bus;
    GError *error;
    GVariant *session_result;
    gboolean session_running;

    error = NULL;

    session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION,
                                  NULL,
                                  &error);

    if (error != NULL)
    {
        g_critical ("Unable to determine if session is running, could not get session bus: %s\n", error->message);
        g_clear_error (&error);

        return FALSE;
    }

    session_result = g_dbus_connection_call_sync (session_bus,
                                                  "org.gnome.SessionManager",
                                                  "/org/gnome/SessionManager",
                                                  "org.gnome.SessionManager",
                                                  "IsSessionRunning",
                                                  NULL,
                                                  G_VARIANT_TYPE ("(b)"),
                                                  G_DBUS_CALL_FLAGS_NONE,
                                                  1000,
                                                  NULL,
                                                  &error);

    if (session_result != NULL)
    {
        g_variant_get (session_result, "(b)", &session_running);
        g_variant_unref (session_result);
    }
    else
    {
        session_running = FALSE;
        g_clear_error (&error);
    }

    g_object_unref (session_bus);

    return session_running;
}

/**
 * xapp_pango_font_string_to_css:
 * @pango_font_string: a pango font description string
 *
 * Converts a pango font description string to a string suitable for use with the css "font" tag. The font description must contain the font family and font size or conversion will fail and %NULL will be returned
 *
 * Returns: (transfer full): the css compatible font string or %NULL if the conversion failed.
 *
 * Since: 2.2
 */
gchar *
xapp_pango_font_string_to_css (const char *pango_font_string)
{
    PangoFontDescription *desc;
    GString *font_string;
    PangoFontMask set;

    desc = pango_font_description_from_string (pango_font_string);
    font_string = g_string_new ("");

    set = pango_font_description_get_set_fields (desc);

    if (!(set & PANGO_FONT_MASK_SIZE) || !(set & PANGO_FONT_MASK_FAMILY))
    {
        return NULL;
    }

    if (set & PANGO_FONT_MASK_STYLE)
    {
        switch (pango_font_description_get_style (desc))
        {
            case PANGO_STYLE_NORMAL:
                g_string_append (font_string, "normal ");
                break;
            case PANGO_STYLE_OBLIQUE:
                g_string_append (font_string, "oblique ");
                break;
            case PANGO_STYLE_ITALIC:
                g_string_append (font_string, "italic ");
                break;
            default:
                break;
        }
    }

    if (set & PANGO_FONT_MASK_VARIANT)
    {
        switch (pango_font_description_get_variant (desc))
        {
            case PANGO_VARIANT_NORMAL:
                g_string_append (font_string, "normal ");
                break;
            case PANGO_VARIANT_SMALL_CAPS:
                g_string_append (font_string, "small-caps ");
                break;
            default:
                break;
        }
    }

    if (set & PANGO_FONT_MASK_WEIGHT)
    {
        switch (pango_font_description_get_weight (desc))
        {
            case PANGO_WEIGHT_THIN:
                g_string_append (font_string, "100 ");
                break;
            case PANGO_WEIGHT_ULTRALIGHT:
                g_string_append (font_string, "200 ");
                break;
            case PANGO_WEIGHT_LIGHT:
            case PANGO_WEIGHT_SEMILIGHT:
                g_string_append (font_string, "300 ");
                break;
            case PANGO_WEIGHT_BOOK:
            case PANGO_WEIGHT_NORMAL:
                g_string_append (font_string, "400 ");
                break;
            case PANGO_WEIGHT_MEDIUM:
                g_string_append (font_string, "500 ");
                break;
            case PANGO_WEIGHT_SEMIBOLD:
                g_string_append (font_string, "600 ");
                break;
            case PANGO_WEIGHT_BOLD:
                g_string_append (font_string, "700 ");
                break;
            case PANGO_WEIGHT_ULTRABOLD:
                g_string_append (font_string, "800 ");
                break;
            case PANGO_WEIGHT_HEAVY:
            case PANGO_WEIGHT_ULTRAHEAVY:
                g_string_append (font_string, "900 ");
                break;
            default:
                break;
        }
    }

    if (set & PANGO_FONT_MASK_STRETCH)
    {
        switch (pango_font_description_get_stretch (desc))
        {
            case PANGO_STRETCH_ULTRA_CONDENSED:
                g_string_append (font_string, "ultra-condensed ");
                break;
            case PANGO_STRETCH_EXTRA_CONDENSED:
                g_string_append (font_string, "extra-condensed ");
                break;
            case PANGO_STRETCH_CONDENSED:
                g_string_append (font_string, "condensed ");
                break;
            case PANGO_STRETCH_SEMI_CONDENSED:
                g_string_append (font_string, "semi-condensed ");
                break;
            case PANGO_STRETCH_NORMAL:
                g_string_append (font_string, "normal ");
                break;
            case PANGO_STRETCH_SEMI_EXPANDED:
                g_string_append (font_string, "semi-expanded ");
                break;
            case PANGO_STRETCH_EXPANDED:
                g_string_append (font_string, "expanded ");
                break;
            case PANGO_STRETCH_EXTRA_EXPANDED:
                break;
            case PANGO_STRETCH_ULTRA_EXPANDED:
                g_string_append (font_string, "ultra-expanded ");
                break;
            default:
                break;
        }
    }

    g_string_append_printf (font_string, "%dpx ", pango_font_description_get_size (desc) / PANGO_SCALE);
    g_string_append (font_string, pango_font_description_get_family (desc));

    return g_string_free (font_string, FALSE);
}


/**
 * xapp_get_tmp_dir:
 *
 * Provides the path to the system's temporary files folder. This is identical to g_get_tmp_dir,
 * but includes the /dev/shm ramdisk as the first choice for a temporary folder.
 * 
 * Returns: (type filename) (transfer none): the directory to use for temporary files.
 * Since: 2.2.16
 */
const gchar *
xapp_get_tmp_dir (void)
{
    static const gchar *tmp_dir = NULL;

    if (tmp_dir == NULL)
    {
        if (access ("/dev/shm", W_OK) == 0)
        {
            tmp_dir = "/dev/shm";
        }
        else
        {
            tmp_dir = g_get_tmp_dir ();
        }
    }

    return tmp_dir;
}