exe's picture

How to hide mouse cursor on Ubuntu? Cursor.Hide does nothing and CursorVisible=false freezes mouse cursor when clicking.

System.Windows.Forms.Cursor.Hide() (for FPP camera) doesn't work on Ubuntu x64. I downloaded OpenTK from SVN (revision 3064), compiled, and switched to CursorVisible = false; and OpenTK.Input.Mouse. A crash was fixed with "sudo ln -s /usr/lib/libXi.so.6 /usr/lib/libXi.so".

Now cursor hides, but can't be moved while pressing mouse buttons. Pressing mouse button completely freezes it. This makes game unplayable.

Download test to reproduce problem, it contains source and compiled binary. (forum thread)

using System;
using System.Collections.Generic;
using System.Text;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Input;
using OpenTK.Graphics.OpenGL;
using System.Drawing;
using System.Drawing.Imaging;
 
namespace OpenTkMacMouseTest
{
    class MainWindow : GameWindow
    {
        public MainWindow(GraphicsMode mode)
            : base(800, 600, mode)
        {
            VSync = VSyncMode.On;
            CursorVisible = false;
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
            if (Keyboard[Key.Escape])
                Exit();
        }
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);
            GL.ClearColor(Color.Black);
            GL.Clear(ClearBufferMask.ColorBufferBit);
            OrthoMode();
            UpdateMouse();
            SwapBuffers();
        }
        int ConstWidth = 1600;
        int ConstHeight = 1200;
        float mousex;
        float mousey;
        void OrthoMode()
        {
            //GL.Disable(EnableCap.DepthTest);
            GL.MatrixMode(MatrixMode.Projection);
            GL.PushMatrix();
            GL.LoadIdentity();
            GL.Ortho(0, ConstWidth, ConstHeight, 0, 0, 1);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.PushMatrix();
            GL.LoadIdentity();
        }
        MouseState oldmouse;
        private void UpdateMouse()
        {
            var state = OpenTK.Input.Mouse.GetState();
            float dx = state.X - oldmouse.X;
            float dy = state.Y - oldmouse.Y;
            oldmouse = state;
            float dx2 = (dx * Math.Abs(dx) * 0.12f);
            float dy2 = (dy * Math.Abs(dy) * 0.12f);
            dx2 += dx * 1.0f;
            dy2 += dy * 1.0f;
            //if (Math.Abs(dx2) > Math.Abs(dx) * 10) { dx2 = dx * 10; }
            //if (Math.Abs(dy2) > Math.Abs(dy) * 10) { dy2 = dy * 10; }
            mousex += dx2;
            mousey += dy2;
            if (mousex < 0) { mousex = 0; }
            if (mousey < 0) { mousey = 0; }
            if (mousex > ConstWidth) { mousex = ConstWidth; }
            if (mousey > ConstHeight) { mousey = ConstHeight; }
 
            Draw2dBitmapFile("mousecursor.png", mousex, mousey, 64, 64);
        }
        [STAThread]
        public static void Main(string[] args)
        {
            GraphicsMode mode = new GraphicsMode(new OpenTK.Graphics.ColorFormat(32),
                24, 0, 2, new OpenTK.Graphics.ColorFormat(32));
            using (MainWindow game = new MainWindow(mode))
            {
                game.Run(60.0);
            }
        }
        bool IsPowerOfTwo(uint x)
        {
            return (
              x == 1 || x == 2 || x == 4 || x == 8 || x == 16 || x == 32 ||
              x == 64 || x == 128 || x == 256 || x == 512 || x == 1024 ||
              x == 2048 || x == 4096 || x == 8192 || x == 16384 ||
              x == 32768 || x == 65536 || x == 131072 || x == 262144 ||
              x == 524288 || x == 1048576 || x == 2097152 ||
              x == 4194304 || x == 8388608 || x == 16777216 ||
              x == 33554432 || x == 67108864 || x == 134217728 ||
              x == 268435456 || x == 536870912 || x == 1073741824 ||
              x == 2147483648);
        }
        bool ALLOW_NON_POWER_OF_TWO = false;
        bool ENABLE_MIPMAPS = true;
        bool ENABLE_TRANSPARENCY = true;
        //http://www.opentk.com/doc/graphics/textures/loading
        public int LoadTexture(Bitmap bmp)
        {
            if ((!ALLOW_NON_POWER_OF_TWO) &&
                (!(IsPowerOfTwo((uint)bmp.Width) && IsPowerOfTwo((uint)bmp.Height))))
            {
                throw new ArgumentException("Non-power-of-two.");
            }
            GL.Enable(EnableCap.Texture2D);
            int id = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, id);
            if (!ENABLE_MIPMAPS)
            {
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            }
            else
            {
                //GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); //DOES NOT WORK ON ATI GRAPHIC CARDS
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1); //DOES NOT WORK ON ???
                int[] MipMapCount = new int[1];
                GL.GetTexParameter(TextureTarget.Texture2D, GetTextureParameter.TextureMaxLevel, out MipMapCount[0]);
                if (MipMapCount[0] == 0)
                {
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                }
                else
                {
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.NearestMipmapLinear);
                }
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, 4);
            }
            BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
 
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0,
                OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);
 
            bmp.UnlockBits(bmp_data);
 
            GL.Enable(EnableCap.DepthTest);
 
            if (ENABLE_TRANSPARENCY)
            {
                GL.Enable(EnableCap.AlphaTest);
                GL.AlphaFunc(AlphaFunction.Greater, 0.5f);
            }
 
 
            if (ENABLE_TRANSPARENCY)
            {
                GL.Enable(EnableCap.Blend);
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                //GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)TextureEnvMode.Blend);
                //GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvColor, new Color4(0, 0, 0, byte.MaxValue));
            }
 
            return id;
        }
        Dictionary<string, int> textures = new Dictionary<string, int>();
        void Draw2dBitmapFile(string filename, float x1, float y1, float width, float height)
        {
            if (!textures.ContainsKey(filename))
            {
                textures[filename] = LoadTexture(filename);
            }
            Draw2dTexture(textures[filename], x1, y1, width, height, null, Color.White);
        }
        private int LoadTexture(string filename)
        {
            return LoadTexture(new Bitmap(filename));
        }
        public void Draw2dTexture(int textureid, float x1, float y1, float width, float height, int? inAtlasId, Color color)
        {
            RectangleF rect;
            if (inAtlasId == null)
            {
                rect = new RectangleF(0, 0, 1, 1);
            }
            else
            {
                //rect = TextureAtlas.TextureCoords2d(inAtlasId.Value, terrain.texturesPacked);
                rect = new RectangleF();
            }
            GL.PushAttrib(AttribMask.ColorBufferBit);
            GL.Color3(color);
            GL.BindTexture(TextureTarget.Texture2D, textureid);
            GL.Enable(EnableCap.Texture2D);
            GL.Disable(EnableCap.DepthTest);
            GL.Begin(BeginMode.Quads);
            float x2 = x1 + width;
            float y2 = y1 + height;
            GL.TexCoord2(rect.Right, rect.Bottom); GL.Vertex2(x2, y2);
            GL.TexCoord2(rect.Right, rect.Top); GL.Vertex2(x2, y1);
            GL.TexCoord2(rect.Left, rect.Top); GL.Vertex2(x1, y1);
            GL.TexCoord2(rect.Left, rect.Bottom); GL.Vertex2(x1, y2);
            GL.End();
            GL.Enable(EnableCap.DepthTest);
            GL.PopAttrib();
        }
        protected override void OnResize(EventArgs e)
        {
            ResizeGraphics();
            base.OnResize(e);
        }
        void ResizeGraphics()
        {
            // Get new window size
            int width = Width;
            int height = Height;
            float aspect = (float)width / height;
 
            // Adjust graphics to window size
            GL.Viewport(0, 0, width, height);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            //GLU.Perspective(45.0, aspect, 1.0, 100.0);
            GL.MatrixMode(MatrixMode.Modelview);
        }
    }
}

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
exe's picture

We still didn't find a solution.