Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 82 → Rev 83

/trunk/wmstatus/libwmstatus.c
0,0 → 1,261
/**
* libwmstatus - get information from the window manager
*/
#include <X11/Xlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
 
//
// Local Variables
//
static Display* disp;
 
//
// Error Handling
//
static int xErrorHandler( Display* disp, XErrorEvent* evt ){
fprintf( stderr, "XError was thrown" );
return 0;
}
 
//
// Local functions
//
static int wm_send_event_long( Window win, const char* msg,
unsigned long data0, unsigned long data1,
unsigned long data2, unsigned long data3,
unsigned long data4 ){
XEvent evt;
 
long mask = SubstructureRedirectMask | SubstructureNotifyMask;
 
memset( &evt, 0, sizeof( evt ) );
evt.xclient.type = ClientMessage;
evt.xclient.serial = 0;
evt.xclient.send_event = True;
evt.xclient.message_type = XInternAtom( disp, msg, False );
evt.xclient.window = win;
evt.xclient.format = 32;
evt.xclient.data.l[ 0 ] = data0;
evt.xclient.data.l[ 1 ] = data1;
evt.xclient.data.l[ 2 ] = data2;
evt.xclient.data.l[ 3 ] = data3;
evt.xclient.data.l[ 4 ] = data4;
 
if( !XSendEvent( disp, DefaultRootWindow( disp ), False, mask, &evt ) ){
return -1;
}
XFlush( disp );
 
return 1;
}
 
//
// Core functions
//
 
int wm_init(){
disp = XOpenDisplay( NULL );
if( disp == NULL ){
//don't set errno, leave as is
return -1;
}
 
XSetErrorHandler( xErrorHandler );
return 0;
}
 
int wm_get_number_desktops(){
if( disp == NULL ) return -1;
return -1;
}
 
void wm_switch_to_desktop( int desktop ){
if( disp == NULL ) return;
 
}
 
void wm_activate_window( int xid ){
if( disp == NULL ) return;
 
if( wm_send_event_long( xid, "_NET_ACTIVE_WINDOW", 0, 0, 0, 0, 0 ) < 0 ){
fprintf( stderr, "ERROR: Cannot activate window\n" );
}
}
 
void wm_close_window( int xid ){
if( disp == NULL ) return;
 
if( wm_send_event_long( xid, "_NET_CLOSE_WINDOW", 0, 0, 0, 0, 0 ) < 0 ){
fprintf( stderr, "ERROR: Cannot close window\n" );
}
}
 
void wm_move_window_to_desktop( int xid, int desktop ){
if( disp == NULL ) return;
 
}
 
int wm_get_window_size( int xid, int* wid, int* height ){
if( disp == NULL ) return -1;
 
int xpos;
int ypos;
Window root;
unsigned int border_wid;
unsigned int depth;
 
if( XGetGeometry( disp, xid, &root, &xpos, &ypos, wid, height, &border_wid, &depth ) != 0 ){
fprintf( stderr, "%s\n", __func__ );
}
XFlush( disp );
}
 
void wm_window_resize( int xid, int wid, int height ){
if( disp == NULL ) return;
 
int xpos;
int ypos;
wm_get_window_location( xid, &xpos, &ypos );
 
if( wm_send_event_long( xid, "_NET_MOVERESIZE_WINDOW", 0, xpos, ypos, wid, height ) < 0 ){
fprintf( stderr, "ERROR: Cannot resize window\n" );
}
}
 
void wm_window_move( int xid, int xpos, int ypos ){
if( disp == NULL ) return;
 
int height;
int wid;
 
wm_get_window_size( xid, &wid, &height );
 
if( wm_send_event_long( xid, "_NET_MOVERESIZE_WINDOW", 0, xpos, ypos, wid, height ) < 0 ){
fprintf( stderr, "ERROR: Cannot resize window\n" );
}
}
 
int wm_get_window_location( int xid, int* xpos, int* ypos ){
if( disp == NULL ) return -1;
 
int wid;
int height;
Window root;
unsigned int border_wid;
unsigned int depth;
 
if( XGetGeometry( disp, xid, &root, xpos, ypos, &wid, &height, &border_wid, &depth ) != 0 ){
fprintf( stderr, "%s\n", __func__ );
}
XFlush( disp );
}
 
void wm_window_set_ltitle( int xid, char* title ){
if( disp == NULL ) return;
 
}
 
void wm_window_set_stitle( int xid, char* title ){
if( disp == NULL ) return;
 
}
 
void wm_window_unmap( int xid ){
if( disp == NULL ) return;
 
}
 
int wm_window_get_xid_for_pid( int pid ){
if( disp == NULL ) return;
 
}
 
int wm_get_current_window(){
if( disp == NULL ) return -1;
 
}
 
int wm_check_xid( int xid ){
if( disp == NULL ) return -1;
 
}
 
int wm_get_xid_for_name( const char* name ){
if( disp == NULL ) return -1;
 
}
 
char* wm_get_window_property( int xid, const char* property ){
if( disp == NULL ) return NULL;
 
Atom prop;
long long_offset = 0;
long long_len = 1024;
Atom prop_type;
Atom type_return;
int format_return;
unsigned long nitems_return;
unsigned long bytes_after_return;
unsigned char* prop_return;
char* ret = NULL;
unsigned int tmp_len;
 
prop_type = XInternAtom( disp, "UTF8_STRING", False );
prop = XInternAtom( disp, property, False );
 
if( XGetWindowProperty( disp, xid, prop, long_offset, long_len, False,
prop_type, &type_return, &format_return,
&nitems_return, &bytes_after_return, &prop_return ) != Success ){
fprintf( stdout, "ERROR: wm_get_window_property %s\n", strerror( errno ) );
return NULL;
}
 
if( prop_type != type_return ){
fprintf( stderr, "ERROR: %s %s\n", __func__, strerror( errno ) );
return NULL;
}
 
tmp_len = ( format_return / 8 ) * nitems_return + 1;
ret = malloc( tmp_len );
if( ret == NULL ) return ret;
memcpy( ret, &(*prop_return), tmp_len );
ret[ tmp_len ] = 0;
 
XFree( prop_return );
return ret;
}
 
Window* wm_get_all_windows( ){
Window root_return;
Window parent_return;
Window* children_return;
unsigned int nchildren_return;
unsigned int x;
 
Window* to_ret;
 
if( XQueryTree( disp, DefaultRootWindow( disp ), &root_return, &parent_return, &children_return, &nchildren_return ) ){
to_ret = calloc( nchildren_return + 1, sizeof( Window ) );
if( to_ret == NULL ) return NULL;
if( children_return == NULL ){
return to_ret;
}
 
for( x = 0; x < nchildren_return; x++ ){
memcpy( to_ret + x, &(children_return[ x ]), sizeof( Window ) );
printf( "child: 0x%X\n", children_return[ x ] );
}
 
 
XFree( children_return );
 
return to_ret;
}
 
return NULL;
}
/trunk/wmstatus/wmstatus.c
0,0 → 1,69
/**
* wmstatus - Get information about the current window manager.
*
* An alternative to wmctrl
*/
#include <stdio.h>
#include <popt.h>
#include "wmstatus.h"
 
//
// Local Variables
//
 
//
// Function Prototypes
//
 
int main( int argc, char** argv ){
int xid;
poptContext pc;
int poptVal;
 
struct poptOption optionsTable[] = {
{ "xid", 'x', POPT_ARG_INT, &xid, 0, "Set the XID to use", NULL },
POPT_AUTOHELP
POPT_TABLEEND
};
 
pc = poptGetContext( NULL, argc, (const char**)argv, optionsTable, 0 );
while( (poptVal = poptGetNextOpt( pc ) ) >= 0 ){}
 
if( poptVal != -1 ){
fprintf( stderr, "some error here fix it\n" );
return 1;
}
 
if( wm_init() < 0 ){
perror( "wm_init" );
}
 
printf( "property: %s\n", wm_get_window_property( 0x02e00026, "_NET_WM_NAME" ) );
 
int wid;
int height;
int xpos;
int ypos;
int x;
 
if( wm_get_window_size( 0x02e00026, &wid, &height ) < 0 ){
fprintf( stderr, "error\n" );
}
 
if( wm_get_window_location( 0x02e00026, &xpos, &ypos ) < 0 ){
fprintf( stderr, "error\n" );
}
 
printf( "%d %d at location %d %d\n", wid, height, xpos, ypos );
 
Window* windows = wm_get_all_windows();
x = 0;
printf( "here\n" );
fflush(stdout);
while( windows[ x ] != NULL ){
printf( "Window: 0x%X\n", windows[ x ] );
x++;
}
 
return 0;
}
/trunk/wmstatus/Makefile
0,0 → 1,18
#VERY QUICK MAKEFILE
# NEED TO FIX MAKEMAKE
 
OBJFILES=libwmstatus.o wmstatus.o
CLIBFLAGS=-lX11
CC=gcc
CFLAGS=-fPIC
 
all: $(OBJFILES) lib wmstatus
 
lib:
gcc -shared -Wl,-soname,libwmstatus.so.0 -o libwmstatus.so libwmstatus.o
wmstatus: wmstatus.o
gcc -o wmstatus wmstatus.o -L. -lwmstatus -lX11 -lpopt
 
clean:
rm $(OBJFILES)
/trunk/wmstatus/wmstatus.h
0,0 → 1,155
/**
* libwmstatus - get information form the window manager
*/
 
#ifndef LIBWMSTATUS_H
#define LIBWMSTATUS_H
 
#include <X11/Xlib.h>
 
/**
* Initialize the connection to the window manager
*/
int wm_init();
 
/**
* Get the number of desktops the window manager has
*/
int wm_get_number_desktops();
 
/**
* Switch to the given desktop
*/
void wm_switch_to_desktop( int desktop );
 
/**
* Activate the given window
*
* @param xid The XID of the window to activate
*/
void wm_activate_window( int xid );
 
/**
* Close the given window
*
* @param xid The window to close
*/
void wm_close_window( int xid );
 
/**
* Move the given window to the current desktop
*
* @param xid The window to move
* @param desktop The desktop to move the window to, -1 means 'current'
*/
void wm_move_window_to_desktop( int xid, int desktop );
 
/**
* Resize the window
*
* @param xid The window to resize
* @param wid The width of the window
* @param height The height of the window
*/
void wm_window_resize( int xid, int wid, int height );
 
/**
* Get the size of the window. Return -1 if window not found
*
* @param xid The window to get the size of
* @param wid The returned window size
* @param height The returned window height
* @return -1 if window not found
*/
int wm_get_window_size( int xid, int* wid, int* height );
 
/**
* Move the window
*
* @param xid The window to move
* @param xpos The X position of the window
* @param ypos The Y position of the window
*/
void wm_window_move( int xid, int xpos, int ypos );
 
/**
* Get the location of the window
*
* @param xid The window to get the location of
* @param xpos The returned xpos
* @param ypos The returned ypos
* @return -1 if window not found/some other error
*/
int wm_get_window_location( int xid, int* xpos, int* ypos );
 
/**
* Set the long title of the winodw
*
* @param xid The window to set the title on
* @param title The title to set
*/
void wm_window_set_ltitle( int xid, char* title );
 
/**
* Set the short title of the winodw
*
* @param xid The window to set the title on
* @param title The title to set
*/
void wm_window_set_stitle( int xid, char* title );
 
/**
* Unmap the given window
*
* @param xid The window to unmap
*/
void wm_window_unmap( int xid );
 
/**
* Get the XID of a window with the given PID
* This returns the first window that it finds
*
* @param pid The PID to look for
*/
int wm_window_get_xid_for_pid( int pid );
 
/**
* Get the currently active window
*
* @return the XID of the currenly active window
*/
int wm_get_current_window();
 
/**
* Check to see if the given XID is valid
*
* @param xid The XID to check for
* @return 1 if valid, 0 otherwise
*/
int wm_check_xid( int xid );
 
/**
* Get the XID for the given name. Returns 0 if not found
*
* @param name The name of the window
* @return The XID of the first window with this name
*/
int wm_get_xid_for_name( const char* name );
 
/**
* Get the specified property for the specified window.
*
* @param xid The XID to get the property from
* @param prop The property to get
* @return The property. This string must be free'd
*/
char* wm_get_window_property( int xid, const char* property );
 
/**
* Get all the windows that are currently on the screen. This is an array of windows, NULL terminated
*
* @param subwindows
*/
Window* wm_get_all_windows( );
 
#endif