1 /*
2 * Copyright © 2009-2018 Siyan Panayotov <contact@siyanpanayotov.com>
3 *
4 * Based on code by (see README for details):
5 * - Björn Lindqvist <bjourne@gmail.com>
6 *
7 * This file is part of Viewnior.
8 *
9 * Viewnior is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Viewnior is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Viewnior. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "uni-utils.h"
24
25 /**
26 * uni_pixbuf_scale_blend:
27 *
28 * A utility function that either scales or composites color depending
29 * on the number of channels in the source image. The last four
30 * parameters are only used in the composite color case.
31 **/
32 void
uni_pixbuf_scale_blend(GdkPixbuf * src,GdkPixbuf * dst,int dst_x,int dst_y,int dst_width,int dst_height,gdouble offset_x,gdouble offset_y,gdouble zoom,GdkInterpType interp,int check_x,int check_y)33 uni_pixbuf_scale_blend (GdkPixbuf * src,
34 GdkPixbuf * dst,
35 int dst_x,
36 int dst_y,
37 int dst_width,
38 int dst_height,
39 gdouble offset_x,
40 gdouble offset_y,
41 gdouble zoom,
42 GdkInterpType interp, int check_x, int check_y)
43 {
44 if (gdk_pixbuf_get_has_alpha (src))
45 gdk_pixbuf_composite_color (src, dst,
46 dst_x, dst_y, dst_width, dst_height,
47 offset_x, offset_y,
48 zoom, zoom,
49 interp,
50 255,
51 check_x, check_y,
52 CHECK_SIZE, CHECK_LIGHT, CHECK_DARK);
53 else
54 gdk_pixbuf_scale (src, dst,
55 dst_x, dst_y, dst_width, dst_height,
56 offset_x, offset_y, zoom, zoom, interp);
57 }
58
59 /**
60 * uni_draw_rect:
61 *
62 * This function is a fixed version of gdk_draw_rectangle. The GDK
63 * function is broken in that drawing a the rectangle (0,0)-[0,0] will
64 * draw a pixel at position (0,0).
65 **/
66 void
uni_draw_rect(GdkDrawable * drawable,GdkGC * gc,gboolean filled,GdkRectangle * rect)67 uni_draw_rect (GdkDrawable * drawable,
68 GdkGC * gc, gboolean filled, GdkRectangle * rect)
69 {
70 if (rect->width <= 0 || rect->height <= 0)
71 return;
72 gdk_draw_rectangle (drawable, gc, filled,
73 rect->x, rect->y, rect->width - 1, rect->height - 1);
74 }
75
76 void
uni_rectangle_get_rects_around(GdkRectangle * outer,GdkRectangle * inner,GdkRectangle around[4])77 uni_rectangle_get_rects_around (GdkRectangle * outer,
78 GdkRectangle * inner, GdkRectangle around[4])
79 {
80 /* Top */
81 around[0] = (GdkRectangle)
82 {
83 outer->x, outer->y, outer->width, inner->y - outer->y};
84 /* Left */
85 around[1] = (GdkRectangle)
86 {
87 outer->x, inner->y, inner->x - outer->x, inner->height};
88 /* Right */
89 around[2] = (GdkRectangle)
90 {
91 inner->x + inner->width,
92 inner->y,
93 (outer->x + outer->width) - (inner->x + inner->width),
94 inner->height};
95 /* Bottom */
96 around[3] = (GdkRectangle)
97 {
98 outer->x,
99 inner->y + inner->height,
100 outer->width,
101 (outer->y + outer->height) - (inner->y + inner->height)};
102 }
103
104 VnrPrefsDesktop
uni_detect_desktop_environment()105 uni_detect_desktop_environment ()
106 {
107 VnrPrefsDesktop environment = VNR_PREFS_DESKTOP_GNOME3;
108
109 gchar *xdg_current_desktop = g_ascii_strup(getenv("XDG_CURRENT_DESKTOP"), -1);
110 gchar *xdg_session_desktop = g_ascii_strup(getenv("XDG_SESSION_DESKTOP"), -1);
111 gchar *desktop_session = g_ascii_strdown(getenv("DESKTOP_SESSION"), -1);
112 gchar *gdmsession = g_ascii_strdown(getenv("GDMSESSION"), -1);
113
114 if (!g_strcmp0(xdg_current_desktop, "GNOME") || !g_strcmp0(xdg_session_desktop, "GNOME"))
115 {
116 if (!g_strcmp0(gdmsession, "gnome-classic") || !g_strcmp0(gdmsession, "gnome-fallback"))
117 {
118 environment = VNR_PREFS_DESKTOP_GNOME2;
119 }
120 else if (!g_strcmp0(gdmsession, "cinnamon"))
121 {
122 environment = VNR_PREFS_DESKTOP_CINNAMON;
123 }
124 }
125 else if (!g_strcmp0(xdg_current_desktop, "XFCE") || !g_strcmp0(xdg_session_desktop, "XFCE"))
126 {
127 environment = VNR_PREFS_DESKTOP_XFCE;
128 }
129 else if (!g_strcmp0(xdg_current_desktop, "MATE") || !g_strcmp0(xdg_session_desktop, "MATE"))
130 {
131 environment = VNR_PREFS_DESKTOP_MATE;
132 }
133 else if (!g_strcmp0(xdg_current_desktop, "LXDE") || !g_strcmp0(xdg_session_desktop, "LXDE"))
134 {
135 environment = VNR_PREFS_DESKTOP_LXDE;
136 }
137 else if (!g_strcmp0(desktop_session, "fluxbox"))
138 {
139 environment = VNR_PREFS_DESKTOP_FLUXBOX;
140 }
141 else
142 {
143 g_warning("Cannot detect desktop environment. Defaulting to GNOME 3.\n");
144 }
145
146 g_free(xdg_current_desktop);
147 g_free(xdg_session_desktop);
148 g_free(desktop_session);
149 g_free(gdmsession);
150
151 return environment;
152 }
153