2 * wmmisc - WindowMaker Dockapp for monitoring misc. information.
3 * Copyright (C) 2003-2006 Jesse S. (luxorfalls@sbcglobal.net)
5 * wmmisc 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 of the License, or
8 * (at your option) any later version.
10 * wmmisc 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.
15 * You should have received a copy of the GNU General Public License
16 * along with wmmisc; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <X11/extensions/shape.h>
31 #include "wmgeneral.h"
33 static Display
*da_display
;
34 static Window da_root_window
;
37 static int da_display_depth
;
38 static XSizeHints da_size_hints
;
39 static XWMHints da_wm_hints
;
40 static Pixel da_bg_pixel
;
41 static Pixel da_fg_pixel
;
42 static char* da_geometry
= NULL
;
43 static Window da_icon_window
;
44 static Window da_window
;
45 static GC da_normal_gc
;
46 static xpm_icon da_window_icon
;
47 static Pixmap da_pixmask
;
58 mouse_region_t mouse_region
[MAX_MOUSE_REGION
];
61 get_xpm( xpm_icon
* window_icon
, char** pixmap_bytes
)
63 XWindowAttributes attributes
;
66 XGetWindowAttributes( da_display
, da_root_window
, &attributes
);
68 window_icon
->attributes
.valuemask
|= ( XpmReturnPixels
| XpmReturnExtensions
);
70 err
= XpmCreatePixmapFromData( da_display
,
75 &window_icon
->attributes
);
77 if ( XpmSuccess
!= err
)
79 fprintf( stderr
, "Not enough free color cells.\n" );
85 get_color_by_name( const char* color_name
)
88 XWindowAttributes attributes
;
90 XGetWindowAttributes( da_display
, da_root_window
, &attributes
);
94 if ( 0 == XParseColor( da_display
, attributes
.colormap
, color_name
, &color
) )
96 fprintf( stderr
, "wmmisc: Can't parse color name: '%s'\n", color_name
);
100 if ( 0 == XAllocColor( da_display
, attributes
.colormap
, &color
))
102 fprintf( stderr
, "wmmisc: Can't allocate memory for color: '%s'\n", color_name
);
110 flush_expose( Window window
)
115 while ( 0 != XCheckTypedWindowEvent( da_display
, window
, Expose
, &dummy
) )
124 redraw_window( void )
126 flush_expose( da_icon_window
);
128 XCopyArea( da_display
,
129 da_window_icon
.pixmap
,
134 da_window_icon
.attributes
.width
,
135 da_window_icon
.attributes
.height
,
139 flush_expose( da_window
);
141 XCopyArea( da_display
,
142 da_window_icon
.pixmap
,
147 da_window_icon
.attributes
.width
,
148 da_window_icon
.attributes
.height
,
154 redraw_window_coords( int x
, int y
)
156 flush_expose( da_icon_window
);
158 XCopyArea( da_display
,
159 da_window_icon
.pixmap
,
164 da_window_icon
.attributes
.width
,
165 da_window_icon
.attributes
.height
,
169 flush_expose( da_window
);
171 XCopyArea( da_display
,
172 da_window_icon
.pixmap
,
177 da_window_icon
.attributes
.width
,
178 da_window_icon
.attributes
.height
,
184 add_mouse_region( int m_index
,
190 if ( MAX_MOUSE_REGION
> m_index
)
192 mouse_region
[m_index
].enable
= 1;
193 mouse_region
[m_index
].top
= m_top
;
194 mouse_region
[m_index
].left
= m_left
;
195 mouse_region
[m_index
].bottom
= m_bottom
;
196 mouse_region
[m_index
].right
= m_right
;
201 check_mouse_region( int x
, int y
)
208 for ( i
= 0; MAX_MOUSE_REGION
> i
&& 0 == found
; ++i
)
210 if ( 0 != mouse_region
[i
].enable
&&
211 x
<= mouse_region
[i
].right
&&
212 x
>= mouse_region
[i
].left
&&
213 y
<= mouse_region
[i
].bottom
&&
214 y
>= mouse_region
[i
].top
)
229 create_xbm_from_xpm( char* xbm
, char** xpm
, int sx
, int sy
)
239 unsigned char bwrite
;
243 sscanf( *xpm
, "%d %d %d %d", &width
, &height
, &numcol
, &depth
);
245 for ( k
= 0; k
!= depth
; ++k
)
251 for ( i
= numcol
+ 1; i
< ( numcol
+ sy
+ 1 ); ++i
)
256 for ( j
= 0; j
< sx
* depth
; j
+= depth
)
262 for ( k
= 0; k
!= depth
; ++k
)
265 curpixel
|= xpm
[i
][j
+ k
];
268 if ( curpixel
!= zero
)
287 copy_xpm_area( int x
,
294 XCopyArea( da_display
,
295 da_window_icon
.pixmap
,
296 da_window_icon
.pixmap
,
307 copy_xbm_area( int x
,
314 XCopyArea( da_display
,
316 da_window_icon
.pixmap
,
327 set_mask_coords( int x
, int y
)
329 XShapeCombineMask( da_display
,
336 XShapeCombineMask( da_display
,
346 open_window( int argc
,
353 unsigned int borderwidth
= 1;
354 XClassHint classHint
;
355 char* display_name
= NULL
;
360 char* geometry
= NULL
;
368 for ( i
= 1; i
< argc
; ++i
)
370 if ( 0 == strcmp( argv
[i
], "-display" ) )
372 display_name
= argv
[++i
];
375 if ( 0 == strcmp( argv
[i
], "-geometry" ) )
377 geometry
= argv
[++i
];
381 da_display
= XOpenDisplay( display_name
);
383 if ( NULL
== da_display
)
386 "%s: Can't open display: '%s'\n",
388 XDisplayName( display_name
) );
392 da_screen
= DefaultScreen( da_display
);
393 da_root_window
= RootWindow( da_display
, da_screen
);
394 da_display_depth
= DefaultDepth( da_display
, da_screen
);
395 da_x_fd
= XConnectionNumber( da_display
);
397 /* Convert XPM to XImage */
398 get_xpm( &da_window_icon
, pixmap_bytes
);
400 /* Create a window to hold the stuff */
401 da_size_hints
.flags
= USSize
| USPosition
;
405 da_bg_pixel
= get_color_by_name( "white" );
406 da_fg_pixel
= get_color_by_name( "black" );
408 XWMGeometry( da_display
,
416 &da_size_hints
.width
,
417 &da_size_hints
.height
,
420 da_size_hints
.width
= 64;
421 da_size_hints
.height
= 64;
423 da_window
= XCreateSimpleWindow( da_display
,
428 da_size_hints
.height
,
433 da_icon_window
= XCreateSimpleWindow( da_display
,
438 da_size_hints
.height
,
444 XSetWMNormalHints( da_display
, da_window
, &da_size_hints
);
445 classHint
.res_name
= wname
;
446 classHint
.res_class
= wname
;
447 XSetClassHint( da_display
, da_window
, &classHint
);
448 XSelectInput( da_display
,
454 StructureNotifyMask
) );
455 XSelectInput( da_display
,
461 StructureNotifyMask
) );
463 if ( 0 == XStringListToTextProperty( &wname
, 1, &name
) )
465 fprintf( stderr
, "%s: can't allocate window name\n", wname
);
469 XSetWMName( da_display
, da_window
, &name
);
471 /* Create GC for drawing */
472 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
473 gcv
.foreground
= da_fg_pixel
;
474 gcv
.background
= da_bg_pixel
;
475 gcv
.graphics_exposures
= 0;
476 da_normal_gc
= XCreateGC( da_display
, da_root_window
, gcm
, &gcv
);
479 da_pixmask
= XCreateBitmapFromData( da_display
,
485 XShapeCombineMask( da_display
,
492 XShapeCombineMask( da_display
,
501 da_wm_hints
.initial_state
= WithdrawnState
;
502 da_wm_hints
.icon_window
= da_icon_window
;
503 da_wm_hints
.icon_x
= da_size_hints
.x
;
504 da_wm_hints
.icon_y
= da_size_hints
.y
;
505 da_wm_hints
.window_group
= da_window
;
506 da_wm_hints
.flags
= ( StateHint
|
511 XSetWMHints( da_display
, da_window
, &da_wm_hints
);
512 XSetCommand( da_display
, da_window
, argv
, argc
);
513 XMapWindow( da_display
, da_window
);
515 if ( NULL
!= geometry
)
517 if ( 2 != sscanf( geometry
, "+%d+%d", &wx
, &wy
) )
519 fprintf( stderr
, "Bad geometry string.\n" );
523 XMoveWindow( da_display
, da_window
, wx
, wy
);
526 if ( NULL
!= display_name
)
528 free( display_name
);