4 * Construct and manage the controller configuration pane
6 * Copyright (c) 2008 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
22 #include "maple/maple.h"
24 #include <glib/gstrfuncs.h>
28 #define KEYBINDING_SIZE 110
30 static void cocoa_config_keysym_hook(void *data, const gchar *keysym);
32 @interface KeyBindingEditor (Private)
33 - (void)updateKeysym: (const gchar *)sym;
36 @implementation KeyBindingEditor
42 [self setFieldEditor: YES];
43 [self setEditable: FALSE];
48 if( lastValue != nil ) {
54 - (void)setPrimed: (BOOL)primed
56 if( primed != isPrimed ) {
59 lastValue = [[NSString stringWithString: [self string]] retain];
60 [self setString: @"<press key>"];
61 input_set_keysym_hook(cocoa_config_keysym_hook, self);
65 input_set_keysym_hook(NULL,NULL);
69 - (void)resignFirstResponder
72 [self setString: lastValue];
75 [super resignFirstResponder];
77 - (void)fireBindingChanged
79 id delegate = [self delegate];
80 if( delegate != nil && [delegate respondsToSelector:@selector(textDidChange:)] ) {
81 [delegate textDidChange: [NSNotification notificationWithName: NSTextDidChangeNotification object: self]];
85 - (void)updateKeysym: (const gchar *)sym
88 [self setString: [NSString stringWithCString: sym]];
90 [self fireBindingChanged];
93 - (void)updateMousesym: (int)button
95 gchar *keysym = input_keycode_to_keysym( &system_mouse_driver, (button+1) );
96 if( keysym != NULL ) {
97 [self updateKeysym: keysym ];
101 - (void)keyPressed: (int)keycode
103 gchar *keysym = input_keycode_to_keysym(NULL, keycode);
104 if( keysym != NULL ) {
105 [self updateKeysym: keysym];
109 - (void)insertText:(id)string
113 - (void)mouseDown: (NSEvent *)event
116 [self updateMousesym: 0];
118 [self setPrimed: YES];
119 [super mouseDown: event];
122 - (void)rightMouseDown: (NSEvent *)event
125 [self updateMousesym: 1];
128 - (void)otherMouseDown: (NSEvent *)event
131 [self updateMousesym: [event buttonNumber]];
134 - (void)keyDown: (NSEvent *) event
136 NSString *chars = [event characters];
138 if( chars != NULL && [chars length] == 1 && [chars characterAtIndex: 0] == 27 ) {
139 // Escape char = abort change
140 [self setString: lastValue];
141 [self setPrimed: NO];
143 [self keyPressed: ([event keyCode]+1)];
146 if( chars != NULL && [chars length] == 1 ) {
147 int ch = [chars characterAtIndex: 0];
150 [self setString: @""];
151 [self fireBindingChanged];
154 [self setPrimed: YES];
157 [super keyDown: event];
161 [super keyDown: event];
165 - (void)flagsChanged: (NSEvent *) event
168 [self keyPressed: ([event keyCode]+1)];
170 [super flagsChanged: event];
174 static void cocoa_config_keysym_hook(void *data, const gchar *keysym)
176 KeyBindingEditor *editor = (KeyBindingEditor *)data;
177 [editor updateKeysym: keysym];
181 @implementation KeyBindingField
184 /*************************** Key-binding sub-view ***********************/
186 #define MAX_KEY_BINDINGS 32
188 @interface ControllerKeyBindingView : NSView
190 maple_device_t device;
191 KeyBindingField *field[MAX_KEY_BINDINGS][2];
193 - (id)initWithFrame: (NSRect)frameRect;
194 - (void)setDevice: (maple_device_t)device;
197 @implementation ControllerKeyBindingView
198 - (id)initWithFrame: (NSRect)frameRect
200 if( [super initWithFrame: frameRect] == nil ) {
211 - (void)removeSubviews
213 [[self subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
215 - (void)controlTextDidChange: (NSNotification *)notify
217 int binding = [[notify object] tag];
218 NSString *val1 = [field[binding][0] stringValue];
219 NSString *val2 = [field[binding][1] stringValue];
220 char buf[ [val1 length] + [val2 length] + 2 ];
221 const gchar *p = NULL;
223 if( [val1 length] == 0 ) {
224 if( [val2 length] != 0 ) {
225 p = [val2 UTF8String];
227 } else if( [val2 length] == 0 ) {
228 p = [val1 UTF8String];
230 sprintf( buf, "%s,%s", [val1 UTF8String], [val2 UTF8String] );
233 maple_set_device_config_value( device, binding, p );
234 lxdream_save_config();
236 - (void)setDevice: (maple_device_t)newDevice
239 [self removeSubviews];
240 if( device != NULL ) {
241 lxdream_config_entry_t config = maple_get_device_config(device);
242 if( config != NULL ) {
245 for( count=0; config[count].key != NULL; count++ );
247 NSSize size = NSMakeSize(85+KEYBINDING_SIZE*2+TEXT_GAP*4, count*(TEXT_HEIGHT+TEXT_GAP)+TEXT_GAP);
248 [self setFrameSize: size];
249 [self scrollRectToVisible: NSMakeRect(0,0,1,1)];
251 for( i=0; config[i].key != NULL; i++ ) {
252 NSRect frame = NSMakeRect(x, y + 2, 85, LABEL_HEIGHT);
253 NSTextField *label = cocoa_gui_add_label(self, NS_(config[i].label), frame);
254 [label setAlignment: NSRightTextAlignment];
256 frame = NSMakeRect( x + 85 + TEXT_GAP, y, KEYBINDING_SIZE, TEXT_HEIGHT);
257 field[i][0] = [[KeyBindingField alloc] initWithFrame: frame];
258 [field[i][0] setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
259 [field[i][0] setTag: i];
260 [field[i][0] setDelegate: self];
261 [self addSubview: field[i][0]];
263 frame = NSMakeRect( x + 85 + KEYBINDING_SIZE + (TEXT_GAP*2), y, KEYBINDING_SIZE, TEXT_HEIGHT);
264 field[i][1] = [[KeyBindingField alloc] initWithFrame: frame];
265 [field[i][1] setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
266 [field[i][1] setTag: i];
267 [field[i][1] setDelegate: self];
268 [self addSubview: field[i][1]];
270 if( config[i].value != NULL ) {
271 gchar **parts = g_strsplit(config[i].value,",",3);
272 if( parts[0] != NULL ) {
273 [field[i][0] setStringValue: [NSString stringWithCString: parts[0]]];
274 if( parts[1] != NULL ) {
275 [field[i][1] setStringValue: [NSString stringWithCString: parts[1]]];
281 y += (TEXT_HEIGHT + TEXT_GAP);
284 [self setFrameSize: NSMakeSize(100,TEXT_HEIGHT+TEXT_GAP) ];
287 [self setFrameSize: NSMakeSize(100,TEXT_HEIGHT+TEXT_GAP) ];
292 /*************************** Top-level controller pane ***********************/
294 @interface LxdreamPrefsControllerPane: LxdreamPrefsPane
296 struct maple_device *save_controller[4];
298 ControllerKeyBindingView *key_bindings;
300 + (LxdreamPrefsControllerPane *)new;
303 @implementation LxdreamPrefsControllerPane
304 + (LxdreamPrefsControllerPane *)new
306 return [[LxdreamPrefsControllerPane alloc] initWithFrame: NSMakeRect(0,0,600,400)];
308 - (id)initWithFrame: (NSRect)frameRect
310 if( [super initWithFrame: frameRect title: NS_("Controllers")] == nil ) {
313 const struct maple_device_class **devices = maple_get_device_classes();
316 int y = [self contentHeight] - TEXT_HEIGHT - TEXT_GAP;
318 NSBox *rule = [[NSBox alloc] initWithFrame:
319 NSMakeRect(210+(TEXT_GAP*3), 1, 1, [self contentHeight] + TEXT_GAP - 2)];
320 [rule setAutoresizingMask: (NSViewMaxXMargin|NSViewHeightSizable)];
321 [rule setBoxType: NSBoxSeparator];
322 [self addSubview: rule];
324 NSRect bindingFrame = NSMakeRect(210+(TEXT_GAP*4), 0,
325 frameRect.size.width - (210+(TEXT_GAP*4)), [self contentHeight] + TEXT_GAP );
326 NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame: bindingFrame];
327 key_bindings = [[ControllerKeyBindingView alloc] initWithFrame: bindingFrame ];
328 [scrollView setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
329 [scrollView setDocumentView: key_bindings];
330 [scrollView setDrawsBackground: NO];
331 [scrollView setHasVerticalScroller: YES];
332 [scrollView setAutohidesScrollers: YES];
334 [self addSubview: scrollView];
335 [key_bindings setDevice: maple_get_device(0,0)];
337 for( i=0; i<MAX_DEVICES; i++ ) {
339 save_controller[i] = NULL;
340 maple_device_t device = maple_get_device(i,0);
342 snprintf( buf, sizeof(buf), _("Slot %d."), i );
343 radio[i] = [[NSButton alloc] initWithFrame: NSMakeRect( x, y, 60, TEXT_HEIGHT )];
344 [radio[i] setTitle: [NSString stringWithUTF8String: buf]];
345 [radio[i] setTag: i];
346 [radio[i] setButtonType: NSRadioButton];
347 [radio[i] setAlignment: NSRightTextAlignment];
348 [radio[i] setTarget: self];
349 [radio[i] setAction: @selector(radioChanged:)];
350 [radio[i] setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
351 [self addSubview: radio[i]];
354 NSPopUpButton *popup = [[NSPopUpButton alloc] initWithFrame: NSMakeRect(x,y,150,TEXT_HEIGHT)
356 [popup addItemWithTitle: NS_("<empty>")];
357 [popup setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
358 [[popup itemAtIndex: 0] setTag: 0];
359 for( j=0; devices[j] != NULL; j++ ) {
360 [popup addItemWithTitle: [NSString stringWithUTF8String: devices[j]->name]];
361 if( device != NULL && device->device_class == devices[j] ) {
362 [popup selectItemAtIndex: (j+1)];
364 [[popup itemAtIndex: (j+1)] setTag: (j+1)];
366 [popup setTarget: self];
367 [popup setAction: @selector(deviceChanged:)];
369 [self addSubview: popup];
370 y -= (TEXT_HEIGHT+TEXT_GAP);
373 [radio[0] setState: NSOnState];
377 - (void)radioChanged: (id)sender
379 int slot = [sender tag];
381 for( i=0; i<MAX_DEVICES; i++ ) {
383 [radio[i] setState: NSOffState];
386 [key_bindings setDevice: maple_get_device(slot,0)];
388 - (void)deviceChanged: (id)sender
390 int slot = [sender tag];
391 int new_device_idx = [sender indexOfSelectedItem] - 1, i;
392 maple_device_class_t new_device_class = NULL;
394 for( i=0; i<MAX_DEVICES; i++ ) {
396 [radio[i] setState: NSOnState];
398 [radio[i] setState: NSOffState];
402 maple_device_t current = maple_get_device(slot,0);
403 maple_device_t new_device = NULL;
404 if( new_device_idx != -1 ) {
405 new_device_class = maple_get_device_classes()[new_device_idx];
407 if( current == NULL ? new_device_class == NULL : current->device_class == new_device_class ) {
409 [key_bindings setDevice: current];
412 if( current != NULL && current->device_class == &controller_class ) {
413 save_controller[slot] = current->clone(current);
415 if( new_device_class == NULL ) {
416 maple_detach_device(slot,0);
418 if( new_device_class == &controller_class && save_controller[slot] != NULL ) {
419 new_device = save_controller[slot];
420 save_controller[slot] = NULL;
422 new_device = maple_new_device( new_device_class->name );
424 maple_attach_device(new_device,slot,0);
426 [key_bindings setDevice: maple_get_device(slot,0)];
427 lxdream_save_config();
431 NSView *cocoa_gui_create_prefs_controller_pane()
433 return [LxdreamPrefsControllerPane new];
.