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__ #pragma once
#define __OSDHELPER__
class osd_dim class osd_dim
{ {
public: public:
osd_dim(const int &w, const int &h) constexpr osd_dim() : m_w(0), m_h(0) { }
: m_w(w), m_h(h) constexpr osd_dim(int w, int h) : m_w(w), m_h(h) { }
{
} constexpr int width() const { return m_w; }
int width() const { return m_w; } constexpr int height() const { return m_h; }
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: private:
int m_w; int m_w;
int m_h; int m_h;
@ -31,30 +31,25 @@ private:
class osd_rect class osd_rect
{ {
public: public:
osd_rect() constexpr osd_rect() : m_x(0), m_y(0), m_d(0, 0) { }
: 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_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(); }
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(); } constexpr osd_dim dim() const { return m_d; }
int right() const { return m_x + m_d.width(); }
osd_rect move_by(int dx, int dy) const { return osd_rect(m_x + dx, m_y + dy, m_d); } constexpr int right() const { return m_x + m_d.width(); }
osd_rect resize(int w, int h) const { return osd_rect(m_x, m_y, w, h); } 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: private:
int m_x; int m_x;
@ -62,4 +57,4 @@ private:
osd_dim m_d; osd_dim m_d;
}; };
#endif // __OSDHELPER__ #endif // MAME_OSD_MODULES_OSDHELPER_H

View File

@ -1,19 +1,29 @@
// license:BSD-3-Clause // 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__ #pragma once
#define __WIN_VIDEO__
#include "modules/osdhelper.h" #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); 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 <process.h>
#include <atomic> #include <atomic>
#include <cstring>
#include <chrono> #include <chrono>
#include <list> #include <list>
#include <memory> #include <memory>
@ -1663,7 +1664,7 @@ void win_window_info::minimize_window()
RECT bounds; RECT bounds;
GetWindowRect(platform_window(), &bounds); GetWindowRect(platform_window(), &bounds);
osd_rect newrect(bounds.left, bounds.top, newsize ); osd_rect newrect(bounds.left, bounds.top, newsize);
SetWindowPos(platform_window(), nullptr, newrect.left(), newrect.top(), newrect.width(), newrect.height(), SWP_NOZORDER); SetWindowPos(platform_window(), nullptr, newrect.left(), newrect.top(), newrect.width(), newrect.height(), SWP_NOZORDER);
@ -1715,17 +1716,35 @@ void win_window_info::adjust_window_position_after_major_change()
if (video_config.keepaspect) if (video_config.keepaspect)
newrect = constrain_to_aspect_ratio(newrect, WMSZ_BOTTOMRIGHT); newrect = constrain_to_aspect_ratio(newrect, WMSZ_BOTTOMRIGHT);
} }
// in full screen, make sure it covers the primary display
else else
{ {
// in full screen, make sure it covers the primary display
std::shared_ptr<osd_monitor_info> monitor = monitor_from_rect(nullptr); std::shared_ptr<osd_monitor_info> monitor = monitor_from_rect(nullptr);
newrect = monitor->position_size(); 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 // adjust the position if different
if (oldrect.left != newrect.left() || oldrect.top != newrect.top() || if (RECT_to_osd_rect(oldrect) != newrect)
oldrect.right != newrect.right() || oldrect.bottom != newrect.bottom())
SetWindowPos(platform_window(), fullscreen() ? HWND_TOPMOST : HWND_TOP, SetWindowPos(platform_window(), fullscreen() ? HWND_TOPMOST : HWND_TOP,
newrect.left(), newrect.top(), newrect.left(), newrect.top(),
newrect.width(), newrect.height(), 0); newrect.width(), newrect.height(), 0);

View File

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