Archive for the ‘Programming’ Category

Yahoo Destined to Screw Up Flickr

December 13th 2008

If Yahoo fucks up Flickr such that it ends up looking like the bloated chunk of webspace the Yahoo main page is, I’m deleting my account.

People don’t think before they do things do they? They really don’t.

Yahoo Layoffs Continue, Flickr Loses Top Designer | Epicenter from Wired.com

Posted by darkhelmet under Intraweb & Programming | No Comments »

Gtk and Ruby threading issues

November 22nd 2008

In one of my classes, we are using Ruby and Gtk, but some issues have popped up. The most obvious is using a block to do GUI update stuff and the like, from another thread. Things die. Puppies are killed.

I found this post on Ruby Forum which fixed the problem.

Relevant code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require "thread"
 
module Gtk
    # Thread-safety stuff.
    # Loosely based on booh, by Guillaume Cottenceau.
 
    PENDING_CALLS_MUTEX = Mutex.new
    PENDING_CALLS = []
 
    def self.thread_protect(&proc)
        if Thread.current == Thread.main
            proc.call
        else
            PENDING_CALLS_MUTEX.synchronize do
                PENDING_CALLS << proc
            end
        end
    end
 
    def self.thread_flush
        if PENDING_CALLS_MUTEX.try_lock
            for closure in PENDING_CALLS
                closure.call
            end
            PENDING_CALLS.clear
            PENDING_CALLS_MUTEX.unlock
        end
    end
 
    def self.init_thread_protect
        Gtk.timeout_add(100) do
            PENDING_CALLS_MUTEX.synchronize do
                for closure in PENDING_CALLS
                    closure.call
                end
                PENDING_CALLS.clear
            end
            true
        end
    end
end

Basically, you call the Gtk.init_thread_protect method first when you start things up, then, whenever you need to do GUI update stuff, just wrap it in a Gtk.thread_protect {} block. Voila. It works. No more crashes. In looking at this code again now, some things could be made more Rubyesque, but we’ll go with it.

My version with minor changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require "thread"
 
module Gtk
    PENDING_CALLS_MUTEX = Mutex.new
    PENDING_CALLS = []
 
    def self.thread_protect(&proc)
        if Thread.current == Thread.main
            proc.call
        else
            PENDING_CALLS_MUTEX.synchronize do
                PENDING_CALLS << proc
            end
        end
    end
 
    def self.thread_flush
        if PENDING_CALLS_MUTEX.try_lock
            PENDING_CALLS.each { |closure| closure.call }
            PENDING_CALLS.clear
            PENDING_CALLS_MUTEX.unlock
        end
    end
 
    def self.init_thread_protect
        Gtk.timeout_add(100) do
            PENDING_CALLS_MUTEX.synchronize do
                PENDING_CALLS.each { |closure| closure.call }
                PENDING_CALLS.clear
            end
            true
        end
    end
end

Posted by darkhelmet under Programming | No Comments »

Randomize wallpaper upon KDE login

October 12th 2008

I wanted my wallpaper to change whenever I logged in, but found no easy way to do this. In KDE you can tell it to slideshow the wallpaper, but that’s not what I wanted. Just, when I log, switch to a new wallpaper. I want to say “pick a file randomly out of the files in a directory and set the wallpaper on all the desktops”

So here’s my ruby script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env ruby
 
require 'rubygems'
require 'getopt/std'
 
module RandomArray
  def random
    self[rand(size)]
  end
end
 
opts = Getopt::Std.getopts("d:n:")
 
raise "No directory given. Use #{__FILE__} -d <directory> -n <number of desktops>" unless opts.include?('d')
raise "No desktop count given. Use #{__FILE__} -d <directory> -n <number of desktops>" unless opts.include?('n')
 
DIR = opts['d']
 
image = Dir.entries(DIR).map do |e|
  "#{DIR}#{File::Separator}#{e}"
end.reject do |e|
  File.directory?(e)
end.extend(RandomArray).random
 
1.upto(opts['n'].to_i) do |i|
  system("dcop kdesktop KBackgroundIface setWallpaper #{i} #{image} 8")
end

I realize python has some dcop stuff and you can sort of just call it, and ruby probably does too, but I couldn’t find good docs on any of that, and I just found the command line thing to call, so I hacked this up in ruby, and it works quite well.

I realize the reject should really be done before the map, and originally had it like that, but then the File.directory? call was more complicated, so I changed it to this.

Make a bash script in ~/.kde/Autostart and add the following.

1
2
3
#!/usr/bin/env bash
 
random-wp.rb -d $HOME/wallpaper -n 4

I have 4 workspaces, hence the 4 at the end. Alter appropriately.

Posted by darkhelmet under Computers & Linux & Programming | No Comments »

App Store Experience of a Developer - Mac Forums

August 3rd 2008

App Store Experience of a Developer – Mac Forums

What would you do in this case? Is there any way to make Apple take notice of me?

Not develop on Apple’s shitty platform maybe?

Fuck iPods…

Posted by darkhelmet under Intraweb & Programming | No Comments »

Pascal case to human readable with C# extension methods

August 2nd 2008

I needed to convert some Pascal case strings to human readable strings. Basically I have an enum containing errors, but wanted a nice way to convert them straight to messages for the exceptions that would be thrown.

Here’s how I did it.

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static string ToHumanFromPascal(this string s)
{
    if (2 > s.Length)
    {
        return s;
    }
 
    var sb = new StringBuilder();
    var ca = s.ToCharArray();
    sb.Append(ca[0]);
    for (int i = 1; i < ca.Length - 1; i++)
    {
        char c = ca[i];
        if (char.IsUpper(c) && (char.IsLower(ca[i + 1]) || char.IsLower(ca[i - 1])))
        {
            sb.Append(' ');
        }
        sb.Append(c);
    }
    sb.Append(ca[ca.Length - 1]);
    return sb.ToString();
}

EDIT: Fixed to handle string of length 0 or 1.

Posted by darkhelmet under Computers & Programming | 3 Comments »

OMG git on Windows

July 25th 2008

Now I can use git on Windows. There is much rejoicing.

msysgit

Getting Started with Git and GitHub on Windows – Kyle Cordes

Posted by darkhelmet under Computers & Programming & Windows | No Comments »

Next »

My wishlist

 Subscribe in a reader
  • Categories

  • Tags

  • Monthly

  • Pages

  • Blogroll

  • Last.fm

  • Einstein@home

  • Word of the Day