Welcome

Troves being gleaned while surfing on the Internet mostly about computer/IT/system skills and tricks, Welcome here ...
Powered By Blogger

Disclaimer

This blog is written by the owner with real practices and tests and intended to hold all original posts except there is a clear declaration for referencing from others. Thanks for tagging with the source link or other tips for reference from here if you would like to quote partial or full text from posts in this blog.

Saturday, January 8, 2011

Enable X Shared Memory Extension Pixmaps

Definitely not encouraged to use, sometimes this feature is needed for using certain applications what depends on this deprecated X window extension.

Found the solution on http://fxc.noaa.gov/FSD-NVIDIA-OB9-FXC.htm. More precisely, an example illustrating the details with Nvidia series on Fedora 14 is shown as follows.

# nvidia-xconfig: X configuration file generated by nvidia-xconfig
# nvidia-xconfig:  version 260.19.29 
# (buildmeister@swio-display-x86-rhel47-04.nvidia.com)  Wed Dec 
# 8 12:27:39 PST 2010


Section "ServerLayout"
    Identifier     "Layout0"
    Screen      0  "Screen0" 0 0
    InputDevice    "Keyboard0" "CoreKeyboard"
    InputDevice    "Mouse0" "CorePointer"
EndSection

Section "Files"
    FontPath        "/usr/share/fonts/default/Type1"
EndSection

Section "InputDevice"

    # generated from default
    Identifier     "Mouse0"
    Driver         "mouse"
    Option         "Protocol" "auto"
    Option         "Device" "/dev/input/mice"
    Option         "Emulate3Buttons" "no"
    Option         "ZAxisMapping" "4 5"
EndSection

Section "InputDevice"

    # generated from data in "/etc/sysconfig/keyboard"
    Identifier     "Keyboard0"
    Driver         "kbd"
    Option         "XkbLayout" "us"
    Option         "XkbModel" "pc105"
EndSection

Section "Monitor"
    Identifier     "Monitor0"
    VendorName     "Unknown"
    ModelName      "Unknown"
    HorizSync       28.0 - 33.0
    VertRefresh     43.0 - 72.0
    Option         "DPMS"
EndSection

Section "Device"
    Identifier     "Device0"
    Driver         "nvidia"
    VendorName     "NVIDIA Corporation"
    Option    "AllowSHMPixmaps" "true"
EndSection

Section "Screen"
    Identifier     "Screen0"
    Device         "Device0"
    Monitor        "Monitor0"
    DefaultDepth    24
    SubSection     "Display"
        Depth       24
    EndSubSection
EndSection

The enabling appendix is simply the single line below.
Option "AllowSHMPixmaps" "true"

Then reboot the system to make it effect.

Following code snippet could be used for verifying the availability of this feature:

int vmajor;

int vminor;
bool vpixmap;
bool shm_flag;
if (XShmQueryVersion(window->getDisplay(),&vmajor,&vminor,&vpixmap) != True) { 
    cerr << "X Shared Memory Extension not supported." << endl;
    shm_flag=false;
}
else if (vpixmap != True) { 
    cerr << "X Shared Memory Extension Pixmap not supported." << endl;
    shm_flag=false;
}
else {
    // do what is relying on the MIT-SHM feature from here on
    ......
}

Thursday, January 6, 2011

How to get the screen resolution in Linux C (and more X window information)

Linux C: get the screen resolution and window size 

Happened to necessitate the retrieval of information about the screen of the canonical X window system (X11)  in use while I am employing Fedora Linux release 14.

Some guys (or gals maybe) suggest achieving this by GTK calls like gdk_screen_get_resolution or the like, I will prefer to using the legacy routines of Xlib for accessing to the X window information. For at least one thing, the X window system is presumably more likely to be installed in Linux OS than is the GTK/GTK+ libraries in my view.

Actually it is quite simpler than it might be postulated to be:

// -----------------------------------------------------------
// Purpose : simply retrieve current X screen resolution and 
//    the size of current root window of the default 
//    screen of curent default window
// -----------------------------------------------------------

#include <X11/Xlib.h>
#include <stdio.h>

