comparison mcabber/src/screen.c @ 939:12fa2ae6445d

Get rid of "busy-waiting" (Christof Meerwald) This patch from Christof Meerwald, slightly modified, reduces mcabber CPU usage and the number of context switches per second.
author Mikael Berthe <mikael@lilotux.net>
date Tue, 11 Jul 2006 22:50:48 +0200
parents 3e592fb33aab
children fc30221b952d
comparison
equal deleted inserted replaced
938:40175f3dcef7 939:12fa2ae6445d
81 static char *multiline, *multimode_subj; 81 static char *multiline, *multimode_subj;
82 int update_roster; 82 int update_roster;
83 int utf8_mode = 0; 83 int utf8_mode = 0;
84 static bool Autoaway; 84 static bool Autoaway;
85 static bool Curses; 85 static bool Curses;
86 static time_t LastActivity;
86 87
87 static char inputLine[INPUTLINE_LENGTH+1]; 88 static char inputLine[INPUTLINE_LENGTH+1];
88 static char *ptr_inputline; 89 static char *ptr_inputline;
89 static short int inputline_offset; 90 static short int inputline_offset;
90 static int completion_started; 91 static int completion_started;
366 367
367 if (!buffer_locale) { 368 if (!buffer_locale) {
368 wprintw(logWnd, 369 wprintw(logWnd,
369 "\n%s*Error: cannot convert string to locale.", strtimestamp); 370 "\n%s*Error: cannot convert string to locale.", strtimestamp);
370 update_panels(); 371 update_panels();
371 doupdate();
372 g_free(buffer); 372 g_free(buffer);
373 g_free(btext); 373 g_free(btext);
374 return; 374 return;
375 } 375 }
376 376
381 buf_specialwindow = btext; 381 buf_specialwindow = btext;
382 382
383 if (Curses) { 383 if (Curses) {
384 wprintw(logWnd, "\n%s", buffer_locale); 384 wprintw(logWnd, "\n%s", buffer_locale);
385 update_panels(); 385 update_panels();
386 doupdate();
387 scr_WriteInWindow(NULL, buf_specialwindow, timestamp, 386 scr_WriteInWindow(NULL, buf_specialwindow, timestamp,
388 HBB_PREFIX_SPECIAL, FALSE); 387 HBB_PREFIX_SPECIAL, FALSE);
389 } else { 388 } else {
390 printf("%s\n", buffer_locale); 389 printf("%s\n", buffer_locale);
391 // ncurses are not initialized yet, so we call directly hbuf routine 390 // ncurses are not initialized yet, so we call directly hbuf routine
731 // Show and refresh the window 730 // Show and refresh the window
732 top_panel(win_entry->panel); 731 top_panel(win_entry->panel);
733 scr_UpdateWindow(win_entry); 732 scr_UpdateWindow(win_entry);
734 top_panel(inputPanel); 733 top_panel(inputPanel);
735 update_panels(); 734 update_panels();
736 doupdate();
737 } else if (!(prefix_flags & HBB_PREFIX_NOFLAG)) { 735 } else if (!(prefix_flags & HBB_PREFIX_NOFLAG)) {
738 setmsgflg = TRUE; 736 setmsgflg = TRUE;
739 } 737 }
740 if (setmsgflg && !special) { 738 if (setmsgflg && !special) {
741 if (special && !winId) 739 if (special && !winId)
756 (unread_msg(NULL) ? '#' : ' '), 754 (unread_msg(NULL) ? '#' : ' '),
757 imstatus2char[jb_getstatus()], (sm ? sm : "")); 755 imstatus2char[jb_getstatus()], (sm ? sm : ""));
758 if (forceupdate) { 756 if (forceupdate) {
759 top_panel(inputPanel); 757 top_panel(inputPanel);
760 update_panels(); 758 update_panels();
761 doupdate();
762 } 759 }
763 g_free(sm); 760 g_free(sm);
764 } 761 }
765 762
766 // scr_DrawMainWindow() 763 // scr_DrawMainWindow()
950 scr_ShowBuddyWindow(); 947 scr_ShowBuddyWindow();
951 } 948 }
952 949
953 // scr_UpdateChatStatus(forceupdate) 950 // scr_UpdateChatStatus(forceupdate)
954 // Redraw the buddy status bar. 951 // Redraw the buddy status bar.
955 // Set forceupdate to TRUE if doupdate() must be called. 952 // Set forceupdate to TRUE if update_panels() must be called.
956 void scr_UpdateChatStatus(int forceupdate) 953 void scr_UpdateChatStatus(int forceupdate)
957 { 954 {
958 unsigned short btype, isgrp, ismuc, isspe; 955 unsigned short btype, isgrp, ismuc, isspe;
959 const char *fullname; 956 const char *fullname;
960 const char *msg = NULL; 957 const char *msg = NULL;
972 wprintw(chatstatusWnd, "~"); 969 wprintw(chatstatusWnd, "~");
973 970
974 if (!current_buddy) { 971 if (!current_buddy) {
975 if (forceupdate) { 972 if (forceupdate) {
976 update_panels(); 973 update_panels();
977 doupdate();
978 } 974 }
979 return; 975 return;
980 } 976 }
981 977
982 fullname = buddy_getname(BUDDATA(current_buddy)); 978 fullname = buddy_getname(BUDDATA(current_buddy));
1000 else 996 else
1001 mvwprintw(chatstatusWnd, 0, 5, "Special buffer: %s", buf_locale); 997 mvwprintw(chatstatusWnd, 0, 5, "Special buffer: %s", buf_locale);
1002 g_free(buf_locale); 998 g_free(buf_locale);
1003 if (forceupdate) { 999 if (forceupdate) {
1004 update_panels(); 1000 update_panels();
1005 doupdate();
1006 } 1001 }
1007 return; 1002 return;
1008 } 1003 }
1009 1004
1010 status = '?'; 1005 status = '?';
1039 g_free(buf_locale); 1034 g_free(buf_locale);
1040 g_free(buf); 1035 g_free(buf);
1041 1036
1042 if (forceupdate) { 1037 if (forceupdate) {
1043 update_panels(); 1038 update_panels();
1044 doupdate();
1045 } 1039 }
1046 } 1040 }
1047 1041
1048 // scr_DrawRoster() 1042 // scr_DrawRoster()
1049 // Display the buddylist (not really the roster) on the screen 1043 // Display the buddylist (not really the roster) on the screen
1083 } 1077 }
1084 1078
1085 // Leave now if buddylist is empty or the roster is hidden 1079 // Leave now if buddylist is empty or the roster is hidden
1086 if (!buddylist || !Roster_Width) { 1080 if (!buddylist || !Roster_Width) {
1087 update_panels(); 1081 update_panels();
1088 doupdate();
1089 curs_set(cursor_backup); 1082 curs_set(cursor_backup);
1090 return; 1083 return;
1091 } 1084 }
1092 1085
1093 name = g_new0(char, Roster_Width); 1086 name = g_new0(char, Roster_Width);
1210 1203
1211 g_free(rline); 1204 g_free(rline);
1212 g_free(name); 1205 g_free(name);
1213 top_panel(inputPanel); 1206 top_panel(inputPanel);
1214 update_panels(); 1207 update_panels();
1215 doupdate();
1216 curs_set(cursor_backup); 1208 curs_set(cursor_backup);
1217 } 1209 }
1218 1210
1219 // scr_RosterVisibility(status) 1211 // scr_RosterVisibility(status)
1220 // Set the roster visibility: 1212 // Set the roster visibility:
1268 if (!(prefix & ~HBB_PREFIX_NOFLAG & ~HBB_PREFIX_HLIGHT)) 1260 if (!(prefix & ~HBB_PREFIX_NOFLAG & ~HBB_PREFIX_HLIGHT))
1269 prefix |= HBB_PREFIX_IN; 1261 prefix |= HBB_PREFIX_IN;
1270 1262
1271 scr_WriteMessage(jidfrom, text, timestamp, prefix); 1263 scr_WriteMessage(jidfrom, text, timestamp, prefix);
1272 update_panels(); 1264 update_panels();
1273 doupdate();
1274 } 1265 }
1275 1266
1276 void scr_WriteOutgoingMessage(const char *jidto, const char *text) 1267 void scr_WriteOutgoingMessage(const char *jidto, const char *text)
1277 { 1268 {
1278 scr_WriteMessage(jidto, text, 0, HBB_PREFIX_OUT|HBB_PREFIX_HLIGHT); 1269 scr_WriteMessage(jidto, text, 0, HBB_PREFIX_OUT|HBB_PREFIX_HLIGHT);
1307 oldmsg = NULL; 1298 oldmsg = NULL;
1308 } 1299 }
1309 } 1300 }
1310 } 1301 }
1311 1302
1303 unsigned int scr_GetAutoAwayTimeout()
1304 {
1305 unsigned int autoaway_timeout = settings_opt_get_int("autoaway");
1306 time_t now;
1307
1308 if (Autoaway || !autoaway_timeout)
1309 return (unsigned)INT_MAX;
1310
1311 time(&now);
1312 if (now > LastActivity + (time_t)autoaway_timeout)
1313 return 0;
1314 else
1315 return LastActivity + (time_t)autoaway_timeout - now;
1316 }
1317
1312 // Check if we should enter/leave automatic away status 1318 // Check if we should enter/leave automatic away status
1313 void scr_CheckAutoAway(int activity) 1319 void scr_CheckAutoAway(int activity)
1314 { 1320 {
1315 static time_t LastActivity;
1316 enum imstatus cur_st; 1321 enum imstatus cur_st;
1317 unsigned int autoaway_timeout = settings_opt_get_int("autoaway"); 1322 unsigned int autoaway_timeout = settings_opt_get_int("autoaway");
1318 1323
1319 if (Autoaway && activity) set_autoaway(FALSE); 1324 if (Autoaway && activity) set_autoaway(FALSE);
1320 if (!autoaway_timeout) return; 1325 if (!autoaway_timeout) return;
1530 // Refresh the window 1535 // Refresh the window
1531 scr_UpdateWindow(win_entry); 1536 scr_UpdateWindow(win_entry);
1532 1537
1533 // Finished :) 1538 // Finished :)
1534 update_panels(); 1539 update_panels();
1535 doupdate();
1536 } 1540 }
1537 1541
1538 // scr_BufferClear() 1542 // scr_BufferClear()
1539 // Clear the current buddy window (used for the /clear command) 1543 // Clear the current buddy window (used for the /clear command)
1540 void scr_BufferClear(void) 1544 void scr_BufferClear(void)
1554 // Refresh the window 1558 // Refresh the window
1555 scr_UpdateWindow(win_entry); 1559 scr_UpdateWindow(win_entry);
1556 1560
1557 // Finished :) 1561 // Finished :)
1558 update_panels(); 1562 update_panels();
1559 doupdate();
1560 } 1563 }
1561 1564
1562 // scr_BufferPurge() 1565 // scr_BufferPurge()
1563 // Purge/Drop the current buddy buffer 1566 // Purge/Drop the current buddy buffer
1564 void scr_BufferPurge(void) 1567 void scr_BufferPurge(void)
1585 // Refresh the window 1588 // Refresh the window
1586 scr_UpdateBuddyWindow(); 1589 scr_UpdateBuddyWindow();
1587 1590
1588 // Finished :) 1591 // Finished :)
1589 update_panels(); 1592 update_panels();
1590 doupdate();
1591 } 1593 }
1592 1594
1593 // scr_BufferScrollLock(lock) 1595 // scr_BufferScrollLock(lock)
1594 // Lock/unlock the current buddy buffer 1596 // Lock/unlock the current buddy buffer
1595 // lock = 1 : lock 1597 // lock = 1 : lock
1630 // Refresh the window 1632 // Refresh the window
1631 scr_UpdateBuddyWindow(); 1633 scr_UpdateBuddyWindow();
1632 1634
1633 // Finished :) 1635 // Finished :)
1634 update_panels(); 1636 update_panels();
1635 doupdate();
1636 } 1637 }
1637 1638
1638 // scr_BufferTopBottom() 1639 // scr_BufferTopBottom()
1639 // Jump to the head/tail of the current buddy window 1640 // Jump to the head/tail of the current buddy window
1640 // (top if topbottom == -1, bottom topbottom == 1) 1641 // (top if topbottom == -1, bottom topbottom == 1)
1658 // Refresh the window 1659 // Refresh the window
1659 scr_UpdateWindow(win_entry); 1660 scr_UpdateWindow(win_entry);
1660 1661
1661 // Finished :) 1662 // Finished :)
1662 update_panels(); 1663 update_panels();
1663 doupdate();
1664 } 1664 }
1665 1665
1666 // scr_BufferSearch(direction, text) 1666 // scr_BufferSearch(direction, text)
1667 // Jump to the next line containing text 1667 // Jump to the next line containing text
1668 // (backward search if direction == -1, forward if topbottom == 1) 1668 // (backward search if direction == -1, forward if topbottom == 1)
1692 // Refresh the window 1692 // Refresh the window
1693 scr_UpdateWindow(win_entry); 1693 scr_UpdateWindow(win_entry);
1694 1694
1695 // Finished :) 1695 // Finished :)
1696 update_panels(); 1696 update_panels();
1697 doupdate();
1698 } else 1697 } else
1699 scr_LogPrint(LPRINT_NORMAL, "Search string not found"); 1698 scr_LogPrint(LPRINT_NORMAL, "Search string not found");
1700 } 1699 }
1701 1700
1702 // scr_BufferPercent(n) 1701 // scr_BufferPercent(n)
1726 // Refresh the window 1725 // Refresh the window
1727 scr_UpdateWindow(win_entry); 1726 scr_UpdateWindow(win_entry);
1728 1727
1729 // Finished :) 1728 // Finished :)
1730 update_panels(); 1729 update_panels();
1731 doupdate();
1732 } 1730 }
1733 1731
1734 // scr_BufferDate(t) 1732 // scr_BufferDate(t)
1735 // Jump to the first line after date t in the buffer 1733 // Jump to the first line after date t in the buffer
1736 // t is a date in seconds since `00:00:00 1970-01-01 UTC' 1734 // t is a date in seconds since `00:00:00 1970-01-01 UTC'
1754 // Refresh the window 1752 // Refresh the window
1755 scr_UpdateWindow(win_entry); 1753 scr_UpdateWindow(win_entry);
1756 1754
1757 // Finished :) 1755 // Finished :)
1758 update_panels(); 1756 update_panels();
1759 doupdate();
1760 } 1757 }
1761 1758
1762 // scr_set_chatmode() 1759 // scr_set_chatmode()
1763 // Public function to (un)set chatmode... 1760 // Public function to (un)set chatmode...
1764 inline void scr_set_chatmode(int enable) 1761 inline void scr_set_chatmode(int enable)
2399 ungetch(ks[i]); 2396 ungetch(ks[i]);
2400 } 2397 }
2401 return; 2398 return;
2402 } 2399 }
2403 2400
2401 inline void scr_DoUpdate(void)
2402 {
2403 doupdate();
2404 }
2405
2404 static int bindcommand(keycode kcode) 2406 static int bindcommand(keycode kcode)
2405 { 2407 {
2406 gchar asciikey[16]; 2408 gchar asciikey[16];
2407 const gchar *boundcmd; 2409 const gchar *boundcmd;
2408 2410
2664 } 2666 }
2665 2667
2666 if (completion_started && key != 9 && key != KEY_RESIZE) 2668 if (completion_started && key != 9 && key != KEY_RESIZE)
2667 scr_end_current_completion(); 2669 scr_end_current_completion();
2668 refresh_inputline(); 2670 refresh_inputline();
2669 if (!update_roster)
2670 doupdate();
2671 return 0; 2671 return 0;
2672 } 2672 }
2673 2673
2674 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ 2674 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */