Googleを使ってたけどなんか使いにくいし
iGoogleとも連動してない感じなので移ってきた。
#エディタからのコピペがうまくいかない。
はてなで書いている人は面白い人が多いのも理由のひとつかもしれない。
以下、ruby習作のrfbcounter
まぁ移植ですわな。
ふとコレがほしくなった。
http://www.jp.sonystyle.com/Qnavi/Detail/VGP-BRMP10.html
-
- -
#!/usr/local/bin/ruby
require "socket"
# rubyRFB - rfbcounter.rb: Counter/clock RFB server
PROGNAME = 'rfbRuby'
$counterval=0
$numdigits=4
$segmentaspect=3
$digitspacing=2
$screenWidth = 300
$screenHeight = 200
$fg = 0
$bg = 255
$border = $screenWidth / 20
$segmentWidth = ($screenWidth -2* $border)/($numdigits*(2+ $segmentaspect)+($numdigits-1)* $digitspacing)
$segmentLength= $segmentWidth* $segmentaspect
$screenHeight = $segmentWidth*(3+2* $segmentaspect)+2* $border
$bottom = [1,0,1,1,0,1,1,0,1,1]
$bottomLeft = [1,0,1,0,0,0,1,0,1,0]
$bottomRight = [1,1,0,1,1,1,1,1,1,1]
$middle = [0,0,1,1,1,1,1,0,1,1]
$topLeft = [1,0,0,0,1,1,1,1,1,1]
$topRight = [1,1,1,1,1,0,0,1,1,1]
$top = [1,0,1,1,0,1,1,1,1,1]
10.times do |i|
$bottom[i] = if $bottom[i] ==1 then $fg else $bg end
$bottomLeft[i] = if $bottomLeft[i] ==1 then $fg else $bg end
$bottomRight[i] = if $bottomRight[i]==1 then $fg else $bg end
$middle[i] = if $middle[i] ==1 then $fg else $bg end
$topLeft[i] = if $topLeft[i] ==1 then $fg else $bg end
$topRight[i] = if $topRight[i] ==1 then $fg else $bg end
$top[i] = if $top[i] ==1 then $fg else $bg end
end
def putSubrect(sock, color, x, y, w, h)
# p 'putSubrect'
sock.write [color].pack('C') # color
sock.write [x].pack('n') # x
sock.write [y].pack('n') # y
sock.write [w].pack('n') # w
sock.write [h].pack('n') # h
end
def draw_digit(sock, digit, segl, segw, fg, bg, xoffset, yoffset)
# p 'draw_digit'
# rfbFlamebufferUpdateRectHeader
sock.write [xoffset].pack('n') # x
sock.write [yoffset].pack('n') # y
sock.write [2*segl+segw].pack('n') # w
sock.write [3*segl+2*segw].pack('n') # h
sock.write [2].pack('N') # encoding rfbEncodingRRE
# rfbRREHeader
sock.write [7].pack('N') # nSubrects
sock.write [bg].pack('C') # bg color
putSubrect(sock, $bottom[digit], segl, 2 * segl + 2 * segw, segw, segl)
putSubrect(sock, $bottomLeft[digit], 0, 2 * segl + segw, segl, segw)
putSubrect(sock, $bottomRight[digit], segl + segw, 2 * segl + segw, segl, segw)
putSubrect(sock, $middle[digit], segl, segl + segw, segw, segl)
putSubrect(sock, $topLeft[digit], 0, segl, segl, segw)
putSubrect(sock, $topRight[digit], segl + segw, segl, segl, segw)
putSubrect(sock, $top[digit], segl, 0, segw, segl)
end
def draw_counter(sock, value, numdigits, segl, segw, fg, bg)
# p 'draw_counter'
xoffset = $border
yoffset = $border
sock.write [0].pack('C') # rfbFlamebufferUpdate
sock.write [0].pack('C') # pad
sock.write [numdigits].pack('n') # n Rects
xoffset += (numdigits - 1) * (segl + 2 * segw + $digitspacing)
numdigits.times do
draw_digit(sock, value % 10, segl, segw, fg, bg, xoffset, yoffset)
xoffset -= segl**1
while true
Thread.start(gs.accept) do |s| # save to dynamic variable
print(s, " is accepted\n")
# Version Check
s.write("RFB 003.003\n")
p 'send RFB Version 003.003'
p 'get RFB Version'
p s.gets
s.write [1].pack('N') # rfbNoAuth
p 'ClientInitMsg'
p s.read(1).unpack('C')[0] # ClientInitMsg
# Server Init Msg
p s.write [ $screenWidth].pack('n') # framebufferWidth
p s.write [ $screenHeight].pack('n') # framebufferHeight
# format BGR233 in PixelFormat
s.write [8].pack('C') # bits per pixel
s.write [8].pack('C') # color depth
s.write [1].pack('C') # bite oeder flag
s.write [1].pack('C') # color map flag
s.write [7].pack('n') # redMax
s.write [7].pack('n') # greenMax
s.write [3].pack('n') # blueMax
s.write [0].pack('C') # red offset
s.write [3].pack('C') # green offset
s.write [6].pack('C') # blue offset
p s.write [0].pack('C') # pad 1
p s.write [0].pack('n') # pad 2
# end of PixelFormat
p s.write [PROGNAME.length].pack('N') # format name Length
p s.write PROGNAME # format name
p PROGNAME.length , PROGNAME
while msg=s.read(1).unpack('C')[0]
case msg
when 0 # rfbSetPixelFormat
p 'SetPixelFormatMsg'
# p s.read(1).unpack('C') # SetPixelFormatMsg
p s.read(1).unpack('C') # pad 1
p s.read(2).unpack('n') # pad 2
# format BGR233 in PixelFormat
p s.read(1).unpack('C') # bits per pixel
p s.read(1).unpack('C') # color depth
p s.read(1).unpack('C') # bite oeder flag
p s.read(1).unpack('C') # color map flag
p s.read(2).unpack('n') # redMax
p s.read(2).unpack('n') # greenMax
p s.read(2).unpack('n') # blueMax
p s.read(1).unpack('C') # red offset
p s.read(1).unpack('C') # green offset
p s.read(1).unpack('C') # blue offset
p s.read(1).unpack('C') # pad 1
p s.read(2).unpack('n') # pad 2
# end of PixelFormat
when 1 # rfbFixColourMapEntries
p 'FixColourMapEntriesMsg'
p 'This server can not handle colormaps'
return
when 2 # rfbSetEncodings
p 'SetEncodingsMsg'
# p s.read(1).unpack('C') # SetEncodingsMsg
p s.read(1).unpack('C') # pad
p n=s.read(2).unpack('n')[0] # n Encodings
enableRRE = false
n.times do
p e=s.read(4).unpack('N')[0] # Encoding
enableRRE = true if e==2 # rfbEncodingRRE
end
unless enableRRE then
p 'Client can not use RRE Encoding'
return
end
when 3 # rfbFlamebufferUpdateRequest
p 'FlamebufferUpdateRequestMsg'
# p s.read(1).unpack('C') # FlamebufferUpdateRequestMsg
inc=s.read(1).unpack('C')[0] # incremental
x=s.read(2).unpack('n') # x
y=s.read(2).unpack('n') # y
w=s.read(2).unpack('n') # w
h=s.read(2).unpack('n') # h
if inc==1 then
# p 'inc'
$counterval = ( $counterval + 1) % 10000
draw_counter(s, $counterval, $numdigits, $segmentWidth, $segmentLength, $fg, $bg)
else
# p 'init'
s.write [0].pack('C') # rfbFlamebufferUpdate
s.write [0].pack('C') # pad
s.write [1].pack('n') # n Rects
# rfbFlamebufferUpdateRectHeader
s.write [0].pack('n') # x
s.write [0].pack('n') # y
s.write [ $screenWidth].pack('n') # w
s.write [ $screenHeight].pack('n') # h
s.write [2].pack('N') # encoding rfbEncodingRRE
# rfbRREHeader
s.write [0].pack('N') # nSubrects
s.write [ $bg].pack('C') # bg color
end
when 4 # rfbKeyEvent
p 'KeyEventMsg'
# p s.read(1).unpack('C')[0] # KeyEventMsg
p s.read(1).unpack('C') # if down true, up false
p s.read(2).unpack('n') # pad
p key=s.read(4).unpack('N')[0] & 0xff # key
case key & 0xff
when 'q'[0],'Q'[0]
exit
end
when 5 # rfbPointerEvent
p 'PointerEventMsg'
# p s.read(1).unpack('C') # PointerEventMsg
p s.read(1).unpack('C') # buttonMask (Mask = 1, 2, 4)
p s.read(2).unpack('n') # x
p s.read(2).unpack('n') # y
when 6 # rfbClientCutText
p 'ClientCutTextMsg'
# p s.read(1).unpack('C') # ClientCutTextMsg
p s.read(1).unpack('C') # pad 1
p s.read(2).unpack('n') # pad 2
p l=s.read(4).unpack('N') # length
p s.read(l) # Text
else
p 'unknown msg'
end
end
print(s, " is gone\n")
s.close
end
end