Linux/Unix Console Graphics/Control - c

I would like to know how to create console applications with better control of input and output using C. For example, how does the text-editor nano display the file contents in a certain portion of the screen and then keep a list of commands stationary on the bottom. I know you can use the win32 api to do this in Windows, but what is the Unix/Linux equivalent?

More than likely there's a ncurses interface that controls the screen drawing and placement of items on the screen in a certain position. The api is standard across the linux systems and on unix also, some may be implementation defined depending on the variant of curses used under the commercial variants of unix but otherwise some standard ncurses functionality are compatible.

Besides ncurses and depending on the task at hands you may find newt, a library for color text mode, widget-based user interfaces, a suitable alternative also. Sometimes visual results are better with newt.

If you just want to do the low level stuff, you probably want to use the termcap or terminfo library.
If you want to do it the way nano and just about every other interactive terminal app does it, you probably want to use ncurses. That way you will spend less time implementing terminal control logic, and more time on the actual app you are developing.

Related

C: How do i go about gaining access to a full terminal window on a *nix system

I am planning on writing a command line text editor in C for a *nix system similar to nano or vim but I am stuck on how I would go about getting access to the entire terminal window so the user can move around the text editor freely, do I need to use a 3rd party library to achieve this result if so what libraries are required, or am I able to achieve this result with the standard C libraries and if so what libraries should I use.
The ncurses library is the simplest way of gaining access to a full terminal window although it can also be achieved with the libc libraries.

Make a text blink from a C console application

Hello I just got started with C and I don't know much about it, and I was wondering if it was possible to change the font/style/appearence of a text in a console application ?
C has no concept of fonts, styles or other visual representational parameters. Unless you're writing a GUI application, you won't be able to specify anything like that.
Non-GUI application (console applications / CLI applications) that output text will do so by writing to stdout or stderr. The terminal/shell/console application that you're using will "intercept" these streams and display the content (eg. text) accordingly. Therefore, it's up to the terminal/shell/console to apply styling and you have to consult your terminal/shell/console documentation for that.
If you're writing text/terminal-based user interfaces you might want to look at libraries such as ncurses.
You could use ANSI escape codes, if your console supports them.
You could also use higher level libraries, like ncurses.
(and you might consider writing a GUI application, using GTK).
Notice that a RaspberryPi or an Arduino or a Web server (e.g. with lighttpd) can run a C software, and might not even have consoles, fonts, colors...

How to control frame size, buffer size, cursor position, and GDI function of the gnome terminal in C with API?

I've programmed in Windows with Win32 Console API, and there are some functions that can control console:
SetConsoleWindowInfo: Sets the current size and position of a console screen buffer's window.
SetConsoleScreenBufferSize: Changes the size of the specified console screen buffer.
SetConsoleCursorPosition: Sets the cursor position in the specified console screen buffer.
SetPixel: The SetPixel function sets the pixel at the specified coordinates to the specified color.
Is there any equivalent function in Ubuntu ( without using ncurses )? APIs?
BTW, where is the developers documentation website of Ubuntu like MSDN?
There are a few things you'll want to note:
Without Ncurses you don't really have to many options (Well you do - excape codes, but that's what NCurses does). It's worth understanding why you can't find this functionality though: Gnome-Terminal is a terminal emulator, not a 'console' like cmd.exe is. There is no API for controlling the terminal because it's simply emulating a proper serial terminal and reading in characters from your programs stdout, and sending characters to stdin (In other words, your running program has no idea what type of terminal it's running on, or even if it's outputting or getting input from a terminal. It could be Gnome-terminal, could also be xfce4-terminal, rxvt, xterm, a file, etc... This is true in Windows but not as much, since cmd.exe is basically the only terminal program and you control the terminal through more direct means.). So the simple answer is you can't, control terminal related things like buffer size. You can control the cursor position and set characters on specefic spots on the screen, but you have to do that through excape codes and they get ugly (Ncurses is a great library in that it abstracts all of that from you). Is there a reason you don't want to use Ncurses? I would rethink what you're doing if you don't want to use it. If you're looking for cross-platform, pdcurses is a curses implementation for Windows you could use.
Also worth noting, you keep refering to Ubuntu and asking for their documentation -- You should keep in mind that Gnome and Gnome-Terminal are separate projects from Ubuntu (Which is more or less just a collection of different programs, with some in-house programs like Unity). Looking for Gnome-Terminal information on Ubuntu's website is like looking for Gnome-Terminal information on Microsoft's website, you just won't find it because it's a separate project, it just happens to be installed by default on Ubuntu. If you look on Gnome's website you may find better information. But, they still don't have an API for controlling terminal specefic stuff like the buffer as they support Ncurses, and you're better off not tying yourself to a specefic terminal or OS. Keep in mind that not all Ubuntu users use Gnome-Terminal, and not all Gnome-Terminal users are running Ubuntu. AFAIK They don't have any type of developer site like MSDN, but they also don't really have much they could put on there. If you're developing for Ubuntu, you're probably then targeting other technologies like GTK, Qt, OpenGL, etc... Which do have documentation (And are more platform agnostic then just being 'Ubuntu-specefic' - Chances are if you write a program for Ubuntu, it'll run on most Linux desktop platforms and not just Ubuntu)

How to write an application that uses the terminal as GUI? (in C)

I'd like to write an application (in C) that uses the terminal of a *nix OS as its GUI. I mean an application of the kinds of emacs, vi etc. that take up the whole terminal window and jump back to the input prompt after quitting.
How would you go about doing that, preferably in C? Can someone point me to a tutorial covering that?
You need to use ncurses:
http://en.wikipedia.org/wiki/Ncurses
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
It is available on all major distros.
Well, actually this is not GUI (graphic user interface) but a text based interface. You can use the library ncurses to create such applications in C.
Use a library like ncurses, it is specifically designed for this purpose.
Throwing in alternate solutions so that this question thread does not look so monotonic:
the slang library (mc uses it, for example)

How to minimize to system tray in C

How can I minimize my app to the system tray as soon as it starts in C?
I am new to C.
Thanks.
Are you talking about Windows and the taskbar status area? If so, check http://msdn.microsoft.com/en-us/library/windows/desktop/bb762159.aspx for the Shell_NotifyIcon function. There are plenty of references, and even some samples linked on how to use it.
Also Notifications and the Notification Area: http://msdn.microsoft.com/en-us/library/windows/desktop/ee330740.aspx
C, all by itself, is not capable of doing what you want. The language was designed to work on as many possible architectures as possible (microwave ovens ... air bag systems ... mouse movement control ...) and not all such architectures know what a "system tray" is.
You need to use specific libraries (which augment the capabilities of Standard C). There are lots and lots (and lots) of external libraries. Most libraries to do the same thing on different platforms are not compatible between each other ... so we need to know what is the target of your code: Windows? Windows Vista? DOS? microwave oven? sattelite solar panel deployer? ... :-)
Create a window but don't show it.
Use Shell_NotifyIcon to create the icon in the notification area.
In order to perform step 2 you will need the window created in step 1.
If you have never programmed in C before and never used the Win32 API before this is an ambitious first project. First of all you should master the basics of showing windows, programming a message loop, handling messages etc. I recommend Programming Windows by Petzold.

Resources