Matlab/Linux/X Figure Focus Stealing Work Around October 24, 2007 Matlab running on Linux / X-windows has terrible window behavior, with at least 3 problems (see below). This note is about a workaround I found for these problems. Good Luck. Brian Armstrong I. The problems At least with the combination listed below I notice 3 problems. The configuration: Matlab R2007a The Fedora core 7 distribution of linux Gnome Metacity window manager (with default settings) The problems: A code segment is included below which draws on several figures, then goes back to computing for a while. The problems I observe are: 1. Focus stealing Try opening a typing into another window (such as emacs) during the svd(). When the figure() arrives, the figure window will steal focus from the editor or other task. Mathworks support indicates: "In case of the issue with changing focus, there is a bug in MATLAB 7.4 (R2007a) in the way that the focus changes from command window to the figure window when PLOT or FIGURE command is used to plot a figure on a Linux machine." 2. figure(2) doesn't expose the figure window for figure(2) Put another window over the figures while the code is running. When the figures redraw, they are not exposed, so there is no way to know they have been redrawn or see the result. This characteristic interacts very badly with problem 3. 3. No way to set the X windows BackingStore attribute When I apply xwininfo to a matlab figure window I get the result below, including the figure window backingstore attribute set to its default value of "NotUseful." Think of issues 2 and 3 in combination. With a program like the sample, it could draw a graphic and continue computing. Because the graphic is under another window, it is not visible. Because it is impossible to turn on BackingStore, even if the user raises the window manually, it will not be redrawn until a future drawing event (drawnow, pause, or computation completes). If the program is compute-intensive, this may be minutes or weeks away. X window attributes are set as parameters to the X library call that creates the window (in this case Matlab from). I understand that it is not possible to externally modify the BackingStore attribute. A Matlab figure has a BackingStore State (see code segment). I think it is a bug that the matlab graphic backingstore state can be "on" (see segment of Matlab listing) but the figure X window is created with attribute is left at its default value of "NotUseful." %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Matlab Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% kk=0 while 1, kk=kk+1,, aa = rand(500); [b,c,d] = svd(aa); for jj = 1:5, figure(jj), clf plot((1:10), rand(1, 10), '+'), end drawnow aa = rand(500); [b,c,d] = svd(aa); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% xwininfo output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [user@995-linux01 ~]$ xwininfo xwininfo: Please select the window about which you would like information by clicking the mouse in that window. xwininfo: Window id: 0x4800143 "Figure 5" ... Window Gravity State: StaticGravity Backing Store State: NotUseful Save Under State: no Map State: IsViewable ... %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% matlab handle graphics state %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% > get(gcf) Alphamap = [ (1 by 64) double array] BackingStore = on ... %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% II. Solutions I did the following things 1. Replace the Metacity window manager with the Sawfish windowmanager. I did this by installing sawfish and setting the following in my .bashrc file ## gnome-session uses this to select sawfish as the window manager export WINDOW_MANAGER="sawfish"; Sawfish has better focus behavior in many regards. 2. Running matlab with no Java virtual machine, calling with % matlab -nojvm See "Measured Behaviors" below 3. Use the -wm ("WhenMapped") command line argument to the invocation of X windows, with the changes below. This overrules the BackingStore attribute of the window. (Although much finer-grain control would be possible if Mathworks would simply pass the BackingStore state in the handle graphic to the graphic window when it is created). // In /usr/bin/startx: defaultserverargs="-wm" // In /etc/X11/xorg.conf: Section "Device" Identifier "Videocard0" ... option "backingstore" "on" option "saveunder" "on" EndSection III. Measured Behaviors Just for completeness, here is a chart of behaviors I obsreved with various combinations of components. Notes: Window Manager: mc=Metacity, sf=Sawfish nojvm: 1 = -nojvm switch present, 0 = -nojvm switch not present (matlab gui used) -wm 1 = -wm on command line of X invocation 0 = -wm not on command line of X invocation Focus Stealing: X = yes, ;) = no. Raise Window: X = no, ;) = yes BackingStore: ;) = yes Window nojvm -wm Focus Raise BackingStore Manager Stealing Window mc 0 0 X X X mc 1 0 ;) X X mc 0 1 X X ;) mc 1 1 ;) X ;) sf 0 0 X ;) X sf 1 0 ;) ;) X sf 0 1 X ;) ;) sf 1 1 ;) ;) ;)