This file is indexed.

/usr/share/openmsx/scripts/osd_keyboard.tcl is in openmsx-data 0.8.2-2.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
namespace eval osd_keyboard {

# KNOWN ISSUES/TODO:
# * Shouldn't use the keymatrix command, but the 'type' command for all keys
#   that are not on the same position in the matrix for all machines
# * lots more? :P

variable is_dingoo [string match *-dingux* $::tcl_platform(osVersion)]

#init vars
variable mouse1_pressed false
variable key_pressed -1
variable key_selected -1
variable keys_held
variable row_starts

#init colors
variable key_color "0x999999c0 0xbbbbbbc0 0xddddddc0 0xffffffc0"
variable key_pressed_color "0x994400c0 0xbb5500c0 0xdd6600c0 0xff8800c0"
variable key_background_color 0x00000080
variable key_hold_color "0x009933f0 0x00bb44f0 0x00dd66f0 0x00ff88ff"
variable key_select_color "0x999933f0 0xbbbb44f0 0xdddd66f0 0xffff88f0"
variable key_edge_color 0xaaaaaaa0
variable key_edge_color_select 0xaaaa00a0
variable key_edge_color_hold 0x00aa44a0
variable key_edge_color_pressed 0xaa4444a0

# Keyboard layout constants.
variable key_height 16
variable key_hspace 2
variable key_vspace 2
variable board_hborder 4
variable board_vborder 4

proc toggle_osd_keyboard {} {
	if {[osd exists kb]} {
		disable_osd_keyboard
	} else {
		enable_osd_keyboard
	}
}

proc disable_osd_keyboard {} {
	variable is_dingoo

	osd destroy kb
	#unbind mouse buttons and hotkeys
	unbind_default "mouse button1 down"
	unbind_default "mouse button1 up"
	unbind_default "mouse button3 down"
	unbind_default "keyb UP"
	unbind_default "keyb DOWN"
	unbind_default "keyb LEFT"
	unbind_default "keyb RIGHT"
	if {$is_dingoo} {
		unbind_default "keyb LCTRL,PRESS"
		unbind_default "keyb LCTRL,RELEASE"
		unbind_default "keyb LALT"
	} else {
		unbind_default "keyb SPACE,PRESS"
		unbind_default "keyb SPACE,RELEASE"
	}
	#reset keyboard matrix
	for {set i 0} {$i <= 8} {incr i} {
		keymatrixup $i 255
	}

	namespace eval ::osd_control {unset close}
}

proc enable_osd_keyboard {} {
	variable is_dingoo
	variable mouse1_pressed false
	variable keys_held [list]
	variable row_starts [list]
	variable key_color
	variable key_background_color
	variable key_edge_color

	# first remove other OSD controlled widgets (like the osd menu)
	if {[info exists ::osd_control::close]} {
		eval $::osd_control::close
	}
	# and tell how to close this widget
	namespace eval ::osd_control {set close ::osd_keyboard::disable_osd_keyboard}

	#bind stuff
	bind_default "mouse button1 down"  {osd_keyboard::key_handler true}
	bind_default "mouse button1 up"    {osd_keyboard::key_handler false}

	bind_default "mouse button3 down"  {osd_keyboard::key_hold_toggle}

	bind_default "keyb UP"     -repeat {osd_keyboard::selection_row -1}
	bind_default "keyb DOWN"   -repeat {osd_keyboard::selection_row +1}
	bind_default "keyb LEFT"   -repeat {osd_keyboard::selection_col -1}
	bind_default "keyb RIGHT"  -repeat {osd_keyboard::selection_col +1}
	if {$is_dingoo} {
		bind_default "keyb LCTRL,PRESS"    {osd_keyboard::selection_press  }
		bind_default "keyb LCTRL,RELEASE"  {osd_keyboard::selection_release}
		bind_default "keyb LALT"           {osd_keyboard::key_hold_toggle  }
	} else {
		bind_default "keyb SPACE,PRESS"    {osd_keyboard::selection_press  }
		bind_default "keyb SPACE,RELEASE"  {osd_keyboard::selection_release}
	}

	#Define Keyboard (how do we handle the shift/ctrl/graph command?)
	set key_basewidth 18
	set rows {
		"F1*26|F2*26|F3*26|F4*26|F5*26|null*8|Select*26|Stop*26|null*8|Home*26|Ins*26|Del*26" \
		 "Esc|1|2|3|4|5|6|7|8|9|0|-|=|\\|BS" \
		 "Tab*28|Q|W|E|R|T|Y|U|I|O|P|\[|]|Return*28" \
		 "Ctrl*32|A|S|D|F|G|H|J|K|L|;|'|`|<--*24" \
		 "Shift*40|Z|X|C|V|B|N|M|,|.|/|Acc|Shift*36" \
		 "null*40|Cap|Grp|Space*158|Cod"
	}

	# Keyboard layout constants.
	variable key_height
	variable key_hspace
	variable key_vspace
	variable board_hborder
	variable board_vborder

	# Create widgets.
	set board_width \
		[expr {15 * $key_basewidth + 14 * $key_hspace + 2 * $board_hborder}]
	set board_height \
		[expr {6 * $key_height + 5 * $key_vspace + 2 * $board_vborder}]
	osd create rectangle kb \
		-x [expr {(320 - $board_width) / 2}] \
		-y 4 \
		-w $board_width \
		-h $board_height -scaled true -rgba $key_background_color
	set keycount 0
	for {set y 0} {$y <= [llength $rows]} {incr y} {
		set y_base [expr {$board_vborder + $y * ($key_height + $key_vspace)}]
		lappend row_starts $keycount
		set x $board_hborder
		foreach {keys} [split [lindex $rows $y] "|"] {
			lassign [split $keys "*"] key_text key_width
			if {$key_width < 1} {set key_width $key_basewidth}
			if {$key_text ne "null"} {
				set key_y $y_base
				set key_h $key_height
				set bordersize 1
				if {$key_text eq "Return"} {
					set bordersize 0
				} elseif {$key_text eq "<--"} {
					set bordersize 0
					incr key_y -$key_vspace
					incr key_h  $key_vspace
				}
				osd create rectangle kb.$keycount \
					-x $x -y $key_y \
					-w $key_width -h $key_h \
					-rgba $key_color \
					-bordersize $bordersize \
					-borderrgba $key_edge_color
				osd create text kb.$keycount.text \
					-x 1.1 \
					-y 0.1 \
					-text $key_text \
					-size 8
				incr keycount
			}
			set x [expr {$x + $key_width + $key_hspace}]
		}
	}

	variable key_selected
	if {$key_selected == -1} {
		# Select the key in the middle of the keyboard.
		key_select [key_at_coord \
			[expr {$board_width / 2}] \
			[expr {$board_vborder + 3.5 * ($key_height + $key_vspace)}]]
	} else {
		update_key_color $key_selected
	}

	return ""
}

proc selection_row {delta} {
	variable row_starts
	variable key_selected

	# Note: Delta as bias makes sure that if a key is exactly in the middle
	#       above/below two other keys, an up/down or down/up sequence will
	#       end on the same key it started on.
	set x [expr {\
		[osd info kb.$key_selected -x] + [osd info kb.$key_selected -w] / 2 \
		+ $delta}]
	set num_rows [expr {[llength $row_starts] - 1}]
	set row [row_for_key $key_selected]
	while {1} {
		# Determine new row.
		incr row $delta
		if {$row < 0} {
			set row [expr {$num_rows - 1}]
		} elseif {$row >= $num_rows} {
			set row 0
		}

		# Get key at new coordinates.
		set first_key [lindex $row_starts $row]
		set y [expr {\
			[osd info kb.$first_key -y] + [osd info kb.$first_key -h] / 2}]
		set new_selection [key_at_coord $x $y]
		if {$new_selection >= 0} {
			break
		}
	}

	key_select $new_selection
}

proc selection_col {delta} {
	variable row_starts
	variable key_selected

	# Figure out first and last key of current row.
	set row [row_for_key $key_selected]
	set row_start [lindex $row_starts $row]
	set row_end [lindex $row_starts [expr {$row + 1}]]

	# Move left or right.
	set new_selection [expr {$key_selected + $delta}]
	if {$new_selection < $row_start} {
		set new_selection [expr {$row_end - 1}]
	} elseif {$new_selection >= $row_end} {
		set new_selection $row_start
	}

	key_select $new_selection
}

proc selection_press {} {
	variable key_selected
	key_press $key_selected
}

proc selection_release {} {
	key_release
}

proc key_press {key_id} {
	variable key_pressed

	set key_pressed $key_id
	key_matrix $key_id down
	update_key_color $key_id
}

proc key_release {} {
	variable key_pressed
	variable keys_held

	if {$key_pressed == -1} {
		return
	}

	set key_id $key_pressed
	set key_pressed -1
	set index [lsearch -exact $keys_held $key_id]
	if {$index != -1} {
		set keys_held [lreplace $keys_held $index $index]
	}
	key_matrix $key_id up
	update_key_color $key_id
}

proc key_select {key_id} {
	variable key_selected

	set old_selected $key_selected
	set key_selected $key_id
	update_key_color $key_selected
	update_key_color $old_selected
}

proc row_for_key {key_id} {
	variable row_starts
	for {set row 0} {$row < [llength $row_starts] - 1} {incr row} {
		set row_start [lindex $row_starts $row]
		set row_end [lindex $row_starts [expr {$row + 1}]]
		if {$row_start <= $key_id && $key_id < $row_end} {
			return $row
		}
	}
	return -1
}

proc update_key_color {key_id} {
	variable key_selected
	variable key_pressed
	variable keys_held
	variable key_color
	variable key_select_color
	variable key_pressed_color
	variable key_hold_color
	variable key_edge_color
	variable key_edge_color_select
	variable key_edge_color_hold
	variable key_edge_color_pressed

	if {$key_id < 0} {
		return
	} elseif {$key_id == $key_pressed} {
		set color $key_pressed_color
		set edge_color $key_edge_color_pressed
	} elseif {$key_id == $key_selected} {
		set color $key_select_color
		set edge_color $key_edge_color_select
	} elseif {$key_id in $keys_held} {
		set color $key_hold_color
		set edge_color $key_edge_color_hold
	} else {
		set color $key_color
		set edge_color $key_edge_color
	}
	osd configure kb.$key_id -rgba $color -borderrgba $edge_color
}

proc key_at_coord {x y} {
	variable key_hspace
	variable key_height
	variable key_vspace
	variable board_vborder
	variable row_starts

	set row [expr {int(floor( \
		($y - $board_vborder + $key_vspace / 2) / ($key_height + $key_vspace) \
		))}]
	if {$row >= 0 && $row < [llength $row_starts] - 1} {
		set row_start [lindex $row_starts $row]
		set row_end [lindex $row_starts [expr {$row + 1}]]
		for {set key_id $row_start} {$key_id < $row_end} {incr key_id} {
			set relx [expr {$x - [osd info kb.$key_id -x] + $key_hspace / 2}]
			if {$relx >= 0 && $relx < [osd info kb.$key_id -w] + $key_hspace} {
				return $key_id
			}
		}
	}
	return -1
}

proc key_at_mouse {} {
	lassign [osd info kb -mousecoord] x y
	key_at_coord [expr {$x * [osd info kb -w]}] \
	             [expr {$y * [osd info kb -h]}]
}

proc key_hold_toggle {} {
	variable keys_held
	variable key_selected
	variable is_dingoo

	if {$is_dingoo} {
		set key_id $key_selected
	} else {
		set key_id [key_at_mouse]
	}
	if {$key_id >= 0} {
		set index [lsearch -exact $keys_held $key_id]
		if {$index == -1} {
			key_matrix $key_id down
			lappend keys_held $key_id
		} else {
			key_matrix $key_id up
			set keys_held [lreplace $keys_held $index $index]
		}
		update_key_color $key_id
	}
}

proc key_handler {mouse_state} {
	if {$mouse_state} {
		set key_id [key_at_mouse]
		if {$key_id >= 0} {
			key_press $key_id
			key_select $key_id
		}
	} else {
		key_release
	}
}

proc key_matrix {keynum state} {
	set key [string trim "[osd info kb.$keynum.text -text]"]

	set km keymatrix$state

	#info from http://map.grauw.nl/articles/keymatrix.php (thanks Grauw)

	switch -- $key {
		"0"      {$km 0 1}
		"1"      {$km 0 2}
		"2"      {$km 0 4}
		"3"      {$km 0 8}
		"4"      {$km 0 16}
		"5"      {$km 0 32}
		"6"      {$km 0 64}
		"7"      {$km 0 128}

		"8"      {$km 1 1}
		"9"      {$km 1 2}
		"-"      {$km 1 4}
		"="      {$km 1 8}
		"\\"     {$km 1 16}
		"\["     {$km 1 32}
		"\]"     {$km 1 64}
		";"      {$km 1 128}

		"'"      {$km 2 1}
		"`"      {$km 2 2}
		","      {$km 2 4}
		"."      {$km 2 8}
		"/"      {$km 2 16}
		"Acc"    {$km 2 32}
		"A"      {$km 2 64}
		"B"      {$km 2 128}

		"C"      {$km 3 1}
		"D"      {$km 3 2}
		"E"      {$km 3 4}
		"F"      {$km 3 8}
		"G"      {$km 3 16}
		"H"      {$km 3 32}
		"I"      {$km 3 64}
		"J"      {$km 3 128}

		"K"      {$km 4 1}
		"L"      {$km 4 2}
		"M"      {$km 4 4}
		"N"      {$km 4 8}
		"O"      {$km 4 16}
		"P"      {$km 4 32}
		"Q"      {$km 4 64}
		"R"      {$km 4 128}

		"S"      {$km 5 1}
		"T"      {$km 5 2}
		"U"      {$km 5 4}
		"V"      {$km 5 8}
		"W"      {$km 5 16}
		"X"      {$km 5 32}
		"Y"      {$km 5 64}
		"Z"      {$km 5 128}

		"Shift"  {$km 6 1}
		"Ctrl"   {$km 6 2}
		"Grp"    {$km 6 4}
		"Cap"    {$km 6 8}
		"Cod"    {$km 6 16}
		"F1"     {$km 6 32}
		"F2"     {$km 6 64}
		"F3"     {$km 6 128}

		"F4"     {$km 7 1}
		"F5"     {$km 7 2}
		"Esc"    {$km 7 4}
		"Tab"    {$km 7 8}
		"Stop"   {$km 7 16}
		"BS"     {$km 7 32}
		"Select" {$km 7 64}
		"Return" {$km 7 128}
		"<--"    {$km 7 128}

		"Space"  {$km 8 1}
		"Home"   {$km 8 2}
		"Ins"    {$km 8 4}
		"Del"    {$km 8 8}
	}

	#cursor keys etc (not implemented... should we?)
	#numeric keyboard?
}

# for Dingoo assign the start button to show the keyboard
if {$is_dingoo} {
	bind_default "keyb RETURN" toggle_osd_keyboard
}

namespace export toggle_osd_keyboard

};# namespace osd_keyboard

namespace import osd_keyboard::*