Categories
PC Nerding

Raspberry PI X11 forwarding with Cygwin

After my little Raspberry PI Mono installation accident, that fucked up everything with Mono, I decided to reinstall Raspbian Wheezy. I know a lot of people that love touchy and clicky interfaces, the tablet market rise reflect this. I personally use my RPi through command line, SSH it over LAN, because I one Desktop environment (my Windows machine) is more than enough for me.
But after a couple mails with a friend trying to view his PI’s lxsession on his Windows machine and explaining him my setup using Cygwin only, he told me there’s no tutorial on this. Nothing new or super cool, just another way.
This guide should work ok with any linux environment, not only RPIs, provide you properly set ssh configurations.

The first thing to do is downloading Cygwin installer, be sure to add xorg, xint and openssh packages while installing. There may be some other needed packages, if you get somenthing like a “command not found”, you have to add that package too.

Now open a Cygwin shell with its nice desktop shortcut and type:
cygstart xwin
cygstart tells Cygwin to run a command and give you back your shell control. Removes the need for two shells (one for launching the Xserver and one for SSH connection). It can be omitted.
xwin Launches the XWin application, which is our Xserver
This is usually enough, but as default setting, xwins create a window big as my current resolution. I prefer using this command:
cygstart xwin -screen 0 1440x900@1 -wgl
Fast explanation:
-screen 0 1440×900@1 tell xwin to create the remote screen window number 0, with a resolution of 1440×900 at monitor 1. Use this parameter as this, modifying only the resolution part. See Note 1 for better explanation.
-wgl will enable OpenGL accelerated graphics on the remote screen window. It is not mandatory and if not enabled the remote screen window will use software rendering.

After that we must make sure that Cywin is aware of this remote display, it can be done by setting and exporting the environment variable DISPLAY.
The newly created window will have name like: Cygwin/X:0.0, make sure to set this variable to whatever is written after the X, like
export DISPLAY=:0.0

Now, using Cygwin, SSH to your PI
ssh -Y pi@address
and after logon, start you windows manager:
lxsession
This should show your Raspberry PI desktop inside your XWin window.

Final notes:
If you want to do all of this stuff at cygwin’s startup, just add the lines
cygstart xwin #+your parameters
export DISPLAY=:0.0

at the end of cygwin\home\user\.bashrc.

Note 1:
See XWin manual for possible parameters.
-screen 0 1440×900@1 explanation:
0 Create windows number 0. It’s like an ID to identify which windows is this. If you want to use multiple remote screens, for each new screen you have to increment this value by one.
1440×900 Is pretty self explanatory. Sets the PI’s max resolution to this size.
@1 Tell XWin on which (real) monitor you want the window to appear. So in case of a multiple monitor setup 1 is the main monitor, while 2, 3, 4, …, are the other monitors. If you have a single monitor this value is always 1.

Categories
PC Nerding

Shortening urls using htaccess

I was looking for a solution for shortening urls for both blog and forums on my indie website Acid.vg. I know there are a lot of shortening services on the web, for both hosted and self-hosted. Both have pros and cons. But the real questions were: Do I really want to send my data to someone else? Do I really want to use more hosting and database space for a self hosted app? Do I really want to keep this app updated?
The answer to all those questions was: NO. Acid.vg is a pretty short domain name, not 4 chars long only like Twitter’s t.co, but short enough to keep his urls short with a little hack and the power of Apache’s mod_rewrite. This will still require a self hosted environment with .htaccess support and an installed cms that identifies its content by IDs (more details as you read).

Acid.vg have a blog cms (WordPress, installed in root / folder) and a forum cms (phpBB3, installed in /forum/ folder), the tutorial splits in two parts, covering both. The same procedure may be adapted for any other cms.

WordPress

On default settings WordPress access his contents (blog post, page, media) through their IDs like mysite.com/?p=ID. Even with seo friendly permalinks on, WordPress still allow to access posts by using ID, we will take advantage from this feature for our hack.
Acid.vg uses Twenty Twelve child theme, this tutorial will assume you are using Twenty Twelve (or a child) but the procedure may be similar with other themes too.

First of we have to decide where to put this shortened link, in my case I went for the entry-meta footer that is shown after each post. This is done by twentytwelve_entry_meta() function found in theme’s functions.php, you can edit it from here (losing changes in case of theme update) or you can copy it in your child theme’s functions.php. An hook filter here would be cooler, but look like its the only way if you want the shorter link to show here.
After copying the whole stuff, add this single line right before the function’s trailing bracket:
printf('<a class="shorter-url" rel="nofollow" href="http://mysite.com/s/%s">Shorter Url</a>.', get_the_ID() );
This function will add the shortened link as http://mysite.com/s/%s, where %s is the post’s ID value we got from WordPress function get_the_ID(). (eg: http://mysite.com/s/1)

Done? Not yet. This only told WordPress how to shorten links using post IDs but it didn’t tell your hosting server how to handle those shortened urls.

Now open your favourite FTP program and navigate to your site’s root, here you will most likely find an .htaccess file, if not create it using your favourite text editor. Add this code to your file. If the file was not empty, make sure to put this code at top.
# BEGIN ShortUrls
<IfModule mod_rewrite.c>
RewriteEngine On
#---TEXT GOES HERE---
</IfModule>
# END ShortUrls

(I will not bother you telling what all this stuff is about, a quick google search will tell you everything.)

