Created
May 6, 2013 09:50
-
-
Save voldyman/5524265 to your computer and use it in GitHub Desktop.
Gerbil old code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//compile w ith valac --pkg gio-2.0 --pkg gdk-3.0 server.vala -o server && ./server | |
//deps: xdotool libgdk gio-2.0 | |
int cx = 0; | |
int cy = 0; | |
Gdk.Display display = null; | |
Gdk.Screen screen = null; | |
Gdk.Device pointer = null; | |
void set_device(Gdk.Device new_device) { | |
pointer = new_device; | |
print("Setting Device '"+pointer.name+"' as controlled device\n"); | |
if(!pointer.has_cursor) | |
print("Device has no cursor!\n"); | |
pointer.get_position(out screen,out cx,out cy); | |
} | |
void process_click (string raw_msg) { | |
//string s; | |
//Process.spawn_command_line_sync ("xdotool click 1", out s); | |
uint key_val; | |
Gdk.ModifierType mod; | |
pointer.get_key (-1, out key_val, out mod); | |
pointer.set_key (0, key_val, mod); | |
} | |
void process_move (string raw_msg) { | |
var msg = raw_msg.substring(1,raw_msg.length-1); | |
var raw_coords = msg.split(";"); | |
cx += int.parse(raw_coords[0]); | |
cy += int.parse(raw_coords[1]); | |
print("Warp:"+cx.to_string()+"|"+cy.to_string()+"\n"); | |
pointer.ungrab(Gdk.CURRENT_TIME); | |
pointer.warp(screen,cx,cy); | |
} | |
public async void worker_func (SocketConnection connection, Cancellable cancellable) { | |
try { | |
DataInputStream istream = new DataInputStream (connection.input_stream); | |
DataOutputStream ostream = new DataOutputStream (connection.output_stream); | |
string line; | |
// Get the received message: | |
while ((line = yield istream.read_line_async (Priority.DEFAULT, cancellable)) != null) { | |
switch(line.@get(0)) { | |
case 'M': | |
process_move(line); | |
break; | |
case 'C': | |
process_click(line); | |
break; | |
} | |
if (line.strip () == "") { | |
cancellable.cancel (); | |
break; | |
} | |
} | |
} catch (Error e) { | |
stdout.printf ("Error: %s\n", e.message); | |
} | |
} | |
int main (string[] args) { | |
Gdk.init (ref args); | |
display = Gdk.Display.get_default (); | |
print("Screens of selected display: "+display.get_n_screens().to_string()+"\n"); | |
var manager = display.get_device_manager (); | |
List<weak Gdk.Device> devices = manager.list_devices(Gdk.DeviceType.MASTER); | |
devices.foreach ((entry) => { | |
if(entry.name == "Virtual core pointer") { | |
set_device(entry); | |
} | |
}); | |
pointer.warp(screen,10,10); | |
try { | |
MainLoop loop = new MainLoop (); | |
// Create a new SocketService: | |
SocketService service = new SocketService (); | |
// Listen on port 26059. | |
service.add_inet_port (26059, null); | |
// Used to shutdown the program: | |
Cancellable cancellable = new Cancellable (); | |
cancellable.cancelled.connect (() => { | |
service.stop (); | |
loop.quit (); | |
}); | |
service.incoming.connect ((connection, source_object) => { | |
worker_func.begin (connection, cancellable); | |
return false; | |
}); | |
service.start (); | |
loop.run (); | |
} catch (Error e) { | |
stdout.printf ("Error: %s\n", e.message); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment