misc fixes

testkeys:
* Clean up and modernise code
* Use std::endl to end lines for its implicit flush
* Centre window (less likely to hide behind taskbar, etc.)

osdwin:
* Ensure new windows are positioned within the work area of a monitor
This commit is contained in:
Vas Crabb 2019-02-18 18:03:39 +11:00
parent 176ba64b26
commit f17f6c9d5c
4 changed files with 108 additions and 90 deletions

View File

@ -6,23 +6,23 @@
//
//============================================================
#pragma once
#ifndef MAME_OSD_MODULES_OSDHELPER_H
#define MAME_OSD_MODULES_OSDHELPER_H
#ifndef __OSDHELPER__
#define __OSDHELPER__
#pragma once
class osd_dim
{
public:
osd_dim(const int &w, const int &h)
: m_w(w), m_h(h)
{
}
int width() const { return m_w; }
int height() const { return m_h; }
constexpr osd_dim() : m_w(0), m_h(0) { }
constexpr osd_dim(int w, int h) : m_w(w), m_h(h) { }
constexpr int width() const { return m_w; }
constexpr int height() const { return m_h; }
constexpr bool operator!=(const osd_dim &other) { return (m_w != other.width()) || (m_h != other.height()); }
constexpr bool operator==(const osd_dim &other) { return (m_w == other.width()) && (m_h == other.height()); }
bool operator!=(const osd_dim &other) { return (m_w != other.width()) || (m_h != other.height()); }
bool operator==(const osd_dim &other) { return (m_w == other.width()) && (m_h == other.height()); }
private:
int m_w;
int m_h;
@ -31,30 +31,25 @@ private:
class osd_rect
{
public:
osd_rect()
: m_x(0), m_y(0), m_d(0,0)
{
}
osd_rect(const int x, const int y, const int &w, const int &h)
: m_x(x), m_y(y), m_d(w,h)
{
}
osd_rect(const int x, const int y, const osd_dim &d)
: m_x(x), m_y(y), m_d(d)
{
}
int top() const { return m_y; }
int left() const { return m_x; }
int width() const { return m_d.width(); }
int height() const { return m_d.height(); }
constexpr osd_rect() : m_x(0), m_y(0), m_d(0, 0) { }
constexpr osd_rect(int x, int y, int w, int h) : m_x(x), m_y(y), m_d(w, h) { }
constexpr osd_rect(int x, int y, const osd_dim &d) : m_x(x), m_y(y), m_d(d) { }
osd_dim dim() const { return m_d; }
constexpr int left() const { return m_x; }
constexpr int top() const { return m_y; }
constexpr int width() const { return m_d.width(); }
constexpr int height() const { return m_d.height(); }
int bottom() const { return m_y + m_d.height(); }
int right() const { return m_x + m_d.width(); }
constexpr osd_dim dim() const { return m_d; }
osd_rect move_by(int dx, int dy) const { return osd_rect(m_x + dx, m_y + dy, m_d); }
osd_rect resize(int w, int h) const { return osd_rect(m_x, m_y, w, h); }
constexpr int right() const { return m_x + m_d.width(); }
constexpr int bottom() const { return m_y + m_d.height(); }
constexpr osd_rect move_by(int dx, int dy) const { return osd_rect(m_x + dx, m_y + dy, m_d); }
constexpr osd_rect resize(int w, int h) const { return osd_rect(m_x, m_y, w, h); }
constexpr bool operator!=(const osd_rect &other) { return (m_x != other.left()) || (m_y != other.top()) || (m_d != other.dim()); }
constexpr bool operator==(const osd_rect &other) { return (m_x == other.left()) && (m_y == other.top()) && (m_d == other.dim()); }
private:
int m_x;
@ -62,4 +57,4 @@ private:
osd_dim m_d;
};
#endif // __OSDHELPER__
#endif // MAME_OSD_MODULES_OSDHELPER_H

View File

@ -1,19 +1,29 @@
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
// copyright-holders:Aaron Giles, Vas Crabb
//============================================================
//
// video.h - Win32 implementation of MAME video routines
// video.h - Win32 video helpers
//
//============================================================
#ifndef MAME_OSD_WINDOWS_VIDEO_H
#define MAME_OSD_WINDOWS_VIDEO_H
#ifndef __WIN_VIDEO__
#define __WIN_VIDEO__
#pragma once
#include "modules/osdhelper.h"
inline osd_rect RECT_to_osd_rect(const RECT &r)
#include <windows.h>
constexpr osd_rect RECT_to_osd_rect(RECT const &r)
{
return osd_rect(r.left, r.top, r.right - r.left, r.bottom - r.top);
}
#endif
inline RECT osd_rect_to_RECT(osd_rect const &r)
{
RECT const result{ r.left(), r.top(), r.right(), r.bottom() };
return result;
}
#endif // MAME_OSD_WINDOWS_VIDEO_H

View File

@ -15,6 +15,7 @@
#include <process.h>
#include <atomic>
#include <cstring>
#include <chrono>
#include <list>
#include <memory>
@ -1715,17 +1716,35 @@ void win_window_info::adjust_window_position_after_major_change()
if (video_config.keepaspect)
newrect = constrain_to_aspect_ratio(newrect, WMSZ_BOTTOMRIGHT);
}
// in full screen, make sure it covers the primary display
else
{
// in full screen, make sure it covers the primary display
std::shared_ptr<osd_monitor_info> monitor = monitor_from_rect(nullptr);
newrect = monitor->position_size();
}
// restrict the window to one monitor and avoid toolbars if possible
HMONITOR const nearest_monitor = MonitorFromWindow(platform_window(), MONITOR_DEFAULTTONEAREST);
if (NULL != nearest_monitor)
{
MONITORINFO info;
std::memset(&info, 0, sizeof(info));
info.cbSize = sizeof(info);
if (GetMonitorInfo(nearest_monitor, &info))
{
if (newrect.right() > info.rcWork.right)
newrect = newrect.move_by(info.rcWork.right - newrect.right(), 0);
if (newrect.bottom() > info.rcWork.bottom)
newrect = newrect.move_by(0, info.rcWork.bottom - newrect.bottom());
if (newrect.left() < info.rcWork.left)
newrect = newrect.move_by(info.rcWork.left - newrect.left(), 0);
if (newrect.top() < info.rcWork.top)
newrect = newrect.move_by(0, info.rcWork.top - newrect.top());
}
}
// adjust the position if different
if (oldrect.left != newrect.left() || oldrect.top != newrect.top() ||
oldrect.right != newrect.right() || oldrect.bottom != newrect.bottom())
if (RECT_to_osd_rect(oldrect) != newrect)
SetWindowPos(platform_window(), fullscreen() ? HWND_TOPMOST : HWND_TOP,
newrect.left(), newrect.top(),
newrect.width(), newrect.height(), 0);

