A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 143 lines 4.4 kB view raw
1/*************************************************************************** 2 * __________ __ ___. 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 * \/ \/ \/ \/ \/ 8 * $Id$ 9 * 10 * Copyright (C) 2010 Thomas Martitz 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License 14 * as published by the Free Software Foundation; either version 2 15 * of the License, or (at your option) any later version. 16 * 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 18 * KIND, either express or implied. 19 * 20 ****************************************************************************/ 21 22package org.rockbox; 23 24import java.nio.ByteBuffer; 25 26import android.content.Context; 27import android.graphics.Bitmap; 28import android.graphics.Canvas; 29import android.graphics.Rect; 30import android.util.DisplayMetrics; 31import android.view.KeyEvent; 32import android.view.MotionEvent; 33import android.view.SurfaceHolder; 34import android.view.SurfaceView; 35import android.view.ViewConfiguration; 36 37public class RockboxFramebuffer extends SurfaceView 38 implements SurfaceHolder.Callback 39{ 40 private final DisplayMetrics metrics; 41 private final ViewConfiguration view_config; 42 private Bitmap btm; 43 44 /* first stage init; needs to run from a thread that has a Looper 45 * setup stuff that needs a Context */ 46 public RockboxFramebuffer(Context c) 47 { 48 super(c); 49 metrics = c.getResources().getDisplayMetrics(); 50 view_config = ViewConfiguration.get(c); 51 getHolder().addCallback(this); 52 /* Needed so we can catch KeyEvents */ 53 setFocusable(true); 54 setFocusableInTouchMode(true); 55 setClickable(true); 56 /* don't draw until native is ready (2nd stage) */ 57 setEnabled(false); 58 } 59 60 private void update(ByteBuffer framebuffer) 61 { 62 SurfaceHolder holder = getHolder(); 63 Canvas c = holder.lockCanvas(); 64 if (c == null) 65 return; 66 67 btm.copyPixelsFromBuffer(framebuffer); 68 synchronized (holder) 69 { /* draw */ 70 c.drawBitmap(btm, 0.0f, 0.0f, null); 71 } 72 holder.unlockCanvasAndPost(c); 73 } 74 75 private void update(ByteBuffer framebuffer, Rect dirty) 76 { 77 SurfaceHolder holder = getHolder(); 78 Canvas c = holder.lockCanvas(dirty); 79 80 if (c == null) 81 return; 82 83 /* can't copy a partial buffer, but it doesn't make a noticeable difference anyway */ 84 btm.copyPixelsFromBuffer(framebuffer); 85 synchronized (holder) 86 { /* draw */ 87 c.drawBitmap(btm, dirty, dirty, null); 88 } 89 holder.unlockCanvasAndPost(c); 90 } 91 92 public boolean onTouchEvent(MotionEvent me) 93 { 94 int x = (int) me.getX(); 95 int y = (int) me.getY(); 96 97 switch (me.getAction()) 98 { 99 case MotionEvent.ACTION_CANCEL: 100 case MotionEvent.ACTION_UP: 101 touchHandler(false, x, y); 102 return true; 103 case MotionEvent.ACTION_MOVE: 104 case MotionEvent.ACTION_DOWN: 105 touchHandler(true, x, y); 106 return true; 107 } 108 109 return false; 110 } 111 112 public boolean onKeyDown(int keyCode, KeyEvent event) 113 { 114 return buttonHandler(keyCode, true); 115 } 116 117 public boolean onKeyUp(int keyCode, KeyEvent event) 118 { 119 return buttonHandler(keyCode, false); 120 } 121 122 private int getDpi() 123 { 124 return metrics.densityDpi; 125 } 126 127 128 private int getScrollThreshold() 129 { 130 return view_config.getScaledTouchSlop(); 131 } 132 133 private native void touchHandler(boolean down, int x, int y); 134 public native static boolean buttonHandler(int keycode, boolean state); 135 136 public native void surfaceCreated(SurfaceHolder holder); 137 public native void surfaceDestroyed(SurfaceHolder holder); 138 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 139 { 140 btm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 141 setEnabled(true); 142 } 143}