Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Capturing the mousemove coordinates

Source code copied from The JavaScript Source: Page Details: Mouse Coordinates [internet.com]
Tested on Firefox.
<!-- ONE STEP TO INSTALL MOUSE COORDINATES:

  1.  Copy the coding into the BODY of your HTML document  -->

<!-- STEP ONE: Paste this code into the BODY of your HTML document  -->

<BODY>

<form name="Show">
X <input type="text" name="MouseX" value="0" size="4"><br>
Y <input type="text" name="MouseY" value="0" size="4"><br>
</form>

<script language="JavaScript1.2">
<!-- Original:  CodeLifter.com (support@codelifter.com) -->
<!-- Web Site:  http://www.codelifter.com -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
  var IE = document.all?true:false;
  if (!IE) document.captureEvents(Event.MOUSEMOVE)
    document.onmousemove = getMouseXY;
    
  var tempX = 0;
  var tempY = 0;
  
  function getMouseXY(e) {
    if (IE) { // grab the x-y pos.s if browser is IE
      tempX = event.clientX + document.body.scrollLeft;
      tempY = event.clientY + document.body.scrollTop;
    }
    else {  // grab the x-y pos.s if browser is NS
      tempX = e.pageX;
      tempY = e.pageY;
    }  
    
    if (tempX < 0){tempX = 0;}
    if (tempY < 0){tempY = 0;}  
    
    document.Show.MouseX.value = tempX;
    document.Show.MouseY.value = tempY;
    
    return true;
  }
//  End -->

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size:  1.33 KB -->

*update 10:55am 21-Mar-08*
I have decided to use the following code instead as it looks a bit cleaner, and more up-to-date.
Tested on Firefox 2 and IE 6.
document.onmousemove = mouseMove;

function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

Reference: How to Drag and Drop in JavaScript [webreference.com]

Xlib - mouseMove

// muove il mouse alle coordinate currentX + x, currentY + y

#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

void mouseMove(int x, int y)
{
	Display *displayMain = XOpenDisplay(NULL);

	if(displayMain == NULL)
	{
		fprintf(stderr, "Errore nell'apertura del Display !!!\n");
		exit(EXIT_FAILURE);
	}

	XWarpPointer(displayMain, None, None, 0, 0, 0, 0, x, y);

	XCloseDisplay(displayMain);
}


// gcc source.c -L /usr/X11R6/lib -lX11

Memcached StartupItem for Mac OS X


Here is an OS X StartupItem for memcached...

/Library/StartupItems/Memcached/StartupParameters.plist
{
  Provides        = ("Memcached");
  Description     = "Memcache Daemon";
  Uses            = ("Network");
  OrderPreference = "None";
}


/Library/StartupItems/Memcached/Memcached
#!/bin/bash
#
# /Library/StartupItems/Memcached/Memcached
#
# Script to startup memcached with OS X. Tested on 10.4 Tiger
#
# To enable, copy this file and StartupParameters.plist to
# /Library/StartupItems/Memcached and add "MEMCACHED=-YES-"
# to /etc/hostconfig ... You can then reboot or execute 
# "sudo /sbin/SystemStarter start Memcached" from a terminal
# to start it up.
#
# I should mention that this file uses my preferred development 
# settings. You can edit them before copying this script over 
# or they can be overriden by creating a config file named 
# memcached.conf in /etc, /opt/etc, or /usr/local/etc with the 
# following contents (modified to suit, of course):
#
#   MEMCACHED_EXE=/usr/local/bin/memcached
#   MEMCACHED_PIDFILE=/var/run/memcached.pid
#   MEMCACHED_MAX_MEM=128
#   MEMCACHED_INTERFACE=127.0.0.1
#   MEMCACHED_PORT=1121
#   MEMCACHED_RUN_AS=nobody 
#
# ------------------------------------------------------------------- 
# NOTE: Memcached < 1.2.0 has issues specifying the interface via the
# '-l' switch on the Mac for some reason so if the service fails to 
# start with the following error:
#
#    bind(): Can't assign requested address
#    failed to listen
#
# then you will need to either upgrade to version 1.2.0+ or forgo
# specifying an interface to listen on by removing the references
# to A_INTERFACE on lines 65 and 75 of this script. 
# ------------------------------------------------------------------- 
#
# Tim Ferrell <s0nspark@gmail.com>
#

