Add automatic input rate selection by polling XRandR.

This commit is contained in:
Brandon Wright 2018-04-14 16:21:13 -05:00
parent 287fab5f1b
commit 569702e75a
2 changed files with 98 additions and 18 deletions

View File

@ -1,6 +1,7 @@
#include <string> #include <string>
#include <stdlib.h> #include <stdlib.h>
#include <gdk/gdkkeysyms.h> #include <gdk/gdkkeysyms.h>
#include <gdk/gdkx.h>
#ifdef USE_GTK3 #ifdef USE_GTK3
#include <gdk/gdkkeysyms-compat.h> #include <gdk/gdkkeysyms-compat.h>
#endif #endif
@ -463,6 +464,66 @@ event_input_rate_changed (GtkRange *range, gpointer data)
return; return;
} }
#ifdef USE_XRANDR
static double XRRGetExactRefreshRate (Display *dpy, Window window)
{
XRRScreenResources *resources = NULL;
XRROutputInfo *output_info = NULL;
XRRCrtcInfo *crtc_info = NULL;
RROutput output;
int event_base;
int error_base;
int version_major;
int version_minor;
double refresh_rate = 0.0;
int i;
if (!XRRQueryExtension (dpy, &event_base, &error_base) ||
!XRRQueryVersion (dpy, &version_major, &version_minor))
{
return refresh_rate;
}
resources = XRRGetScreenResources (dpy, window);
output = XRRGetOutputPrimary (dpy, window);
output_info = XRRGetOutputInfo (dpy, resources, output);
crtc_info = XRRGetCrtcInfo (dpy, resources, output_info->crtc);
for (i = 0; i < resources->nmode; i++)
{
if (resources->modes[i].id == crtc_info->mode)
{
XRRModeInfo *m = &resources->modes[i];
refresh_rate = (double) m->dotClock / m->hTotal / m->vTotal;
break;
}
}
XRRFreeCrtcInfo (crtc_info);
XRRFreeOutputInfo (output_info);
XRRFreeScreenResources (resources);
return refresh_rate;
}
#endif
static void
event_set_input_rate (GtkButton *widget, gpointer data)
{
#ifdef USE_XRANDR
Snes9xPreferences *preferences = (Snes9xPreferences *) data;
GdkWindow *gdk_window = gtk_widget_get_window (GTK_WIDGET(top_level->get_window()));
Display *dpy = GDK_DISPLAY_XDISPLAY (gdk_window_get_display (gdk_window));
Window window = GDK_COMPAT_WINDOW_XID (gdk_window);
double rate = XRRGetExactRefreshRate(dpy, window);
if (rate != 0.0)
preferences->set_slider("sound_input_rate", (int) (rate * 32040 / 60.09881389744051 + 0.5));
#endif
}
static void static void
event_about_clicked (GtkButton *widget, gpointer data) event_about_clicked (GtkButton *widget, gpointer data)
{ {
@ -561,6 +622,7 @@ Snes9xPreferences::Snes9xPreferences (Snes9xConfig *config) :
{ "game_data_browse", G_CALLBACK (event_game_data_browse) }, { "game_data_browse", G_CALLBACK (event_game_data_browse) },
{ "game_data_clear", G_CALLBACK (event_game_data_clear) }, { "game_data_clear", G_CALLBACK (event_game_data_clear) },
{ "about_clicked", G_CALLBACK (event_about_clicked) }, { "about_clicked", G_CALLBACK (event_about_clicked) },
{ "set_input_rate", G_CALLBACK (event_set_input_rate) },
#ifdef USE_JOYSTICK #ifdef USE_JOYSTICK
{ "calibrate", G_CALLBACK (event_calibrate) }, { "calibrate", G_CALLBACK (event_calibrate) },
#endif #endif
@ -1060,6 +1122,7 @@ Snes9xPreferences::show (void)
else else
{ {
gtk_widget_hide (get_widget ("resolution_box")); gtk_widget_hide (get_widget ("resolution_box"));
gtk_widget_hide (get_widget ("set_input_rate_button"));
} }
#ifdef USE_HQ2X #ifdef USE_HQ2X

View File

@ -4023,7 +4023,7 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">False</property> <property name="receives_default">False</property>
<property name="tooltip_text" translatable="yes">Adjust input rate automatically</property> <property name="tooltip_text" translatable="yes">Smoothes out slight hiccups in sound input rate</property>
<property name="draw_indicator">True</property> <property name="draw_indicator">True</property>
</object> </object>
<packing> <packing>
@ -4085,7 +4085,7 @@
<object class="GtkTable" id="table7"> <object class="GtkTable" id="table7">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="n_rows">5</property> <property name="n_rows">6</property>
<property name="n_columns">2</property> <property name="n_columns">2</property>
<property name="column_spacing">10</property> <property name="column_spacing">10</property>
<property name="row_spacing">5</property> <property name="row_spacing">5</property>
@ -4140,8 +4140,8 @@
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
<property name="right_attach">2</property> <property name="right_attach">2</property>
<property name="top_attach">3</property> <property name="top_attach">4</property>
<property name="bottom_attach">4</property> <property name="bottom_attach">5</property>
<property name="x_options">GTK_FILL</property> <property name="x_options">GTK_FILL</property>
<property name="y_options">GTK_FILL</property> <property name="y_options">GTK_FILL</property>
</packing> </packing>
@ -4154,8 +4154,8 @@
<property name="label" translatable="yes">Buffer size:</property> <property name="label" translatable="yes">Buffer size:</property>
</object> </object>
<packing> <packing>
<property name="top_attach">3</property> <property name="top_attach">4</property>
<property name="bottom_attach">4</property> <property name="bottom_attach">5</property>
<property name="x_options">GTK_FILL</property> <property name="x_options">GTK_FILL</property>
<property name="y_options">GTK_FILL</property> <property name="y_options">GTK_FILL</property>
</packing> </packing>
@ -4168,8 +4168,8 @@
<property name="label" translatable="yes">Dynamic rate limit:</property> <property name="label" translatable="yes">Dynamic rate limit:</property>
</object> </object>
<packing> <packing>
<property name="top_attach">4</property> <property name="top_attach">5</property>
<property name="bottom_attach">5</property> <property name="bottom_attach">6</property>
<property name="x_options">GTK_FILL</property> <property name="x_options">GTK_FILL</property>
<property name="y_options">GTK_FILL</property> <property name="y_options">GTK_FILL</property>
</packing> </packing>
@ -4198,8 +4198,8 @@
</object> </object>
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
<property name="top_attach">4</property> <property name="top_attach">5</property>
<property name="bottom_attach">5</property> <property name="bottom_attach">6</property>
<property name="y_options">GTK_FILL</property> <property name="y_options">GTK_FILL</property>
<property name="x_options">GTK_FILL</property> <property name="x_options">GTK_FILL</property>
</packing> </packing>
@ -4212,8 +4212,8 @@
<property name="label" translatable="yes">Input rate:</property> <property name="label" translatable="yes">Input rate:</property>
</object> </object>
<packing> <packing>
<property name="top_attach">1</property> <property name="top_attach">2</property>
<property name="bottom_attach">2</property> <property name="bottom_attach">3</property>
<property name="x_options">GTK_FILL</property> <property name="x_options">GTK_FILL</property>
<property name="y_options">GTK_FILL</property> <property name="y_options">GTK_FILL</property>
</packing> </packing>
@ -4231,8 +4231,8 @@
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
<property name="right_attach">2</property> <property name="right_attach">2</property>
<property name="top_attach">1</property> <property name="top_attach">2</property>
<property name="bottom_attach">2</property> <property name="bottom_attach">3</property>
<property name="y_options">GTK_FILL</property> <property name="y_options">GTK_FILL</property>
</packing> </packing>
</child> </child>
@ -4266,6 +4266,23 @@
<property name="y_options">GTK_FILL</property> <property name="y_options">GTK_FILL</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkButton" id="set_input_rate_button">
<property name="label" translatable="yes">Automatically Set Input Rate</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="set_input_rate" swapped="no"/>
</object>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options">GTK_FILL</property>
</packing>
</child>
<child> <child>
<object class="GtkLabel" id="label139"> <object class="GtkLabel" id="label139">
<property name="visible">True</property> <property name="visible">True</property>
@ -4274,8 +4291,8 @@
<property name="label" translatable="yes">Video rate:</property> <property name="label" translatable="yes">Video rate:</property>
</object> </object>
<packing> <packing>
<property name="top_attach">2</property> <property name="top_attach">3</property>
<property name="bottom_attach">3</property> <property name="bottom_attach">4</property>
<property name="x_options">GTK_FILL</property> <property name="x_options">GTK_FILL</property>
<property name="y_options">GTK_FILL</property> <property name="y_options">GTK_FILL</property>
</packing> </packing>
@ -4289,8 +4306,8 @@
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
<property name="right_attach">2</property> <property name="right_attach">2</property>
<property name="top_attach">2</property> <property name="top_attach">3</property>
<property name="bottom_attach">3</property> <property name="bottom_attach">4</property>
<property name="y_options">GTK_FILL</property> <property name="y_options">GTK_FILL</property>
</packing> </packing>
</child> </child>