FWIW, I had implemented this a few years ago with my (long defunct) Linux-D3D project. I've attached what I had.
It's written in C++, but converting it to C shouldn't be too difficult. It hasn't had any real testing, so I can't say how good of an implementation it is (I only implemented it to satisfy a simple demo app), but perhaps it may be a good starting point.