Adding upstream version 0.4.1.
[debian/lxdm.git] / src / greeter-utils.c
1 #include <gdk/gdk.h>
2 #include <gdk/gdkx.h>
3 #include <gtk/gtk.h>
4 #include <X11/Xlib.h>
5 #include <string.h>
6 #include <cairo.h>
7 #include <cairo-xlib.h>
8 #include <unistd.h>
9
10 static cairo_surface_t *cairo_surface_create_from_pixbuf (GdkWindow *root,GdkPixbuf *pixbuf)
11 {
12 Display *dpy=GDK_WINDOW_XDISPLAY(root);
13 int scr=DefaultScreen(dpy);
14 Visual *visual=DefaultVisual(dpy,scr);
15 gint width=gdk_pixbuf_get_width(pixbuf);
16 gint height=gdk_pixbuf_get_height(pixbuf);
17 Pixmap pix=XCreatePixmap(dpy,GDK_WINDOW_XID(root),width,height,DefaultDepth(dpy,scr));
18 cairo_surface_t *surface=cairo_xlib_surface_create(dpy,pix,visual,width,height);
19 cairo_t *cr=cairo_create(surface);
20 gdk_cairo_set_source_pixbuf(cr,pixbuf,0,0);
21 cairo_paint(cr);
22 cairo_destroy(cr);
23 return surface;
24 }
25
26 int ui_get_geometry(GdkWindow *win,GdkRectangle *rc)
27 {
28 #if GTK_CHECK_VERSION(2,24,0)
29 GdkScreen *screen=gdk_window_get_screen(win);
30 #else
31 GdkScreen *screen=gdk_screen_get_default();
32 #endif
33 #if GTK_CHECK_VERSION(2,24,0)
34 gint monitor=gdk_screen_get_primary_monitor(screen);
35 #else
36 gint monitor=0;
37 #endif
38 gdk_screen_get_monitor_geometry(screen,monitor,rc);
39 return 0;
40 }
41
42 void ui_set_bg(GdkWindow *win,GKeyFile *config)
43 {
44 GdkPixbuf *bg_img=NULL;
45 GdkColor bg_color;
46 GdkWindow *root=gdk_get_default_root_window();
47 char *p=g_key_file_get_string(config,"display","bg",NULL);
48 gdk_color_parse("#222E45",&bg_color);
49 if( p && p[0] != '#' )
50 {
51 bg_img = gdk_pixbuf_new_from_file(p, 0);
52 }
53 if( p && p[0] == '#' )
54 {
55 gdk_color_parse(p, &bg_color);
56 }
57 g_free(p);
58
59 /* set background */
60 if( bg_img )
61 {
62 p = g_key_file_get_string(config, "display", "bg_style", 0);
63 if( !p || !strcmp(p, "stretch") )
64 {
65 GdkPixbuf *pb;
66 GdkRectangle rc;
67 ui_get_geometry(win,&rc);
68 pb = gdk_pixbuf_scale_simple(bg_img,
69 rc.width,
70 rc.height,
71 GDK_INTERP_BILINEAR);
72 g_object_unref(bg_img);
73 bg_img = pb;
74 }
75 g_free(p);
76
77 #ifdef ENABLE_GTK3
78 cairo_surface_t *surface;
79 cairo_pattern_t *pattern;
80 surface=cairo_surface_create_from_pixbuf(root,bg_img);
81 pattern=cairo_pattern_create_for_surface(surface);
82 g_object_unref(bg_img);
83 gdk_window_set_background_pattern(win,pattern);
84 gdk_window_set_background_pattern(root,pattern);
85 cairo_pattern_destroy(pattern);
86 #else
87 GdkPixmap *pix = NULL;
88 gdk_pixbuf_render_pixmap_and_mask(bg_img, &pix, NULL, 0);
89 g_object_unref(bg_img);
90 gdk_window_set_back_pixmap(win,pix,FALSE);
91 gdk_window_set_back_pixmap(root,pix,FALSE);
92 g_object_unref(pix);
93 #endif
94 }
95 else
96 {
97 #ifdef ENABLE_GTK3
98 if(win) gdk_window_set_background(win,&bg_color);
99 gdk_window_set_background(root,&bg_color);
100 #else
101 GdkColormap *map;
102 if(win)
103 {
104 map=(GdkColormap*)gdk_drawable_get_colormap(win);
105 gdk_colormap_alloc_color(map, &bg_color, TRUE, TRUE);
106 gdk_window_set_background(win, &bg_color);
107 }
108 map=(GdkColormap*)gdk_drawable_get_colormap(root);
109 gdk_colormap_alloc_color(map, &bg_color, TRUE, TRUE);
110 gdk_window_set_background(root, &bg_color);
111 #endif
112 }
113 }
114
115 void ui_set_focus(GdkWindow *win)
116 {
117 #if GTK_CHECK_VERSION(2,24,0)
118 Display *dpy=gdk_x11_display_get_xdisplay(gdk_window_get_display(win));
119 #else
120 Display *dpy=gdk_x11_display_get_xdisplay(gdk_display_get_default());
121 #endif
122 gdk_flush();
123 while(1)
124 {
125 XWindowAttributes attr;
126 XGetWindowAttributes(dpy,GDK_WINDOW_XID(win),&attr);
127 if(attr.map_state == IsViewable) break;
128 usleep(10000);
129 }
130 XSetInputFocus(dpy,GDK_WINDOW_XID(win),RevertToNone,CurrentTime);
131 }
132
133 void ui_add_cursor(void)
134 {
135 GdkCursor *cur;
136 GdkWindow *root=gdk_get_default_root_window();
137 cur = gdk_cursor_new(GDK_LEFT_PTR);
138 gdk_window_set_cursor(root, cur);
139 XDefineCursor(gdk_x11_get_default_xdisplay(),
140 GDK_WINDOW_XID(gdk_get_default_root_window()),
141 GDK_CURSOR_XCURSOR(cur));
142 gdk_cursor_unref(cur);
143 }
144
145 void ui_set_cursor(GdkWindow *win,int which)
146 {
147 GdkCursor *cursor=gdk_cursor_new(which);
148 gdk_window_set_cursor (win,cursor);
149 gdk_cursor_unref(cursor);
150 }