Robert Basic's blog

Archives for March, 2013

Learning English

by Robert Basic on March 29, 2013.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

Most of my knowledge of the English language is self-taught. I had English classes in elementary school, but that was more or less singing "London bridge is falling down" and reciting a list of irregular verbs. I also had a semester of English in college, but that again consisted of reciting a list of irregular verbs (true, this time the list was longer) and reading and translating engineering texts. Good for learning how to read a technical manual, but not so much when it comes to having a conversation with other people. Other than that, it's all from computers, music, films, books.

My mom has an old dictionary that I used to read when I was a kid. I remember spending hours upon hours flipping through the pages, learning words, trying to figure out how to pronounce them. Guess I was a nerd already when I was only seven years old.

In the past few months Swizec was quite persistent with his nagging of how terrible my English is, especially when it comes down to the grammar, so I decided to do something about it.

I started taking private English classes.

I had my first hour and a half long lesson yesterday evening. I enjoyed it very much, had a lot of fun and already learned a thing or two (at least I think I did). I am taking the classes from an English teacher who also happens to be my friend. But, don't let the friendship fool you! She's not afraid off bashing me when I make a mistake: "Hold on Robert! That sentence doesn't make any sense!" (or something along those lines). And I think it is actually a good thing that we know each other from before as it is much easier to just start a random discussion and not just go through the textbook examples. All in all, very enjoyable classes and I know I will learn a lot from them.

And to show that I am really serious about this, I started another blog dedicated to writing called Magic of Writing. And yes, I am aware that that sounds something like a five year old would come up with, but then again writing is magical. There's not much there yet, but I already have a few ideas in my head that will hopefully find their way onto paper.

Tags: english, language, learning, reading, writing.
Categories: Blablabla, Free time.

Need help on your PHP projects? Let's talk!

Saturday night hack - coords

by Robert Basic on March 24, 2013.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

When I was just starting out learning programming, everything was so simple. I did not care about design patterns and best practices and unit tests and how will users use that piece of code. Hell, I did not even know those things exist. I was having fun, I was learning, I was free to do whatever I wanted to do, I was playing, I was like a child. Not that there is something wrong caring about those things now, but then I was able to put out a piece of code that was fixing a core of one problem I had and that was it. Once I was done with that, I would move on to the next problem. For a long time now I was missing that feeling of not caring, just fix the damn problem and move on. Just to slap together some crappy piece of code, use it once or twice and then forget about it.

And that was exactly what I did last night. I sat down and in some five or six hours I put together coords. It is an ugly as hell little pygtk application, void of any good practices, no tests, just a few comments here and there and that’s it. And I had fun writing it! I completely lost track of time while hacking, got into the zone and today, after some six hours of sleep I woke up feeling like I was on a vacation for a week.

The application itself doesn’t do much, it helps determine coordinates on your desktop. Start the application, click “track”, drag the mouse from the top-left corner you’re interested in to the bottom-left one and that’s it. The entire functionality is shown in this ten second long gif that runs somewhere here on the page. The best part is that it actually solves a problem I had, it helps me determine coordinates on my desktop and then I can use those coordinates for byzanz-record. I loved every second I spent hacking on this.

Best part is that even this little application had a quite an interesting challenge to solve, namely, to determine the position of the mouse anywhere on the screen. It’s no big deal to determine the position of the mouse inside your application, but once you want to break out of it, well, it gets bit tricky.

With pygtk one can only subscribe to events that happen inside the application itself. To go lower than that one needs to use a different library, something like xlib (python-xlib from python). After much poking around I found a way to do it from pygtk itself. It is possible to get hold of the root window instance, which is created by the X server itself (you can’t create a root window from an application, or make an application be a root window, afaik). Once you have the root window, grab the pointer, and then filter events you are interested in on the root window before they get sent from the X server to gtk. Or at least that is how I understood this whole process. While having control over the pointer, get the mouse coordinates from the time left button is pressed till the time it is released. Don’t forget to ungrab/release the pointer once your done. And that’s all there is to it, more or less.

The interesting parts are:

def start_tracking(self, widget, data=None):
    mask = gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK
    self.root_window = gtk.gdk.get_default_root_window()
    gtk.gdk.pointer_grab(self.root_window, False, mask)
    self.root_window.add_filter(self.track_region, self.region)
def track_region(self, event, region):
    x, y, flags = event.window.get_pointer()
    if 'GDK_BUTTON1_MASK' in flags.value_names \
            and region.track_started == False:
        region.start_x = x
        region.start_y = y
        region.track_started = True
        region.track_ended = False
    if 'GDK_BUTTON1_MASK' not in flags.value_names \
            and region.track_started == True:
        region.end_x = x
        region.end_y = y
        region.track_ended = True
        region.track_started = False
        # ungrab the pointer so we get control back
        gtk.gdk.pointer_ungrab()
        self.show_region_values(region)
    return gtk.gdk.FILTER_CONTINUE

Isn’t it ugly? Very. But it works and it solves the problem I had. Btw you can check out the code on github to have a bit more context for all this.

Happy hackin’!

Tags: byzanz, hack, pygtk, python.
Categories: Development, Programming, Software.

Need help on your PHP projects? Let's talk!