Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch OpenGL app straight from a windowless Linux Terminal

Tags:

c

linux

opengl

How exactly would one go about getting an OpenGL app to run fullscreen straight from the terminal (Ubuntu Server 9.04)? I've developed an application for visual diagnostics on my server, but, I'm not totally sure the best way to get it to run in a windowless environment.


Ideally, I would run my program:

./visualdiagnostics

and have that launch the OpenGL app. Then, via a simple Ctrl+X key binding, I'll kill the app and go back to the terminal.


Do I need to install X11 and then somehow launch it from within the program? What would be the best way to detect if it's already running and, start/stop it if necessary?

And FYI: No, I'm not trying to get this to run over Putty or anything... I have a monitor hooked straight up to the server. The server has proper video drivers installed.

like image 322
brianreavis Avatar asked Oct 11 '09 23:10

brianreavis


3 Answers

There are several parts to your task. Keep in mind that some of this can be very distro-specific; but since you said Ubuntu we'll talk Ubuntu!

Also you tagged this question C however I am starting off with a common Linux pattern: a native application with a Bash shell script wrapper. Perhaps once you get things working well you might fold that functionality into C if you have to.

Detecting whether X is running

Being root can help a lot. Some things that work.

  1. pgrep Xorg
  2. Check whether /var/lib/gdm/:0.Xauth exists. This will be there even if nobody has logged in but GDM is running.
  3. ls -l /home/*/.Xauthority (Even if you're not root you can at least confirm whether you are running X.

Piggybacking an existing X session

You did not specifically mention it but if you are root at the console, or if you want to run the app as the same user who is already logged in, it's pretty easy.

You have to get the DISPLAY and XAUTHORITY environment variables right, and once you do you can use the existing X display.

For DISPLAY you might just assume :0 or you could find an existing X program (x-session-manager is the GNOME standard) and read its environment from /proc/PID/environ. Variables are in key=value format delimited by a null byte. For example, if its PID is 12345:

cat /proc/12345/environ \
  | ruby -ne 'puts $_.split("\0").select {|e| e.starts_with? "DISPLAY=" }'

For XAUTHORITY you could get it the same way. Or if you prefer guessing, it's almost always /home/whoever/.Xauthority

Once you have those two variables, running X code is easy, for example:

env DISPLAY=:0 XAUTHORITY=/home/brian/.Xauthority ./visualdiagnostics

Stopping X

This one is easy if you're root: /etc/init.d/gdm stop. killall Xorg will work too.

If you are a user, kill your own Xorg or x-session-manager process. (I'd welcome input from others for the canonical way to do this. Maybe some dbus-send message?)

Starting X

I would recommend xinit whose goal in life is to fire X and run exactly one program.

For example: xinit ./visualdiagnostics

You can also tell xinit what resolution to run X at which may or may not be important to you. (This becomes important in the full-screen section below.)

The problem with this is you will have no window manager— no maximize and minimize buttons. It's not just cosmetic. Usually an app is useless because a popup window cannot be moved or you cannot focus on the right input field. However if you have a special app it could be sufficient (see full-screen below).

The next step would be my answer to everything: another shell script wrapper! Something simple that starts the window manager and then becomes your program should work.

#!/bin/bash
#
# Start visualdiagnostics once xinit calls me.

/usr/bin/metacity& # Or ratpoison, or fluxbox, or compiz, etc.
exec ./visualdiagnostics

It's important to exec (become) the main program because once that first program exits, X will shut down.

Running fullscreen

I am not 100% certain on this. Some ideas:

  • Try the standard X -geometry parameters to set 0,0 as the upper-left corner and +x+y for your horizontal and vertical size. How do you know the size? Either you hard-coded it when you launched xinit or you could ask the X server. xwininfo -root will tell you and there is an xlib API call that would do that too—check the xwininfo source I guess.
  • Your app itself can request maximization and/or resizing to fill the screen. I'm not familiar but it is definitely in the X API.
  • Some of the more configurable window managers can be pre-configured to run you maximized already. This is probably what I personally would check first. Your wrapper script could create a $HOME/.fluxboxrc just by echoing some hard-coded configs > the file.

Summary

The others are right. X is not strictly necessary sine OpenGL can run against a framebuffer. However considering how ubiquitous X is and how much work has gone into automating it for distributions, I would probably invest my effort into the X route as it might be easier long-term even though it's a little convoluted.

(By the way, I sincerely hope when you say "terminal" you mean you are at the text console, not gnome-terminal that would be awful! :)

like image 156
JasonSmith Avatar answered Nov 03 '22 08:11

JasonSmith


Well I am clearly not sure my answer might help you out.

Long ago when I was student, I manage to do so (launching an openGL app from a terminal only linux installation) by installing frame buffer. As long as I remember I needed to recompile my kernel (as framebuffer was/is a kernel module).

This was maybe 5 years ago on a debian distrib, and I don't know how does it work now for up-to-date debian distrib as Ubuntu. Maybe framebuffer is compiled statically in the binary kernel provided by default with Ubuntu. May be not. Maybe framebuffer is irrelevant now... Or I may be totally wrong and not remembering every details of my own adventure 5 years ago now ..

Have a look on Google ! ;-)

Hope it will help...

**

Update:

**
What is frame buffer ?
How to install it? Here or there

like image 25
yves Baumes Avatar answered Nov 03 '22 10:11

yves Baumes


As yves pointed out, you can avoid running the X server if you use the framebuffer. Actually, the framebuffer modules are often yet available (for example, they are used to have the tux logo during the kernel start or a text terminal with fancy images in the background), this anyway depends on the distribution and the settings you are using.

The kernel side is quite primitive so I'd suggest to use some higher level library such as DirectFB. The framebuffer is usable without problems but don't expect the same maturity level than a full blown X server.

like image 45
ntd Avatar answered Nov 03 '22 08:11

ntd