终端编译opengl程序编译运行,从一个没有窗户的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.

解决方案

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.

pgrep Xorg

Check whether /var/lib/gdm/:0.Xauth exists. This will be there even if nobody has logged in but GDM is running.

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! :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值