Tizen 2.1 release
[platform/core/uifw/e17.git] / src / preload / e_hack.c
1 #include "config.h"
2 #include "e_hack.h"
3
4 /* FIXME:
5  * * gnome-terminal does this funky thing where a new gnome-temrinal process
6  * tries to message an  existing one asking it to create a new terminal. this
7  * means none of these properties ever end up on a new term window - in fact
8  * only the ones that are on the first term process. we need a way of knowing
9  * this, OR making sure no new iwndows other than the first appear with these
10  * properties. this also leads to handling splash windows - we might want then
11  * the first 2 or 3 windows with it.
12  * 
13  * we need to discuss this... it's an interesting hack that solves a LOT of
14  * things (and that we can maybe in future expand to hacking away at gtk and
15  * qt directly)
16  * 
17  * anyway - for now this is fairly harmless and just adds a property to all
18  * top-level app windows
19  */
20
21 /* prototypes */
22 static void __e_hack_set_properties(Display *display, Window window);
23
24 /* dlopened xlib so we can find the symbols in the real xlib to call them */
25 static void *lib_xlib = NULL;
26
27 /* the function that actually sets the properties on toplevel window */
28 static void
29 __e_hack_set_properties(Display *display, Window window)
30 {
31    static Atom a_hack = 0;
32    char *env = NULL;
33    char buf[4096];
34    char buf2[4096];
35    uid_t uid;
36    pid_t pid, ppid;
37    struct utsname ubuf;
38
39    if (!a_hack) a_hack = XInternAtom(display, "__E_HACK", False);
40    buf[0] = 0;
41    buf[sizeof(buf) - 1] = 0;
42    uid = getuid();
43    snprintf(buf2, sizeof(buf2), "uid: %i\n", uid);
44    strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
45    pid = getpid();
46    snprintf(buf2, sizeof(buf2), "pid: %i\n", pid);
47    strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
48    ppid = getppid();
49    snprintf(buf2, sizeof(buf2), "ppid: %i\n", ppid);
50    strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
51    if (!uname(&ubuf))
52      {
53         snprintf(buf2, sizeof(buf2), "machine_name: %s\n", ubuf.nodename);
54         strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
55      }
56    if ((env = getenv("E_LAUNCH_ID")))
57      {
58         snprintf(buf2, sizeof(buf2), "launch_id: %s\n", env);
59         strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
60      }
61    if ((env = getenv("USER")))
62      {
63         snprintf(buf2, sizeof(buf2), "username: %s\n", env);
64         strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
65      }
66    if ((env = getenv("E_DESK")))
67      {
68         snprintf(buf2, sizeof(buf2), "e_desk: %s\n", env);
69         strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
70      }
71    if ((env = getenv("E_ZONE")))
72      {
73         snprintf(buf2, sizeof(buf2), "e_zone: %s\n", env);
74         strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
75      }
76    if ((env = getenv("E_CONTAINER")))
77      {
78         snprintf(buf2, sizeof(buf2), "e_container: %s\n", env);
79         strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
80      }
81    if ((env = getenv("E_MANAGER")))
82      {
83         snprintf(buf2, sizeof(buf2), "e_manager: %s\n", env);
84         strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
85      }
86    XChangeProperty(display, window, a_hack, XA_STRING, 8, PropModeReplace, (unsigned char *)buf, strlen(buf));
87 }
88
89 /* XCreateWindow intercept hack */
90 Window
91 XCreateWindow(
92               Display *display,
93               Window parent,
94               int x, int y,
95               unsigned int width, unsigned int height,
96               unsigned int border_width,
97               int depth,
98               unsigned int class,
99               Visual *visual,
100               unsigned long valuemask,
101               XSetWindowAttributes *attributes
102               )
103 {
104    static Window (*func)
105       (
106        Display *display,
107        Window parent,
108        int x, int y,
109        unsigned int width, unsigned int height,
110        unsigned int border_width,
111        int depth,
112        unsigned int class,
113        Visual *visual,
114        unsigned long valuemask,
115        XSetWindowAttributes *attributes
116        ) = NULL;
117    int i;
118
119    /* find the real Xlib and the real X function */
120    if (!lib_xlib) lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
121    if (!func) func = dlsym (lib_xlib, "XCreateWindow");
122
123    /* multihead screen handling loop */
124    for (i = 0; i < ScreenCount(display); i++)
125      {
126         /* if the window is created as a toplevel window */
127         if (parent == RootWindow(display, i))
128           {
129              Window window;
130              
131              /* create it */
132              window = (*func) (display, parent, x, y, width, height, 
133                                 border_width, depth, class, visual, valuemask, 
134                                 attributes);
135              /* set properties */
136              __e_hack_set_properties(display, window);
137              /* return it */
138              return window;
139           }
140      }
141    /* normal child window - create as usual */
142    return (*func) (display, parent, x, y, width, height, border_width, depth,
143                    class, visual, valuemask, attributes);
144 }
145
146 /* XCreateSimpleWindow intercept hack */
147 Window
148 XCreateSimpleWindow(
149                     Display *display,
150                     Window parent,
151                     int x, int y,
152                     unsigned int width, unsigned int height,
153                     unsigned int border_width,
154                     unsigned long border,
155                     unsigned long background
156                     )
157 {
158    static Window (*func)
159       (
160        Display *display,
161        Window parent,
162        int x, int y,
163        unsigned int width, unsigned int height,
164        unsigned int border_width,
165        unsigned long border,
166        unsigned long background
167        ) = NULL;
168    int i;
169    
170    /* find the real Xlib and the real X function */
171    if (!lib_xlib) lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
172    if (!func) func = dlsym (lib_xlib, "XCreateSimpleWindow");
173    
174    /* multihead screen handling loop */
175    for (i = 0; i < ScreenCount(display); i++)
176      {
177         /* if the window is created as a toplevel window */
178         if (parent == RootWindow(display, i))
179           {
180              Window window;
181              
182              /* create it */
183              window = (*func) (display, parent, x, y, width, height, 
184                                 border_width, border, background);
185              /* set properties */
186              __e_hack_set_properties(display, window);
187              /* return it */
188              return window;
189           }
190      }
191    /* normal child window - create as usual */
192    return (*func) (display, parent, x, y, width, height, 
193                    border_width, border, background);
194 }
195
196 /* XReparentWindow intercept hack */
197 int
198 XReparentWindow(
199                 Display *display,
200                 Window window,
201                 Window parent,
202                 int x, int y
203                 )
204 {
205    static int (*func)
206       (
207        Display *display,
208        Window window,
209        Window parent,
210        int x, int y
211        ) = NULL;
212    int i;
213    
214    /* find the real Xlib and the real X function */
215    if (!lib_xlib) lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
216    if (!func) func = dlsym (lib_xlib, "XReparentWindow");
217    
218    /* multihead screen handling loop */
219    for (i = 0; i < ScreenCount(display); i++)
220      {
221         /* if the window is created as a toplevel window */
222         if (parent == RootWindow(display, i))
223           {
224              /* set properties */
225              __e_hack_set_properties(display, window);
226              /* reparent it */
227              return (*func) (display, window, parent, x, y);
228           }
229      }
230    /* normal child window reparenting - reparent as usual */
231    return (*func) (display, window, parent, x, y);
232 }