namespace {
-bool error_flag=false;
+bool error_flag = false;
std::string error_msg;
#ifndef WIN32
char err[128];
XGetErrorText(display, event->error_code, err, sizeof(err));
- string request_code=Msp::lexical_cast(static_cast<int>(event->request_code));
+ string request_code = Msp::lexical_cast(static_cast<int>(event->request_code));
char req[128];
XGetErrorDatabaseText(display, "XRequest", request_code.c_str(), request_code.c_str(), req, sizeof(req));
- string msg=Msp::format("Request %s failed with %s [%08X]", req, err, event->resourceid);
+ string msg = Msp::format("Request %s failed with %s [%08X]", req, err, event->resourceid);
if(error_flag)
cerr<<"Discarding error: "<<msg<<'\n';
else
{
cerr<<msg<<'\n';
- error_msg=msg;
- error_flag=true;
+ error_msg = msg;
+ error_flag = true;
}
return 0;
break;
VideoMode mode(info.dmPelsWidth, info.dmPelsHeight);
- mode.rate=info.dmDisplayFrequency;
+ mode.rate = info.dmDisplayFrequency;
modes.push_back(mode);
}
DEVMODE info;
if(EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &info))
{
- orig_mode=VideoMode(info.dmPelsWidth, info.dmPelsHeight);
- orig_mode.rate=info.dmDisplayFrequency;
+ orig_mode = VideoMode(info.dmPelsWidth, info.dmPelsHeight);
+ orig_mode.rate = info.dmDisplayFrequency;
}
#else
if(disp_name.empty())
- priv->display=XOpenDisplay(0);
+ priv->display = XOpenDisplay(0);
else
- priv->display=XOpenDisplay(disp_name.c_str());
+ priv->display = XOpenDisplay(disp_name.c_str());
if(!priv->display)
throw Exception("Couldn't open X display");
XSetErrorHandler(x_error_handler);
#ifdef WITH_XF86VIDMODE
- int screen=DefaultScreen(priv->display);
+ int screen = DefaultScreen(priv->display);
int nmodes;
XF86VidModeModeInfo **infos;
XF86VidModeGetAllModeLines(priv->display, screen, &nmodes, &infos);
for(int i=0; i<nmodes; ++i)
{
- XF86VidModeModeInfo &info=*infos[i];
+ XF86VidModeModeInfo &info = *infos[i];
VideoMode mode(info.hdisplay, info.vdisplay);
if(info.htotal && info.vtotal)
- mode.rate=info.dotclock/(info.htotal*info.vtotal);
+ mode.rate = info.dotclock/(info.htotal*info.vtotal);
modes.push_back(mode);
}
XF86VidModeModeLine modeline;
int dotclock;
XF86VidModeGetModeLine(priv->display, screen, &dotclock, &modeline);
- orig_mode=VideoMode(modeline.hdisplay, modeline.vdisplay);
+ orig_mode = VideoMode(modeline.hdisplay, modeline.vdisplay);
if(modeline.htotal && modeline.vtotal)
- orig_mode.rate=dotclock/(modeline.htotal*modeline.vtotal);
+ orig_mode.rate = dotclock/(modeline.htotal*modeline.vtotal);
#endif
#endif
}
void Display::add_window(Window &wnd)
{
- priv->windows[wnd.get_private().window]=&wnd;
+ priv->windows[wnd.get_private().window] = &wnd;
}
void Display::remove_window(Window &wnd)
{
#if defined(WIN32)
DEVMODE info;
- info.dmSize=sizeof(DEVMODE);
- info.dmFields=DM_PELSWIDTH|DM_PELSHEIGHT;
- info.dmPelsWidth=mode.width;
- info.dmPelsHeight=mode.height;
+ info.dmSize = sizeof(DEVMODE);
+ info.dmFields = DM_PELSWIDTH|DM_PELSHEIGHT;
+ info.dmPelsWidth = mode.width;
+ info.dmPelsHeight = mode.height;
if(mode.rate)
{
- info.dmFields|=DM_DISPLAYFREQUENCY;
- info.dmDisplayFrequency=mode.rate;
+ info.dmFields |= DM_DISPLAYFREQUENCY;
+ info.dmDisplayFrequency = mode.rate;
}
ChangeDisplaySettings(&info, CDS_FULLSCREEN);
#elif defined(WITH_XF86VIDMODE)
- int screen=DefaultScreen(priv->display);
+ int screen = DefaultScreen(priv->display);
int nmodes;
XF86VidModeModeInfo **infos;
XF86VidModeGetAllModeLines(priv->display, screen, &nmodes, &infos);
for(int i=0; i<nmodes; ++i)
{
- XF86VidModeModeInfo &info=*infos[i];
+ XF86VidModeModeInfo &info = *infos[i];
- unsigned rate=0;
+ unsigned rate = 0;
if(info.htotal && info.vtotal)
- rate=info.dotclock/(info.htotal*info.vtotal);
+ rate = info.dotclock/(info.htotal*info.vtotal);
if(info.hdisplay==mode.width && info.vdisplay==mode.height && (mode.rate==0 || rate==mode.rate))
{
XF86VidModeSwitchToMode(priv->display, screen, &info);
else
break;
#else
- int pending=XPending(priv->display);
+ int pending = XPending(priv->display);
if(pending==0)
break;
check_error();
- map<WindowHandle, Window *>::iterator j=priv->windows.find(event.xevent.xany.window);
+ map<WindowHandle, Window *>::iterator j = priv->windows.find(event.xevent.xany.window);
if(j!=priv->windows.end())
{
/* Filter keyboard autorepeat. If this packet is a KeyRelease and
indicate autorepeat and must be dropped. */
if(event.xevent.type==KeyRelease && !j->second->get_keyboard_autorepeat() && pending>0)
{
- XKeyEvent &kev=event.xevent.xkey;
+ XKeyEvent &kev = event.xevent.xkey;
XEvent ev2;
XPeekEvent(priv->display, &ev2);
if(ev2.type==KeyPress)
{
- XKeyEvent &kev2=ev2.xkey;
+ XKeyEvent &kev2 = ev2.xkey;
if(kev2.window==kev.window && kev2.time==kev.time && kev2.keycode==kev.keycode)
{
XNextEvent(priv->display, &ev2);
{
if(error_flag)
{
- error_flag=false;
+ error_flag = false;
throw Exception(error_msg);
}
}
Private *priv;
public:
- Display(const std::string &disp_name=std::string());
+ Display(const std::string &disp_name = std::string());
~Display();
const Private &get_private() const { return *priv; }
#ifdef WIN32
throw Exception("DrawContext not supported on win32 yet");
#else
- priv=new Private;
+ priv = new Private;
- ::Display *dpy=display.get_private().display;
+ ::Display *dpy = display.get_private().display;
- priv->use_shm=XShmQueryExtension(dpy);
+ priv->use_shm = XShmQueryExtension(dpy);
XWindowAttributes wa;
XGetWindowAttributes(dpy, window.get_private().window, &wa);
if(priv->use_shm)
{
- priv->image=XShmCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, &priv->shminfo, wa.width, wa.height);
+ priv->image = XShmCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, &priv->shminfo, wa.width, wa.height);
if(!priv->image)
throw Exception("Could not create shared memory XImage");
- priv->shminfo.shmid=shmget(IPC_PRIVATE, priv->image->bytes_per_line*priv->image->height, IPC_CREAT|0666);
- priv->shminfo.shmaddr=priv->image->data=reinterpret_cast<char *>(shmat(priv->shminfo.shmid, 0, 0));
- priv->shminfo.readOnly=false;
+ priv->shminfo.shmid = shmget(IPC_PRIVATE, priv->image->bytes_per_line*priv->image->height, IPC_CREAT|0666);
+ priv->shminfo.shmaddr=priv->image->data = reinterpret_cast<char *>(shmat(priv->shminfo.shmid, 0, 0));
+ priv->shminfo.readOnly = false;
XShmAttach(dpy, &priv->shminfo);
}
else
{
- priv->image=XCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, 0, wa.width, wa.height, 8, 0);
+ priv->image = XCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, 0, wa.width, wa.height, 8, 0);
if(!priv->image)
throw Exception("Could not create XImage");
- priv->image->data=new char[priv->image->bytes_per_line*priv->image->height];
+ priv->image->data = new char[priv->image->bytes_per_line*priv->image->height];
}
#endif
}
void DrawContext::update()
{
#ifndef WIN32
- ::Display *dpy=display.get_private().display;
- WindowHandle wnd=window.get_private().window;
+ ::Display *dpy = display.get_private().display;
+ WindowHandle wnd = window.get_private().window;
- GC gc=XCreateGC(dpy, wnd, 0, 0);
+ GC gc = XCreateGC(dpy, wnd, 0, 0);
if(priv->use_shm)
XShmPutImage(dpy, wnd, gc, priv->image, 0, 0, 0, 0, priv->image->width, priv->image->height, false);
window(wnd)
{
#ifdef WITH_OPENGL
- priv=new Private;
+ priv = new Private;
#ifdef WIN32
- HDC dc=GetDC(window.get_private().window);
+ HDC dc = GetDC(window.get_private().window);
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
- pfd.nSize=sizeof(pfd);
- pfd.nVersion=1;
- pfd.dwFlags=PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
+ pfd.nSize = sizeof(pfd);
+ pfd.nVersion = 1;
+ pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
if(opts.doublebuffer)
- pfd.dwFlags|=PFD_DOUBLEBUFFER;
- pfd.iPixelType=PFD_TYPE_RGBA;
+ pfd.dwFlags |= PFD_DOUBLEBUFFER;
+ pfd.iPixelType = PFD_TYPE_RGBA;
if(opts.alpha)
- pfd.cAlphaBits=1;
- pfd.cDepthBits=1;
+ pfd.cAlphaBits = 1;
+ pfd.cDepthBits = 1;
if(opts.stencil)
- pfd.cStencilBits=1;
+ pfd.cStencilBits = 1;
- int pf_index=ChoosePixelFormat(dc, &pfd);
+ int pf_index = ChoosePixelFormat(dc, &pfd);
if(!pf_index)
throw Exception("Couldn't find a suitable pixel format");
SetPixelFormat(dc, pf_index, &pfd);
- priv->context=wglCreateContext(dc);
+ priv->context = wglCreateContext(dc);
wglMakeCurrent(dc, priv->context);
ReleaseDC(window.get_private().window, dc);
attribs.push_back(0);
- ::Display *dpy=display.get_private().display;
+ ::Display *dpy = display.get_private().display;
- XVisualInfo *vi=glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
+ XVisualInfo *vi = glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
if(!vi)
throw Exception("Couldn't find a suitable GLX visual");
- priv->context=glXCreateContext(dpy, vi, 0, true);
+ priv->context = glXCreateContext(dpy, vi, 0, true);
XSetWindowAttributes attr;
- attr.colormap=XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
+ attr.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
- priv->subwnd=XCreateWindow(dpy, window.get_private().window, 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
+ priv->subwnd = XCreateWindow(dpy, window.get_private().window, 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
XMapWindow(dpy, priv->subwnd);
XFree(vi);
wglMakeCurrent(0, 0);
wglDeleteContext(priv->context);
#else
- ::Display *dpy=display.get_private().display;
+ ::Display *dpy = display.get_private().display;
glXMakeCurrent(dpy, 0, 0);
glXDestroyContext(dpy, priv->context);
{
#ifdef WITH_OPENGL
#ifdef WIN32
- HDC dc=GetDC(window.get_private().window);
+ HDC dc = GetDC(window.get_private().window);
SwapBuffers(dc);
ReleaseDC(window.get_private().window, dc);
#else
Private *priv;
public:
- GLContext(Window &wnd, const GLOptions &opts=GLOptions());
+ GLContext(Window &wnd, const GLOptions &opts = GLOptions());
~GLContext();
void swap_buffers();
Image::Private::Private()
{
#ifdef WITH_DEVIL
- id=0;
+ id = 0;
#endif
#ifdef WITH_LIBPNG
- fmt=RGB;
- width=0;
- height=0;
- data=0;
+ fmt = RGB;
+ width = 0;
+ height = 0;
+ data = 0;
#endif
}
#ifdef WITH_LIBPNG
void read(png_struct *png, png_byte *data, png_size_t size)
{
- IO::Base *in=reinterpret_cast<IO::Base *>(png_get_io_ptr(png));
+ IO::Base *in = reinterpret_cast<IO::Base *>(png_get_io_ptr(png));
in->read(reinterpret_cast<char *>(data), size);
}
void load_png(IO::Base &in, Image::Private &priv)
{
- png_struct *png=0;
- png_info *info=0;
- priv.data=0;
+ png_struct *png = 0;
+ png_info *info = 0;
+ priv.data = 0;
try
{
- png=png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
- info=png_create_info_struct(png);
+ png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
+ info = png_create_info_struct(png);
if(setjmp(png_jmpbuf(png)))
throw Exception("Error loading PNG image");
int depth;
int color;
png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
- priv.width=width;
- priv.height=height;
+ priv.width = width;
+ priv.height = height;
if(depth!=8)
throw Exception("Only 8-bit PNG images are supported");
switch(color)
{
- case PNG_COLOR_TYPE_PALETTE: priv.fmt=COLOR_INDEX; break;
- case PNG_COLOR_TYPE_GRAY: priv.fmt=LUMINANCE; break;
- case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt=LUMINANCE_ALPHA; break;
- case PNG_COLOR_TYPE_RGB: priv.fmt=RGB; break;
- case PNG_COLOR_TYPE_RGB_ALPHA: priv.fmt=RGBA; break;
+ case PNG_COLOR_TYPE_PALETTE: priv.fmt = COLOR_INDEX; break;
+ case PNG_COLOR_TYPE_GRAY: priv.fmt = LUMINANCE; break;
+ case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt = LUMINANCE_ALPHA; break;
+ case PNG_COLOR_TYPE_RGB: priv.fmt = RGB; break;
+ case PNG_COLOR_TYPE_RGB_ALPHA: priv.fmt = RGBA; break;
default: throw Exception("Unknown color type");
}
- unsigned nchans=png_get_channels(png, info);
+ unsigned nchans = png_get_channels(png, info);
if(nchans==4 && priv.fmt==RGB)
png_set_strip_alpha(png);
- unsigned rowstride=priv.width*nchans;
- priv.data=new char[rowstride*priv.height];
+ unsigned rowstride = priv.width*nchans;
+ priv.data = new char[rowstride*priv.height];
for(unsigned y=0; y<priv.height; ++y)
png_read_row(png, reinterpret_cast<png_byte *>(priv.data+rowstride*(priv.height-1-y)), 0);
#ifdef WITH_DEVIL
void ensure_devil_image(unsigned &id)
{
- static bool init_done=false;
+ static bool init_done = false;
if(!init_done)
{
ilInit();
ilEnable(IL_ORIGIN_SET);
ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
- init_done=true;
+ init_done = true;
}
if(!id)
{
if(msg==WM_CREATE)
{
- CREATESTRUCT *cs=reinterpret_cast<CREATESTRUCT *>(lparam);
+ CREATESTRUCT *cs = reinterpret_cast<CREATESTRUCT *>(lparam);
SetWindowLong(hwnd, 0, reinterpret_cast<LONG>(cs->lpCreateParams));
}
else
{
- Msp::Graphics::Window *wnd=reinterpret_cast<Msp::Graphics::Window *>(GetWindowLong(hwnd, 0));
+ Msp::Graphics::Window *wnd = reinterpret_cast<Msp::Graphics::Window *>(GetWindowLong(hwnd, 0));
Msp::Graphics::Window::Event ev;
- ev.msg=msg;
- ev.wparam=wparam;
- ev.lparam=lparam;
+ ev.msg = msg;
+ ev.wparam = wparam;
+ ev.lparam = lparam;
if(wnd && wnd->event(ev))
return 0;
}
Window::Window(Display &dpy, unsigned w, unsigned h, bool fs):
display(dpy)
{
- options.width=w;
- options.height=h;
- options.fullscreen=fs;
+ options.width = w;
+ options.height = h;
+ options.fullscreen = fs;
init();
}
void Window::init()
{
- visible=false;
- kbd_autorepeat=true;
- resizing=false;
- priv=new Private;
+ visible = false;
+ kbd_autorepeat = true;
+ resizing = false;
+ priv = new Private;
#ifdef WIN32
- static bool wndclass_created=false;
+ static bool wndclass_created = false;
if(!wndclass_created)
{
WNDCLASSEX wndcl;
- wndcl.cbSize=sizeof(WNDCLASSEX);
- wndcl.style=0;
- wndcl.lpfnWndProc=&wndproc_;
- wndcl.cbClsExtra=0;
- wndcl.cbWndExtra=sizeof(Window *);
- wndcl.hInstance=reinterpret_cast<HINSTANCE>(Application::get_data());
- wndcl.hIcon=0;
- wndcl.hCursor=LoadCursor(0, IDC_ARROW);
- wndcl.hbrBackground=0;
- wndcl.lpszMenuName=0;
- wndcl.lpszClassName="mspgbase";
- wndcl.hIconSm=0;
+ wndcl.cbSize = sizeof(WNDCLASSEX);
+ wndcl.style = 0;
+ wndcl.lpfnWndProc = &wndproc_;
+ wndcl.cbClsExtra = 0;
+ wndcl.cbWndExtra = sizeof(Window *);
+ wndcl.hInstance = reinterpret_cast<HINSTANCE>(Application::get_data());
+ wndcl.hIcon = 0;
+ wndcl.hCursor = LoadCursor(0, IDC_ARROW);
+ wndcl.hbrBackground = 0;
+ wndcl.lpszMenuName = 0;
+ wndcl.lpszClassName = "mspgbase";
+ wndcl.hIconSm = 0;
if(!RegisterClassEx(&wndcl))
throw Exception("Couldn't register window class");
- wndclass_created=true;
+ wndclass_created = true;
}
RECT rect;
SetRect(&rect, 0, 0, options.width, options.height);
- int style=(options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
+ int style = (options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
if(!options.resizable)
- style&=~WS_THICKFRAME;
- int exstyle=(options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
+ style &= ~WS_THICKFRAME;
+ int exstyle = (options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
AdjustWindowRectEx(&rect, style, false, exstyle);
- priv->window=CreateWindowEx(exstyle,
+ priv->window = CreateWindowEx(exstyle,
"mspgbase",
"Window",
style,
throw Exception("CreateWindowEx failed");
#else
- ::Display *dpy=display.get_private().display;
+ ::Display *dpy = display.get_private().display;
- priv->wm_delete_window=XInternAtom(dpy, "WM_DELETE_WINDOW", true);
- priv->invisible_cursor=0;
+ priv->wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", true);
+ priv->invisible_cursor = 0;
XSetWindowAttributes attr;
- attr.override_redirect=options.fullscreen;
- attr.event_mask=ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask|EnterWindowMask;
+ attr.override_redirect = options.fullscreen;
+ attr.event_mask = ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask|EnterWindowMask;
- priv->window=XCreateWindow(dpy,
+ priv->window = XCreateWindow(dpy,
DefaultRootWindow(dpy),
0, 0,
options.width, options.height,
if(!options.resizable)
{
XSizeHints hints;
- hints.flags=PMinSize|PMaxSize;
- hints.min_width=hints.max_width=options.width;
- hints.min_height=hints.max_height=options.height;
+ hints.flags = PMinSize|PMaxSize;
+ hints.min_width=hints.max_width = options.width;
+ hints.min_height=hints.max_height = options.height;
XSetWMNormalHints(dpy, priv->window, &hints);
}
#else
vector<unsigned char> buf(title.begin(), title.end());
XTextProperty prop;
- prop.value=&buf[0];
- prop.encoding=XA_STRING;
- prop.format=8;
- prop.nitems=title.size();
+ prop.value = &buf[0];
+ prop.encoding = XA_STRING;
+ prop.format = 8;
+ prop.nitems = title.size();
XSetWMName(display.get_private().display, priv->window, &prop);
display.check_error();
#endif
void Window::reconfigure(const WindowOptions &opts)
{
- bool fullscreen_changed=(opts.fullscreen!=options.fullscreen);
- resizing=(opts.width!=options.width || opts.height!=options.height);
+ bool fullscreen_changed = (opts.fullscreen!=options.fullscreen);
+ resizing = (opts.width!=options.width || opts.height!=options.height);
- options=opts;
+ options = opts;
#ifdef WIN32
RECT rect;
SetRect(&rect, 0, 0, options.width, options.height);
- int style=(options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
+ int style = (options.fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW);
if(!options.resizable)
- style&=~WS_THICKFRAME;
- int exstyle=(options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
+ style &= ~WS_THICKFRAME;
+ int exstyle = (options.fullscreen ? WS_EX_APPWINDOW : WS_EX_OVERLAPPEDWINDOW);
AdjustWindowRectEx(&rect, style, false, exstyle);
if(fullscreen_changed)
else
SetWindowPos(priv->window, 0, 0, 0, rect.right-rect.left, rect.bottom-rect.top, SWP_NOMOVE|SWP_NOZORDER);
#else
- ::Display *dpy=display.get_private().display;
+ ::Display *dpy = display.get_private().display;
- bool was_visible=visible;
+ bool was_visible = visible;
if(fullscreen_changed)
{
if(was_visible)
// Wait for the window to be unmapped. This makes window managers happy.
XEvent ev;
- int ev_type=UnmapNotify;
+ int ev_type = UnmapNotify;
XPeekIfEvent(dpy, &ev, match_event_type, reinterpret_cast<char *>(&ev_type));
}
XSetWindowAttributes attr;
- attr.override_redirect=options.fullscreen;
+ attr.override_redirect = options.fullscreen;
XChangeWindowAttributes(dpy, priv->window, CWOverrideRedirect, &attr);
}
XSizeHints hints;
if(options.resizable)
- hints.flags=0;
+ hints.flags = 0;
else
{
- hints.flags=PMinSize|PMaxSize;
- hints.min_width=hints.max_width=options.width;
- hints.min_height=hints.max_height=options.height;
+ hints.flags = PMinSize|PMaxSize;
+ hints.min_width=hints.max_width = options.width;
+ hints.min_height=hints.max_height = options.height;
}
XSetWMNormalHints(dpy, priv->window, &hints);
void Window::set_keyboard_autorepeat(bool r)
{
- kbd_autorepeat=r;
+ kbd_autorepeat = r;
}
void Window::show_cursor(bool s)
#ifdef WIN32
ShowCursor(s);
#else
- ::Display *dpy=display.get_private().display;
+ ::Display *dpy = display.get_private().display;
if(s)
XUndefineCursor(dpy, priv->window);
{
if(!priv->invisible_cursor)
{
- int screen=DefaultScreen(dpy);
+ int screen = DefaultScreen(dpy);
- Pixmap pm=XCreatePixmap(dpy, priv->window, 1, 1, 1);
- GC gc=XCreateGC(dpy, pm, 0, 0);
+ Pixmap pm = XCreatePixmap(dpy, priv->window, 1, 1, 1);
+ GC gc = XCreateGC(dpy, pm, 0, 0);
XSetFunction(dpy, gc, GXclear);
XDrawPoint(dpy, pm, gc, 0, 0);
XFreeGC(dpy, gc);
XColor black;
- black.pixel=BlackPixel(dpy, screen);
+ black.pixel = BlackPixel(dpy, screen);
XQueryColor(dpy, DefaultColormap(dpy, screen), &black);
- priv->invisible_cursor=XCreatePixmapCursor(dpy, pm, pm, &black, &black, 0, 0);
+ priv->invisible_cursor = XCreatePixmapCursor(dpy, pm, pm, &black, &black, 0, 0);
XFreePixmap(dpy, pm);
}
#else
XMapRaised(display.get_private().display, priv->window);
#endif
- visible=true;
+ visible = true;
if(options.fullscreen)
{
#else
XUnmapWindow(display.get_private().display, priv->window);
#endif
- visible=false;
+ visible = false;
if(options.fullscreen)
display.restore_mode();
bool Window::event(const Event &evnt)
{
#ifdef WIN32
- WPARAM wp=evnt.wparam;
- LPARAM lp=evnt.lparam;
+ WPARAM wp = evnt.wparam;
+ LPARAM lp = evnt.lparam;
switch(evnt.msg)
{
case WM_KEYDOWN:
signal_pointer_motion.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp));
break;
case WM_SIZE:
- options.width=LOWORD(lp);
- options.height=HIWORD(lp);
+ options.width = LOWORD(lp);
+ options.height = HIWORD(lp);
signal_resize.emit(options.width, options.height);
break;
case WM_CLOSE:
return false;
}
#else
- const XEvent &ev=evnt.xevent;
+ const XEvent &ev = evnt.xevent;
switch(ev.type)
{
case ButtonPress:
case ConfigureNotify:
if((ev.xconfigure.width==static_cast<int>(options.width) && ev.xconfigure.height==static_cast<int>(options.height)) == resizing)
{
- options.width=ev.xconfigure.width;
- options.height=ev.xconfigure.height;
- resizing=false;
+ options.width = ev.xconfigure.width;
+ options.height = ev.xconfigure.height;
+ resizing = false;
signal_resize.emit(options.width, options.height);
}
#ifdef WITH_XF86VIDMODE
if(options.fullscreen)
{
- ::Display *dpy=display.get_private().display;
- int screen=DefaultScreen(dpy);
+ ::Display *dpy = display.get_private().display;
+ int screen = DefaultScreen(dpy);
XF86VidModeSetViewPort(dpy, screen, ev.xconfigure.x, ev.xconfigure.y);
}
#endif
Private *priv;
public:
- Window(Display &, unsigned w, unsigned h, bool fs=false);
+ Window(Display &, unsigned w, unsigned h, bool fs = false);
Window(Display &, const WindowOptions &);
private:
void init();
void BinaryControl::set_threshold(float t)
{
- threshold=t;
+ threshold = t;
}
void BinaryControl::on_press()
{
if(!state)
{
- state=true;
+ state = true;
signal_press.emit();
}
}
{
if(state)
{
- state=false;
+ state = false;
signal_release.emit();
}
}
void Control::capture(Device &d)
{
notify_callbacks();
- capture_dev=&d;
+ capture_dev = &d;
capture_dev->signal_button_press.connect(sigc::mem_fun(this, &Control::button_press));
capture_dev->signal_axis_motion.connect(sigc::mem_fun(this, &Control::axis_motion));
}
void Control::cancel_capture()
{
notify_callbacks();
- capture_dev=0;
+ capture_dev = 0;
connect_signals();
}
void Control::set_source(const ControlSource &s)
{
notify_callbacks();
- src=s;
+ src = s;
connect_signals();
}
{
if(capture_dev)
{
- src.dev=capture_dev;
- src.type=BUTTON;
- src.index=i;
+ src.dev = capture_dev;
+ src.type = BUTTON;
+ src.index = i;
notify_callbacks();
- capture_dev=0;
+ capture_dev = 0;
connect_signals();
signal_capture_complete.emit();
}
{
if(capture_dev)
{
- ControlSrcType type=NONE;
+ ControlSrcType type = NONE;
if(v<-0.9)
- type=AXIS_NEG;
+ type = AXIS_NEG;
else if(v>0.9)
- type=AXIS_POS;
+ type = AXIS_POS;
if(type!=NONE)
{
- src.dev=capture_dev;
- src.type=type;
- src.index=i;
+ src.dev = capture_dev;
+ src.type = type;
+ src.index = i;
notify_callbacks();
- capture_dev=0;
+ capture_dev = 0;
connect_signals();
signal_capture_complete.emit();
}
void set_source(const ControlSource &);
const ControlSource &get_source() const { return src; }
protected:
- virtual void on_press() =0;
- virtual void on_release() =0;
- virtual void on_motion(float, float) =0;
+ virtual void on_press() = 0;
+ virtual void on_release() = 0;
+ virtual void on_motion(float, float) = 0;
private:
void connect_signals();
if(state!=buttons[btn])
{
- buttons[btn]=state;
+ buttons[btn] = state;
if(event)
{
if(value!=axes[axis])
{
- float old=axes[axis];
- axes[axis]=value;
+ float old = axes[axis];
+ axes[axis] = value;
if(event)
signal_axis_motion.emit(axis, value, value-old);
Hub::Hub()
{
- name="Hub";
+ name = "Hub";
}
unsigned Hub::attach(Device &dev)
{
- unsigned index=devices.size();
+ unsigned index = devices.size();
devices.push_back(&dev);
dev.signal_button_press.connect(sigc::bind(sigc::mem_fun(this, &Hub::button_press), index));
dev.signal_button_release.connect(sigc::bind(sigc::mem_fun(this, &Hub::button_release), index));
std::string Hub::get_button_name(unsigned btn) const
{
- unsigned dev_index=btn>>12;
+ unsigned dev_index = btn>>12;
if(dev_index>=devices.size())
throw InvalidParameterValue("Button does not exist");
- const Device &dev=*devices[dev_index];
+ const Device &dev = *devices[dev_index];
return dev.get_name()+": "+dev.get_button_name(btn&0xFFF);
}
std::string Hub::get_axis_name(unsigned axis) const
{
- unsigned dev_index=axis>>12;
+ unsigned dev_index = axis>>12;
if(dev_index>=devices.size())
throw InvalidParameterValue("Axis does not exist");
- const Device &dev=*devices[dev_index];
+ const Device &dev = *devices[dev_index];
return dev.get_name()+": "+dev.get_axis_name(axis&0xFFF);
}
Keyboard::Keyboard(Graphics::EventSource &s):
source(s)
{
- name="Keyboard";
+ name = "Keyboard";
buttons.resize(N_KEYS_, false);
if(btn==0)
return "None";
#ifndef WIN32
- const char *str=XKeysymToString(key_to_sys(btn));
+ const char *str = XKeysymToString(key_to_sys(btn));
if(!str)
return format("Key %d", btn);
return str;
#else
char buf[128];
- unsigned scan=MapVirtualKey(key_to_sys(btn), MAPVK_VK_TO_VSC);
+ unsigned scan = MapVirtualKey(key_to_sys(btn), MAPVK_VK_TO_VSC);
if(!GetKeyNameText(scan<<16, buf, sizeof(buf)))
return format("Key %d", btn);
return buf;
unsigned key_from_sys(unsigned code)
{
- static bool init_done=false;
+ static bool init_done = false;
static map<unsigned, unsigned> reverse_map;
if(!init_done)
{
for(unsigned i=0; i<N_KEYS_; ++i)
if(keymap[i])
- reverse_map[keymap[i]]=i;
+ reverse_map[keymap[i]] = i;
- init_done=true;
+ init_done = true;
}
- map<unsigned, unsigned>::const_iterator i=reverse_map.find(code);
+ map<unsigned, unsigned>::const_iterator i = reverse_map.find(code);
if(i!=reverse_map.end())
return i->second;
unsigned result = 0;
for(unsigned i=0; i<N_MODS_; ++i)
if(mod&modmap[i])
- result|=1<<i;
+ result |= 1<<i;
return result;
}
Mouse::Mouse(Graphics::EventSource &s):
source(s)
{
- name="Mouse";
+ name = "Mouse";
buttons.resize(3);
axes.resize(2);
void SmoothControl::set_dead_zone(float d)
{
- dead_zone=d;
+ dead_zone = d;
}
void SmoothControl::set_threshold(float t)
{
- threshold=t;
+ threshold = t;
}
void SmoothControl::pair(SmoothControl *ctrl)
if(paired_ctrl)
{
- SmoothControl *old_pair=paired_ctrl;
- paired_ctrl=0;
+ SmoothControl *old_pair = paired_ctrl;
+ paired_ctrl = 0;
old_pair->pair(0);
}
- paired_ctrl=ctrl;
+ paired_ctrl = ctrl;
if(paired_ctrl)
paired_ctrl->pair(this);
void SmoothControl::on_motion(float v, float r)
{
if(v<-threshold)
- value=-1;
+ value = -1;
else if(v>threshold)
- value=1;
+ value = 1;
else if(v<-dead_zone)
- value=(v+dead_zone)/(threshold-dead_zone);
+ value = (v+dead_zone)/(threshold-dead_zone);
else if(v>dead_zone)
- value=(v-dead_zone)/(threshold-dead_zone);
+ value = (v-dead_zone)/(threshold-dead_zone);
else
- value=0;
+ value = 0;
signal_motion.emit(value);