1 /* xlp_win.c - XLP functions for manipulating windows
2 
3    Copyright 2001 Carl Worth
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <X11/Xlib.h>
19 #include <X11/Xutil.h>
20 
21 #include "xlp_win.h"
22 #include "xlp_color.h"
23 
24 #ifdef DMALLOC
25 #include "dmalloc.h"
26 #endif
27 
28 static void xlp_configure_notify_cb(XEvent *xev, void *data);
29 
xlp_win_init(xlp_win_t * xlp_win,xlp_t * xlp,char * geometry,char * default_geometry,char * fg_color,char * bg_color,char * font,char * name,char * class)30 int xlp_win_init(xlp_win_t *xlp_win, xlp_t *xlp, char *geometry, char *default_geometry, char *fg_color, char *bg_color, char *font, char *name, char *class)
31 {
32     XSizeHints size_hints;
33     XClassHint class_hint;
34     int x, y, width, height;
35     Display *dpy = xlp->dpy;
36 
37     xlp_win->xlp = xlp;
38     xlp_win->dpy = dpy;
39     xlp_win->screen = DefaultScreen(dpy);
40 
41     size_hints.flags = PWinGravity;
42     XWMGeometry(dpy, xlp_win->screen,
43 		geometry, default_geometry, 0,
44 		&size_hints, &x, &y, &width, &height,
45 		&size_hints.win_gravity);
46 
47     xlp_color_init(&xlp_win->fg, xlp_win, fg_color);
48     xlp_color_init(&xlp_win->bg, xlp_win, bg_color);
49 
50     xlp_win->window = XCreateSimpleWindow(dpy,
51 					  DefaultRootWindow(dpy),
52 					  x, y, width, height, 1,
53 					  xlp_win->fg.core_color.pixel,
54 					  xlp_win->bg.core_color.pixel);
55     xlp_win->window_owner = 1;
56     xlp_win->event_mask = NoEventMask;
57     XSetWMNormalHints(dpy, xlp_win->window, &size_hints);
58 
59     /* We want to track the size/position of all windows */
60     xlp_win_register_callback(xlp_win, ConfigureNotify,
61 			      xlp_configure_notify_cb, xlp_win);
62 
63     class_hint.res_name = name;
64     class_hint.res_class = class;
65     XSetClassHint(dpy, xlp_win->window, &class_hint);
66 
67     XStoreName(dpy, xlp_win->window, name);
68 
69     xlp_win->draw = XftDrawCreate(dpy, xlp_win->window,
70 				  DefaultVisual(dpy, xlp_win->screen),
71 				  DefaultColormap(dpy, xlp_win->screen));
72     if (xlp_win->draw == NULL) {
73 	fprintf(stderr, "%s: An error occurred during XftDrawCreate\n", __FUNCTION__);
74 	return -1;
75     }
76 
77     xlp_win->font = XftFontOpenName(dpy, xlp_win->screen, font);
78     if (xlp_win->font == NULL) {
79 	fprintf(stderr, "%s: An error occurred during XftFontOpenName\n", __FUNCTION__);
80 	return -1;
81     }
82 
83     return 0;
84 }
85 
xlp_win_init_from_window(xlp_win_t * xlp_win,xlp_t * xlp,Window window,char * fg_color,char * bg_color)86 int xlp_win_init_from_window(xlp_win_t *xlp_win, xlp_t *xlp, Window window, char *fg_color, char *bg_color)
87 {
88     xlp_win->xlp = xlp;
89     xlp_win->dpy = xlp->dpy;
90     xlp_win->window = window;
91     xlp_win->window_owner = 0;
92     xlp_win->screen = DefaultScreen(xlp->dpy);
93 
94     xlp_color_init(&xlp_win->fg, xlp_win, fg_color);
95     xlp_color_init(&xlp_win->bg, xlp_win, bg_color);
96 
97     xlp_win->event_mask = NoEventMask;
98 
99     /* We want to track the size/position of all windows */
100     xlp_win_register_callback(xlp_win, ConfigureNotify,
101 			      xlp_configure_notify_cb, xlp_win);
102 
103     xlp_win->draw = NULL;
104     xlp_win->font = NULL;
105 
106     return 0;
107 }
108 
xlp_win_deinit(xlp_win_t * xlp_win)109 void xlp_win_deinit(xlp_win_t *xlp_win)
110 {
111     if (xlp_win->font) {
112 	XftFontClose(xlp_win->dpy, xlp_win->font);
113 	xlp_win->font = NULL;
114     }
115     if (xlp_win->draw) {
116 	XftDrawDestroy(xlp_win->draw);
117 	xlp_win->draw = NULL;
118     }
119     if (xlp_win->window_owner) {
120 	XDestroyWindow(xlp_win->dpy, xlp_win->window);
121     }
122     xlp_color_deinit(&xlp_win->bg, xlp_win);
123     xlp_color_deinit(&xlp_win->fg, xlp_win);
124 }
125 
xlp_win_map(xlp_win_t * xlp_win)126 int xlp_win_map(xlp_win_t *xlp_win)
127 {
128     XMapWindow(xlp_win->dpy, xlp_win->window);
129 
130     return 0;
131 }
132 
xlp_win_unmap(xlp_win_t * xlp_win)133 int xlp_win_unmap(xlp_win_t *xlp_win)
134 {
135     XUnmapWindow(xlp_win->dpy, xlp_win->window);
136 
137     return 0;
138 }
139 
xlp_win_register_window_callback(xlp_win_t * xlp_win,Window window,int type,xlp_cb_fun_t cb_fun,void * data)140 int xlp_win_register_window_callback(xlp_win_t *xlp_win,
141 				     Window window,
142 				     int type,
143 				     xlp_cb_fun_t cb_fun, void *data)
144 {
145     return xlp_register_window_callback(xlp_win->xlp, xlp_win,
146 					window, type, cb_fun, data);
147 }
148 
xlp_win_register_callback(xlp_win_t * xlp_win,int type,xlp_cb_fun_t cb_fun,void * data)149 int xlp_win_register_callback(xlp_win_t *xlp_win, int type,
150 			      xlp_cb_fun_t cb_fun, void *data)
151 {
152     return xlp_register_callback(xlp_win->xlp, xlp_win, type, cb_fun, data);
153 }
154 
xlp_win_register_timeout(xlp_win_t * xlp_win,long delay_ms,xlp_to_fun_t to_fun,void * data,struct timeval * start_tv_ret)155 int xlp_win_register_timeout(xlp_win_t *xlp_win, long delay_ms,
156 			     xlp_to_fun_t to_fun, void *data,
157 			     struct timeval *start_tv_ret)
158 {
159     return xlp_register_timeout(xlp_win->xlp, delay_ms,
160 				to_fun, data, start_tv_ret);
161 }
162 
xlp_configure_notify_cb(XEvent * xev,void * data)163 static void xlp_configure_notify_cb(XEvent *xev, void *data)
164 {
165     XConfigureEvent *cev = &xev->xconfigure;
166     xlp_win_t *xlp_win = (xlp_win_t *) data;
167 
168     if (xev->xany.window == xlp_win->window) {
169 	xlp_win->x = cev->x;
170 	xlp_win->y = cev->y;
171 	xlp_win->width = cev->width;
172 	xlp_win->height = cev->height;
173     }
174 }
175