Cocoa UI on terminal app - c

I've been tasked to build a Cocoa interface for a C terminal application.
There are no requirments other than the GUI.
My question is:
Would it be best and/or fastest to make the terminal calls or get source code (which I do have authorization to use) and call the functions directly from Cocoa?
Please state pros and cons for educational purposes.

It would be best (faster, easier) to call the functions directly from Cocoa.
Of course you realise that Cocoa apps must be Sandboxed if they are going on the Mac App Store and Sandboxing can make many, previously trivial, operations much more complicated?

Related

D-Bus API or C library to control firewalld

I'm working on a project, implementing everything in C language. As a part of the project, we need to be able to control and configure firewalld, firewall of the current system.
firewalld is implemented in Python and an interface is available. However, we don't want to make Python calls from C or vice versa.
There are command line tools to configure firewalld (e.g. firewall-cmd) but we don't want to make such calls from C either.
I recently started working on firewalld, I don't know much about its internals. I've read that it uses D-Bus, I also don't know much about D-Bus.
There is a C library developed by Thomas Woerner: libfirewall.
However, it's been more than a year and a half since the last commit so it's not maintained. Other than libfirewall, I don't know any firewalld interface in C.
I gave libfirewall a shot. It got me some problems when both compiling and running the examples and I still have problems to resolve. Is it worth to continue with libfirewall? Should I use it?
Is there any other interface that I'm not aware of?
Possibly naive question due to lack of understanding of D-Bus: I thought, maybe, with a D-Bus interface, I can issue commands to firewalld. Can it be done? (i.e. Does D-Bus work like that?) Can we write a program that mimics, say firewall-cmd, and interacts with D-Bus in the same way and at the end allows us to control firewalld?
If this is possible, how to do it and what to use? libdbus and GDBus have relatively good documentation although libdbus requires good deal of effort. They even said "If you use this low-level API directly, you're signing up for some pain." in the documentation. In any case I'll be in need of examples or any kind of text demonstrating their usage.
How should I approach this problem?
Yes, you can issue commands to firewalld via D-Bus. I haven't checked but expect that firewall-cmd is itself implemented as a D-Bus client.
The D-Bus API is extensively documented: https://firewalld.org/documentation/man-pages/firewalld.dbus.html. The documentation should give you a rough idea what can be accomplished through the API. You could try the D-Bus debugger d-feet to interact with firewalld without any code.
GDBus is definitely the easiest way use D-Bus from C but it's still not trivial and firewalld is a fairly complex API: Using it may require some expertise (completely depending on what you need to do).

How Can I control another programs?

my question is general for all languages, but I'm using only C, so, I would like to get answer in this one.
My question is, How Can I control the behavior of another applications that I didn't write?
For example:
How Can I fill this entry box (in
this site) using C? Do I need to
control the browser? (I'm using
firefox.)
Still in this example, How Can I open
another tab in this browser using C?
(see, I want to control this
application such another one)
How Can I embed a program im my one?
How Can I fill a database program using your gui, doing it by c?
and so on...
Thanks a lot!
There's no concrete or single answer to your (multiple) questions because every program varies. Short of the desired application having an API, you can resort to using low level Win32 commands to identify handles of processes and windows within those processes which you want to change/read. It's by no means a straightforward or scalable process though.
You'd have to interact with that programs API.
Some programs are scriptable and publish APIs that allow other programs to send commands to them. If your "target" app does something like this, then that will typically be your best bet.
If the app doesn't have a C API, then you probably can't control it in C. That doesn't mean it's impossible, only that you might need a different language. If your app is a Windows GUI app, you can use AutoIt to interact with the GUI programmatically.
Typically you use a program's application programming interface (API) to gain access to publicly available functions that let you accomplish tasks within that program. However, not every application has an API that you can use.
You should start by looking at the documentation of the application you're intending to take control of with your program and seeing if they have an API suitable for your needs.
Most of the time application need API to work with each other.
There is another way around to automatise actions done to applications (like an user would). For example on Windows there is an "language" named AutoIt which can interact with your computer.
I'm not really sure if this is what your seeking but it can do almost all the thing you asked for.
question is, How Can I control the behavior of another applications that I didn't write
Answer: that depends on the application and platform (linux, windows, mac, ...). It generally does not depend on your language of choice.
As an example, quite a lot of gtk/ kde programs on linux can be partially controlled via the dbus messaging bus. Those apps are designed to be controlled in that way.
I think firefox has a command line option to open a new website using an already running browser.
Applescript or automator on a mac can be used to control some apps too, I believe.
In short, make a separate question on what you want to do exactly, stating both platform and app you need to control.