int getRootWindowSize(int *w, int *h)
{
 Display* pdsp = NULL;
 Window wid = 0;
 XWindowAttributes xwAttr;

 pdsp = XOpenDisplay( NULL );
 if ( !pdsp ) {
  fprintf(stderr, "Failed to open default display.\n");
  return -1;
 }

 wid = DefaultRootWindow( pdsp );
 if ( 0 > wid ) {
  fprintf(stderr, "Failed to obtain the root windows Id "
      "of the default screen of given display.\n");
  return -2;
 }
 
 Status ret = XGetWindowAttributes( pdsp, wid, &xwAttr );
 *w = xwAttr.width;
 *h = xwAttr.height;

 XCloseDisplay( pdsp );
 return 0;
}

int getScreenSize(int *w, int*h)
{

 Display* pdsp = NULL;
 Screen* pscr = NULL;

 pdsp = XOpenDisplay( NULL );
 if ( !pdsp ) {
  fprintf(stderr, "Failed to open default display.\n");
  return -1;
 }

    pscr = DefaultScreenOfDisplay( pdsp );
 if ( !pscr ) {
  fprintf(stderr, "Failed to obtain the default screen of given display.\n");
  return -2;
 }

 *w = pscr->width;
 *h = pscr->height;

 XCloseDisplay( pdsp );
 return 0;
}

int main()
{
 int w, h;

 getScreenSize(&w, &h);
 printf (" Screen:  width = %d, height = %d \n", w, h);

 getRootWindowSize(&w, &h);
 printf (" Root Window:  width = %d, height = %d \n", w, h);
 
 return 1;
 
}


/* gcc -o $@ $< -lX11 */


/* set ts=4 sts=4 tw=100 sw=4 */
You can also retrieve more information about the X window system you are currently using, such as display name and the tag of vendor.
// -----------------------------------------------------------
// Purpose : simply retrieve current X windows information
// -----------------------------------------------------------
#define XLIB_ILLEGAL_ACCESS
#include <X11/Xlib.h>
#include <stdio.h>

int main()
{

 Display* pdsp = XOpenDisplay(NULL);
 Window wid = DefaultRootWindow(pdsp);

 Screen* pwnd = DefaultScreenOfDisplay(pdsp);
 int sid = DefaultScreen(pdsp);
 
 XWindowAttributes xwAttr;
 XGetWindowAttributes(pdsp,wid,&xwAttr);

 printf (" name : %s\n vendor : %s\n", pdsp->display_name, pdsp->vendor);
 printf (" pos : (%d, %d), width = %d, height = %d \n",
   xwAttr.x, xwAttr.y, xwAttr.width, xwAttr.height);

 XCloseDisplay( pdsp );
 
 return 1;
}


/* gcc -o $@ $< -lX11 */
/* set ts=4 sts=4 tw=100 sw=4 */

Note that here "#define XLIB_ILLEGAL_ACCESS" was used for accessing to some of the data member in the Display structure in C.

For more data member accessible, see the definition/declarations in the header /usr/include/X11/Xlib.h.

PS: It is thankfully in the courtesy of http://www.craftyfella.com/2010/01/syntax-highlighting-with-blogger-engine.html to make the code snippets I posted above have a highlighted effect and look organized more trimly (otherwise it is always bit of mangled!) Thanks to craftyfella!

Monday, January 3, 2011

Can not find man page for GLUT routines in Linux?

I made up the man page for GLUT (so that I can ^K to call the API manual out while the cursor is on the words symbolizing GLUT routines) by the simplest way:

sudo yum  list "*glut*"

more than a couple was found and following two are among the alternatives:

sudo -y install freeglut freeglut-evel


now enter into vim again and try ^K on "glutWindowSize" for instance, check if it works now.

Fix bugs for Buidling installation of G3D 7.01 in Fedora 14

Today I am porting such and such a  project which is dependent on G3D and for historical reasons this project could not work with other versions of G3D including the latest ones but versions before 8.0 since it calls some of the routines in the Libraries whose prototypes had been modified!

In conclusion, we have two bugs to fix before the G3D 7.01 library can be successfully built from the downloaded source code found at http://sourceforge.net/projects/g3d/files/g3d-cpp/7.01/G3D-7.01-src.zip/download in Fedora 14. I deem  the same problems and solutions  may as well be portable to other Linux distributions and various versions of specific distribution.

