Added support for Keymap files

Display characters for each key in the selected font
Pressed modifiers are taken into account


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@8395 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval
2004-07-14 16:10:25 +00:00
parent 26b5401027
commit 06938ea6ce
9 changed files with 1032 additions and 674 deletions
+2 -1
View File
@@ -5,5 +5,6 @@ AddResources Keymap : Keymap.rdef ;
Preference Keymap :
KeymapApplication.cpp
KeymapWindow.cpp
KeymapListItem.cpp
KeymapListItem.cpp
Keymap.cpp
: be ;
+142
View File
@@ -0,0 +1,142 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
//
// Copyright (c) 2004, Haiku
//
// This software is part of the Haiku distribution and is covered
// by the Haiku license.
//
//
// File: Keymap.cpp
// Author: Sandor Vroemisse, Jérôme Duval
// Description: Keymap Preferences
// Created : July 12, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#include "Keymap.h"
#include <stdio.h>
#include <string.h>
#include <ByteOrder.h>
#include <File.h>
static void
print_key( char *chars, int32 offset )
{
int size = chars[offset++];
switch( size ) {
case 0:
// Not mapped
printf( "N/A" );
break;
case 1:
// 1-byte UTF-8/ASCII character
printf( "%c", chars[offset] );
break;
default:
// 2-, 3-, or 4-byte UTF-8 character
{
char *str = new char[size + 1];
strncpy( str, &(chars[offset]), size );
str[size] = 0;
printf( "%s", str );
delete [] str;
}
break;
}
printf( "\t" );
}
void
Keymap::GetChars(int32 keyCode, uint32 modifiers, char **chars)
{
int32 offset = 0;
switch (modifiers & 0xff) {
case 0: offset = fKeys.normal_map[keyCode]; break;
case B_SHIFT_KEY: offset = fKeys.shift_map[keyCode]; break;
case B_CAPS_LOCK: offset = fKeys.caps_map[keyCode]; break;
case B_CAPS_LOCK|B_SHIFT_KEY: offset = fKeys.caps_shift_map[keyCode]; break;
case B_OPTION_KEY: offset = fKeys.option_map[keyCode]; break;
case B_OPTION_KEY|B_SHIFT_KEY: offset = fKeys.option_shift_map[keyCode]; break;
case B_OPTION_KEY|B_CAPS_LOCK: offset = fKeys.option_caps_map[keyCode]; break;
case B_OPTION_KEY|B_SHIFT_KEY|B_CAPS_LOCK: offset = fKeys.option_caps_shift_map[keyCode]; break;
case B_CONTROL_KEY: offset = fKeys.control_map[keyCode]; break;
}
int size = fChars[offset++];
switch( size ) {
case 0:
// Not mapped
*chars = NULL;
break;
default:
// 1-, 2-, 3-, or 4-byte UTF-8 character
{
char *str = *chars = new char[size + 1];
strncpy(str, &(fChars[offset]), size );
str[size] = 0;
}
break;
}
}
void
Keymap::DumpKeymap()
{
// Print a chart of the normal, shift, option, and option+shift
// keys.
printf( "Key #\tNormal\tShift\tCaps\tC+S\tOption\tO+S\tO+C\tO+C+S\tControl\n" );
for( int idx = 0; idx < 128; idx++ ) {
printf( " 0x%x\t", idx );
print_key( fChars, fKeys.normal_map[idx] );
print_key( fChars, fKeys.shift_map[idx] );
print_key( fChars, fKeys.caps_map[idx] );
print_key( fChars, fKeys.caps_shift_map[idx] );
print_key( fChars, fKeys.option_map[idx] );
print_key( fChars, fKeys.option_shift_map[idx] );
print_key( fChars, fKeys.option_caps_map[idx] );
print_key( fChars, fKeys.option_caps_shift_map[idx] );
print_key( fChars, fKeys.control_map[idx] );
printf( "\n" );
}
}
status_t
Keymap::Load(entry_ref &ref)
{
status_t err;
BFile file(&ref, B_READ_ONLY);
if ((err = file.InitCheck()) != B_OK) {
printf("error %s\n", strerror(err));
return err;
}
if (file.Read(&fKeys, sizeof(fKeys)) < (ssize_t)sizeof(fKeys)) {
return B_BAD_VALUE;
}
for (uint32 i=0; i<sizeof(fKeys)/4; i++)
((uint32*)&fKeys)[i] = B_BENDIAN_TO_HOST_INT32(((uint32*)&fKeys)[i]);
uint32 chars_size;
if (file.Read(&chars_size, sizeof(uint32)) < (ssize_t)sizeof(uint32)) {
return B_BAD_VALUE;
}
chars_size = B_BENDIAN_TO_HOST_INT32(chars_size);
if (!fChars)
delete[] fChars;
fChars = new char[chars_size];
file.Read(fChars, chars_size);
return B_OK;
}
+34
View File
@@ -0,0 +1,34 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
//
// Copyright (c) 2004, Haiku
//
// This software is part of the Haiku distribution and is covered
// by the Haiku license.
//
//
// File: Keymap.h
// Author: Jérôme Duval
// Description: Keymap Preferences
// Created : July 12, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#ifndef KEYMAP_H
#define KEYMAP_H
#include <InterfaceDefs.h>
#include <Entry.h>
class Keymap
{
public:
status_t Load(entry_ref &ref);
void DumpKeymap();
void GetChars(int32 keyCode, uint32 modifiers, char **chars);
private:
char *fChars;
key_map fKeys;
};
#endif //KEYMAP_H
+36 -68
View File
@@ -1,7 +1,24 @@
#include <add-ons/input_server/InputServerDevice.h>
#include <storage/File.h>
#include <app/Application.h>
#include <storage/Directory.h>
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
//
// Copyright (c) 2004, Haiku
//
// This software is part of the Haiku distribution and is covered
// by the Haiku license.
//
//
// File: KeymapApplication.cpp
// Author: Sandor Vroemisse, Jérôme Duval
// Description: Keymap Preferences
// Created : July 12, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#include <InputServerDevice.h>
#include <File.h>
#include <Application.h>
#include <Directory.h>
#include <Entry.h>
#include <Path.h>
#if DEBUG
#include <interface/Input.h>
#include <stdio.h>
@@ -27,67 +44,11 @@ void KeymapApplication::MessageReceived( BMessage * message )
}
/*
* Returns a BList containing BEntry objects
* representing the systems' keymap files
*/
BList* KeymapApplication::SystemMaps()
{
// TODO: find a constant containing this path and use that instead
return EntryList( "/boot/beos/etc/Keymap" );
}
BList* KeymapApplication::UserMaps()
{
// TODO: find a constant containing this path and use that instead
return EntryList( "/boot/home/config/settings/Keymap" );
}
BList* KeymapApplication::EntryList( char *directoryPath )
{
BList *entryList;
BDirectory *directory;
BEntry *currentEntry;
entryList = new BList();
directory = new BDirectory();
if( directory->SetTo( directoryPath ) == B_OK )
// put each files' name in the list
while( true ) {
currentEntry = new BEntry();
if( directory->GetNextEntry( currentEntry, true ) != B_OK ) {
delete currentEntry;
break;
}
entryList->AddItem( currentEntry );
#if DEBUG
char name[B_FILE_NAME_LENGTH];
currentEntry->GetName( name );
printf("Found: %s\n",name);
#endif //DEBUG
}
else {
// something went wrong; no system keymaps today
// TODO: catch error codes and act appropriately
}
delete directory;
return entryList;
}
BEntry* KeymapApplication::CurrentMap()
{
BEntry *entry;
// TODO: find a constant containing this path and use that instead
entry = new BEntry( "/boot/home/config/settings/Key_map" );
return entry;
}
bool KeymapApplication::UseKeymap( BEntry *keymap )
/*bool
KeymapApplication::UseKeymap( BEntry *keymap )
{ // Copies keymap to ~/config/settings/key_map
BFile *inFile;
BFile *outFile;
@@ -132,14 +93,18 @@ bool KeymapApplication::UseKeymap( BEntry *keymap )
return true;
}
bool KeymapApplication::IsValidKeymap( BFile *inFile )
bool
KeymapApplication::IsValidKeymap( BFile *inFile )
{
// TODO: implement this thing
return true;
}
int KeymapApplication::Copy( BFile *original, BFile *copy )
int
KeymapApplication::Copy( BFile *original, BFile *copy )
{
char *buffer[ COPY_BUFFER_SIZE ];
int offset = 0;
@@ -169,9 +134,11 @@ int KeymapApplication::Copy( BFile *original, BFile *copy )
}
return errorCode;
}
}*/
int KeymapApplication::NotifyInputServer()
int
KeymapApplication::NotifyInputServer()
{
// This took me days to find out. Go figure.
// Be didn't need to restart the thing - they prolly used
@@ -182,10 +149,11 @@ int KeymapApplication::NotifyInputServer()
}
int main ()
int
main ()
{
new KeymapApplication;
be_app->Run();
delete be_app;
return 0;
return B_OK;
}
+19 -11
View File
@@ -1,12 +1,24 @@
#ifndef OBOS_KEYMAP_APPLICATION_H
#define OBOS_KEYMAP_APPLICATION_H
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
//
// Copyright (c) 2004, Haiku
//
// This software is part of the Haiku distribution and is covered
// by the Haiku license.
//
//
// File: KeymapApplication.h
// Author: Sandor Vroemisse, Jérôme Duval
// Description: Keymap Preferences
// Created : July 12, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#ifndef KEYMAP_APPLICATION_H
#define KEYMAP_APPLICATION_H
#include <storage/Entry.h>
#include <support/List.h>
#define APP_SIGNATURE "application/x-vnd.OpenBeOS-Keymap"
#define COPY_BUFFER_SIZE 1 * 1024
class KeymapWindow;
@@ -14,19 +26,15 @@ class KeymapApplication : public BApplication {
public:
KeymapApplication();
void MessageReceived(BMessage *message);
BList* SystemMaps();
BList* UserMaps();
BEntry* CurrentMap();
bool UseKeymap( BEntry *keymap );
protected:
KeymapWindow *fWindow;
BList* EntryList( char *directoryPath );
bool IsValidKeymap( BFile *inFile );
int Copy( BFile *original, BFile *copy );
int NotifyInputServer();
};
#endif // OBOS_KEYMAP_APPLICATION_H
#endif // KEYMAP_APPLICATION_H
+18 -18
View File
@@ -1,3 +1,18 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
//
// Copyright (c) 2004, Haiku
//
// This software is part of the Haiku distribution and is covered
// by the Haiku license.
//
//
// File: KeymapListItem.cpp
// Author: Sandor Vroemisse, Jérôme Duval
// Description: Keymap Preferences
// Created : July 12, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
/*
* A BStringItem modified such that it holds
* the BEntry object it corresponds with
@@ -6,23 +21,8 @@
#include "KeymapListItem.h"
KeymapListItem::KeymapListItem( BEntry *keymap )
: BStringItem( "" )
KeymapListItem::KeymapListItem( entry_ref &keymap, const char* name )
: BStringItem( name ? name : keymap.name ),
fKeymap(keymap)
{
char name[B_FILE_NAME_LENGTH];
fKeymap = keymap;
keymap->GetName( name );
SetText( name );
}
KeymapListItem::~KeymapListItem()
{
delete fKeymap;
}
BEntry* KeymapListItem::KeymapEntry()
{
return fKeymap;
}
+23 -11
View File
@@ -1,24 +1,36 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
//
// Copyright (c) 2004, Haiku
//
// This software is part of the Haiku distribution and is covered
// by the Haiku license.
//
//
// File: KeymapListItem.h
// Author: Sandor Vroemisse, Jérôme Duval
// Description: Keymap Preferences
// Created : July 12, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
/*
* A BStringItem modified such that it holds
* the BEntry object it corresponds with
*/
#ifndef OBOS_KEYMAP_LIST_ITEM_H
#define OBOS_KEYMAP_LIST_ITEM_H
#include <interface/ListItem.h>
#include <storage/Entry.h>
#ifndef KEYMAP_LIST_ITEM_H
#define KEYMAP_LIST_ITEM_H
#include <ListItem.h>
#include <Entry.h>
class KeymapListItem : public BStringItem {
public:
KeymapListItem( BEntry *keymap );
~KeymapListItem();
BEntry *KeymapEntry();
KeymapListItem( entry_ref &keymap, const char* name = NULL);
entry_ref & KeymapEntry() { return fKeymap; };
protected:
BEntry *fKeymap;
entry_ref fKeymap;
};
#endif //OBOS_KEYMAP_LIST_ITEM_H
#endif //KEYMAP_LIST_ITEM_H
File diff suppressed because it is too large Load Diff
+43 -27
View File
@@ -1,15 +1,27 @@
#ifndef OBOS_KEYMAP_WINDOW_H
#define OBOS_KEYMAP_WINDOW_H
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
//
// Copyright (c) 2004, Haiku
//
// This software is part of the Haiku distribution and is covered
// by the Haiku license.
//
//
// File: KeymapWindow.h
// Author: Sandor Vroemisse, Jérôme Duval
// Description: Keymap Preferences
// Created : July 12, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#ifndef KEYMAP_WINDOW_H
#define KEYMAP_WINDOW_H
#include <interface/Window.h>
#include <support/List.h>
#include <interface/MenuBar.h>
#include "Keymap.h"
#if !DEBUG
#define WINDOW_TITLE "Keymap"
#else
#define WINDOW_TITLE "OBOS Keymap"
#endif //DEBUG
#define WINDOW_TITLE "Keymap"
#define WINDOW_LEFT_TOP_POSITION BPoint( 80, 25 )
#define WINDOW_DIMENSIONS BRect( 0,0, 612,256 )
@@ -19,17 +31,24 @@ class KeymapApplication;
class MapView : public BView
{
public:
MapView(BRect rect, const char *name);
MapView(BRect rect, const char *name, Keymap *keymap);
void Draw(BRect rect);
void DrawKey(BRect rect, bool pressed, bool vertical = false);
void DrawKey(int32 keyCode);
void DrawBorder(BRect borderRect);
//void Pulse();
void AttachedToWindow();
void KeyDown(const char* bytes, int32 numBytes);
void KeyUp(const char* bytes, int32 numBytes);
void MessageReceived(BMessage *msg);
void SetFontFamily(const font_family family) {fCurrentFont.SetFamilyAndStyle(family, NULL); };
private:
key_info fOldKeyInfo;
BRect fKeysRect[128];
bool fKeysVertical[128];
uint8 fKeyState[16];
BFont fCurrentFont;
Keymap *fCurrentMap;
BTextView *fTextView;
};
@@ -38,31 +57,28 @@ public:
KeymapWindow( BRect frame );
bool QuitRequested();
void MessageReceived( BMessage* message );
//void AllAttached();
protected:
KeymapApplication *fApplication;
BView *fPlaceholderView;
BListView *fSystemListView;
BListView *fUserListView;
// the map that's currently highlighted
BEntry *fSelectedMap;
BButton *fUseButton;
BButton *fRevertButton;
const char *title;
BMenu *fFontMenu;
MapView *fMapView;
BMenuBar *AddMenuBar();
void AddMaps();
BList *ListItemsFromEntryList( BList * entryList);
KeymapListItem* ItemFromEntry( BEntry *entry );
void HandleSystemMapSelected( BMessage* );
void HandleUserMapSelected( BMessage* );
void HandleMapSelected( BMessage *selectionMessage,
BListView * selectedView, BListView * otherView );
void UseKeymap();
BMenuBar *AddMenuBar();
void AddMaps(BView *placeholderView);
//KeymapListItem* ItemFromEntry( BEntry *entry );
void UseKeymap();
void FillSystemMaps();
void FillUserMaps();
BEntry* CurrentMap();
Keymap fCurrentMap;
};
#endif // OBOS_KEYMAP_WINDOW_H
#endif // KEYMAP_WINDOW_H