Now, delete the entire line were I put TEXT GOES HERE, and paste this stuff inside
RewriteCond %{REQUEST_URI} ^/s/$
RewriteRule ^s/$ http://www.mysite.com/ [L]
RewriteCond %{REQUEST_URI} ^/s/([0-9]+)$
RewriteRule ^s/([0-9]+)$ http://www.mysite.com/?p=$1 [L]

Let’s see what this code does: Read the line in pairs, basically they tell you if RewriteCond condition is met mod_rewrite will rewrite the url using the specified RewriteRule
Line #1 and #2:
Since the shortened url make use of the posts IDs, if this ID is missing (RewriteCond searches for “mysite.com/s/”), we only redirect the browser to the main site.
Line #3 and #4:
If the url ends with a number (eg: mysite.com/s/34), take the numerical part ([0-9]+) after /s/ and point the browser to mysite.com/?p=ID (eg: mysite.com/?p=34).

Now we’re done.

Will the site loose his fancy SEO permalinks?
Absolutely no! WordPress will then take care of loading the correct post and, if permalink are enabled, printing the correct (longer) permalink in the browser address bar.

But this method only shortens the links by 1 char compared to ?p=ID method! I know, mysite.com/?p=1 and mysite.com/s/1 are respectively 15 and 16 chars long. But this is the case only when wordpress is on root. When it is installed on /blog/ or, worse, /wordpress/ folder, the difference between mysite.com/s/1 and mysite.com/wordpress/?p=1 becomes obvious.
Read the phpBB3 part to better understand what I mean.

phpBB3

In my current setup PhpBB3 runs from a subfolder /forum/ and it shows post through a php page viewtopic.php?p=ID, making the full url very long mysite.com/forum/viewtopic.php?p=ID. Having this url short as mysite.com/f/ID would be awesome!

Like we did with WordPress we have to show those shortened ID somewhere in the forums. The best place to put this link is inside viewtopic_body.html, right before or after the topic title. In most themes you can find a line similar to this one
(Note: you don’t have to scan for the entire document, it is usually found in the first 5 lines)
<a href="{U_VIEW_TOPIC}">{TOPIC_TITLE}</a>

Next to this that you can paste something like:
<a class="shortlink" rel="nofollow" href="http://mysite.com/f/{S_TOPIC_ID}">Shorter Url</a>
This will make the post link much shorter, will not work on his own.
Get back to your .htaccess file and paste this code, right after the previous Rewrite codes.
RewriteCond %{REQUEST_URI} ^/f/$
RewriteRule ^f/$ http://www.mysite.com/forum/ [L]
RewriteCond %{REQUEST_URI} ^/f/([0-9]+)$
RewriteRule ^f/([0-9]+)$ http://www.mysite.com/forum/viewtopic.php?p=$1 [L]

Like previous codes
Line #1 and #2:
Will tell browsers to load the forum main page when the browser is trying to load a shortlink with no id.
Line #3 and #4:
Will send browsers to the correct link http://www.mysite.com/forum/viewtopic.php?p=ID.

Here you can see a big difference: mysite.com/forum/viewtopic.php?p=1 is 34 characters long, while mysite.com/f/1 is only 14, 20 characters less.

Do I have to use mysite.com/s/ and mysite.com/f/? No, you can use whatever you want like b/ or s-, just make sure your RewriteConds and RewriteRules use the correct regular expressions.

Categories
Game Development PC Nerding

Raspberry PI and Random values

I’m developing a simple rpg game, testing it with .NET on Win and Mono on Win/Mac/Linux works perfectly. Today I decided to test it on my Arch Raspberry PI to see if it could run well on this device and the result is that the game won’t even start for some “Array out of index” exception that does not appear with other systems.
After some time debugging here and there, I found out that the Random.Next function does not behave correctly on RPi.
Running this test code:
Random r = new Random();
for (int i = 0; i<10; i++) { Console.WriteLine(r.Next(0,7)); }

on the RPi prints ten times the number seven!
This is wrong for two reasons: First obliviously it's not returning random numbers and secondly it is returning the maxValue, which should be "exclusive" and it may never be returned by the function.
So calling something like
array[random.Next(0,array.Length)]
should return a value between 0 and array.Length-1, but it returns array.Length causing my out of index exception.

I tried testing Random.Next(), Random.Next(int) and Random.NextDouble() and they all behave correctly. I dunno if this is a bug in Mono:ARM or something related to the Raspberry PI.

UPDATE: Further investigations lead me to think that the problem was actually Math.Round(double), which is called in Random.Next. I'm not sure I understood it completely but looks like it is a well know problem: Mono:ARM does not support PI's hard float OSs, causing errors while working with floating point numbers. There's a patch for that and even an official RPi Debian distro with soft float support, which I won't bother installing right now. Next time PI, next time.

UPDATE: 3 April
Found an hard float version of Mono here on official Pi forums. I tested it on a clean upgraded dist-upgraded Raspbian "wheezy" from 2013 Feb 09 image and it works, remember to expand the root filesystem through raspi-config or to uninstall unneeded packages as wheezy's root partition is 2gb only and is filled with junk stuff (at least junk for me, as I use my RPI via command line SSH). On my Arch setup for some reason it was not working, but an user reported in that thread that it works on his Arch, so I think was my fault, probably some file from my previous mono test that wasn't deleted or overwritten.