Initial dockapps git repo
[dockapps.git] / wmnotify-1.0.0 / src / dockapp.c
blob5cdbefad9780ce8f050594ef6a9d83a98611f61d
1 /*
2 * dockapp.c -- routines for managing dockapp windows and icons.
4 * Copyright (C) 2003 Hugo Villeneuve <hugo@hugovil.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
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 St, Fifth Floor, Boston, MA 02110-1301, USA.
21 /* Define filename_M */
22 #define DOCKAPP_M 1
24 #if HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <X11/Xlib.h>
32 #include <X11/xpm.h>
33 #include <X11/extensions/shape.h>
35 #include "common.h"
36 #include "dockapp.h"
39 #define XLIB_FAILURE 0
40 #define XLIB_SUCCESS 1
42 /* Specifies the border width */
43 #define BWIDTH 1
45 /* Width and height in pixels of Window Maker icons. */
46 #define ICON_SIZE 64
49 static void
50 CreateIconFromXpmData( char *pixmap_data[] )
52 int status;
54 dockapp.xpm_icon.attributes.valuemask |=
55 ( XpmReturnPixels | XpmReturnExtensions );
57 /* Using the XPM library to read XPM data from the array in the included XPM
58 file. The 'shapemask' Pixmap variable is set to an additional 1-bit deep
59 pixmap that can then be used as a shape mask for the XShapeCombineMask()
60 function. */
61 status = XpmCreatePixmapFromData( dockapp.display, dockapp.root_win,
62 pixmap_data, &dockapp.xpm_icon.image,
63 &dockapp.xpm_icon.shapemask,
64 &dockapp.xpm_icon.attributes );
65 if( status != XpmSuccess ) {
66 fprintf( stderr, "%s: XpmCreatePixmapFromData() failed\n", PACKAGE );
67 ErrorLocation( __FILE__, __LINE__ );
68 exit( EXIT_FAILURE );
73 static Pixel
74 GetColor( char *name )
76 int status;
77 bool res;
78 XColor color;
79 XWindowAttributes attributes;
81 status = XGetWindowAttributes( dockapp.display, dockapp.root_win,
82 &attributes );
83 if( status == XLIB_FAILURE ) {
84 fprintf( stderr, "%s: XGetWindowAttributes() failed\n", PACKAGE );
85 ErrorLocation( __FILE__, __LINE__ );
86 exit( EXIT_FAILURE );
89 color.pixel = 0;
90 res = (bool) XParseColor( dockapp.display, attributes.colormap, name,
91 &color );
92 if( res == false ) {
93 fprintf( stderr, "%s: Can't parse %s.\n", PACKAGE, name );
94 ErrorLocation( __FILE__, __LINE__ );
95 exit( EXIT_FAILURE );
98 res = (bool) XAllocColor( dockapp.display, attributes.colormap, &color );
99 if( res == false ) {
100 fprintf( stderr, "%s: Can't allocate %s.\n", PACKAGE, name );
101 ErrorLocation( __FILE__, __LINE__ );
102 exit( EXIT_FAILURE );
105 return color.pixel;
109 static void
110 flush_expose( Window win )
112 XEvent dummy;
113 bool res = true;
115 while( res != false ) {
116 res = (bool) XCheckTypedWindowEvent( dockapp.display, win, Expose, &dummy );
121 void
122 RedrawWindow( void )
124 flush_expose( dockapp.iconwin );
126 (void) XCopyArea( dockapp.display, dockapp.xpm_icon.image, dockapp.iconwin,
127 dockapp.NormalGC, 0, 0, dockapp.xpm_icon.attributes.width,
128 dockapp.xpm_icon.attributes.height, 0, 0 );
130 flush_expose( dockapp.win );
132 (void) XCopyArea( dockapp.display, dockapp.xpm_icon.image, dockapp.win,
133 dockapp.NormalGC, 0, 0, dockapp.xpm_icon.attributes.width,
134 dockapp.xpm_icon.attributes.height, 0, 0 );
138 void
139 copyXPMArea( int x, int y, unsigned int sx, unsigned int sy, int dx, int dy )
141 (void) XCopyArea( dockapp.display, dockapp.xpm_icon.image,
142 dockapp.xpm_icon.image, dockapp.NormalGC, x, y, sx, sy,
143 dx, dy );
147 /*******************************************************************************
148 * New window creation and initialization for a Dockable Application
149 ******************************************************************************/
150 void
151 InitDockAppWindow( int argc, char *argv[], char *pixmap_data[],
152 char *display_arg, char *geometry_arg )
154 XGCValues gcv;
155 XSizeHints size_hints;
156 XWMHints wm_hints;
157 int status;
158 int gravity = 0; /* Used to store the gravity value returned by XWMGeometry,
159 but not used. */
161 /* Opening a connection to the X server. */
162 dockapp.display = XOpenDisplay( display_arg );
163 if( dockapp.display == NULL ) {
164 fprintf( stderr, "%s: Can't open display: %s\n", PACKAGE,
165 XDisplayName( display_arg ) );
166 ErrorLocation( __FILE__, __LINE__ );
167 exit( EXIT_FAILURE );
170 dockapp.screen = DefaultScreen( dockapp.display );
171 dockapp.root_win = RootWindow( dockapp.display, dockapp.screen );
172 dockapp.d_depth = DefaultDepth( dockapp.display, dockapp.screen );
174 /* Create a window to hold the stuff */
175 size_hints.flags = USSize | USPosition;
176 size_hints.x = 0;
177 size_hints.y = 0;
179 /* Constructing window's geometry information. */
180 /* XWMGeometry() returns an 'int', but Xlib documentation doesn't explain
181 it's meaning. */
182 XWMGeometry( dockapp.display, dockapp.screen, geometry_arg, NULL, BWIDTH,
183 &size_hints, &size_hints.x, &size_hints.y, &size_hints.width,
184 &size_hints.height, &gravity );
186 size_hints.width = ICON_SIZE;
187 size_hints.height = ICON_SIZE;
188 dockapp.back_pix = GetColor("white");
189 dockapp.fore_pix = GetColor("black");
191 dockapp.win = XCreateSimpleWindow( dockapp.display, dockapp.root_win,
192 size_hints.x, size_hints.y,
193 (unsigned int) size_hints.width,
194 (unsigned int) size_hints.height,
195 BWIDTH, dockapp.fore_pix,
196 dockapp.back_pix );
198 dockapp.iconwin = XCreateSimpleWindow( dockapp.display, dockapp.win,
199 size_hints.x, size_hints.y,
200 (unsigned int) size_hints.width,
201 (unsigned int) size_hints.height,
202 BWIDTH, dockapp.fore_pix,
203 dockapp.back_pix );
205 /* Configuring Client to Window Manager Communications. */
207 /* WM_NORMAL_HINTS property: size hints for a window in it's normal state. */
208 /* Replaces the size hints for the WM_NORMAL_HINTS property on the specified
209 window. */
210 XSetWMNormalHints( dockapp.display, dockapp.win, &size_hints );
212 /* Setting the WM_CLASS property. */
214 char *app_name = argv[0];
215 XClassHint wm_class;
217 /* The res_name member contains the application name.
218 The res_class member contains the application class. */
219 /* The name set in this property may differ from the name set as WM_NAME.
220 That is, WM_NAME specifies what should be displayed in the title bar and,
221 therefore, can contain temporal information (for example, the name of a
222 file currently in an editor's buffer). On the other hand, the name
223 specified as part of WM_CLASS is the formal name of the application that
224 should be used when retrieving the application's resources from the
225 resource database. */
226 wm_class.res_name = app_name;
227 wm_class.res_class = app_name;
228 (void) XSetClassHint( dockapp.display, dockapp.win, &wm_class );
231 /* Setting the WM_NAME property.
232 This specifies what should be displayed in the title bar (usually the
233 application name). */
235 XTextProperty text_prop;
237 char *app_name = argv[0];
238 const int string_count = 1;
240 status = XStringListToTextProperty( &app_name, string_count, &text_prop );
241 if( status == 0 ) {
242 fprintf( stderr, "%s: XStringListToTextProperty() failed\n", PACKAGE );
243 ErrorLocation( __FILE__, __LINE__ );
244 exit( EXIT_FAILURE );
247 XSetWMName( dockapp.display, dockapp.win, &text_prop );
249 /* Freing the storage for the value field. */
250 (void) XFree( text_prop.value );
253 /* WM_HINTS --> Additional hints set by the client for use by the Window
254 Manager. */
255 /* XWMHints wm_hints; */
257 /* WithdrawnState, NormalState or IconicState. Must be set to WithdrawnState
258 for DockApp. */
259 wm_hints.flags = StateHint | IconWindowHint | IconPositionHint |
260 WindowGroupHint;
261 wm_hints.initial_state = WithdrawnState; /* Withdrawn, Normal */
262 wm_hints.icon_window = dockapp.iconwin;
263 wm_hints.icon_x = size_hints.x;
264 wm_hints.icon_y = size_hints.y;
265 wm_hints.window_group = dockapp.win;
266 (void) XSetWMHints( dockapp.display, dockapp.win, &wm_hints );
268 /* Sets the WM_COMMAND property. This sets the command and arguments used to
269 invoke the application. */
270 (void) XSetCommand( dockapp.display, dockapp.win, argv, argc );
272 /* ... */
273 (void) XSelectInput( dockapp.display, dockapp.win,
274 ButtonPressMask | ExposureMask | ButtonReleaseMask |
275 PointerMotionMask | StructureNotifyMask );
277 (void) XSelectInput( dockapp.display, dockapp.iconwin,
278 ButtonPressMask | ExposureMask | ButtonReleaseMask |
279 PointerMotionMask | StructureNotifyMask );
281 /* Create GC for drawing */
282 gcv.foreground = dockapp.fore_pix;
283 gcv.background = dockapp.back_pix;
284 gcv.graphics_exposures = 0;
285 dockapp.NormalGC = XCreateGC( dockapp.display, dockapp.root_win,
286 GCForeground | GCBackground |
287 GCGraphicsExposures, &gcv );
289 /* Convert XPM data to XImage */
290 CreateIconFromXpmData( pixmap_data );
292 XShapeCombineMask( dockapp.display, dockapp.win, ShapeBounding, 0, 0,
293 dockapp.xpm_icon.shapemask, ShapeSet );
295 XShapeCombineMask( dockapp.display, dockapp.iconwin, ShapeBounding, 0, 0,
296 dockapp.xpm_icon.shapemask, ShapeSet );
298 /* Making the new window visible. */
299 (void) XMapWindow( dockapp.display, dockapp.win );