View File

@ -10,25 +10,21 @@
//
//============================================================
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <wchar.h>
#include "osdcore.h"
#include "SDL2/SDL.h"
#include "osdcore.h"
#include <iostream>
#include <string>
//#include "unicode.h"
struct key_lookup_table
{
int code;
const char *name;
};
struct key_lookup_table { int code; const char *name; };
#define KE(x) { SDL_SCANCODE_##x, "SDL_SCANCODE_" #x },
static key_lookup_table sdl_lookup[] =
static constexpr key_lookup_table sdl_lookup[] =
{
KE(UNKNOWN)
@ -287,58 +283,56 @@ static key_lookup_table sdl_lookup[] =
KE(APP1)
KE(APP2)
{-1, ""}
};
static const char * lookup_key_name(const key_lookup_table *kt, int kc)
static char const *lookup_key_name(int kc)
{
int i=0;
while (kt[i].code>=0)
for (key_lookup_table const &k : sdl_lookup)
{
if (kc==kt[i].code)
return kt[i].name;
i++;
if (k.code == kc)
return k.name;
}
return NULL;
return nullptr;
}
int main(int argc, char *argv[])
{
SDL_Event event;
int quit = 0;
char lasttext[20] = "";
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",
SDL_GetError());
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
SDL_CreateWindow("Input Test", 0, 0, 100, 100,0 );
SDL_CreateWindow("Input Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 100, 100, 0);
SDL_Event event;
bool quit = false;
std::string lasttext;
while (SDL_PollEvent(&event) || !quit) {
switch(event.type) {
case SDL_QUIT:
quit = 1;
quit = true;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
quit=1;
else
{
printf("ITEM_ID_XY %s %s\n",
lookup_key_name(sdl_lookup, event.key.keysym.scancode), "");
lasttext[0] = 0;
if (event.key.keysym.sym == SDLK_ESCAPE) {
quit = true;
} else {
std::cout
<< "ITEM_ID_XY "
<< lookup_key_name(event.key.keysym.scancode)
<< ' '
<< std::endl;
lasttext.clear();
}
break;
case SDL_KEYUP:
printf("ITEM_ID_XY %s %s\n",
lookup_key_name(sdl_lookup, event.key.keysym.scancode), lasttext);
std::cout
<< "ITEM_ID_XY "
<< lookup_key_name(event.key.keysym.scancode)
<< ' '
<< lasttext
<< std::endl;
break;
case SDL_TEXTINPUT:
strcpy(lasttext, event.text.text);
lasttext = event.text.text;
break;
}
event.type = 0;