toggle fullscreen using xlib

这篇博客探讨了如何利用Xlib库在Linux环境中实现窗口的全屏切换功能,内容涉及Linux系统的底层交互和图形界面管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

enum
{
  _NET_WM_STATE_REMOVE =0,
  _NET_WM_STATE_ADD = 1,
  _NET_WM_STATE_TOGGLE =2

};

int main()
{
  Display * pDisplay = XOpenDisplay(NULL);
  int screen = DefaultScreen(pDisplay);

  XSetWindowAttributes attr;
  attr.border_pixel = 0;
  attr.background_pixel = 0;
  attr.event_mask = ExposureMask | StructureNotifyMask;

  Window parentWindow = RootWindow(pDisplay, screen);
  Window window = XCreateWindow(pDisplay,
                                parentWindow,
                                0,0, //left top
                                640, 480,
                                0,
                                0,
                                InputOutput,
                                CopyFromParent,
                                CWBackPixel | CWBorderPixel |
CWEventMask,
                                &attr);

  XWarpPointer(pDisplay, None, window, 0, 0, 0, 0, 100, 100);
  XGrabKeyboard(pDisplay, window, True, GrabModeAsync, GrabModeAsync,
CurrentTime);
  XMapRaised(pDisplay, window);

  XSelectInput(pDisplay, window, KeyPressMask | ButtonPressMask);
  bool isFullscreen = false;
  bool run = true;
  while (run)
  {
    XEvent event;
    KeySym keySym;

    while (XPending(pDisplay) > 0)
    {
      XNextEvent(pDisplay, &event);

      switch(event.type)
      {
        case KeyPress:
        {
          isFullscreen = !isFullscreen;

          Atom wmState = XInternAtom(pDisplay, "_NET_WM_STATE", False);
          Atom fullScreen = XInternAtom(pDisplay,
                "_NET_WM_STATE_FULLSCREEN", False);

          XEvent xev;
        xev.xclient.type=ClientMessage;
        xev.xclient.serial = 0;
        xev.xclient.send_event=True;
        xev.xclient.window=window;
        xev.xclient.message_type=wmState;
        xev.xclient.format=32;
        xev.xclient.data.l[0] = (isFullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE);
        xev.xclient.data.l[1] = fullScreen;
        xev.xclient.data.l[2] = 0;

        XSendEvent(pDisplay, DefaultRootWindow(pDisplay), False,
                   SubstructureRedirectMask | SubstructureNotifyMask,
                    &xev);
          break;
        }
        case ButtonPress:
        {
          run = false;
          break;
        }
        default:
        {
          break;
        }

      }
    }
  }
  XCloseDisplay(pDisplay);

}

阅读(343) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值