Skip to content

Commit eb7cb38

Browse files
committed
vdisp/gl: add platform opt/help/info
1 parent 7868344 commit eb7cb38

File tree

1 file changed

+68
-6
lines changed

1 file changed

+68
-6
lines changed

src/video_display/gl.cpp

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#ifdef HAVE_CONFIG_H
7070
#include "config.h" // for HAVE_SPOUT, HAVE_SYPHON
7171
#endif
72+
#include "compat/strings.h" // for strcasecmp
7273
#include "debug.h"
7374
#include "gl_context.h"
7475
#include "host.h"
@@ -365,6 +366,16 @@ static constexpr pair<int64_t, string_view> keybindings[] = {
365366
pair<int64_t, string_view>{K_CTRL_UP, "make window 10% bigger"}
366367
};
367368

369+
const static struct {
370+
int platform_id;
371+
const char *name;
372+
} platform_map[] = {
373+
{ GLFW_PLATFORM_WIN32, "Win32" },
374+
{ GLFW_PLATFORM_COCOA, "Cocoa" },
375+
{ GLFW_PLATFORM_WAYLAND, "Wayland" },
376+
{ GLFW_PLATFORM_X11, "X11" },
377+
};
378+
368379
/* Prototyping */
369380
static bool check_display_gl_version(bool print_ver);
370381
static bool display_gl_init_opengl(struct state_gl *s);
@@ -528,6 +539,38 @@ static void gl_print_monitors(bool fullhelp) {
528539
if (!fullhelp) {
529540
cout << "(use \"fullhelp\" to see modes)\n";
530541
}
542+
printf("\n");
543+
}
544+
545+
static void
546+
gl_print_platforms()
547+
{
548+
printf("available platforms:\n");
549+
for (unsigned i = 0; i < ARR_COUNT(platform_map); ++i) {
550+
if (glfwPlatformSupported(platform_map[i].platform_id)) {
551+
color_printf("\t- " TBOLD("%s") "\n", platform_map[i].name);
552+
}
553+
}
554+
color_printf("\n");
555+
}
556+
557+
static void
558+
gl_print_current_platform()
559+
{
560+
const int platform = glfwGetPlatform();
561+
const char *name = "UNKNOWN/ERROR";
562+
for (unsigned i = 0; i < ARR_COUNT(platform_map); ++i) {
563+
if (platform_map[i].platform_id == platform) {
564+
name = platform_map[i].name;
565+
break;
566+
}
567+
}
568+
#ifdef __linux__
569+
int ll = LOG_LEVEL_NOTICE;
570+
#else
571+
int ll = LOG_LEVEL_VERBOSE;
572+
#endif
573+
log_msg(ll, MOD_NAME "Using platform: %s\n", name);
531574
}
532575

533576
#define FEATURE_PRESENT(x) (strcmp(STRINGIFY(x), "1") == 0 ? "on" : "off")
@@ -590,6 +633,7 @@ static void gl_show_help(bool full) {
590633
col() << TBOLD("\t--param " GL_DISABLE_10B_OPT_PARAM_NAME) << "\tdo not set 10-bit framebuffer (performance issues)\n";
591634
col() << "\n" TBOLD(
592635
"[1]") " position doesn't work in Wayland\n";
636+
col() << TBOLD("\tplatform=<p>") << "\tuse platform (usable only in Linux)\n";
593637
} else {
594638
color_printf(
595639
"\t(use \"" TBOLD("fullhelp") "\" to see options)\n");
@@ -607,7 +651,9 @@ static void gl_show_help(bool full) {
607651
return;
608652
}
609653
gl_print_monitors(full);
610-
color_printf("\n");
654+
if (full) {
655+
gl_print_platforms();
656+
}
611657
GLFWwindow *window = glfwCreateWindow(32, 32, DEFAULT_WIN_NAME, nullptr, nullptr);
612658
if (window != nullptr) {
613659
glfwMakeContextCurrent(window);
@@ -704,6 +750,19 @@ parse_hints(struct state_gl *s, bool window_hints, char *hints)
704750
}
705751
}
706752

753+
static bool
754+
set_platform(struct state_gl *s, const char *platform)
755+
{
756+
for (unsigned i = 0; i < ARR_COUNT(platform_map); ++i) {
757+
if (strcasecmp(platform_map[i].name, platform) == 0) {
758+
s->init_hints[GLFW_PLATFORM] = platform_map[i].platform_id;
759+
return true;
760+
}
761+
}
762+
MSG(ERROR, "Unknown platform: %s\n", platform);
763+
return false;
764+
}
765+
707766
static void
708767
list_hints()
709768
{
@@ -721,7 +780,7 @@ list_hints()
721780
"<https://github.com/glfw/glfw/blob/master/include/GLFW/"
722781
"glfw3.h>).\n\n");
723782

724-
color_printf("Available hints keys and values:\n");
783+
color_printf("Some of hints keys and values:\n");
725784
for (const auto &h : hint_map) {
726785
color_printf("\t" TBOLD("%s") " - %#x\n", h.first.c_str(),
727786
h.second);
@@ -735,6 +794,7 @@ list_hints()
735794
static bool
736795
display_gl_parse_fmt(struct state_gl *s, char *ptr)
737796
{
797+
bool ret = true;
738798
char *tok, *save_ptr = NULL;
739799

740800
while((tok = strtok_r(ptr, ":", &save_ptr)) != NULL) {
@@ -799,9 +859,7 @@ display_gl_parse_fmt(struct state_gl *s, char *ptr)
799859
s->use_pbo = strcasecmp(tok, "pbo") == 0 ? 1 : 0;
800860
} else if (strstr(tok, "size=") == tok ||
801861
strstr(tok, "fixed_size=") == tok) {
802-
if (!set_size(s, tok)) {
803-
return false;
804-
}
862+
ret = ret && set_size(s, tok);
805863
} else if (strcmp(tok, "fixed_size") == 0) {
806864
s->fixed_size = true;
807865
} else if (strcmp(tok, "noresizable") == 0) {
@@ -810,6 +868,8 @@ display_gl_parse_fmt(struct state_gl *s, char *ptr)
810868
parse_hints(s, true, strchr(tok, '=') + 1);
811869
} else if (strstr(tok, "init_hint=") == tok) {
812870
parse_hints(s, false, strchr(tok, '=') + 1);
871+
} else if (IS_KEY_PREFIX(tok, "platform")) {
872+
ret = ret && set_platform(s, strchr(tok, '=') + 1);
813873
} else if (strcmp(tok, "list_hints") == 0) {
814874
list_hints();
815875
return false;
@@ -820,7 +880,7 @@ display_gl_parse_fmt(struct state_gl *s, char *ptr)
820880
ptr = NULL;
821881
}
822882

823-
return true;
883+
return ret;
824884
}
825885

826886
static void * display_gl_init(struct module *parent, const char *fmt, unsigned int flags) {
@@ -1714,6 +1774,8 @@ static bool display_gl_init_opengl(struct state_gl *s)
17141774
return false;
17151775
}
17161776

1777+
gl_print_current_platform();
1778+
17171779
if (s->req_monitor_idx != -1) {
17181780
int count = 0;
17191781
GLFWmonitor **mon = glfwGetMonitors(&count);

0 commit comments

Comments
 (0)