1. Edit   GLG3D.lib/include/GLG3D/Discovery2.h, add the header including definition of the macro "INT_MAX" which was referred at line 68 in this file, where the first problem you will run across in the first try of building using "./buildg3d  all" or "buildg3d --install all", or so forth the similar.

 I simply added "#include <limits.h>" right after "#include <string.h>", actually an c++ header equivalent is "climits".


(Where is "INT_MAX", "LONG_MAX", etc, defined?)
I found the container header of this macro at http://www.fredosaurus.com/notes-cpp/90summaries/summary-headers.html, you may find it useful further in the future.

2. In order to get a complete installation, test code should not be skipped either, so this fix is necessary:

in tools/viewer/MD2Viewer.cpp : 108, change
currentPose = MD2Model::Pose::Pose(MD2Model::STAND, 0);
into
currentPose = MD2Model::Pose(MD2Model::STAND, 0);

I am bit of wondering why there the class Pose 's constructor should be called in this explicit way with this originally buggy code.

3. now get back to the source directory (where ./buildg3d lies),
./buildg3d --install all"

then the library will be installed straightforwardly.

A Funny Time Varying VIM color scheme switching

Vi/vim is really the most popular editor under Linux esp. for those advanced users or Linux fans, even though there are dozen of other featured ones including visual editors and the most worthy of mentioning, Emacs for programming gurus.

Here I just got a whimsy yet actually a fortuitous idea, to make this editor of daily use to be adaptive with current local time periods such as morning, noon, evening and other phases of a day.

The snippet  meant to be placed in or tailed at the vim configuration file, as is mostly the /etc/vimrc or $HOME/vimrc, will make the vim editor apply different color scheme that is meaningfully in accordance with the time of period when vim is launched. Additionally, color schemes for other time of period than morning and evening will be changed with the weekday.


" # intelligent colorscheme switching
let weekday=system('date +%u')
let hour=system('date +%H')

" if the shell command gets any exception during execution, nothing will be 
" done on color scheme setting
if !v:shell_error


   " in other time slots than mornings and evenings, scheme to be applied depends 
   " on which the weekday it is today, beginning from 1 as Monday, through 6 as 
   " Saturday and Sunday numbered as 0
    if     1== weekday
        let coloration="elflord"
    elseif 2 == weekday
        let coloration="koehler"
    elseif 3 == weekday
        let coloration="slate"
    elseif 4 == weekday
        let coloration="zellner"
    elseif 5 == weekday
        let coloration="torte"
    elseif 6 == weekday
        let coloration="ron"
    else "Sundays
        let coloration="blue"
    endif 
 
    " exert the color scheme application
    execute "colorscheme ".coloration 
 
    " in mornings, alwasy using the "morning" scheme
    if hour >= 7 && hour <= 10
        let coloration="morning"
    " in evenings, alwasy using the "evening" scheme
    elseif hour >= 18 && hour <= 23
        let coloration="evening"
    endif
 
endif


Just give it a try, you may be intrigued with use of it afterwards like me!

-----------------------------------
other interesting links
-------------------------------------

Full range of vim color scheme implemented in a Google code project
http://vimcolorschemetest.googlecode.com/svn/html/index-c.html

A find-tweaked color scheme for Mac:
http://blog.toddwerth.com/entries/show/8

Saturday, January 1, 2011

Install Nvidia Driver (non Gefore series) in Fedora 14

Intro: 

While here places a complete guide that is already great for novices in Fedora community at http://www.if-not-true-then-false.com/2010/fedora-14-nvidia-drivers-install-guide-disable-nouveau-driver/, following text will act as a complementary instruction for those who are using Nvidia Driver Card other than in the Geforce / Geforce FX series,  Quadro FX, for instance. (some parts actually are learned from the this page)

Steps: 

1. update to the latest kernel and restart the OS
by
yum update kernel*
reboot

2. make sure a correct xorg configuration, i.e. /etc/X11/xorg.conf, contains things about nvidia like the following
Section "Files"
    ModulePath   "/usr/lib64/xorg/modules/extensions/nvidia"
    ModulePath   "/usr/lib64/xorg/modules"
