(NSW) Small cleanups

This commit is contained in:
twinaphex 2017-12-29 00:44:41 +01:00
parent 946b765e61
commit 5c876647ed
4 changed files with 574 additions and 485 deletions

View File

@ -19,234 +19,267 @@
#include <malloc.h>
#include <stdint.h>
#include "../audio_driver.h"
#include<libtransistor/nx.h>
static const int sample_rate = 48000;
static const int max_num_samples = sample_rate;
static const int num_channels = 2;
#include "../audio_driver.h"
#include "../verbosity.h"
static const int sample_rate = 48000;
static const int max_num_samples = sample_rate;
static const int num_channels = 2;
static const size_t sample_buffer_size = ((max_num_samples * num_channels * sizeof(uint16_t)) + 0xfff) & ~0xfff;
// don't think this can be in mapped memory, since samples get DMA'd out of it
/* don't think this can be in mapped memory, since samples get DMA'd out of it */
static uint16_t __attribute__((aligned(0x1000))) sample_buffer_1[sample_buffer_size/sizeof(uint16_t)];
static uint16_t __attribute__((aligned(0x1000))) sample_buffer_2[sample_buffer_size/sizeof(uint16_t)];
static uint16_t __attribute__((aligned(0x1000))) sample_buffer_3[sample_buffer_size/sizeof(uint16_t)];
static uint16_t *sample_buffers[3] = {sample_buffer_1, sample_buffer_2, sample_buffer_3};
typedef struct {
audio_output_t output;
handle_t event;
audio_output_buffer_t buffers[3];
audio_output_buffer_t *current_buffer;
bool blocking;
bool is_paused;
uint64_t last_append;
unsigned latency;
typedef struct
{
audio_output_t output;
handle_t event;
audio_output_buffer_t buffers[3];
audio_output_buffer_t *current_buffer;
bool blocking;
bool is_paused;
uint64_t last_append;
unsigned latency;
} switch_audio_t;
static ssize_t switch_audio_write(void *data, const void *buf, size_t size) {
static ssize_t switch_audio_write(void *data, const void *buf, size_t size)
{
size_t to_write = size;
switch_audio_t *swa = (switch_audio_t*) data;
//printf("write %ld samples\n", size/sizeof(uint16_t));
#if 0
RARCH_LOG("write %ld samples\n", size/sizeof(uint16_t));
#endif
if(swa->current_buffer == NULL) {
uint32_t num;
if(audio_ipc_output_get_released_buffer(&swa->output, &num, &swa->current_buffer) != RESULT_OK) {
printf("failed to get released buffer?\n");
return -1;
}
//printf("got buffer, num %d, ptr %p\n", num, swa->current_buffer);
if(num < 1) {
swa->current_buffer = NULL;
}
if(swa->current_buffer == NULL) {
if(swa->blocking) {
printf("no buffer, blocking...\n");
while(swa->current_buffer == NULL) {
uint32_t handle_idx;
svcWaitSynchronization(&handle_idx, &swa->event, 1, 33333333);
svcResetSignal(swa->event);
uint32_t num;
if(audio_ipc_output_get_released_buffer(&swa->output, &num, &swa->current_buffer) != RESULT_OK) {
return -1;
}
}
} else {
//printf("no buffer, nonblocking...\n");
return 0;
}
}
if (!swa->current_buffer)
{
uint32_t num;
if (audio_ipc_output_get_released_buffer(&swa->output, &num, &swa->current_buffer) != RESULT_OK)
{
RARCH_LOG("Failed to get released buffer?\n");
return -1;
}
swa->current_buffer->data_size = 0;
}
#if 0
RARCH_LOG("got buffer, num %d, ptr %p\n", num, swa->current_buffer);
#endif
size_t to_write = size;
if(to_write > sample_buffer_size - swa->current_buffer->data_size) {
if (num < 1)
swa->current_buffer = NULL;
if (!swa->current_buffer)
{
if (swa->blocking)
{
RARCH_LOG("No buffer, blocking...\n");
while(swa->current_buffer == NULL)
{
uint32_t num;
uint32_t handle_idx;
svcWaitSynchronization(&handle_idx, &swa->event, 1, 33333333);
svcResetSignal(swa->event);
if (audio_ipc_output_get_released_buffer(&swa->output, &num, &swa->current_buffer) != RESULT_OK)
return -1;
}
} else {
/* no buffer, nonblocking... */
return 0;
}
}
swa->current_buffer->data_size = 0;
}
if (to_write > sample_buffer_size - swa->current_buffer->data_size)
to_write = sample_buffer_size - swa->current_buffer->data_size;
}
memcpy(((uint8_t*) swa->current_buffer->sample_data) + swa->current_buffer->data_size, buf, to_write);
swa->current_buffer->data_size+= to_write;
swa->current_buffer->buffer_size = sample_buffer_size;
swa->current_buffer->data_size += to_write;
swa->current_buffer->buffer_size = sample_buffer_size;
if(swa->current_buffer->data_size > (48000*swa->latency)/1000) {
result_t r;
if((r = audio_ipc_output_append_buffer(&swa->output, swa->current_buffer)) != RESULT_OK) {
printf("failed to append buffer: 0x%x\n", r);
if (swa->current_buffer->data_size > (48000*swa->latency)/1000)
{
result_t r = audio_ipc_output_append_buffer(&swa->output, swa->current_buffer);
if (r != RESULT_OK)
{
RARCH_ERR("failed to append buffer: 0x%x\n", r);
return -1;
}
swa->current_buffer = NULL;
}
//printf("submitted %ld samples, %ld samples since last submit\n", to_write/num_channels/sizeof(uint16_t), (svcGetSystemTick() - swa->last_append) * sample_rate / 19200000);
#if 0
RARCH_LOG("submitted %ld samples, %ld samples since last submit\n",
to_write/num_channels/sizeof(uint16_t),
(svcGetSystemTick() - swa->last_append) * sample_rate / 19200000);
#endif
swa->last_append = svcGetSystemTick();
return to_write;
}
static bool switch_audio_stop(void *data) {
switch_audio_t *swa = (switch_audio_t*) data;
swa->is_paused = true;
return audio_ipc_output_stop(&swa->output) == RESULT_OK;
static bool switch_audio_stop(void *data)
{
switch_audio_t *swa = (switch_audio_t*) data;
if (!swa)
return false;
swa->is_paused = true;
return audio_ipc_output_stop(&swa->output) == RESULT_OK;
}
static bool switch_audio_start(void *data, bool is_shutdown) {
switch_audio_t *swa = (switch_audio_t*) data;
static bool switch_audio_start(void *data, bool is_shutdown)
{
switch_audio_t *swa = (switch_audio_t*) data;
printf("start\n");
if(audio_ipc_output_start(&swa->output) != RESULT_OK) {
return false;
}
swa->is_paused = false;
return true;
if (audio_ipc_output_start(&swa->output) != RESULT_OK)
return false;
swa->is_paused = false;
return true;
}
static bool switch_audio_alive(void *data) {
switch_audio_t *swa = (switch_audio_t*) data;
if(!swa) {
return false;
}
return !swa->is_paused;
static bool switch_audio_alive(void *data)
{
switch_audio_t *swa = (switch_audio_t*) data;
if (!swa)
return false;
return !swa->is_paused;
}
static void switch_audio_free(void *data) {
switch_audio_t *swa = (switch_audio_t*) data;
static void switch_audio_free(void *data)
{
switch_audio_t *swa = (switch_audio_t*) data;
audio_ipc_output_close(&swa->output);
audio_ipc_finalize();
free(swa);
audio_ipc_output_close(&swa->output);
audio_ipc_finalize();
free(swa);
}
static bool switch_audio_use_float(void *data) {
static bool switch_audio_use_float(void *data)
{
(void) data;
return false; // force INT16
return false; /* force INT16 */
}
static size_t switch_audio_write_avail(void *data) {
switch_audio_t *swa = (switch_audio_t*) data;
static size_t switch_audio_write_avail(void *data)
{
switch_audio_t *swa = (switch_audio_t*) data;
return swa->current_buffer == NULL ? 0 : swa->current_buffer->buffer_size;
if (!swa || !swa->current_buffer)
return 0;
return swa->current_buffer->buffer_size;
}
static void switch_audio_set_nonblock_state(void *data, bool state) {
switch_audio_t *swa = (switch_audio_t*) data;
static void switch_audio_set_nonblock_state(void *data, bool state)
{
switch_audio_t *swa = (switch_audio_t*) data;
if(swa) {
swa->blocking = !state;
}
if (swa)
swa->blocking = !state;
}
static void *switch_audio_init(const char *device,
unsigned rate, unsigned latency,
unsigned block_frames,
unsigned *new_rate) {
switch_audio_t *swa = (switch_audio_t*) calloc(1, sizeof(*swa));
if(!swa) {
return NULL;
}
result_t r;
if((r = audio_ipc_init()) != RESULT_OK) {
goto fail;
}
char names[8][0x20];
uint32_t num_names;
if(audio_ipc_list_outputs(&names[0], 8, &num_names) != RESULT_OK) {
goto fail_audio_ipc;
}
if(num_names != 1) {
printf("got back more than one AudioOut\n");
goto fail_audio_ipc;
}
unsigned *new_rate)
{
result_t r;
unsigned i;
char names[8][0x20];
uint32_t num_names = 0;
switch_audio_t *swa = (switch_audio_t*) calloc(1, sizeof(*swa));
if(audio_ipc_open_output(names[0], &swa->output) != RESULT_OK) {
goto fail_audio_ipc;
}
if (!swa)
return NULL;
if(swa->output.sample_rate != sample_rate) {
printf("expected sample rate of %d, got sample rate of %d\n", sample_rate, swa->output.sample_rate);
goto fail_audio_output;
}
r = audio_ipc_init();
if(swa->output.num_channels != num_channels) {
printf("expected %d channels, got %d\n", num_channels, swa->output.num_channels);
goto fail_audio_output;
}
if (r != RESULT_OK)
goto fail;
if(swa->output.sample_format != PCM_INT16) {
printf("expected PCM_INT16, got %d\n", swa->output.sample_format);
goto fail_audio_output;
}
if (audio_ipc_list_outputs(&names[0], 8, &num_names) != RESULT_OK)
goto fail_audio_ipc;
if(audio_ipc_output_register_buffer_event(&swa->output, &swa->event) != RESULT_OK) {
goto fail_audio_output;
}
if (num_names != 1)
{
RARCH_ERR("got back more than one AudioOut\n");
goto fail_audio_ipc;
}
swa->blocking = block_frames;
*new_rate = swa->output.sample_rate;
if (audio_ipc_open_output(names[0], &swa->output) != RESULT_OK)
goto fail_audio_ipc;
for(int i = 0; i < 3; i++) {
swa->buffers[i].ptr = &swa->buffers[i].sample_data;
swa->buffers[i].sample_data = sample_buffers[i];
swa->buffers[i].buffer_size = sample_buffer_size;
swa->buffers[i].data_size = sample_buffer_size;
swa->buffers[i].unknown = 0;
if (swa->output.sample_rate != sample_rate)
{
RARCH_ERR("expected sample rate of %d, got sample rate of %d\n",
sample_rate, swa->output.sample_rate);
goto fail_audio_output;
}
if(audio_ipc_output_append_buffer(&swa->output, &swa->buffers[i]) != RESULT_OK) {
goto fail_audio_output;
}
}
if (swa->output.num_channels != num_channels)
{
RARCH_ERR("expected %d channels, got %d\n", num_channels,
swa->output.num_channels);
goto fail_audio_output;
}
swa->current_buffer = NULL;
swa->latency = latency;
if (swa->output.sample_format != PCM_INT16)
{
RARCH_ERR("expected PCM_INT16, got %d\n", swa->output.sample_format);
goto fail_audio_output;
}
swa->last_append = svcGetSystemTick();
if(audio_ipc_output_start(&swa->output) != RESULT_OK) {
goto fail_audio_output;
}
if (audio_ipc_output_register_buffer_event(&swa->output, &swa->event) != RESULT_OK)
goto fail_audio_output;
swa->blocking = block_frames;
*new_rate = swa->output.sample_rate;
for(i = 0; i < 3; i++)
{
swa->buffers[i].ptr = &swa->buffers[i].sample_data;
swa->buffers[i].sample_data = sample_buffers[i];
swa->buffers[i].buffer_size = sample_buffer_size;
swa->buffers[i].data_size = sample_buffer_size;
swa->buffers[i].unknown = 0;
if (audio_ipc_output_append_buffer(&swa->output, &swa->buffers[i]) != RESULT_OK)
goto fail_audio_output;
}
swa->current_buffer = NULL;
swa->latency = latency;
swa->last_append = svcGetSystemTick();
if (audio_ipc_output_start(&swa->output) != RESULT_OK)
goto fail_audio_output;
return swa;
printf("init'd switch audio driver\n");
return swa;
fail_audio_output:
audio_ipc_output_close(&swa->output);
audio_ipc_output_close(&swa->output);
fail_audio_ipc:
audio_ipc_finalize();
audio_ipc_finalize();
fail:
free(swa);
return NULL;
free(swa);
return NULL;
}
static size_t switch_audio_buffer_size(void *data) {
(void) data;
return sample_buffer_size;
static size_t switch_audio_buffer_size(void *data)
{
(void) data;
return sample_buffer_size;
}
audio_driver_t audio_switch = {

View File

@ -6,6 +6,8 @@
#include <retro_math.h>
#include <formats/image.h>
#include <libtransistor/nx.h>
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
@ -27,225 +29,255 @@
#include "../../tasks/tasks_internal.h"
#endif
#include<libtransistor/nx.h>
static void *switch_init(const video_info_t *video, const input_driver_t **input, void **input_data);
static bool switch_frame(void *data, const void *frame, unsigned width, unsigned height, uint64_t frame_count, unsigned pitch, const char *msg, video_frame_info_t *video_info);
static void switch_set_nonblock_state(void *data, bool toggle);
static bool switch_alive(void *data);
static bool switch_focus(void *data);
static bool switch_suppress_screensaver(void *data, bool enable);
static void switch_viewport_info(void *data, struct video_viewport *vp);
static bool read_viewport(void *data, uint8_t *buffer, bool is_idle);
typedef struct {
surface_t surface;
unsigned width, height;
unsigned rotation;
bool vsync;
revent_h vsync_h;
struct video_viewport vp;
typedef struct
{
bool vsync;
unsigned width, height;
unsigned rotation;
surface_t surface;
revent_h vsync_h;
struct video_viewport vp;
} switch_video_t;
static uint32_t image[1280*720];
static void *switch_init(const video_info_t *video,
const input_driver_t **input, void **input_data) {
printf("loading switch gfx driver, width: %d, height: %d\n", video->width, video->height);
const input_driver_t **input, void **input_data)
{
unsigned x, y;
switch_video_t *sw = malloc(sizeof(*sw));
if (!sw)
return NULL;
switch_video_t *sw = malloc(sizeof(*sw));
if(sw == NULL) {
return NULL;
}
RARCH_LOG("loading switch gfx driver, width: %d, height: %d\n", video->width, video->height);
if(libtransistor_context.magic != LIBTRANSISTOR_CONTEXT_MAGIC) {
printf("running under CTU, skipping graphics init...\n");
} else {
result_t r;
if((r = display_init()) != RESULT_OK) {
free(sw);
return NULL;
}
if((r = display_open_layer(&sw->surface)) != RESULT_OK) {
display_finalize();
free(sw);
return NULL;
}
if((r = display_get_vsync_event(&sw->vsync_h)) != RESULT_OK) {
display_finalize();
free(sw);
return NULL;
}
}
sw->vp.x = 0;
sw->vp.y = 0;
sw->vp.width = 1280;
sw->vp.height = 720;
sw->vp.full_width = 1280;
sw->vp.full_height = 720;
video_driver_set_size(&sw->vp.width, &sw->vp.height);
if (libtransistor_context.magic != LIBTRANSISTOR_CONTEXT_MAGIC)
RARCH_LOG("running under CTU, skipping graphics init...\n");
else
{
result_t r = display_init();
if (r != RESULT_OK)
{
free(sw);
return NULL;
}
r = display_open_layer(&sw->surface);
sw->vsync = video->vsync;
*input = NULL;
*input_data = NULL;
if (r != RESULT_OK)
{
display_finalize();
free(sw);
return NULL;
}
r = display_get_vsync_event(&sw->vsync_h);
for(int x = 0; x < 1280; x++) {
for(int y = 0; y < 720; y++) {
image[(y*1280)+x] = 0xFF000000;
}
}
return sw;
if (r != RESULT_OK)
{
display_finalize();
free(sw);
return NULL;
}
}
sw->vp.x = 0;
sw->vp.y = 0;
sw->vp.width = 1280;
sw->vp.height = 720;
sw->vp.full_width = 1280;
sw->vp.full_height = 720;
video_driver_set_size(&sw->vp.width, &sw->vp.height);
sw->vsync = video->vsync;
*input = NULL;
*input_data = NULL;
for(x = 0; x < 1280; x++)
{
for(y = 0; y < 720; y++)
image[(y*1280)+x] = 0xFF000000;
}
return sw;
}
static void switch_wait_vsync(switch_video_t *sw) {
static void switch_wait_vsync(switch_video_t *sw)
{
uint32_t handle_idx;
svcWaitSynchronization(&handle_idx, &sw->vsync_h, 1, 33333333);
svcResetSignal(sw->vsync_h);
}
static bool switch_frame(void *data, const void *frame,
unsigned width, unsigned height,
uint64_t frame_count, unsigned pitch,
const char *msg, video_frame_info_t *video_info) {
switch_video_t *sw = data;
unsigned width, unsigned height,
uint64_t frame_count, unsigned pitch,
const char *msg, video_frame_info_t *video_info)
{
unsigned x, y;
result_t r;
uint64_t begin, done_copying, post_vsync, pre_swizzle, post_swizzle,
copy_ms, swizzle_ms, vsync_ms;
int tgtw, tgth, centerx, centery;
uint16_t *frame_pixels = frame;
uint32_t *out_buffer = NULL;
switch_video_t *sw = data;
int xsf = 1280 / width;
int ysf = 720 / height;
int sf = xsf;
int xsf = 1280/width;
int ysf = 720/height;
int sf = xsf;
if(ysf < sf) { sf = ysf; }
if (ysf < sf)
sf = ysf;
uint16_t *frame_pixels = frame;
int tgtw = width * sf;
int tgth = height * sf;
int centerx = (1280-tgtw)/2;
int centery = (720-tgth)/2;
tgtw = width * sf;
tgth = height * sf;
centerx = (1280-tgtw)/2;
centery = (720-tgth)/2;
uint64_t begin = svcGetSystemTick();
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
uint32_t spixel = frame_pixels[(y*pitch/sizeof(uint16_t)) + x];
uint8_t r = (spixel >> 11) & 31; r = (r * 256) / 32;
uint8_t g = (spixel >> 5) & 63; g = (g * 256) / 64;
uint8_t b = (spixel >> 0) & 31; b = (b * 256) / 32;
uint32_t dpixel = (r << 0) | (g << 8) | (b << 16) | (0xFF << 24);
for(int subx = 0; subx < xsf; subx++) {
for(int suby = 0; suby < ysf; suby++) {
image[(((y*sf)+suby+centery)*1280) + ((x*sf)+subx+centerx)] = dpixel;
}
}
}
}
begin = svcGetSystemTick();
uint64_t done_copying = svcGetSystemTick();
//if(frame_count > 6000) {
// display_finalize();
// exit(0);
//}
for(x = 0; x < width; x++)
{
for(y = 0; y < height; y++)
{
uint32_t spixel = frame_pixels[(y*pitch/sizeof(uint16_t)) + x];
uint32_t dpixel = 0;
uint8_t r = (spixel >> 11) & 31;
uint8_t g = (spixel >> 5) & 63;
uint8_t b = (spixel >> 0) & 31;
r = (r * 256) / 32;
g = (g * 256) / 64;
b = (b * 256) / 32;
dpixel = (r << 0) | (g << 8) | (b << 16) | (0xFF << 24);
if(libtransistor_context.magic != LIBTRANSISTOR_CONTEXT_MAGIC) {
printf("running under CTU; skipping frame\n");
return true;
}
if(msg != NULL && strlen(msg) > 0) {
printf("message: %s\n", msg);
}
for (subx = 0; subx < xsf; subx++)
for (suby = 0; suby < ysf; suby++)
image[(((y*sf)+suby+centery)*1280)
+ ((x*sf)+subx+centerx)] = dpixel;
}
}
if(sw->vsync) {
switch_wait_vsync(sw);
}
done_copying = svcGetSystemTick();
uint64_t post_vsync = svcGetSystemTick();
result_t r;
uint32_t *out_buffer;
if((r = surface_dequeue_buffer(&sw->surface, &out_buffer)) != RESULT_OK) {
return false;
}
#if 0
if (frame_count > 6000)
{
display_finalize();
exit(0);
}
#endif
uint64_t pre_swizzle = svcGetSystemTick();
gfx_slow_swizzling_blit(out_buffer, image, 1280, 720, 0, 0);
uint64_t post_swizzle = svcGetSystemTick();
if((r = surface_queue_buffer(&sw->surface)) != RESULT_OK) {
return false;
}
uint64_t copy_ms = (done_copying - begin) / 19200;
uint64_t swizzle_ms = (post_swizzle - pre_swizzle) / 19200;
uint64_t vsync_ms = (post_vsync - done_copying) / 19200;
printf("frame %d benchmark: copy %ld ms, swizzle %ld ms, vsync %ld ms\n", frame_count, copy_ms, swizzle_ms, vsync_ms);
return true;
if (libtransistor_context.magic != LIBTRANSISTOR_CONTEXT_MAGIC)
{
RARCH_LOG("running under CTU; skipping frame\n");
return true;
}
if (msg != NULL && strlen(msg) > 0)
RARCH_LOG("message: %s\n", msg);
if (sw->vsync)
switch_wait_vsync(sw);
post_vsync = svcGetSystemTick();
r = surface_dequeue_buffer(&sw->surface, &out_buffer);
if (r != RESULT_OK)
return false;
pre_swizzle = svcGetSystemTick();
gfx_slow_swizzling_blit(out_buffer, image, 1280, 720, 0, 0);
post_swizzle = svcGetSystemTick();
r = surface_queue_buffer(&sw->surface);
if (r != RESULT_OK)
return false;
copy_ms = (done_copying - begin) / 19200;
swizzle_ms = (post_swizzle - pre_swizzle) / 19200;
vsync_ms = (post_vsync - done_copying) / 19200;
RARCH_LOG("frame %d benchmark: copy %ld ms, swizzle %ld ms, vsync %ld ms\n", frame_count, copy_ms, swizzle_ms, vsync_ms);
return true;
}
static void switch_set_nonblock_state(void *data, bool toggle) {
switch_video_t *sw = data;
sw->vsync = !toggle;
static void switch_set_nonblock_state(void *data, bool toggle)
{
switch_video_t *sw = data;
sw->vsync = !toggle;
}
static bool switch_alive(void *data) {
static bool switch_alive(void *data)
{
(void) data;
return true;
}
static bool switch_focus(void *data) {
static bool switch_focus(void *data)
{
(void) data;
return true;
}
static bool switch_suppress_screensaver(void *data, bool enable) {
static bool switch_suppress_screensaver(void *data, bool enable)
{
(void) data;
(void) enable;
return false;
}
static bool switch_has_windowed(void *data) {
static bool switch_has_windowed(void *data)
{
(void) data;
return false;
}
static void switch_free(void *data) {
static void switch_free(void *data)
{
switch_video_t *sw = data;
free(sw);
display_finalize();
}
static bool switch_set_shader(void *data,
enum rarch_shader_type type, const char *path) {
(void) data;
(void) type;
(void) path;
enum rarch_shader_type type, const char *path)
{
(void) data;
(void) type;
(void) path;
return false;
return false;
}
static void switch_set_rotation(void *data, unsigned rotation) {
switch_video_t *sw = data;
sw->rotation = rotation;
static void switch_set_rotation(void *data, unsigned rotation)
{
switch_video_t *sw = data;
if (!sw)
return;
sw->rotation = rotation;
}
static void switch_viewport_info(void *data, struct video_viewport *vp) {
switch_video_t *sw = data;
*vp = sw->vp;
static void switch_viewport_info(void *data, struct video_viewport *vp)
{
switch_video_t *sw = data;
*vp = sw->vp;
}
static bool switch_read_viewport(void *data, uint8_t *buffer, bool is_idle) {
(void) data;
(void) buffer;
static bool switch_read_viewport(void *data, uint8_t *buffer, bool is_idle)
{
(void) data;
(void) buffer;
return true;
return true;
}
static void switch_get_poke_interface(void *data, const video_poke_interface_t **iface) {
(void) data;
(void) iface;
static void switch_get_poke_interface(void *data,
const video_poke_interface_t **iface)
{
(void) data;
(void) iface;
}
video_driver_t video_switch = {

View File

@ -13,88 +13,97 @@
#define MAX_PADS 10
typedef struct switch_input {
const input_device_driver_t *joypad;
bool blocked;
typedef struct switch_input
{
const input_device_driver_t *joypad;
bool blocked;
} switch_input_t;
static void switch_input_poll(void *data) {
static void switch_input_poll(void *data)
{
switch_input_t *sw = (switch_input_t*) data;
if(sw->joypad) {
if (sw->joypad)
sw->joypad->poll();
}
}
static int16_t switch_input_state(void *data,
rarch_joypad_info_t joypad_info,
const struct retro_keybind **binds,
unsigned port, unsigned device,
unsigned idx, unsigned id) {
switch_input_t *sw = (switch_input_t*) data;
rarch_joypad_info_t joypad_info,
const struct retro_keybind **binds,
unsigned port, unsigned device,
unsigned idx, unsigned id)
{
switch_input_t *sw = (switch_input_t*) data;
if (port > MAX_PADS-1)
return 0;
if (port > MAX_PADS-1)
return 0;
switch (device) {
case RETRO_DEVICE_JOYPAD:
//return input_joypad_pressed(sw->joypad,
// joypad_info, port, binds[port], id);
if(sw->joypad) {
return sw->joypad->button(port, id);
}
case RETRO_DEVICE_ANALOG:
if(binds[port]) {
return input_joypad_analog(sw->joypad,
joypad_info, port, idx, id, binds[port]);
}
break;
}
return 0;
switch (device)
{
case RETRO_DEVICE_JOYPAD:
#if 0
return input_joypad_pressed(sw->joypad,
joypad_info, port, binds[port], id);
#else
if (sw->joypad)
return sw->joypad->button(port, id);
#endif
break;
case RETRO_DEVICE_ANALOG:
if (binds[port])
return input_joypad_analog(sw->joypad,
joypad_info, port, idx, id, binds[port]);
break;
}
return 0;
}
static void switch_input_free_input(void *data) {
switch_input_t *sw = (switch_input_t*) data;
static void switch_input_free_input(void *data)
{
switch_input_t *sw = (switch_input_t*) data;
if (sw && sw->joypad) {
sw->joypad->destroy();
}
if (sw && sw->joypad)
sw->joypad->destroy();
free(sw);
free(sw);
}
static void* switch_input_init(const char *joypad_driver) {
switch_input_t *sw = (switch_input_t*) calloc(1, sizeof(*sw));
if (!sw)
return NULL;
static void* switch_input_init(const char *joypad_driver)
{
switch_input_t *sw = (switch_input_t*) calloc(1, sizeof(*sw));
if (!sw)
return NULL;
sw->joypad = input_joypad_init_driver(joypad_driver, sw);
sw->joypad = input_joypad_init_driver(joypad_driver, sw);
return sw;
return sw;
}
static uint64_t switch_input_get_capabilities(void *data) {
(void) data;
static uint64_t switch_input_get_capabilities(void *data)
{
(void) data;
return (1 << RETRO_DEVICE_JOYPAD) | (1 << RETRO_DEVICE_ANALOG);
return (1 << RETRO_DEVICE_JOYPAD) | (1 << RETRO_DEVICE_ANALOG);
}
static const input_device_driver_t *switch_input_get_joypad_driver(void *data) {
switch_input_t *sw = (switch_input_t*) data;
if(sw) {
return sw->joypad;
}
return NULL;
static const input_device_driver_t *switch_input_get_joypad_driver(void *data)
{
switch_input_t *sw = (switch_input_t*) data;
if (sw)
return sw->joypad;
return NULL;
}
static void switch_input_grab_mouse(void *data, bool state) {
(void)data;
(void)state;
static void switch_input_grab_mouse(void *data, bool state)
{
(void)data;
(void)state;
}
static bool switch_input_set_rumble(void *data, unsigned port,
enum retro_rumble_effect effect, uint16_t strength) {
enum retro_rumble_effect effect, uint16_t strength)
{
(void)data;
(void)port;
(void)effect;
@ -103,20 +112,20 @@ static bool switch_input_set_rumble(void *data, unsigned port,
return false;
}
static bool switch_input_keyboard_mapping_is_blocked(void *data) {
switch_input_t *sw = (switch_input_t*) data;
if (!sw) {
return false;
}
return sw->blocked;
static bool switch_input_keyboard_mapping_is_blocked(void *data)
{
switch_input_t *sw = (switch_input_t*) data;
if (!sw)
return false;
return sw->blocked;
}
static void switch_input_keyboard_mapping_set_block(void *data, bool value) {
switch_input_t *sw = (switch_input_t*) data;
if(!sw) {
return;
}
sw->blocked = value;
static void switch_input_keyboard_mapping_set_block(void *data, bool value)
{
switch_input_t *sw = (switch_input_t*) data;
if (!sw)
return;
sw->blocked = value;
}
input_driver_t input_switch = {

View File

@ -2,6 +2,8 @@
#include "../../config.h"
#endif
#include<libtransistor/nx.h>
#include "../input_driver.h"
#include "../../tasks/tasks_internal.h"
@ -10,7 +12,6 @@
#include "../../command.h"
#include "string.h"
#include<libtransistor/nx.h>
#ifndef MAX_PADS
#define MAX_PADS 10
@ -20,125 +21,139 @@ static uint16_t pad_state[MAX_PADS];
static int16_t analog_state[MAX_PADS][2][2];
extern uint64_t lifecycle_state;
static const char *switch_joypad_name(unsigned pad) {
return "Switch Controller";
static const char *switch_joypad_name(unsigned pad)
{
return "Switch Controller";
}
static void switch_joypad_autodetect_add(unsigned autoconf_pad) {
if(!input_autoconfigure_connect(
switch_joypad_name(autoconf_pad), // name
NULL, // display name
switch_joypad.ident, // driver
autoconf_pad, // idx
0, // vid
0)) { // pid
input_config_set_device_name(autoconf_pad, switch_joypad_name(autoconf_pad));
}
static void switch_joypad_autodetect_add(unsigned autoconf_pad)
{
if(!input_autoconfigure_connect(
switch_joypad_name(autoconf_pad), /* name */
NULL, /* display name */
switch_joypad.ident, /* driver */
autoconf_pad, /* idx */
0, /* vid */
0)) /* pid */
input_config_set_device_name(autoconf_pad, switch_joypad_name(autoconf_pad));
}
static bool switch_joypad_init(void *data) {
hid_init();
switch_joypad_autodetect_add(0);
switch_joypad_autodetect_add(1);
printf("init switch joypad\n");
return true;
static bool switch_joypad_init(void *data)
{
hid_init();
switch_joypad_autodetect_add(0);
switch_joypad_autodetect_add(1);
return true;
}
static bool switch_joypad_button(unsigned port_num, uint16_t key) {
if(port_num >= MAX_PADS) {
return false;
}
static bool switch_joypad_button(unsigned port_num, uint16_t key)
{
if(port_num >= MAX_PADS)
return false;
//printf("button(%d, %d)\n", port_num, key);
return (pad_state[port_num] & (1 << key));
#if 0
RARCH_LOG("button(%d, %d)\n", port_num, key);
#endif
return (pad_state[port_num] & (1 << key));
}
static void switch_joypad_get_buttons(unsigned port_num, retro_bits_t *state) {
printf("get buttons %d\n", port_num);
if(port_num < MAX_PADS) {
BITS_COPY16_PTR(state, pad_state[port_num]);
} else {
BIT256_CLEAR_ALL_PTR(state);
}
static void switch_joypad_get_buttons(unsigned port_num, retro_bits_t *state)
{
if(port_num < MAX_PADS)
{
BITS_COPY16_PTR(state, pad_state[port_num]);
}
else
{
BIT256_CLEAR_ALL_PTR(state);
}
}
static int16_t switch_joypad_axis(unsigned port_num, uint32_t joyaxis) {
int val = 0;
int axis = -1;
bool is_neg = false;
bool is_pos = false;
static int16_t switch_joypad_axis(unsigned port_num, uint32_t joyaxis)
{
int val = 0;
int axis = -1;
bool is_neg = false;
bool is_pos = false;
if(joyaxis == AXIS_NONE || port_num >= MAX_PADS) {
}
if(joyaxis == AXIS_NONE || port_num >= MAX_PADS)
{
/* TODO/FIXME - implement */
}
if(AXIS_NEG_GET(joyaxis) < 4) {
axis = AXIS_NEG_GET(joyaxis);
is_neg = true;
} else if(AXIS_POS_GET(joyaxis) < 4) {
axis = AXIS_POS_GET(joyaxis);
is_pos = true;
}
if(AXIS_NEG_GET(joyaxis) < 4)
{
axis = AXIS_NEG_GET(joyaxis);
is_neg = true;
}
else if(AXIS_POS_GET(joyaxis) < 4)
{
axis = AXIS_POS_GET(joyaxis);
is_pos = true;
}
switch(axis) {
case 0:
val = analog_state[port_num][0][0];
break;
case 1:
val = analog_state[port_num][0][1];
break;
case 2:
val = analog_state[port_num][1][0];
break;
case 3:
val = analog_state[port_num][1][1];
break;
}
if(is_neg && val > 0) {
val = 0;
} else if(is_pos && val < 0) {
val = 0;
}
return val;
switch(axis)
{
case 0:
val = analog_state[port_num][0][0];
break;
case 1:
val = analog_state[port_num][0][1];
break;
case 2:
val = analog_state[port_num][1][0];
break;
case 3:
val = analog_state[port_num][1][1];
break;
}
if(is_neg && val > 0)
val = 0;
else if(is_pos && val < 0)
val = 0;
return val;
}
static bool switch_joypad_query_pad(unsigned pad) {
printf("query %d\n", pad);
return pad < MAX_PADS && pad_state[pad];
static bool switch_joypad_query_pad(unsigned pad)
{
return pad < MAX_PADS && pad_state[pad];
}
static void switch_joypad_destroy(void) {
hid_finalize();
static void switch_joypad_destroy(void)
{
hid_finalize();
}
static void switch_joypad_poll(void) {
hid_controller_t *controllers = hid_get_shared_memory()->controllers;
hid_controller_t *cont = &controllers[0];
hid_controller_state_entry_t ent = cont->main.entries[cont->main.latest_idx];
pad_state[0] = 0;
pad_state[0]|= (ent.button_state & 0x1000) ? (1 << RETRO_DEVICE_ID_JOYPAD_LEFT) : 0;
pad_state[0]|= (ent.button_state & 0x8000) ? (1 << RETRO_DEVICE_ID_JOYPAD_DOWN) : 0;
pad_state[0]|= (ent.button_state & 0x4000) ? (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT) : 0;
pad_state[0]|= (ent.button_state & 0x2000) ? (1 << RETRO_DEVICE_ID_JOYPAD_UP) : 0;
pad_state[0]|= (ent.button_state & 0x0400) ? (1 << RETRO_DEVICE_ID_JOYPAD_START) : 0;
pad_state[0]|= (ent.button_state & 0x0800) ? (1 << RETRO_DEVICE_ID_JOYPAD_SELECT) : 0;
pad_state[0]|= (ent.button_state & 0x0004) ? (1 << RETRO_DEVICE_ID_JOYPAD_X) : 0;
pad_state[0]|= (ent.button_state & 0x0008) ? (1 << RETRO_DEVICE_ID_JOYPAD_Y) : 0;
pad_state[0]|= (ent.button_state & 0x0002) ? (1 << RETRO_DEVICE_ID_JOYPAD_B) : 0;
pad_state[0]|= (ent.button_state & 0x0001) ? (1 << RETRO_DEVICE_ID_JOYPAD_A) : 0;
pad_state[0]|= (ent.button_state & 0x0080) ? (1 << RETRO_DEVICE_ID_JOYPAD_R) : 0;
pad_state[0]|= (ent.button_state & 0x0040) ? (1 << RETRO_DEVICE_ID_JOYPAD_L) : 0;
pad_state[0]|= (ent.button_state & 0x0200) ? (1 << RETRO_DEVICE_ID_JOYPAD_R2) : 0;
pad_state[0]|= (ent.button_state & 0x0100) ? (1 << RETRO_DEVICE_ID_JOYPAD_L2) : 0;
analog_state[0][RETRO_DEVICE_INDEX_ANALOG_LEFT][RETRO_DEVICE_ID_ANALOG_X] = ent.left_stick_x / 0x20000;
analog_state[0][RETRO_DEVICE_INDEX_ANALOG_LEFT][RETRO_DEVICE_ID_ANALOG_Y] = ent.left_stick_y / 0x20000;
analog_state[0][RETRO_DEVICE_INDEX_ANALOG_RIGHT][RETRO_DEVICE_ID_ANALOG_X] = ent.left_stick_x / 0x20000;
analog_state[0][RETRO_DEVICE_INDEX_ANALOG_RIGHT][RETRO_DEVICE_ID_ANALOG_Y] = ent.left_stick_y / 0x20000;
static void switch_joypad_poll(void)
{
hid_controller_t *controllers = hid_get_shared_memory()->controllers;
hid_controller_t *cont = &controllers[0];
hid_controller_state_entry_t ent = cont->main.entries[cont->main.latest_idx];
pad_state[0] = 0;
pad_state[0] |= (ent.button_state & 0x1000) ? (1 << RETRO_DEVICE_ID_JOYPAD_LEFT) : 0;
pad_state[0] |= (ent.button_state & 0x8000) ? (1 << RETRO_DEVICE_ID_JOYPAD_DOWN) : 0;
pad_state[0] |= (ent.button_state & 0x4000) ? (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT) : 0;
pad_state[0] |= (ent.button_state & 0x2000) ? (1 << RETRO_DEVICE_ID_JOYPAD_UP) : 0;
pad_state[0] |= (ent.button_state & 0x0400) ? (1 << RETRO_DEVICE_ID_JOYPAD_START) : 0;
pad_state[0] |= (ent.button_state & 0x0800) ? (1 << RETRO_DEVICE_ID_JOYPAD_SELECT) : 0;
pad_state[0] |= (ent.button_state & 0x0004) ? (1 << RETRO_DEVICE_ID_JOYPAD_X) : 0;
pad_state[0] |= (ent.button_state & 0x0008) ? (1 << RETRO_DEVICE_ID_JOYPAD_Y) : 0;
pad_state[0] |= (ent.button_state & 0x0002) ? (1 << RETRO_DEVICE_ID_JOYPAD_B) : 0;
pad_state[0] |= (ent.button_state & 0x0001) ? (1 << RETRO_DEVICE_ID_JOYPAD_A) : 0;
pad_state[0] |= (ent.button_state & 0x0080) ? (1 << RETRO_DEVICE_ID_JOYPAD_R) : 0;
pad_state[0] |= (ent.button_state & 0x0040) ? (1 << RETRO_DEVICE_ID_JOYPAD_L) : 0;
pad_state[0] |= (ent.button_state & 0x0200) ? (1 << RETRO_DEVICE_ID_JOYPAD_R2) : 0;
pad_state[0] |= (ent.button_state & 0x0100) ? (1 << RETRO_DEVICE_ID_JOYPAD_L2) : 0;
analog_state[0][RETRO_DEVICE_INDEX_ANALOG_LEFT][RETRO_DEVICE_ID_ANALOG_X] = ent.left_stick_x / 0x20000;
analog_state[0][RETRO_DEVICE_INDEX_ANALOG_LEFT][RETRO_DEVICE_ID_ANALOG_Y] = ent.left_stick_y / 0x20000;
analog_state[0][RETRO_DEVICE_INDEX_ANALOG_RIGHT][RETRO_DEVICE_ID_ANALOG_X] = ent.left_stick_x / 0x20000;
analog_state[0][RETRO_DEVICE_INDEX_ANALOG_RIGHT][RETRO_DEVICE_ID_ANALOG_Y] = ent.left_stick_y / 0x20000;
}
input_device_driver_t switch_joypad = {
@ -149,7 +164,7 @@ input_device_driver_t switch_joypad = {
switch_joypad_get_buttons,
switch_joypad_axis,
switch_joypad_poll,
NULL, // set_rumble
NULL, /* set_rumble */
switch_joypad_name,
"switch"
};