1 /*
2 
3 This file is from Nitrogen, an X11 background setter.
4 Copyright (C) 2006  Dave Foster & Javeed Shaikh
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 
20 */
21 
22 #ifndef _SETBG_H_
23 #define _SETBG_H_
24 
25 #include "main.h"
26 #include <gdk/gdkx.h>
27 #include <math.h>
28 #ifdef USE_XINERAMA
29 #include <X11/extensions/Xinerama.h>
30 #endif
31 
32 typedef int (*XErrorHandler)(Display*, XErrorEvent*);
33 
34 /**
35  * Utility class for setting the background image.
36  *
37  * @author	Dave Foster <daf@minuslab.net>
38  * @date	30 Aug 2005
39  */
40 class SetBG {
41 	public:
42         // NOTE: do not change the order of these, they are changed to integer and
43         // put on disk.
44 		enum SetMode {
45 			SET_SCALE,
46 			SET_TILE,
47 			SET_CENTER,
48 			SET_ZOOM,
49             SET_AUTO,
50             SET_ZOOM_FILL
51 		};
52 
53         enum RootWindowType {
54             DEFAULT,
55             NAUTILUS,
56             XFCE,
57             UNKNOWN,
58             XINERAMA,
59             NEMO,
60             PCMANFM,
61             IGNORE      // Conky, etc
62         };
63 
64         typedef struct RootWindowData {
65             Window window;
66             RootWindowType type;
67             std::string wm_class;
68 
RootWindowDataRootWindowData69             RootWindowData() : type(UNKNOWN) {}
RootWindowDataRootWindowData70             RootWindowData(Window newWindow) : type(UNKNOWN), window(newWindow) {}
RootWindowDataRootWindowData71             RootWindowData(Window newWindow, RootWindowType newType) : type(newType), window(newWindow) {}
RootWindowDataRootWindowData72             RootWindowData(Window newWindow, RootWindowType newType, std::string newClass) : type(newType), window(newWindow), wm_class(newClass) {}
73         } RootWindowData;
74 
75 		virtual bool set_bg(Glib::ustring &disp,
76                             Glib::ustring file,
77                             SetMode mode,
78                             Gdk::Color bgcolor);
79 
80         virtual void restore_bgs();
81 
82         virtual Glib::ustring make_display_key(gint head) = 0;
83         virtual std::map<Glib::ustring, Glib::ustring> get_active_displays() = 0;
84         virtual Glib::ustring get_fullscreen_key() = 0;
85 
86         static SetBG* get_bg_setter();
87 
88 		static Glib::ustring mode_to_string( const SetMode mode );
89 		static SetMode string_to_mode( const Glib::ustring str );
90 
91         // public methods used on save/shutdown to work with first pixmaps
92         void clear_first_pixmaps();
93         void reset_first_pixmaps();
94         void disable_pixmap_save();
95 
96         virtual bool save_to_config();
97 
98 	protected:
99 
100         virtual Glib::ustring get_prefix() = 0;
101 
102 		static Glib::RefPtr<Gdk::Pixbuf> make_scale(const Glib::RefPtr<Gdk::Pixbuf>, const gint, const gint, Gdk::Color);
103 		static Glib::RefPtr<Gdk::Pixbuf> make_tile(const Glib::RefPtr<Gdk::Pixbuf>, const gint, const gint, Gdk::Color);
104 		static Glib::RefPtr<Gdk::Pixbuf> make_center(const Glib::RefPtr<Gdk::Pixbuf>, const gint, const gint, Gdk::Color);
105 		static Glib::RefPtr<Gdk::Pixbuf> make_zoom(const Glib::RefPtr<Gdk::Pixbuf>, const gint, const gint, Gdk::Color);
106 		static Glib::RefPtr<Gdk::Pixbuf> make_zoom_fill(const Glib::RefPtr<Gdk::Pixbuf>, const gint, const gint, Gdk::Color);
107         static SetMode get_real_mode(const Glib::RefPtr<Gdk::Pixbuf>, const gint, const gint);
108 
109 		static guint32 GdkColorToUint32(const Gdk::Color);
110 
111         static int handle_x_errors(Display *display, XErrorEvent *error);
112         static std::vector<RootWindowData> find_desktop_windows(Display *display, Window curwindow);
113         static RootWindowData get_root_window_data(Glib::RefPtr<Gdk::Display> display);
114         static RootWindowData check_window_type(Display *display, Window window);
115 
116         Glib::RefPtr<Gdk::Pixbuf> make_resized_pixbuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf, SetBG::SetMode mode, Gdk::Color bgcolor, gint tarw, gint tarh);
117         virtual Glib::RefPtr<Gdk::Display> get_display(const Glib::ustring& disp);
118 
119         virtual bool get_target_dimensions(Glib::ustring& disp, gint winx, gint winy, gint winw, gint winh, gint& tarx, gint& tary, gint& tarw, gint& tarh);
120         Glib::RefPtr<Gdk::Pixmap> get_or_create_pixmap(Glib::ustring disp, Glib::RefPtr<Gdk::Display> _display, Glib::RefPtr<Gdk::Window> window, gint winw, gint winh, gint wind, Glib::RefPtr<Gdk::Colormap> colormap);
121 
122 
123         Pixmap* get_current_pixmap(Glib::RefPtr<Gdk::Display> _display);
124         void set_current_pixmap(Glib::RefPtr<Gdk::Display> _display, Pixmap* new_pixmap);
125 
126         // data for saving initial pixmap on first set (for restore later)
127         bool has_set_once;
128         std::map<Glib::ustring, Pixmap*> first_pixmaps;
129 };
130 
131 /**
132  * Concrete setter for X windows (default).
133  */
134 class SetBGXWindows : public SetBG {
135     public:
136         virtual std::map<Glib::ustring, Glib::ustring> get_active_displays();
137         virtual Glib::ustring get_fullscreen_key();
138     protected:
139         virtual Glib::ustring get_prefix();
140         virtual Glib::ustring make_display_key(gint head);
141         virtual Glib::RefPtr<Gdk::Display> get_display(Glib::ustring& disp);
142 };
143 
144 #ifdef USE_XINERAMA
145 class SetBGXinerama : public SetBG {
146     public:
147         void set_xinerama_info(XineramaScreenInfo* xinerama_info, gint xinerama_num_screens);
148         virtual std::map<Glib::ustring, Glib::ustring> get_active_displays();
149         virtual Glib::ustring get_fullscreen_key();
150 
151     protected:
152         // xinerama stuff
153         XineramaScreenInfo* xinerama_info;
154         gint xinerama_num_screens;
155 
156         virtual Glib::ustring get_prefix();
157         virtual Glib::ustring make_display_key(gint head);
158         virtual bool get_target_dimensions(Glib::ustring& disp, gint winx, gint winy, gint winw, gint winh, gint& tarx, gint& tary, gint& tarw, gint& tarh);
159 };
160 #endif
161 
162 class SetBGGnome : public SetBG {
163     public:
164 		virtual bool set_bg(Glib::ustring &disp,
165                             Glib::ustring file,
166                             SetMode mode,
167                             Gdk::Color bgcolor);
168 
169         virtual std::map<Glib::ustring, Glib::ustring> get_active_displays();
170         virtual Glib::ustring get_fullscreen_key();
171         virtual bool save_to_config();
172     protected:
173         virtual Glib::ustring get_prefix();
174         virtual Glib::ustring make_display_key(gint head);
175         virtual Glib::ustring get_gsettings_key();
176         virtual void set_show_desktop();
177 };
178 
179 class SetBGNemo : public SetBGGnome {
180     public:
181         virtual bool save_to_config();
182     protected:
183         virtual Glib::ustring get_gsettings_key();
184         virtual void set_show_desktop();
185 };
186 
187 class SetBGPcmanfm : public SetBGGnome {
188     public:
189         virtual Glib::ustring get_fullscreen_key();
190 		virtual bool set_bg(Glib::ustring &disp,
191                             Glib::ustring file,
192                             SetMode mode,
193                             Gdk::Color bgcolor);
194 
195         virtual std::map<Glib::ustring, Glib::ustring> get_active_displays();
196         virtual bool save_to_config();
197     protected:
198         virtual Glib::ustring make_display_key(gint head);
199 };
200 
201 #endif
202