EndSection

(If for 32bit platform, use lib instead of lib64 in the paths above)
(If the xorg.conf file does not exist before, simply create it and add the stuff above)

3. Change Configuration of the Grub Bootloader by changing the current /boot/grub/grub.conf into text 
like the following 

default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz 
 
hiddenmenu 
title Fedora (2.6.35.10-74.fc14.x86_64)
 root (hd0,0)
 kernel /boot/vmlinuz-2.6.35.10-74.fc14.x86_64 ro root=UUID=e36dbe41-49ac-458f-8942-ba3fbdd22e32
 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 
KEYTABLE=us rhgb quiet rdblacklist=nouveau nouveau.modeset=0
 initrd /boot/initramfs-2.6.35.10-74.fc14.x86_64.img 
 
title Fedora Linux 14 (2.6.35.6-45.fc14.x86_64)
 root (hd0,0)
 kernel /boot/vmlinuz-2.6.35.6-45.fc14.x86_64 ro root=UUID=e36dbe41-49ac-458f-8942-ba3fbdd22e32 
rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16
 KEYTABLE=us rhgb quiet
 initrd /boot/initramfs-2.6.35.6-45.fc14.x86_64.img 
 
title windows 7
 rootnoverify (hd0,3)
 chainloader +1

the key is the blued text, which is added to disable the default display driver : nouveau. If this is 
missed, you will be told in the step 4 below that the installer can not proceed due to the nouveau is not
disabled.

4. download driver from Nvidia official site, for linux x86_64 we are using 
http://us.download.nvidia.com/XFree86/Linux-x86_64/260.19.29/NVIDIA-Linux-x86_64-260.19.29.run 
for now, wget it to a handy location, /root, say.

5. reboot,  then select the latest kernel item, and
 press "e" to edit it
 choose the "kernel /boot/vmlinuz-2.6.35.10-7...." line and press "e" to edit again
 append "single" to the line and press enter to get back to the previous interface
 press "b" to trigger the bootstrap
 
6. now that get in the Level 1 (single user mode), enter Level 3 (I did not check if this is necessary)
by "/sbin/init 3"
 
7. Run the installer
 sh /root/NVIDIA-Linux-x86_64-260.19.29.run -a
 
