Vizible’s Blog

Perl – Example Data Types

Posted in perl by wearetherock on August 26, 2009

1. Scalar
=============================

$first_name = "Wearetherock";
$last_name= "Ok";
$salary = 20;
print $first_name, $last_name, $salary;

~
2. Array
=============================

@names = ("Java", "C", "Python", "Perl");
print "@names";
print "$names[0] and $names[2]";
print "$names[-1]\n";
$names[4] = "Lisp";

function : pop, push, shift, unshift, splice, sort

~
3. Hash
=============================

%employee = (
	"Name" => "Wearetherock",
	"Phone" => "119",
	"Position" => "ZEO"
);
print $employee{"Name"};
$employee{"SSN"} = "9999-0000-5555";

function : keys, values, each, delete

~

Advertisements

Python – SflPhone Dbus

Posted in Python by wearetherock on August 26, 2009

import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
from gobject import GObject
from SimpleXMLRPCServer import SimpleXMLRPCServer
import thread

class SlfPhoneConnector :

	def __init__(self) :
		self.activeCalls = {}

		account = {
			"username" : "1111", 
			"Account.type" : "SIP", 
			"hostname" : "192.168.1.109", 
			"Account.alias" : "1111", 
			"password":"1111", 
			"Account.enable" : "TRUE"
		}
		session = dbus.SessionBus()

		conf_obj = session.get_object("org.sflphone.SFLphone", "/org/sflphone/SFLphone/ConfigurationManager")  
		self.conf_mgr = dbus.Interface(conf_obj ,"org.sflphone.SFLphone.ConfigurationManager")

		call_obj = session.get_object("org.sflphone.SFLphone", "/org/sflphone/SFLphone/CallManager")  
		self.call_mgr = dbus.Interface(call_obj ,"org.sflphone.SFLphone.CallManager")

		'''
		i= session.get_object("org.sflphone.SFLphone", "/org/sflphone/SFLphone/ConfigurationManager")  
		instance = dbus.Interface(object,"org.sfphone.SFLphone.Instance")
		'''

		self.call_mgr.connect_to_signal('incomingCall', self.on_incoming_call)
		self.call_mgr.connect_to_signal('callStateChanged', self.on_call_state_changed)

		self.account_id = self.conf_mgr.addAccount(account)

		self.conf_mgr.sendRegister(self.account_id, 1)			 
		
		#self.call_mgr.placeCall(self.account_id, self.account_id, "2222" )	 

	def on_incoming_call(self, account, callid, to):
		print "Incoming call: " + account + ", " + callid + ", " + to
		self.activeCalls[callid] = {'Account': account, 'To': to, 'State': '' }   

		self.call_mgr.accept(str(callid))

	# On call state changed event, set the values for new calls, 
	# or delete the call from the list of active calls
	def on_call_state_changed(self, callid, state):
		print "Call state changed: " + callid + ", " + state
		if state == "HUNGUP":
			try:
				del self.activeCalls[callid]
			except KeyError:
				print "Call " + callid + " didn't exist. Cannot delete."

		elif state in [ "RINGING", "CURRENT", "INCOMING", "HOLD" ]:
			try:
				self.activeCalls[callid]['State'] = state 
			except KeyError, e:
				print "This call didn't exist!: " + callid + ". Adding it to the list."
				callDetails = self.getCallDetails(callid)
				self.activeCalls[callid] = {'Account': callDetails['ACCOUNTID'], 'To': callDetails['PEER_NUMBER'], 'State': state }
		elif state in [ "BUSY", "FAILURE" ]:
			try:
				del self.activeCalls[callid]
			except KeyError, e:
				print "This call didn't exist!: " + callid   


	def getCallDetails(self, callid):
		"""Return informations on this call if exists"""
		return self.call_mgr.getCallDetails(callid)

	def place_call(self, callid):
		self.call_mgr.placeCall(self.account_id, self.account_id, callid)

	def hangup(self) :
		call0 = self.activeCalls.keys()[0]
		self.call_mgr.hangUp(call0)


def start_server(obj):
	server = SimpleXMLRPCServer( ("localhost", 9988), allow_none= True)
	server.register_instance(obj)

	print "server start @localhost 9988 forever ..."
	server.serve_forever()

if __name__ == "__main__" :

	DBusGMainLoop(set_as_default=True)
	s  = SlfPhoneConnector()

	thread.start_new_thread(start_server(s,))

	loop = gobject.MainLoop()
	loop.run()

C – Create share library

Posted in Programming by wearetherock on August 21, 2009

1. Code
==================================

// test.c
#include 
static char *name;
void printName(){
	printf("name = %s\n", name);
}
void setName(char *n){
	name = n;
}

~
2. Compile
==================================

> gcc -c test.c -o test.o
> gcc -shared -o libfoo.so test.o

~
3. Test
==================================

>>> from ctypes import cdll
>>> lib = cdll.LoadLibrary('libfoo.so')
>>> lib.setName('java')
27090740
>>> lib.printName()
name = java
12
>>>
Tagged with: , , ,

Python – Simulate mouse click

Posted in Programming, Python by wearetherock on August 20, 2009
import time
import win32api
import win32con

time.sleep(1)
win32api.SetCursorPos((100,100))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(0.05)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)

~

Linux – How to simulate key press

Posted in Uncategorized by wearetherock on August 15, 2009

C code from http://stackoverflow.com/questions/1262310/simulate-keypress-linux-c-console-application

// test.c
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>

int main(){
 Display *display;
 unsigned int keycode;
 display = XOpenDisplay(NULL);
 keycode = XKeysymToKeycode(display, XK_A);
 XTestFakeKeyEvent(display, keycode, True, 0);
 keycode = XKeysymToKeycode(display, XK_B);
 XTestFakeKeyEvent(display, keycode, True, 0);
 keycode = XKeysymToKeycode(display, XK_C);
 XTestFakeKeyEvent(display, keycode, True, 0);
 keycode = XKeysymToKeycode(display, XK_D);
 XTestFakeKeyEvent(display, keycode, True, 0);
 XTestFakeKeyEvent(display, keycode, False, 0);
 XFlush(display);
}

Compile

# gcc -lX11 -lXtst test.c

Output

# ./a.out
# abcd
~
Tagged with: , , ,