From 37bb87fdff7e13d71371d364b0f9bd2cffc28f67 Mon Sep 17 00:00:00 2001 From: mathias Date: Wed, 1 Jun 2005 10:06:15 +0000 Subject: [PATCH] * added -cursor image:file=, uses xrender and xpm/imlib2 * minor tweaks in alock_util.c --HG-- extra : convert_revision : svn%3Aeebe1cee-a9af-4fe4-bd26-ad572b19c5ab/trunk%4033 --- CHANGELOG | 4 ++ README | 2 +- SConstruct | 21 ++++-- alock.txt | 2 + scons_alock.py | 6 ++ src/SConscript | 2 + src/alock.c | 36 ++++++++++ src/alock.h | 33 --------- src/alock_utils.c | 42 ++++++----- src/bg_blank.c | 1 - src/cursor_image.c | 173 +++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 264 insertions(+), 58 deletions(-) create mode 100644 src/cursor_image.c diff --git a/CHANGELOG b/CHANGELOG index a380e91..d126cc7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,9 @@ Version 1.0 +2005-06-01: + + * added -cursor image:file=, uses xrender and xpm/imlib2 + 2005-05-25: * solved problems install-problems: use at least scons 0.96.90 diff --git a/README b/README index eba094b..83aedaf 100644 --- a/README +++ b/README @@ -1,7 +1,7 @@ alock - README ============== Mathias Gumz -v1.0, 24 May 2005 +v1.0, 01 June 2005 About ----- diff --git a/SConstruct b/SConstruct index 24edcd3..971b7e5 100644 --- a/SConstruct +++ b/SConstruct @@ -9,6 +9,9 @@ import sys import os from scons_alock import * +from SCons.Script.SConscript import SConsEnvironment # just do this once +SConsEnvironment.InstallAsMode = alock_installAs + alock_name = 'alock' alock_version = '1.0' alock_optfile = [ 'scons.opts', 'user.opts' ] @@ -43,7 +46,8 @@ alock_options.AddOptions( BoolOption('hash', 'support for -auth ', 1), BoolOption('imlib2', 'support for -bg image via imlib2', 1), - BoolOption('xrender', 'support for -bg shade via xrender', 1), + BoolOption('xrender', 'support for -bg shade via xrender or -cursor image', 1), + BoolOption('xpm', 'support for reading images via libxpm', 1), BoolOption('xcursor', 'support for -bg xcursor:', 1), @@ -131,6 +135,11 @@ if alock_env['xcursor']: print "sorry, no xcursor-support found." conf.Finish() +if alock_env['xpm']: + alock_env.AppendUnique( + CPPDEFINES = ['HAVE_XPM'], + LIBS = [ 'Xpm' ]) + if alock_env['imlib2']: conf = alock_env.Configure() print "Checking for Imlib2... ", @@ -198,10 +207,10 @@ alock_env.AddPostAction('alock.html', Chmod('alock.html', 0644)) ############################################################################ # # installing -alock_env.Install(alock_instdir_bin, alock_target) -alock_env.Install(alock_instdir_meta, alock_meta_files) -alock_env.Install(alock_instdir_man, alock_manpage) -alock_env.Install(alock_instdir_meta_contrib, alock_contrib_files) +#alock_env.InstallAs(prefixCombiner(alock_instdir_bin, ['alock'], os.sep), [alock_target], 0755) +#alock_env.InstallAs(prefixCombiner(alock_meta_files, alock_instdir_meta, os.sep), alock_meta_files, 0644) +#alock_env.InstallAs(prefixCombiner(alock_instdir_man, [alock_manpage], os.sep), [alock_manpage], 0644) +#alock_env.InstallAs(prefixCombiner(alock_instdir_meta_contrib, alock_contrib_files, os.sep), alock_contrib_files, 0645) # TODO: add a "scons dist" command which builds a propper tarball #alock_env.Alias('dist', alock_env.Tar(alock_target + '-' + alock_version, @@ -213,6 +222,6 @@ alock_env.Install(alock_instdir_meta_contrib, alock_contrib_files) # aliases alock_env.Alias('docs', alock_doc_files) -alock_env.Alias('install', alock_instdir) +#alock_env.Alias('install', alock_instdir) # vim:ft=python diff --git a/alock.txt b/alock.txt index c40e79b..b406256 100644 --- a/alock.txt +++ b/alock.txt @@ -75,6 +75,8 @@ OPTIONS * fg= - xcursor - Use the given in xcursor-format * file= + - image - Use the given + * file= - none - No change to the current cursor AUTHOR diff --git a/scons_alock.py b/scons_alock.py index e860764..1c25779 100644 --- a/scons_alock.py +++ b/scons_alock.py @@ -21,6 +21,12 @@ def prefixCombiner(prefix, itemlist, glue=''): result.append(prefix + glue + item) return result + +def alock_installAs(env, dest, source, mode = 0644): + env.InstallAs(dest, source) + for file in dest: + env.Chmod(file, mode) + # http://scons.tigris.org/servlets/ReadMsg?listName=users&msgNo=2739 # http://scons.tigris.org/servlets/ReadMsg?list=users&msgNo=2783 def alock_installFunc(dest, source, env): diff --git a/src/SConscript b/src/SConscript index 95807c0..1044053 100644 --- a/src/SConscript +++ b/src/SConscript @@ -29,6 +29,8 @@ if build['imlib2']: bg_sources += [ 'bg_image.c' ] if build['xrender']: bg_sources += [ 'bg_shade.c'] + if build['xrender'] or build['xpm']: + cursor_sources += ['cursor_image.c'] alock_sources += auth_sources + bg_sources + cursor_sources diff --git a/src/alock.c b/src/alock.c index b61ec6b..6c111f8 100644 --- a/src/alock.c +++ b/src/alock.c @@ -32,6 +32,18 @@ /*------------------------------------------------------------------*\ globals \*------------------------------------------------------------------*/ +extern struct aAuth alock_auth_none; +#ifdef HASH_PWD +extern struct aAuth alock_auth_md5; +extern struct aAuth alock_auth_sha1; +#endif /* HASH_PWD */ +#ifdef PASSWD_PWD +extern struct aAuth alock_auth_passwd; +#endif /* PASSWD_PWD */ +#ifdef PAM_PWD +extern struct aAuth alock_auth_pam; +#endif /* PAM_PWD */ + static struct aAuth* alock_authmodules[] = { &alock_auth_none, #ifdef PAM_PWD @@ -46,6 +58,16 @@ static struct aAuth* alock_authmodules[] = { #endif /* HASH_PWD */ NULL }; +/*------------------------------------------------------------------*\ +\*------------------------------------------------------------------*/ +extern struct aBackground alock_bg_none; +extern struct aBackground alock_bg_blank; +#ifdef HAVE_IMLIB2 +extern struct aBackground alock_bg_image; +#endif /* HAVE_IMLIB2 */ +#ifdef HAVE_XRENDER +extern struct aBackground alock_bg_shade; +#endif /* HAVE_XRENDER */ static struct aBackground* alock_backgrounds[] = { &alock_bg_none, @@ -58,6 +80,17 @@ static struct aBackground* alock_backgrounds[] = { #endif /* HAVE_XRENDER */ NULL }; +/* ---------------------------------------------------------------- *\ +\* ---------------------------------------------------------------- */ +extern struct aCursor alock_cursor_none; +extern struct aCursor alock_cursor_theme; +extern struct aCursor alock_cursor_glyph; +#ifdef HAVE_XCURSOR +extern struct aCursor alock_cursor_xcursor; +#endif /* HAVE_XCURSOR */ +#ifdef HAVE_XRENDER +extern struct aCursor alock_cursor_image; +#endif /* HAVE_XRENDER */ static struct aCursor* alock_cursors[] = { &alock_cursor_none, @@ -66,6 +99,9 @@ static struct aCursor* alock_cursors[] = { #ifdef HAVE_XCURSOR &alock_cursor_xcursor, #endif /* HAVE_XCURSOR */ +#ifdef HAVE_XRENDER + &alock_cursor_image, +#endif /* HAVE_XRENDER */ NULL }; /*------------------------------------------------------------------*\ diff --git a/src/alock.h b/src/alock.h index 0a6ceb8..2ba1823 100644 --- a/src/alock.h +++ b/src/alock.h @@ -82,38 +82,5 @@ int alock_shade_pixmap(const struct aXInfo* xinfo, unsigned int width, unsigned int height); -/*------------------------------------------------------------------*\ -\*------------------------------------------------------------------*/ -extern struct aBackground alock_bg_none; -extern struct aBackground alock_bg_blank; -#ifdef HAVE_IMLIB2 -extern struct aBackground alock_bg_image; -#endif /* HAVE_IMLIB2 */ -#ifdef HAVE_XRENDER -extern struct aBackground alock_bg_shade; -#endif /* HAVE_XRENDER */ -/*------------------------------------------------------------------*\ -\*------------------------------------------------------------------*/ -extern struct aAuth alock_auth_none; -#ifdef HASH_PWD -extern struct aAuth alock_auth_md5; -extern struct aAuth alock_auth_sha1; -#endif /* HASH_PWD */ -#ifdef PASSWD_PWD -extern struct aAuth alock_auth_passwd; -#endif /* PASSWD_PWD */ -#ifdef PAM_PWD -extern struct aAuth alock_auth_pam; -#endif /* PAM_PWD */ -/*------------------------------------------------------------------*\ -\*------------------------------------------------------------------*/ -extern struct aCursor alock_cursor_none; -extern struct aCursor alock_cursor_theme; -extern struct aCursor alock_cursor_glyph; -#ifdef HAVE_XCURSOR -extern struct aCursor alock_cursor_xcursor; -#endif /* HAVE_XCURSOR */ -/* ---------------------------------------------------------------- *\ -\* ---------------------------------------------------------------- */ #endif // _ALOCK_H_ diff --git a/src/alock_utils.c b/src/alock_utils.c index 8763246..9234c0d 100644 --- a/src/alock_utils.c +++ b/src/alock_utils.c @@ -57,14 +57,22 @@ int alock_alloc_color(const struct aXInfo* xinfo, const char* color_name, \*------------------------------------------------------------------*/ int alock_check_xrender(const struct aXInfo* xinfo) { #ifdef HAVE_XRENDER - int major_opcode, first_event, first_error; - if (XQueryExtension(xinfo->display, "RENDER", - &major_opcode, - &first_event, &first_error) == False) { - printf("alock: error, no xrender-support found\n"); - return 0; + static int have_xrender = 0; + static int checked_already = 0; + + if (!checked_already) { + int major_opcode, first_event, first_error; + if (XQueryExtension(xinfo->display, "RENDER", + &major_opcode, + &first_event, &first_error) == False) { + printf("alock: error, no xrender-support found\n"); + have_xrender = 0; + } else + have_xrender = 1; + + checked_already = 1; } - return 1; + return have_xrender; #else printf("alock: error, i wasnt compiled to support xrender.\n"); return 0; @@ -86,14 +94,18 @@ int alock_shade_pixmap(const struct aXInfo* xinfo, Picture alpha_pic = None; XRenderPictFormat* format = None; - XRenderPictFormat alpha_format; - alpha_format.type = PictTypeDirect; - alpha_format.depth = 8; - alpha_format.direct.alpha = 0; - alpha_format.direct.alphaMask = 0xff; + { + XRenderPictFormat alpha_format; + unsigned long mask = PictFormatType|PictFormatDepth|PictFormatAlpha|PictFormatAlphaMask; + alpha_format.type = PictTypeDirect; + alpha_format.depth = 8; + alpha_format.direct.alpha = 0; + alpha_format.direct.alphaMask = 0xff; + + format = XRenderFindFormat(dpy, mask, &alpha_format, 0); + } - format = XRenderFindStandardFormat(dpy, PictStandardA8); if (!format) { printf("error, couldnt find valid format for alpha.\n"); XFreePixmap(dpy, dst_pm); @@ -138,10 +150,6 @@ int alock_shade_pixmap(const struct aXInfo* xinfo, #endif /* HAVE_XRENDER */ } - - - /* ---------------------------------------------------------------- *\ \* ---------------------------------------------------------------- */ - diff --git a/src/bg_blank.c b/src/bg_blank.c index 2359517..daf62a8 100644 --- a/src/bg_blank.c +++ b/src/bg_blank.c @@ -39,7 +39,6 @@ static int alock_bg_blank_init(const char* args, struct aXInfo* xinfo) { XWindowAttributes xgwa; XSetWindowAttributes xswa; long xsmask = 0; - XColor tmp_color; char* color_name = strdup("black"); if (!xinfo || !args) diff --git a/src/cursor_image.c b/src/cursor_image.c new file mode 100644 index 0000000..f8dbbc9 --- /dev/null +++ b/src/cursor_image.c @@ -0,0 +1,173 @@ +/* ---------------------------------------------------------------- *\ + + file : cursor_image.c + author : m. gumz + copyr : copyright (c) 2005 by m. gumz + + license : see LICENSE + + start : Mi 01 June 2005 10:48:21 CEST + + $Id: $ + +\* ---------------------------------------------------------------- */ +/* ---------------------------------------------------------------- *\ + + about : + + provide -cursor image:file= + +\* ---------------------------------------------------------------- */ + +/* ---------------------------------------------------------------- *\ + includes +\* ---------------------------------------------------------------- */ +#include +#include +#ifdef HAVE_IMLIB2 +# include +#elif HAVE_XPM +# include +#endif /* HAVE_IMLIB2 | HAVE_XPM */ +#include +#include +#include +#include "alock.h" + +/* ---------------------------------------------------------------- *\ +\* ---------------------------------------------------------------- */ + +static Cursor cursor = 0; + +static int alock_cursor_image_init(const char* args, struct aXInfo* xinfo) { + + char* filename = NULL; + + if (!xinfo || !args) + return 0; + + if (strstr(args, "image:") == args && strlen(&args[6]) > 0) { + char* arguments = strdup(&args[6]); + char* tmp; + char* arg = NULL; + for (tmp = arguments; tmp; ) { + arg = strsep(&tmp, ","); + if (arg) { + if (strstr(arg, "file=") == arg && strlen(arg) > 6) { + if (!filename) + filename = strdup(&arg[5]); + } + } + } + free(arguments); + } + + + if (!filename) { + printf("alock: error, missing argument for [image].\n"); + return 0; + } + + if (!alock_check_xrender(xinfo)) { + printf("alock: error, no running xrender extension found [image].\n"); + free(filename); + return 0; + } + + { + unsigned int w = 0; + unsigned int h = 0; + Pixmap cursor_pm = None; + +#ifdef HAVE_IMLIB2 + { + Imlib_Image img; + Imlib_Context ctx = imlib_context_new(); + + imlib_context_push(ctx); + imlib_context_set_display(xinfo->display); + imlib_context_set_visual(DefaultVisual(xinfo->display, DefaultScreen(xinfo->display))); + imlib_context_set_colormap(xinfo->colormap); + + img = imlib_load_image_without_cache(filename); + if (img) { + imlib_context_set_image(img); + w = imlib_image_get_width(); + h = imlib_image_get_height(); + cursor_pm = XCreatePixmap(xinfo->display, + xinfo->root, + w, h, + DefaultDepth(xinfo->display, DefaultScreen(xinfo->display))); + imlib_context_set_drawable(cursor_pm); + imlib_render_image_on_drawable(0, 0); + imlib_free_image_and_decache(); + } + imlib_context_pop(); + imlib_context_free(ctx); + } +#elif HAVE_XPM + if (!cursor_pm) { + XImage* img = NULL; + XpmReadFileToImage(xinfo->display, filename, &img, NULL, NULL); + if (img) { + w = img->width; + h = img->height; + + cursor_pm = XCreatePixmap(xinfo->display, + xinfo->root, + w, h, + DefaultDepth(xinfo->display, DefaultScreen(xinfo->display))); + XPutImage(xinfo->display, cursor_pm, + DefaultGC(xinfo->display, DefaultScreen(xinfo->display)), + img, + 0, 0, 0, 0, w, h); + XDestroyImage(img); + } + } +#else +#warning compiling this file without having either imlib2 or xpm is pretty useless since no image can be loaded. +#endif /* HAVE_XPM|HAVE_IMLIB2 */ + + if (!cursor_pm) { + printf("alock: error while loading [%s] in [image].\n", filename); + free(filename); + return 0; + } + + { + XRenderPictFormat* format = XRenderFindVisualFormat(xinfo->display, + DefaultVisual(xinfo->display, DefaultScreen(xinfo->display))); + if (format) { + Picture cursor_pic = XRenderCreatePicture(xinfo->display, cursor_pm, format, 0, 0); + cursor = XRenderCreateCursor(xinfo->display, cursor_pic, w / 2, h / 2); + XRenderFreePicture(xinfo->display, cursor_pic); + } + + } + } + + free(filename); + + xinfo->cursor = cursor; + + return cursor; +} + +static int alock_cursor_image_deinit(struct aXInfo* xinfo) { + if (!xinfo || !cursor) + return 0; + + XFreeCursor(xinfo->display, cursor); + return 1; +} + +struct aCursor alock_cursor_image = { + "image", + alock_cursor_image_init, + alock_cursor_image_deinit +}; + + +/* ---------------------------------------------------------------- *\ +\* ---------------------------------------------------------------- */ +