# Suppress the annoying "$1: unbound variable" error 
if [ -z $1 ] ; then
  echo "Usage: $0 [start|stop|restart] "
  exit 1
fi

# Source the common setup functions for startup scripts
test -r /etc/rc.common || exit 1
source /etc/rc.common

NAME=memcached
DESC="Memcached server"

# look for a config file name ${NAME}.conf in /etc, /opt/etc, 
# and /usr/local/etc ... in that order.
[ -r /etc/${NAME}.conf ] && source /etc/${NAME}.conf
[ -r /opt/etc/${NAME}.conf ] && source /opt/etc/${NAME}.conf
[ -r /usr/local/etc/${NAME}.conf ] && source /usr/local/etc/${NAME}.conf

# these are _my_ defaults... 
DAEMON="${MEMCACHED_EXE:=/usr/local/bin/memcached}"
A_PIDFILE="-P ${MEMCACHED_PIDFILE:=/var/run/memcached.pid}"
A_MAXMEM="-m ${MEMCACHED_MAX_MEM:=128}"
A_INTERFACE="-l ${MEMCACHED_INTERFACE:=127.0.0.1}"
A_PORT="-p ${MEMCACHED_PORT:=11211}"
A_RUNAS="-u ${MEMCACHED_RUN_AS:=nobody}"

StartService () 
{
  if [ "${MEMCACHED:=-NO-}" = "-YES-" ] && ! GetPID ${NAME} > /dev/null; then
    if [ -f /var/run/${NAME}.StartupItem ] ; then exit ; fi
    touch /var/run/${NAME}.StartupItem
    echo "Starting ${DESC}"
    ${DAEMON} -d ${A_PIDFILE} ${A_MAXMEM} ${A_INTERFACE} ${A_PORT} ${A_RUNAS} 
  fi
}

StopService ()
{
  if PID=$(GetPID ${NAME}); then
    echo "Stopping ${DESC}, (PID ${PID})"
    kill -TERM "${PID}"
    rm -f ${A_PIDFILE}
  else
    echo "${DESC} not running."
  fi
  rm -f /var/run/${NAME}.StartupItem
}

RestartService () { StopService; StartService; }

if test -x $DAEMON ; then
  RunService "$1"
else
  echo "Could not find ${DAEMON}!"
fi



One-click connect from cygwin to full-screen Linux (X stuff required)

Optional: execute the following once (see http://hacks.oreilly.com/pub/h/66 for
details), to avoid typing in the password.

set LINUX_HOST=mylinuxhost
ssh-keygen -t rsa
ssh %USERNAME%@%LINUX_HOST% "mkdir .ssh; chmod 0700 .ssh"
bash -c 'scp ~/.ssh/id_rsa.pub %LINUX_HOST%:.ssh/authorized_keys2'


Now, if you save the following as a BATCH file, you can just click it
to connect to fullscreen Linux:

set LINUX_HOST=mylinuxhost
start /min xinit 
xhost +%LINUX_HOST%
ssh %LINUX_HOST% "declare -x DISPLAY=%COMPUTERNAME%:0; echo $DISPLAY;gnome-session"'


NOTES:
1.
Sometimes xhost + does not execute in time. O well. Just close
all windows that got opened and try again.

2.
If you didn't do the step one (ssh keys), you will be prompted
to enter the linux password; this increases the chances of the above
error.

3.
The single terminal window appearing in the Linux desktop is in fact
a window from your PC.

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS