Archive for May, 2010

Broken comments

h/t to Dave, the guy who contacted me and mentioned that he was unable to post comments on this blog; I didn’t realize the comments system was not functioning.

Now why the WordPress comment system would go down was a bit confusing and frustrating to diagnose. WP simply sent you to a blank page (wp-comments-post.php#comment-). After ruling out it was not an issue with the theme or a problem with a blank line in wp-comments-post.php (the first 2 cases mentioned here). I tried repairing the wp_comments table in the DB, which didn’t work (the post linked to mentions going further and wiping out the data and recreating the table, but I didn’t want to go that far). I ran across a few posts (such as this one) on the WP forums about similar issues, but nothing that led to a solution.

I decided to dig into wp-comments-post.php and started playing around with a few echo statements to see where things were going wrong. Turns out, wp_new_comment() was always returning a comment_id of 0. Not good. Assuming the WP code was correct (I had no reason to suspect otherwise), it had to be a problem with the database. The problem and solution turned out to be pretty trivial: the comment_ID field of the wp_comments table had the auto_increment attribute missing (which needs to be set; you can see the WP database scheme here). So all new comments were being assigned the default value of 0.

I’m not sure why this would happen; maybe an issue resulting from an upgrade my host did a while back or perhaps an issue with the WP auto-update process.

Making better progress bars

An interesting study I came across a while back, regarding changing the animation of progress bars to alter users’ perception of how long an activity (e.g. a file download) takes to complete. By just changing the animation of the progress bar, an activity can seem 10% faster to users.

… Harrison’s group generated a series of animated progress bars: some pulsated between pale and dark blue at varying speeds, and others had pale blue ripples moving either left or right, also at different rates, as the bar crept forward (see video).

Many participants said that progress bars which pulsated increasingly quickly made the download time seem shorter than those that pulsated increasingly slowly.

A significant number also said downloads were faster when ripples in the progress bars moved to the left rather than to the right.

Note the key phrase, increasingly quickly; the pulsating of the progress bar has to accelerate.

In terms of the ripples, it seems they need to accelerate (I’m assuming, the article wasn’t specific on this point) or decelerate as well. Constant velocity ripples (such as those in OS X progress bars) actually result in a negative effect:

Apple uses left-moving ripples that travel at a constant velocity for progress bars in Mac OS X. However, many of the volunteers in Harrison’s study said that this type of animation appeared slower than one in which the left-moving ripples slowed down as the download neared completion.

Managed wrapper for the BTObex library

More open-source goodness. A managed wrapper for the BTObex library. The source includes the BTObexWrapper.h header file which contains the wrapper code (shown below) and a compiled managed DLL, for those who just want to include it into their projects and use it ASAP.

// Managed C++/CLI wrapper for the BTObex Library
// Include the BTObex library files into the project (BTOBEX sub-directory) and compile as a managed DLL

/*
    Copyright (c) 2010, Avishkar Autar
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright
         notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
         notice, this list of conditions and the following disclaimer in the
         documentation and/or other materials provided with the distribution.
        * Neither the name of the <organization> nor the
         names of its contributors may be used to endorse or promote products
         derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/



#pragma once

#pragma
unmanaged
    #ifndef WIN32_LEAN_AND_MEAN
    
#define WIN32_LEAN_AND_MEAN
    
#endif

    #include <winsock2.h>
    #include <Ws2bth.h>
    #include <BluetoothAPIs.h>

    #pragma comment(lib, "ws2_32.lib")

    
#include <windows.h>
    #include <cguid.h>

    #include "BTOBEX/BTSock.h"
    #include "BTOBEX/OBEX.h"
    #include "BTOBEX/BTOBEX.h"

#pragma managed

using
namespace System;
using namespace System::Runtime::InteropServices;

namespace BTObex
{
    
public ref class BluetoothDeviceProfile
    {
        
public:
            System::String^        name;
            System::String^        address;
// (address):port, typically repeat of device address
            int                    port;
    };

    
public ref class BluetoothDevice
    {
        
public:
            System::String^                            name;
            System::String^                            localAddress;
            System::String^                            deviceAddress;
            System::Collections::ArrayList^         supportedProfiles;

            
virtual System::String^ ToString() override
            {
                
return name;
            }
    };

    
public ref class Progress
    {
        
public:
            System::IntPtr^        current;
            System::IntPtr^        total;
    };

    
public ref class BTObexFileIO
    {
        
protected:
            BTOBEX::Session* btSession;
            BTOBEX::Progress* opProgress;

            
wchar_t* MarshalStringToCStr(System::String^ str)
            {
                
wchar_t* ret = new wchar_t[str->Length+1];

                
for(int i=0; i<str->Length; i++)
                    ret[i] = str[i];

                ret[str->Length] = 0;

                
return ret;
            }

            
void DeleteMarshaledCStr(wchar_t* str)
            {
                
delete [] str;
                str = 0;
            }

        
public:
            BTObexFileIO() : btSession(0)
            {
                opProgress =
new BTOBEX::Progress();
            }

            ~BTObexFileIO()
            {
                
delete opProgress;
            }

            
void QueryDevices(System::Collections::ArrayList^ devices, bool queryOnlyOBEXFileTransferProfiles)
            {
                BTSock::Session* btSession =
new BTSock::Session();

                std::vector<BTSock::Device*> btDevices;
                
if(queryOnlyOBEXFileTransferProfiles)
                {
                    btSession->DeviceQueryObject()->PerformQuery(btDevices, OBEXFileTransferServiceClass_UUID);
                }
                
else
                {
                    btSession->DeviceQueryObject()->PerformQuery(btDevices);
                }

                
for(int i=0; i<(int)btDevices.size(); i++)
                {
                    BluetoothDevice^ dev =
gcnew BluetoothDevice();
                    dev->name =
gcnew System::String( btDevices[i]->name.c_str() );
                    dev->localAddress =
gcnew System::String( btDevices[i]->localAddress.c_str() );
                    dev->deviceAddress =
gcnew System::String( btDevices[i]->deviceAddress.c_str() );
                    dev->supportedProfiles =
gcnew System::Collections::ArrayList();

                    
for(int j=0; j<(int)btDevices[i]->supportedProfiles.size(); j++)
                    {
                        BluetoothDeviceProfile^ profile =
gcnew BluetoothDeviceProfile();
                        profile->name =
gcnew System::String( btDevices[i]->supportedProfiles[j]->name.c_str() );
                        profile->address =
gcnew System::String( btDevices[i]->supportedProfiles[j]->address.c_str() );
                        profile->port = btDevices[i]->supportedProfiles[j]->port;

                        dev->supportedProfiles->Add(profile);
                    }

                    devices->Add(dev);
                }

                
delete btSession;
            }
                

            
bool Connect(System::String^ address, int port, int packetSize)
            {
                
wchar_t* addr = MarshalStringToCStr(address);

                btSession =
new BTOBEX::Session();
                
bool connOk = btSession->Connect(addr, port, packetSize);

                DeleteMarshaledCStr(addr);

                
return connOk;
            }

            
void Disconnect()
            {
                btSession->Disconnect();
                
delete btSession;
            }

            
bool SetPathBackward()
            {
                
return btSession->SetPathBackward();
            }

            
bool SetPathForward(System::String^ folderName)
            {
                
wchar_t* folderNameCStr = MarshalStringToCStr(folderName);

                
bool setOk = btSession->SetPathForward(folderNameCStr);

                DeleteMarshaledCStr(folderNameCStr);

                
return setOk;
            }

            
bool GetFolderListing(System::Collections::ArrayList^ result)
            {
                OBEX::NakedVector<OBEX::byte> utf8Bytes;
                
bool getOk = btSession->GetFolderListing(utf8Bytes);

                
if(getOk)
                {
                    
for(int i=0; i<utf8Bytes.Count(); i++)
                    {
                        result->Add(
gcnew System::Byte(utf8Bytes[i]) );
                    }
                }

                
return getOk;
            }

            
bool DeleteObject(System::String^ objName)
            {
                
wchar_t* objNameCStr = MarshalStringToCStr(objName);

                
bool delOk = btSession->DeleteObject(objNameCStr);

                DeleteMarshaledCStr(objNameCStr);

                
return delOk;
            }

            
bool PutFile(System::String^ remoteFileName, System::String^ localFilePath, Progress^ progress)
            {
                progress->current =
gcnew System::IntPtr( &opProgress->current );
                progress->total =
gcnew System::IntPtr( &opProgress->total );

                
wchar_t* remoteFileNameCStr = MarshalStringToCStr(remoteFileName);
                
wchar_t* localFilePathCStr = MarshalStringToCStr(localFilePath);

                
bool putOk = btSession->PutFile(remoteFileNameCStr, localFilePathCStr, opProgress);

                DeleteMarshaledCStr(remoteFileNameCStr);
                DeleteMarshaledCStr(localFilePathCStr);

                
return putOk;
            }

            
bool GetFile(System::String^ remoteFileName, System::String^ localFilePath, Progress^ progress)
            {
                progress->current =
gcnew System::IntPtr( &opProgress->current );
                progress->total =
gcnew System::IntPtr( &opProgress->total );
                
                
wchar_t* remoteFileNameCStr = MarshalStringToCStr(remoteFileName);
                
wchar_t* localFilePathCStr = MarshalStringToCStr(localFilePath);

                
bool getOk = btSession->GetFile(remoteFileNameCStr, localFilePathCStr, opProgress);

                DeleteMarshaledCStr(remoteFileNameCStr);
                DeleteMarshaledCStr(localFilePathCStr);

                
return getOk;
            }
    };
}

Be sure to use a #2 pencil

… or not:

The #2-pencil requirement is mostly historical. Because modern scantron systems can use all the sophistication of image sensors and computer image analysis, they can recognize marks made with a variety of materials and they can even pick out the strongest of several marks. If they choose to ignore marks made with materials other than pencil, it’s because they’re trying to be certain that they’re recognizing only marks made intentionally by the user. Basically, these systems can “see” most of the details that you can see with your eyes and they judge the markings almost as well as a human would.

from how everything works.

BTObex Library

I’ve released the BTObex library (I’m playing around with using bitbucket for hosting the code) I wrote a few months ago which is a Win32 bluetooth library that allows for file transfers with devices that support the Bluetooth File Transfer Profile. This library uses the MS Bluetooth stack (included w/ Win XP SP2 and later).

For those wondering what OBEX is and what Bluetooth has to do with it: Bluetooth devices support a set of profiles, one of which is the File Transfer Profile. However, the FTP itself doesn’t really take care of anything much, once the connection is established, actual file transfers are handled by the OBEX protocol.

Bluetooth FTP stack

OBEX was originally designed for transfers across infrared devices and is not handled by the Bluetooth SIG, its specs are controlled by the IrDA. Now, aside from being a useless bit of trivia, this is important because while the Bluetooth SIG publishes a ton of documentation, the IrDA does not. The documentation on the specs for OBEX requires signing up for IrDA membership and paying some hefty membership fees. That said I was able to get my hands on a copy of the OBEX documentation here. How long that remains up and the legality of me reading it is up in the air. Charging for documentation is a shitty practice by the IrDA and I can only hope that the Bluetooth SIG ditches OBEX for something better in the future.

There’s little-to-no documentation for the library, so I’m going to use this blog post to provide some examples.

Link with necessary libraries

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "irprops.lib")

Include header file

#include "BTOBEX.h"

The entire library simply consists of a bunch of header files. BTOBEX.h will include everything else.

Query all Bluetooth devices

std::vector<BTSock::Device*> btDevices;
BTSock::Session* btSession =
new BTSock::Session();
btSession->DeviceQueryObject()->PerformQuery(btDevices);
delete btSession;

The vector btDevices will contain an entry for each device paired with the system. Look at BTSockDevice.h to see the structure of the BTSock::Device class.

Make a connection

BTOBEX::Session* btObexSession = new BTOBEX::Session();
bool connOk = btObexSession->Connect(devAddress, port, 8192);

devAddress is the deviceAddress member of a BTSock::Device object.
port is the port number for a given profile, for the file transfer profile it’s 5. If you can’t connect on the port, it likely means that the profile is not support by the device.

Set the current directory on the device

bool setPathOk = btObexSession->SetPathForward(L"my_picture");

OBEX only allows you to move forward into a subdirectory (SetPathForward) or jump back to a parent directory (SetPathForward).

Get a directory listing

OBEX::NakedVector<OBEX::byte> xmlBytes;
btObexSession->GetFolderListing(xmlBytes);

The bytes are UTF-8 bytes that form a XML document (an XML parser is not part of the library). The XML doc will have folder and/or file elements, each with a required name attribute. Directory listings are one of the dumbest aspects of the OBEX protocol; whereas everything else is binary and well-defined, directory listings are verbose and text-based (remember, this is a low bandwidth connection) and have no required attribute aside from name. Other attributes (e.g. size) may be present, but they’re optional, meaning, in general, you’ll likely avoid them in order to develop for the lowest common denominator.

Get a file from the device

bool getOk = btObexSession->GetFile(L"remote.jpg", L"local.jpg", new BTOBEX::Progress());

The last parameter is optional, you can pass a BTOBEX::Progress object if you wish to poll the progress of the transfer from another thread.

Put a file on the device

bool putOk = btObexSession->PutFile(L"remote.jpg", L"local.jpg", new BTOBEX::Progress());

The last parameter is optional, you can pass a BTOBEX::Progress object if you wish to poll the progress of the transfer from another thread.

Disconnect and clean up

btObexSession->Disconnect();
delete btObexSession;

Have fun! The code is released under the BSD license. Any comments or bug reports are greatly appreciated.

The world’s only immortal animal

The turritopsis nutricula species of jellyfish:

immortal jellyfish
Photo by Peter Schuchert

Since it is capable of cycling from a mature adult stage to an immature polyp stage and back again, there may be no natural limit to its life span. Scientists say the hydrozoan jellyfish is the only known animal that can repeatedly turn back the hands of time and revert to its polyp state (its first stage of life).

The key lies in a process called transdifferentiation, where one type of cell is transformed into another type of cell. Some animals can undergo limited transdifferentiation and regenerate organs, such as salamanders, which can regrow limbs. Turritopsi nutricula, on the other hand, can regenerate its entire body over and over again. Researchers are studying the jellyfish to discover how it is able to reverse its aging process.

Embedding WebKit in a WinForms application

This is how to embed a WebKit instance within a WinForms application. I promised, a long time ago, to show this, but never got around to it. The process itself is pretty simple, assuming you have WebKit compiled (which can be pretty difficult) because you’ll need to be able to link with the WebKit libraries, specificially: WebKit.lib and WebKitGUID.lib (which, of course, have dependencies on all of the other libraries produced in the compilation). I’m going to be using a older version of WebKit, the same one I played with in my previous blog posts, as I don’t have time to check out the newest version of WebKit. Hopefully, the interfaces are the same or similar and what I’m about to describe here can be adapted easily to work with the latest version.

What we’re going to do is create a CLI wrapper to allow the .Net application to access the native WebKit code. To mix managed and unmanaged code, we’re going to need to create a C++/CLI application. Yes, it is going to be ugly code, but it works and (I think) much simpler than trying to P/Invoke functions in WebKit.dll.

Here’s the wrapper code (I’ve also included a compiled dll in the /Release folder):
WebKitCLIWrapper.dll.zip

WebKitNativeController.h and WebKitNativeController.cpp is adapted from the demo code provided by Apple. WebKitCLIWrapper.h (shown below) is the C++/CLI .NET code that wraps the native controller.

#pragma once

#include
"WebKitNativeController.h"

namespace WebKitCLIWrapper
{
    
using namespace System;
    
using namespace System::Collections;

    
public ref class Wrapper
    {
        
public:
            Wrapper(IntPtr hwnd, String^ htmlContent)
            {
                webKitController =
new WebKitNativeController();

                IntPtr bstrPtr = System::Runtime::InteropServices::Marshal::StringToBSTR(htmlContent);
                webKitController->Init((HWND)(
int)hwnd, (BSTR*)&bstrPtr);
                System::Runtime::InteropServices::Marshal::FreeBSTR(bstrPtr);
            }

            ~Wrapper()
            {
                webKitController->UnInit();
                webKitController->Release();
            }

            
void LoadURL(String^ url)
            {

                IntPtr bstrPtr = System::Runtime::InteropServices::Marshal::StringToBSTR(url);
                webKitController->LoadURL(*((BSTR*)&bstrPtr));
                System::Runtime::InteropServices::Marshal::FreeBSTR(bstrPtr);
            }
            
        
protected:
            WebKitNativeController*        webKitController;
    };
}

Now, we can simply include WebKitCLIWrapper.dll in any .Net project as a reference and access the wrapper to create a WebKit instance. So if your in Visual C#, create a new WinForms app, create a Panel (panel1) on a form, and add the following to the form’s Load event:

WebKitCLIWrapper.Wrapper wrap = new Wrapper(panel1.Handle, "");
wrap.LoadURL(
"http://arstechnica.com/");

However, before running the application, you need all the necessary libraries and necessary resources referenced by WebKit.dll in the app’s working directory (I like to set it to Debug).

For convenience, here’s a zip containing all you’ll need:
WebKit_dlls+resources.zip
(as I mentioned, this is an older version of WebKit)

With all the necessary files in place, launch the app, and a WebKit instance should load in the specified panel (panel1) along with the web page specified in LoadURL() call.

arstechnica.com loaded in a webkit container within a WinForms application

If you’d like to display and process a specific piece of HTML code, get rid of the LoadURL() call, place the code to be embedded in a string, and pass it as the 2nd argument to the WebKitCLIWrapper.Wrapper constructor. For example, this will load and embed a youtube video:

string embed = "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/SuBqIrgKnNg&hl=en&fs=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"http://www.youtube.com/v/SuBqIrgKnNg&hl=en&fs=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object>";
WebKitCLIWrapper.
Wrapper wrap = new Wrapper(panel1.Handle, embed);

cookie monster npr interview, displayed in a webkit instance with a WinForms panel

The wrapper interface is very bare-bones, but it’s good enough if you just need to display some web or HTML content in your app or, if your looking to build a .NET web browser, it will hopefully provide a starting point for how to interact with WebKit.