Programs running inside of QEMU can sometimes use more CPU time than is really
needed. To solve this problem, we just need to throttle the virtual CPU. This
feature will stop laptops from burning up. 

This patch adds a menu called Speed that has menu items from 10 to 1. They
represent the speed options. 10 is full speed and 1 is slowest.

Signed-off-by: John Arbuckle <programmingk...@gmail.com>
---
 ui/cocoa.m | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/ui/cocoa.m b/ui/cocoa.m
index 26d4a1c..293e910 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -35,6 +35,7 @@
 #include "sysemu/blockdev.h"
 #include "qemu-version.h"
 #include <Carbon/Carbon.h>
+#include "qom/cpu.h"
 
 #ifndef MAC_OS_X_VERSION_10_5
 #define MAC_OS_X_VERSION_10_5 1050
@@ -828,6 +829,7 @@ QemuCocoaView *cocoaView;
 - (void)openDocumentation:(NSString *)filename;
 - (IBAction) do_about_menu_item: (id) sender;
 - (void)make_about_window;
+- (void)adjustSpeed:(id)sender;
 @end
 
 @implementation QemuCocoaAppController
@@ -1234,6 +1236,41 @@ QemuCocoaView *cocoaView;
     [superView addSubview: copyright_label];
 }
 
+/* Used by the Speed menu items */
+- (void)adjustSpeed:(id)sender
+{
+    /* 10 has two digits in it - plus the NULL termination at the end */
+    const int max_title_size = 3;
+    char menu_item_title[max_title_size];
+    int speed, menu_number, count, item;
+    NSMenu *menu;
+    NSArray *itemArray;
+
+    // uncheck all the menu items in the Speed menu
+    menu = [sender menu];
+       if(menu != nil)
+       {
+               count = [[menu itemArray] count];
+               itemArray = [menu itemArray];
+               for(item = 0; item < count; item++)  // unselect each item
+                       [[itemArray objectAtIndex: item] setState: NSOffState];
+       }
+
+    // check the menu item
+    [sender setState: NSOnState];
+
+    // get the menu number
+    snprintf(menu_item_title, max_title_size, "%s", [[sender title] 
cStringUsingEncoding: NSASCIIStringEncoding]);
+    sscanf(menu_item_title, "%d", &menu_number);
+
+     /* Calculate the speed */
+    // inverse relationship between menu item number and speed
+    speed = -1 * menu_number + 10;
+    speed = speed * 10;
+    cpu_throttle_set(speed);
+    COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), 
'%');
+}
+
 @end
 
 
@@ -1316,6 +1353,25 @@ int main (int argc, const char * argv[]) {
     [menuItem setSubmenu:menu];
     [[NSApp mainMenu] addItem:menuItem];
 
+    // Speed menu
+    menu = [[NSMenu alloc] initWithTitle:@"Speed"];
+    menuItem = [[[NSMenuItem alloc] initWithTitle:@"10 (fastest)" 
action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease];
+    [menuItem setState: NSOnState];
+    [menu addItem: menuItem];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"9" 
action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"8" 
action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"7" 
action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"6" 
action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"5" 
action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"4" 
action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"3" 
action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"2" 
action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"1" 
action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]];
+    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"0 (slowest)" 
action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]];
+    menuItem = [[[NSMenuItem alloc] initWithTitle:@"Speed" action:nil 
keyEquivalent:@""] autorelease];
+    [menuItem setSubmenu:menu];
+    [[NSApp mainMenu] addItem:menuItem];
+
     // Window menu
     menu = [[NSMenu alloc] initWithTitle:@"Window"];
     [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" 
action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // 
Miniaturize
-- 
2.7.2



Reply via email to