How can I best deploy a web application written in C?

Say I have fancy new algorithm written in C,
int addone(int a) {
return a + 1;
}
And I want to deploy as a web application, for example at
http://example.com/addone?a=5
which responds with,
Content-Type: text/plain
6
What is the best way to host something like this? I have an existing setup using Python mod_wsgi on Apache2, and for testing I've just built a binary from C and call as a subprocess using Python's os.popen2.
I want this to be very fast and not waste overhead (ie I don't need this other Python stuff at all). I can dedicate the whole server to it, re-compile anything needed, etc.
I'm thinking about looking into Apache C modules. Is that useful? Or I may build SWIG wrappers to call directly from Python, but again that seems wasteful if I'm not using Python at all. Any tips?
The easiest way should be to write this program as a CGI app (http://en.wikipedia.org/wiki/.cgi). It would run with any webserver that supports the Common Gateway Interface.
The output format needs to follow the CGI rules.
If you want to take full advantage of the web server capabilities then you can write an Apache module in C. That needs a bit more preparation but gives you full control.
Maybe this tiny dynamic webserver in C to be used with C language can help you.. it should be easy to use and self-contained.
Probably the fastest solution you can adopt according to the benchmarks shown on their homepage!
This article from yesterday has a good discussion on why not to use C as a web framework. I think an easy solution for you would be to use ctypes, it's certainly faster than starting a subprocess. You should make sure that your method is thread safe and that you check your input argument.
from ctypes import *
libcompute = CDLL("libcompute.so")
libcompute.addone(int(a))
I'm not convinced that you're existing general approach might not be the best one. I'm not saying that Apache/Python is necessarily the correct one but there is something compelling about separating the concerns in your architecture being composed of highly focused elements that are specialists in their functions within the overall system.
Having your C-based algorithm server being decoupled from the HTTP server may give you access to things like HTTP scalability and caching facilities that might otherwise have to be in-engineered (or reinvented) within your algorithm component if things are too tightly coupled.
I don't think performance concerns in of themselves are always the best or only reasons when designing an architecture. For example the a YAWS deployment with a C-based driver could be a very performant option.
I have just setup a web service using libmicrohttpd and have had amazing results. On a quad core I've been handling 20400 requests a second and the CPU is running only at 58%. This is probably going to be deployed on a server with 8 cores, so I'm expecting much better results. A very simple C service will be even faster!
I have tried GWAN, it is very good, but it's closed, and doesn't play well with virtual environments. I will give #Gil kudos being good at supporting it here though. We just had a few issues and found LibMicroHttpd works better for our needs.
If you go here, you may need to update your openssl if you're using CentOs from axivo
rpm -ivh --nosignature http://rpm.axivo.com/redhat/axivo-release-6-1.noarch.rpm
yum --disablerepo=* --enablerepo=axivo update openssl-devel
You can try Duda I/O, it only requires a Linux host: http://duda.io

Approaches to a GUI for a Large C Program

In our Bioinformatics lab we've recently been asked to create a GUI for a program written (and optimized) in C. Any GUI we designed would need to be able to feed input to and receive output from the C program, while also being easily portable to both Windows and Mac. What are ways to go about doing this?
If your looking for a GUI toolkit that works on windows/mac in C, have you considered GTK?
Download QT or wxWidgets.
Why do the hard work when someone already has and will let you use it for free? I prefer QT myself, btw.
Edit: It does mean using C++ but it will work perfectly with C code.
Use a separate program.
It doesn't break the existing code - especially important if the code has bio knowledge that a programmer might not know/understand/test.
Gui's change often, in a couple of years you are going to be rewriting the gui for NewSuperOsToolkit(tm) but the underlying worker code won't change. We have atmospheric modelling code that I'm sure was originally written in latin.
You keep the ability to run the engine code as a batch, in parallel on MPI in the cloud, and a bunch of other ways you haven't thought of.
You could write the front end GUIs in Java, and have them feed input and receive output from your C programs. I've known a couple of groups here at work that do something similar with C# and C code (but they don't have the multi-platform restraint).
This way you don't necessarily have to create your GUIs using a C toolkit.
Depending on the nature of the program, you could create an entirely separate GUI application (in any language you prefer), and fork/execute the existing program from it, redirecting it's stdin and stdout to your GUI program. Depending on how the existing program works, this could work well, or be very cumbersome.
You could also extend the existing program with GUI code in something like GTK (which has a C API), or you could use Qt (which is C++, but there's usually no problems calling C functions from C++ if you define them as 'extern "C"').
There are a number of scripting with languages that can make this easy. I'd go for tcl/tk it works on mac pretty much out of the box, and is cross platform across pretty much any machine you can think of.
Also GTK is great and a number of scripting languages have bindings for it.
You probably don't want to write your gui in c if you don't have to. use a rapid develoment scripting language.
If you want a native GUI, then go with wxWidgets or QT. (I use wxWidgets ...)
The alternative is to have an HTML based interface using Javascript and CSS. You avoid GUI libraries altogether, you get cross-platform support and the outcome is better on some dimensions, worse on others.
If you really want a standalone user interface, you can integrate it into a binary with something like WebKit.

OS independent clipboard copy/paste text in C

I'm working on a project that's supposed to work on both Windows and Linux (with an unofficial Mac port as well) that emulates a true colour system console.
My problem is that recently there appeared a request for textfield support (yes, console-based) and it would be cool to add the possibility of copying text to clipboard and pasting from it. Is there a way of achieving this that will:
be done in C (not C++),
work in both Windows and in Linux (preprocessor macros are an option if there's no platform-independent code),
require no extra libraries to link to?
Thanks in advance for your help.
If you're not using a cross platform UI library (like wx or something), then it sounds like you're just going to have to write native clipboard code for each platform you want to support.
Remember, on Macintoshes, you copy with Command-C, not Ctrl+C :)
The clipboard is inherently an operating system defined concept. The C language itself has no knowledge of what a clipboard is or how to operate on it. You must either interface directly with the OS, or use a portability library that does this on your behalf. There is no way around this.
Personally I would define my your own function
getClipboardText();
That is defined in two different header files (linux_clipboard.h, windows_clipboard.h, etc) and then do pre-proccessor stuff to load the appropriate one accordingly. I don't really code in C/C++ so I'm sorry if that didn't make any sense or is bad practice but that's how I'd go about doing this.
#if WIN32
#include windows_clipboard.h
#endif
That sort of thing
Remember:
For linux you have to deal with different window managers (Gnome, KDE) all with different ways of managing the clipboard. Keep this in mind when designing your app.
You may be able to communicate to the clipboard by using xclip. You can use this python script here to do this job via communicating with 'dcop' and 'klipper' here. That is for KDE, I do not know how it would be done under GNOME... You may also be able to do this independantly of either GNOME/KDE by using DBUS, although I cannot say 100% confidently on that either...
Just be aware, that for a truly cross-platform job, you have to take into account of the different GUI's such as under Linux, X is the main window manager interface and either GNOME/KDE sits on top of it..I am not singling out other GUI's such as FluxBox, WindowMaker to name but a few, and that there will be a lot of platform dependant code, and also in conjunction, you will be dealing with Windows clipboard as well..all in all, a big integrated code...
Have you not considered looking at the raw X programming API for clipboard support? Maybe that might be better as I would imagine, GNOME/KDE etc are using the X's API to do the clipboard work...if that is confirmed, then the work would be cut out and be independant of the major GUI interfaces...(I hope that would be the case as it would make life easier for your project!)
Perhaps using compile-time switches, for each platform...WIN, KDE, GNOME, MAC or use the one that is already pre-defined..
Hope this helps,
Best regards,
Tom.

Resources