Skip to content | Skip to navigation
< technical journal entry below >
For about 4 years I've been obsessed with X Window System programming on Linux, specifically in the window manager space. The window manager is a special application which manages windows providing titlebars, buttons, taskbars and menus.
Now I don't do anything revolutionary in this area, I do my tinkering and I get immense pleasure from it. In early 2000 I believe it was, I discovered the aewm window manager. This little gem is written in C and was less than 3000 lines of code. I studied it, studied it again and again and then converted it to C++ changing stuff here and there. That resulting program I called aewm++. From a minimal window manager stand point I think it works pretty damn well and adds some missing things not found in aewm. I use this program everyday as my graphical user interface. It runs well at 400 mhz which is the speed of my box. However, the code is showing it's age and it also reveals how little I knew over the last three years about how to write proper code. I've been told before that I judge myself too hard, however we are our worst critics and sometimes it's a good thing that we are! Always striving to be better!
Anyway it seems to me that my brain just doesn't do well with native code. The language constructs are arcane and complex. I make tons of mistakes and spend hours laboring over stupid little problems.
I've been dreaming for about 2 years now for a higher level language to write a window manager in. Before I went to Korea I started on a Java JNI (Java Native Interface) wrapper around the X Windows C Library. I got pretty far and actually had part of a window manager coded. I was really stoked about having a nice highly object oriented language to describe a window manager in. This code never got finished because I had to go to Korea for a year and it got put on the back burner. I then forgot about it and when I got back the code no longer worked with the latest version of the Java Development Kit. There was too much code to go through to figure out what had changed and why it didn't work anymore.
So I went back the drawing board, chatted with my online friends in the #sandbox on irc.freenode.net and kept hinting about the possibility of coding another window manager. However there was one thing keeping me from making the plunge back into the code. That hurdle was the language. I really didn't want to do it in C++ or C. I gave in, coded a little in C then scrapped that and coded some in C++. The language choice was the huge problem. I just didn't want to use any of those languages, they just feel like they get in my way. I can't describe the program the way I want to with the syntax they provide me. I started taking notes, pages of them actually. I seem to think about this stuff at least 4 hours a day. I keep running through these scenarios in my head about how I'd tackle the different problems associated with completing a program like this.
I kept doing my research hoping that I'd find a binding to some other high level language that I could finally code this project in. Well that day still hasn't materialized. So on a whim I decided to try out C# on Linux ( go-mono.com ). I had been using .NET on Windows for a while coding some webpages. I was familiar with the language but not proficient. I started tinkering with trying to call native code from it. After a couple hours I discovered that for most things it was rather easy. I thought to myself, this just might work. I set a goal for myself, a few weeks ago I coded a C application that created a window and painted the current time and date in it. Not a very hard application to code in C, but a good challenge to see if I could figure out how to call enough native C code functions to write the logic in C#. It took me a week or so to work out all the issues. This time also involved learning alot about how C# can interface with native code. I also was quite rusty on the language constructs. So I set myself a goal and I set out and I reached it. On a scale of one to ten I would say my overall satisfaction level is ten! The application is coded entirely in C# with native methods that wrap the C low level functions found in the Xlib.
That brings us to today. Nobody that I can find is doing anything like this in C#. There are people out there coding applications using GUI toolkits like GTK# and QT# but nobody is coding raw Xlib in C#. I'm pretty stoked about this and that it's working extremely nicely and fast. My machine is just a 400mhz machine and I can't see any difference between my C# code and my C code. Since the clock is fully functioning and for all intents and purposes finished the next test is coding the aewm window manager in C#. If I can pull this off then I have what I've been looking for for 2 years, a language with high level constructs and that's completely object oriented to describe my GUI dream apps!
For the curious the code is on my website. The code is still rather small and I'm trying to make it as clean as possible. The glue to make it work in C# is written in C. If you look at the code I think you'll see that I've tried to keep it as simple as possible so to keep hard to find bugs at the absolute minimum.
For those that want to know how to call C code from C# I'll give some examples. C# uses a technology called Platform Invoke by which C# can call native code. Say for instance you had a function in C that looks like this:
#include <stdio.h>
/* My super cool C function */
void myfunction(char* str) {
printf("%s\n", str);
}
On a linux/unix box you'd compile this code as a shared library. Then you'd define a function prototype in your C# code like so:
using System;
using System.Runtime.InteropServices;
public class Test {
[DllImport("mysharedlib.so")]
public static void myfunction(string str);
public static void Main()
{
myfunction("Hello,World");
}
}
Now your resulting C# app can call down to C code. This example is extremely easy and for probably 80% of the time you'll have an easy time wrapping C functions. It becomes more tricky when you want to pass arrays and structures back and forth between C and C#. But if you design your API correctly you can minimize this.
So in me reaching that goal of having a more descriptive language to write my graphical user interfaces in, I have to make a sacrifice by writing just a little more C code. However, once that code is done then I'll be able to focus once and for all on coding the app I want instead of getting tripped up with 20 year old language constructs and methodologies.
So in the future I'll add more and more too it, continually monitoring the feasibility of reaching my ultimate goal. I'm almost certain that there is a solution to this out there. Wether it's the most sane is yet to be determined. If you are interested in anything I've been talking about feel free to contact me.
The proverbial screenshot can be seen here of aewm++ and the brand new C# based clock app.
This post was edited by majic on Jan 23, 2005.
Nisha, I'm guessing the reason why you cannot do this in C is because when C was invented, Windows 95 wasn't out yet, much less Visual Studio.
What Majic is doing is the reverse, or using C code dlls in his C# program. Nicely done Majic! You'll have to let me know how you managed to produce a dll from C code and where to put it for Visual Studio to find.
That's cool. I had heard of that capability before, but never put it to the test. In "theory," all that is necessary is to find the proper converters, and any language is possible to utilize in C# code. I'm guessing Java converter would be a popular pick.
I'm happy to hear it is virtually just as fast. Have you done complicated programs in this manner? I don't know how comparable 3000 lines of code is to a 3000-line C code with a C# wrapper, but it must be slower (even if undetectably so).
If the world should blow itself up,the last audible voice would be an expert saying it can't be done
I wanted to know how i can call a C# DLL from C...
I searched alot in google , but couldnot find anything on it...
I'm not sure what you mean and I know next to nothing about C#, but if you're trying to access COM/.NET objects from C, you didn't find anything on Google because it's next to impossible (or at least several weeks of painful work).
The best solution in this case would be a wrapper DLL which accesses the C# DLL and exports C-style functions for your program. I know this is possible in C++ and Delphi, I don't know about other languages.
Alternatively you could write a .NET-capable program that provides access to the DLL via a pipe or a socket.
Good luck!
"God is dead." - Nietzsche, 1882 "Nietzsche is dead." - God, 1900
This post was edited by null on Aug 18, 2005.
(as your site gave a 404)
Yeah it's no longer there. I took the blog down ages ago, I am working on my own custom blog software to put in it's place.
I abandoned the C# Xlib wrapper because it was much easier to do the same stuff using Ruby. C# has a nice interface to C dll's through the P/Invoke facility but when I discovered how simple Ruby's interface was to C I scrapped the whole C# thing. My Ruby Xlib extension is light years beyond anything I did in C#. If you want to see the C# stuff I can try to dig it up tonight and put it back on my site. Let me know.
I already ported the calls I needed and created a small class for it.
Thankfully C#'s P/Invoke mechanism is easy enough to use in order to call C dll functions. Secondly the Xlib functions are fairly straight forward to wrap. If you take a look at my Ruby stuff you should be able to see that most functions are more less a one to one mapping with little complex data passing. The structure of my Ruby Xlib wrapper could very easily be ported to C#.
The ruby stuff is at http://frankhale.org/ruby_window_management-12June2005.tar.gz
Sorry there is no documentation yet, if you have any questions about any of the code or you need something explained then just let me know. I got so wrapped up in writing the code that I just never took the time (like I never had the additional time) to create docs for it.