(if you are installing for the first time, you might already nvidia-installer on you machine,
then you don't need to download beforehand, instead a simpler way is :
 
nvidia-install -a --update  
 
you can by this get the latest driver and install it. but this will demand a network access
of course, see this page (http://surfingtroves.blogspot.com/2010/12/linux-access-network
-service-in-single.html ) for enabling network access in the single user mode, as is suitable
for being at Level 3. 
 
8. when finished, restart, and the installation should be well done.


Surfing Troves: Building installation of FreeWRL for Fedora 12/13/...

Surfing Troves: Building installation of FreeWRL for Fedora 12/13/...: "The official site of FreeWRL only provides installation guide for Ubuntu in the range of Linux series. While different OS has different too..."

Friday, December 31, 2010

uninterruptedly running program in the background but need enter password or something ?



For instance, wanna copy a big file, more than 10G, say, from remote host to local machine by a shell like :

 scp -r user@hostname/ip:/home/data/bigdata   /home/user/ 

but fearful of the interruption if the session will be terminated, so use nohup to achieve an interrupted execution to guard against being drawn to end due to the termination somehow of the current login session. by

nohup scp -r user@hostname/ip:/home/data/bigdata   /home/user/  

But most of the time you also want to lay this task go in the background since it should take a multitude of time to remote-copy a file as big as 10G, so change into

nohup scp -r user@hostname/ip:/home/data/bigdata   /home/user/ &

By this point it comes what I want to share: you are guaranteed to run across an OOPS:

nohup: ignoring input and appending output to `nohup.out'

[1]+  Exit 1                  nohup scp -r user@hostname/ip:/home/data/bigdata /home/user/


this is just because the process is suspended to receive a user input for password.

So there seemingly exists an ambivalent occasion here.  By session controlling command, we can circumvent around this plausible dilemma:

nohup scp -r user@hostname/ip:/home/data/bigdata   /home/user/  
enter the password
CTRL+Z ( force the foreground process back to the background to run like a daemon )
 [1] 3581

bg $jobid (you can see the jobid appearing after CTRL+Z, here it is 1)


Later if wanna change it back to run in the foreground,  do it by


fg $jobid.

* you can also change the default output of nohup and include all the copying log into a self-defined log file by

nohup scp -r user@hostname/ip:/home/data/bigdata   /home/user/   1> bigdata.scp.log 2>&1

Restore Linux installation after installing Windows 7 which overwrote the MBR

By default Linux uses the grub Bootloader to manage OS booting with support of multiple OS, and this makes Linux, Fedora distribution say, really gentle as it is generous to keep its predecessors in the context of multiple boot of operating systems.

As such if you install Linux on top of current Windows system, the installer simply adds an boot item for the existing windows start up. It is not true the other way around, however!

When you install a Windows OS on top of current Linux, windows bootloader will not care about things already exist and bit of bluntly overwrite the grub bootloader thus make you lose the Linux boot.

Good news is there do be a way to remedy this loss, the marvellous guy I will be thankful to get the grub back is grub4dos.

Here we go from Windows 7!


2. unpack and retrieve following files
glrdr
glrdr.mbr
menu.lst

place them under the root directory of the boot partition in Windows like C:\

3. expand a slot for the grub bootloader in current Windows bootloader (bootmgr) using bcdedit :

bcdedit /create /d "Grub4Dos" /application bootsector
note to record the id generated in the output.

then use this same id with following commands:
bcdedit /set {id} device partition=C:
bcdedit /set {id} path \grldr.mbr
bcdedit /displayorder {id} /addlast

As a result this will add an item for grub4dos in the Windows bootloader, so that we can boot Linux through grub4dos indirectly.

4. add boot item in the grub bootloader
edit menu.lst to make sure it contains item(s) for the "lost" Linux boot, it should seem like the example below:

default 0
timeout 3
hiddenmenu title Windows 7
find --set-root --ignore-floppies /bootmgr
chainloader /bootmgr

title Fedora (2.6.31.5-127.fc12.x86_64)

root (hd0,0)/boot
kernel /vmlinuz-2.6.31.5-127.fc12.x86_64 ro root=/dev/sda1 LANG=zh_CN.UTF-8 KEYBOARDTYPE=pc
KEYTABLE=us rhgb quiet
initrd /initramfs-2.6.31.5-127.fc12.x86_64.img

title fedora (2.6.32.21-168.fc12.x86_64)

root (hd0,0)/boot
kernel /vmlinuz-2.6.32.21-168.fc12.x86_64 ro root=/dev/sda1 LANG=zh_CN.UTF-8 KEYBOARDTYPE=pc
KEYTABLE=us rhgb quiet
initrd /initramfs-2.6.32.21-168.fc12.x86_64.img



If you do not remember what the file names are such as initramfs-2.6.31.5-127.fc12.x86_64.img and vmlinuz-2.6.31.5-127.fc12.x86_64, esp. those other values like the stuff on the kernel line (" root=/dev/sda1 LANG=zh_CN.UTF-8 KEYBOARDTYPE=pc KEYTABLE=us rhgb quiet), you can
find a tool to look at the /boot/grub/grub.conf in the current Windows, a reliable one I have ever use is ext2fs which can be downloaded from http://www.chrysocome.net/explore2fs. For other similar tools, Google around with "ext file explorer for windows".

5. Now restart windows, in the boot menu, select "Grub4Dos" you will see the classical GRUB  interface and you can select the Linux boot item with different kernel version therein. By this, we get Linux installation back and enjoy the dual or multiple boot at one host. In fact the Linux boot restoration steps described above is somewhat similar to that for fresh installation of Linux from Windows 7 without using a Boot CD/DVD.


 
Further problems are welcome to be posted as comments below.

How to restore or upgrade Fedora linux without losing user data at all

When you are inflicted with some faults of current Linux, the Fedora distribution, say, you might want to restore the OS kernel only without impairing all current user data even user applications installed after the previous installation, just like for Windows 2000/NT/XP/7, etc. there is a "restoring installation" option during the installation tour.

Just follow steps below to achieve this, given all your user data including configurations and profiles even history records like cookies, etc. are located at /home/user, and /home is mounted differently from the OS kernel and its legacy things. For example, you have "df -h" leading to output like following things:

/dev/sda1              99G  7.5G   86G   8% /
tmpfs                 2.0G  468K  2.0G   1% /dev/shm
/dev/sda2             148G   70G   71G  50% /home

[ For upgrading to a higher version of Linux even to a different distribution, the guide below is just exactly suited ]

1. download and place installation package
a. get the OS installation image, Fedora-14-x86_64-DVD.iso, say from the official site

b. extract files and directories from the image and copy them to the partition holding data you want to keep, e.g. the /home at /dev/sda2 in this case

su 
mkdir /mnt/fcinstall
mount -o loop -t iso9660 Fedora-14-x86_64-DVD.iso /mnt/fcinstall

cp -fr /mnt/fcinstall/isolinux/{vmlinuz,initrd.img} /mnt/fcinstall/images  /home
mv Fedora-14-x86_64-DVD.iso /home

* you can also use some unpacking tool for extracting from ISO instead of using mount.


2. edit  /boot/grub/grub.conf and add following :
(presuming current partition is (hd0, 0) in terms of (hard_drive, partition), corresponding to sda1 listed above in this case)

title fcinstall
root (hd0, 0)
kernel /vimlinuz
initrd /initrd.img



 *this is to prepare for a boot item in the bootloader for reinstalling.  You can either add a new item exclusively for this reinstallation like above
or just modify the bootloader item for the Linux in the current use,

3. keep everything you want to retain AWAY from the OS partition, for example, all stuff other than that are under /home/.  if you have ever installed VirtualBox and created an virtual windows Xp image as the root account, you will need to :
move /root/.VirtualBox  /home/user, say.  this step is the first IMPORTANT really.

4. after ascertaining nothing missed on the /home partition as the user data to keep, restart now

5. following steps is verisimilar to the ordinary fresh installation of Linux, except certain cautions as follows:

a. at this step




CHOOSE  "create custom layout"

b. in the partition editing interface,
choose /dev/sda1 to edit, and mount it onto "/", and format it. (it is advisable to format it since we want to a clean fresh installation not patching things up or left garbage there). Should format into "ext4" file system as well.


and, choose /dev/sda2 to edit, NOTE that just to mount it to /home and NEVER choose to format it, otherwise everything we are doing is just of no damn use and become meaningless!  this is the second KEY step, i.e. the single most IMPORTANT step for your goal towards "restorative installation"

c. back to the partition interface and go next, then follow every step as the fresh installation until it is finished and restart again.

6.restore the user profile

Now create the user account you had before, /home/user for instance.


su
useradd -d /home/user -G user -p ******  -s /bin/bash user

By skipping the -m option, a skeleton profile will not be copied to /home/user, and we are to keep using original data all and get it associated with this restored user account as before.

if you like to use GUI, in the following interface
Mind you, do NOT choose "Create home directory".


(Actually after the reinstallation, you DO need a user account other than root to login the system, so just do it right there.  The way I was doing is to create a temporary user account and then delete it just because I wan habituated to think of the command line operation then! )

7. Post restoration - Site clearing !

Now that we get a reinstalled linux and everything kept there just like before the restoration, we can safely remove the boot item for our recovery task.

- edit /boot/grub/grub.conf and remove what we added right now.
- unmount /mnt/fcinstall
- remove the installation image and boot bzImage we copied to /home/ before if you like. Surely you can simply keep them there in case we need another reinstallation afterwards, hope you will not be of that bad luck though :)



Now, you just are going to enjoy everything you have before, including the Desktop, gnome-terminal settings, Mozilla bookmarks even visit history, application (like VirtualBox and Windows Xp virtual OS, etc.) and .....


[ Other Notes ]

From this text, written from my definite practice (which is not so smooth at the time though), we should suggest that in the future use of Linux :

Install new application as into the user partition (or a single other partition than that for holding the OS itself) as possible. The same principles is applicable for data placement. In order to keep this consistency, you have better to designated "--prefix=/home/user/..." if you are to install some extraneous packages and modules by building the source code.

In a word, the more spread you anchored your data, the less handy to do a restorative installation being described.